OneHotEncoder中的active_features_属性

eth*_*luo 2 machine-learning scikit-learn

我是机器学习的新手,我正在努力了解OneHotEncoder的功能.我可以将它与LabelEncoder等其他东西区分开来.特别是,我发现active_features_上的文档特别令人困惑.

http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html#sklearn.preprocessing.OneHotEncoder

它也在文档中提到过 feature_indices_

feature_indices_:
形状数组(n_features)
特征范围的索引.原始数据中的特征i被映射到feature_indices_ [i]到feature_indices_ [i + 1]的特征(之后可能被active_features_屏蔽)

这是什么意思,这里的面具是什么?

谢谢!

Ibr*_*iev 7

OneHotEncoder对分类特征进行编码,(特征值是分类的),例如特征"车辆"可以具有来自集合{"car","motorcycle","truck",......}的值.当一个暗示您在这些值之间没有任何顺序时使用此要素类型,例如,汽车与摩托车或卡车不具有可比性,尽管您使用整数对"car","motorcycle","truck"}进行编码,你想学习估计器,这并不意味着分类特征值之间的任何关系.要将此要素类型转换为二进制或有理数,并仍然保持无序值的属性,可以使用One Hot Encoding.这是一种非常常见的技术:它将创建n新的二进制特征,而不是原始数据集中的每个分类特征,其中n- 原始分类特征中的唯一值的数量.如果您想知道这些新二进制特征在结果数据集中的确切位置 - 您将必须使用feature_indices_属性,i原始数据集中分类特征的所有新二进制特征现在都在feature_indices_[i]:feature_indices_[i+1]新数据集的列中.

OneHotEncoder根据数据集中此特征的值确定每个分类特征的范围,请看以下示例:

dataset = [[0, 0],
           [1, 1],
           [2, 4],
           [0, 5]]

# First categorial feature has values in range [0,2] and dataset contains all values from that range.
# Second feature has values in range [0,5], but values (2, 3) are missing.
# Assuming that one encoded categorial values with that integer range, 2 and 3 must be somewhere, or it's sort of error.
# Thus OneHotEncoder will remove columns of values 2 and 3 from resulting dataset
enc = OneHotEncoder()
enc.fit(dataset)

print(enc.n_values_)
# prints array([3,6])
# first feature has 3 possible values, i.e 3 columns in resulting dataset
# second feature has 6 possible values
print(enc.feature_indices_)
# prints array([0, 3, 9])
# first feature decomposed into 3 columns (0,1,2), second — into 6 (3,4,5,6,7,8)
print(enc.active_features_)
# prints array([0, 1, 2, 3, 4, 7, 8])
# but two values of second feature never occurred, so active features doesn't list (5,6), and resulting dataset will not contain those columns too
enc.transform(dataset).toarray()
# prints this array
array([[ 1.,  0.,  0.,  1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.,  1.,  0.],
       [ 1.,  0.,  0.,  0.,  0.,  0.,  1.]])
Run Code Online (Sandbox Code Playgroud)