[P/M/K]sklearn.preprocessing.LabelEncoder() & pandas.factorize

sklearn.preprocessing.LabelEncoder() & pandas.factorize

I am used to use

data.loc[:, "MSZoning"] = pd.factorize(data.MSZoning)[0]

Actually what it does is exactly the same with LabelEncoder. The main difference is in sklearn you have obey rules like .fit and.transform.

from sklearn import preprocessing    
# Test data
df = DataFrame(['A', 'B', 'B', 'C'], columns=['Col'])    
df['Fact'] = pd.factorize(df['Col'])[0]
le = preprocessing.LabelEncoder()
df['Lab'] = le.fit_transform(df['Col'])

print(df)
#   Col  Fact  Lab
# 0   A     0    0
# 1   B     1    1
# 2   B     1    1
# 3   C     2    2
[1]: https://stackoverflow.com/questions/40336502/want-to-know-the-diff-among-pd-factorize-pd-get-dummies-sklearn-preprocessing

猜你喜欢

转载自blog.csdn.net/qq_40820196/article/details/82821153