【发布时间】:2019-06-28 07:30:29
【问题描述】:
这是我的代码:
#Importing the dataset
dataset = pd.read_csv('insurance.csv')
X = dataset.iloc[:, :-2].values
X = pd.DataFrame(X)
#Encoding Categorical data
from sklearn.preprocessing import LabelEncoder
labelencoder_X = LabelEncoder()
X[:, 1:2] = labelencoder_X.fit_transform(X[:, 1:2])
样本数据集
age sex bmi children smoker region charges
19 female 27.9 0 yes southwest 16884.924
18 male 33.77 1 no southeast 1725.5523
28 male 33 3 no southeast 4449.462
33 male 22.705 0 no northwest 21984.47061
32 male 28.88 0 no northwest 3866.8552
31 female 25.74 0 no southeast 3756.6216
46 female 33.44 1 no southeast 8240.5896
37 female 27.74 3 no northwest 7281.5056
37 male 29.83 2 no northeast 6406.4107
60 female 25.84 0 no northwest 28923.13692
运行 labelencoder 时出现以下错误
文件“E:\Anaconda2\lib\site-packages\pandas\core\generic.py”,行 1840,在 _get_item_cache res = cache.get(item) TypeError: unhashable 输入
什么可能导致这个错误?
【问题讨论】:
-
尝试使用
X.loc[:, 1:2]或X.iloc[:, 1:2]而不是X[:, 1:2]... -
@MaxU,我试过了,还是一样的错误
-
请在您的问题中提供一个小样本数据集,这将有助于重现此错误
-
@MaxU 在上面添加了示例数据,我正在使用 Spyder 和 python 2.7。感谢您的帮助
-
请阅读how to make good reproducible pandas examples并相应地编辑您的帖子。为了帮助您,我们不会从图片中键入此数据集;)
标签: python pandas scikit-learn