+-
我正进入(状态
TypeError: unhashable type: ‘slice’
执行以下代码时,在Python中编码分类数据.有人可以帮忙吗?
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('50_Startups.csv')
y=dataset.iloc[:, 4]
X=dataset.iloc[:, 0:4]
# Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()
X[:, 3] = labelencoder_X.fit_transform(X[:, 3])
最佳答案
X是一个数据帧,无法通过X [:,3]等切片术语进行访问.您必须通过
iloc
或
X.values
访问.但是,您构建X的方式使它成为副本……所以.我会用价值观
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
# dataset = pd.read_csv('50_Startups.csv')
dataset = pd.DataFrame(np.random.rand(10, 10))
y=dataset.iloc[:, 4]
X=dataset.iloc[:, 0:4]
# Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()
# I changed this line
X.values[:, 3] = labelencoder_X.fit_transform(X.values[:, 3])
点击查看更多相关文章
转载注明原文:Python“TypeError:unhashable type:’slice’”用于编码分类数据 - 乐贴网