简体   繁体   中英

Sample code from scikit-learn.org is giving error

when I try to learn One HotEncoder from sklearn library, I tried the following sample code given provided by scikit-learn.org. But this code gives me error and I am not able to debug it.

from sklearn.preprocessing import OneHotEncoder
enc = OneHotEncoder(handle_unknown='ignore')
X = [['Male', 1], ['Female', 3], ['Female', 2]]
enc.fit(X)

I get this error:

array = np.array(array, dtype=dtype, order=order, copy=copy)
ValueError: could not convert string to float: 'Male'

I am assuming if this code is written on scikit-learn.org, then the problem must be in my environment or is it something I am doing wrong here.

It is arguably related to your scikit-learn version. From version 0.20, OneHotEncoder can handle strings, which was not the case in older versions - notice that the documentation for v0.19 doesn't provide the same example: https://scikit-learn.org/0.19/modules/generated/sklearn.preprocessing.OneHotEncoder.html

The release notes for the version Version 0.20.2 (December 18) mention this specifically:

Major Feature : Expanded preprocessing.OneHotEncoder to allow to encode categorical string features as a numeric array using a one-hot (or dummy) encoding scheme

So, upgrade your scikit-learn to the current version (0.20), and you should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM