sklearn 字符转化数字编码 - xd21303/Notebook GitHub Wiki

a = np.array(['male','male','female','null'])

from sklearn.preprocessing import LabelEncoder

b = LabelEncoder()
c=b.fit_transform(a.reshape(4,1))
print(c)

[1 1 0 2]
>>> le = preprocessing.LabelEncoder()  
>>> le.fit(["paris", "paris", "tokyo", "amsterdam"])  
LabelEncoder()  
>>> list(le.classes_)  
['amsterdam', 'paris', 'tokyo']  
>>> le.transform(["tokyo", "tokyo", "paris"])  
array([2, 2, 1])  
>>> list(le.inverse_transform([2, 2, 1]))  
['tokyo', 'tokyo', 'paris'] 
  1. 新建一个LabelEncoder
  2. fit
  3. transform 字符转化为数字
  4. inverse_transform 反编码,数字转化为字符