OneHotEncoder中的active_features_属性

3

我是机器学习的新手,想要了解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_掩码)

这是什么意思,这里的掩码是什么?

谢谢!

1个回答

8
OneHotEncoder编码分类特征(值为分类的特征),例如特征“车辆”可以有来自集合{"汽车","摩托车","卡车",...}的值。当一个人暗示你在这些值之间没有任何顺序时,就会使用这种特征类型,例如汽车与摩托车或卡车不可比较,尽管您正在使用整数对集合“汽车”,“摩托车”,“卡车”进行编码,您希望学习估计器不暗示分类特征值之间的任何关系。要将此特征类型转换为二进制或有理数,并仍然保持无序值的属性,您可以使用One Hot Encoding。这是一种非常常见的技术:它将原始数据集中每个分类特征替换为n个新的二进制特征,其中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.]])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接