函数作用
- 将不同的类别进行编码
函数的使用
使用LabelEncoder()对数据集编码
weather = ['sunny','windy','cloudy','rainy','sunny']
en = sklearn.prepocessing.LabelEncoder()
en = en.fit(['sunny','windy','cloudy','rainy'])
weather = en.transform(weather)
# weather = [2 3 0 1 2]
直接使用LabelEncoder().fit_transform()编码
weather = ['sunny','windy','cloudy','rainy','sunny']
weather = sklearn.prepocessing.LabelEncoder().fit_transform(weather)
print(weather)
# weather = [2 3 0 1 2]
反向推导出编码前对应的原始标签
en = LabelEncoder()
en = en.fit(['sunny','windy','cloudy','rainy'])
weather = [2 3 0 1 2]
weather = en.inverse_transform(weather)
# weather = ['sunny' 'windy' 'cloudy' 'rainy' 'sunny']
版权声明:本文为qq_45279570原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。