from sklearn.preprocessing import OneHotEncoder
enc=OneHotEncoder(handle_unknown='ignore')
X=[['gender', 1], ['NationalITy', 2], ['PlaceofBirth', 3],['StageID', 4], ['GradeID', 5], ['SectionID', 6],['Topic', 7], ['Semester', 8], ['Relation', 9],['raisedhands', 1], ['VisITedResources', 2], ['AnnouncementsView', 3],['Discussion', 4], ['ParentAnsweringSurvey', 5], ['ParentschoolSatisfaction', 6],['Class',7]]
enc.fit_transform(X)
ValueError跟踪(最近的呼叫 最后)在() ----> 1个enc.fit_transform(X)
〜\ Anaconda3 \ lib \ site-packages \ sklearn \ preprocessing \ data.py在 fit_transform(self,X,y)2017“”“ 2018返回 _transform_selected(X,self._fit_transform, -> 2019 self.categorical_features,copy = True)2020 2021 def _transform(self,X):
〜\ Anaconda3 \ lib \ site-packages \ sklearn \ preprocessing \ data.py在 _transform_selected(X,变换,选择,复制)1807 X:数组或稀疏矩阵,shape =(n_samples,n_features_new)1808
“” -> 1809 X = check_array(X,accept_sparse ='csc',copy = copy,dtype = FLOAT_DTYPES)1810 1811如果isinstance(selected, six.string_types),然后选择==“ all”:〜\ Anaconda3 \ lib \ site-packages \ sklearn \ utils \ validation.py在 check_array(array,accept_sparse,dtype,order,copy, force_all_finite,ensure_2d,allow_nd,ensure_min_samples, sure_min_features,warn_on_dtype,estimator) 第431章 其他432 -> 433 array = np.array(array,dtype = dtype,order = order,copy = copy) 434 435,如果sure_2d:
ValueError:无法将字符串转换为float:“性别”
答案 0 :(得分:0)
这篇文章中
OneHotEncoder Error: cannot convert string to float
您会看到它不起作用,但可以使用整数。但是,现在,文档说它可以使用整数。
也许您应该更新您的版本,因为它应该能够转换字符串。
对我来说,编辑后的代码片段运行得很好。