sklearn.preprocessing.LabelEncoder的使用
在训练模型之前,我们通常都要对训练数据进行一定的处理。将类别编号就是一种常用的处理方法,比如把类别“男”,“女”编号为0和1。可以使用sklearn.preprocessing中的LabelEncoder处理这个问题。
作用
将n个类别编码为0~n-1之间的整数(包含0和n-1)。
例子
假设我们要对性别数据进行编码,则数据可以分为两种情况:无NaN,有NaN。
首先导入要使用的包
import numpy as np
import pandas as pd
from sklearn import preprocessing
无NaN
数据如下
sex = pd.Series(["male", "female", "female", "male"])