sklearn.preprocessing.LabelEncoder
作用:
將字串陣列進行轉換成固定的數字
以方便之後進行運算
概念類似此
["a" , "b" ,"c"] => [0 ,1 ,2]
code:
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder
stringdata = ['a' , 'b' ,'c' , 'd' , 'e' , 'b']
le = LabelEncoder()
le.fit(stringdata)
print("LabelEncoder classes_: \n" , le.classes_)
print("LabelEncoder: \n" , le.transform(stringdata))
print("LabelEncoder inverse_transform \n" , le.inverse_transform([0,1,2,3]))
output:
LabelEncoder classes_:
['a' 'b' 'c' 'd' 'e']
LabelEncoder:
[0 1 2 3 4 1]
LabelEncoder inverse_transform
['a' 'b' 'c' 'd']
運算過程會先將字串進行排序
再依序從0開始匹配一個數字
source code
沒有留言:
張貼留言