1
votes
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 Traceback (most recent call last) in () ----> 1 enc.fit_transform(X)

~\Anaconda3\lib\site-packages\sklearn\preprocessing\data.py in fit_transform(self, X, y) 2017 """ 2018 return _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 in _transform_selected(X, transform, selected, copy) 1807 X : array or sparse matrix, shape=(n_samples, n_features_new) 1808
""" -> 1809 X = check_array(X, accept_sparse='csc', copy=copy, dtype=FLOAT_DTYPES) 1810 1811 if isinstance(selected, six.string_types) and selected == "all":

~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator) 431 force_all_finite) 432 else: --> 433 array = np.array(array, dtype=dtype, order=order, copy=copy) 434 435 if ensure_2d:

ValueError: could not convert string to float: 'gender'

1
It works for me using scikit-learn-0.20.3 and scipy-1.2.1Devesh Kumar Singh

1 Answers

1
votes

From this post,

OneHotEncoder Error: cannot convert string to float

you can see that it does not work but with integers. However, now, the documentation says that it works with integers.

Maybe you should update your version, because it should be able to convert strings.

For me the piece of code from the edit has worked perfectly.