离散型数据编码

对比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'))

发布了118 篇原创文章 · 获赞 132 · 访问量 60万+

猜你喜欢

转载自blog.csdn.net/qfikh/article/details/103299221