对比LabelEncoder 和OneHot编码:
LabelEncoder编码:
from sklearn import preprocessing
import pandas as pd
df = pd.DataFrame([
[-1 , 'A'],
[2 , 'B'],
[1 , 'A']])
df.columns = ['age', 'class']
le = preprocessing.LabelEncoder()
age_df=le.fit_transform(df['age'])
print(list(le.classes_))
print(age_df)
# 三个类别分别为0 1 2
age_df = pd.get_dummies(age_df, prefix='color_df')
age_df
OneHot编码
import pandas as pd
df = pd.DataFrame([
[-1 , 'A'],
[2 , 'B'],
[1 , 'A']])
df.columns = ['age', 'class']
print(df)
print(pd.get_dummies(df['age'],prefix='age'))