在scikit-learn中使用OneHotEncoder准备用于分类的序数和名义特征

时间:2015-02-08 20:00:44

标签: scikit-learn feature-extraction

我想准备一个包含连续,名义和序数特征的数据集进行分类。我在下面有一些解决方法,但我想知道是否有更好的方法使用scikit-learn的编码器?

让我们考虑以下示例数据集:

import pandas as pd
df = pd.DataFrame([['green', 'M', 10.1, 'class1'], ['red', 'L', 13.5, 'class2'], ['blue', 'XL', 15.3, 'class1']])
df.columns = ['color', 'size', 'prize', 'class label']
df

enter image description here

现在,类标签可以通过标签编码器简单地转换(分类器忽略类标签中的顺序)。

from sklearn.preprocessing import LabelEncoder
class_le = LabelEncoder()
df['class label'] = class_le.fit_transform(df['class label'].values)

我会像这样转换序数特征列size

size_mapping = {
           'XL': 3,
           'L': 2,
           'M': 1}

df['size'] = df['size'].apply(lambda x: size_mapping[x])
df

enter image description here

最后是序数color功能:

color_mapping = {
           'green': [0,0,1],
           'red': [0,1,0],
           'blue': [1,0,0]}

df['color'] = df['color'].apply(lambda x: color_mapping[x])
df

enter image description here

y = df['class label'].values
X = df.iloc[:, :-1].values
X = np.apply_along_axis(func1d= lambda x: np.array(x[0] + list(x[1:])), axis=1, arr=X)
X

array([[  0. ,   0. ,   1. ,   1. ,  10.1],
       [  0. ,   1. ,   0. ,   2. ,  13.5],
       [  1. ,   0. ,   0. ,   3. ,  15.3]])

1 个答案:

答案 0 :(得分:3)

您可以使用DictVectorizer进行标称编码,使过程更清晰。您也可以应用' size_maping'直接与.map()

import pandas as pd
df = pd.DataFrame([['green', 'M', 10.1, 'class1'], ['red', 'L', 13.5, 'class2'], ['blue', 'XL', 15.3, 'class1']])
df.columns = ['color', 'size', 'prize', 'class label']

from sklearn.preprocessing import LabelEncoder
class_le = LabelEncoder()
df['class label'] = class_le.fit_transform(df['class label'].values)

size_mapping = {
       'XL': 3,
       'L': 2,
       'M': 1}

df['size'] = df['size'].map(size_mapping)

feats =df.transpose().to_dict().values()

from sklearn.feature_extraction import DictVectorizer
Dvec = DictVectorizer()

Dvec.fit_transform(feats).toarray()

返回:

array([[  0. ,   0. ,   1. ,   0. ,  10.1,   1. ],
       [  1. ,   0. ,   0. ,   1. ,  13.5,   2. ],
       [  0. ,   1. ,   0. ,   0. ,  15.3,   3. ]])

获取功能名称:

 Dvec.get_feature_names()

 ['class label', 'color=blue', 'color=green', 'color=red', 'prize', 'size']