active_features_ OneHotEncoder 中的属性

active_features_ attribute in OneHotEncoder

我是机器学习的新手,我想了解 OneHotEncoder 的作用。我可以将它与其他东西(例如 LabelEncoder)区分开来。特别是,我发现关于 active_features_ 的文档特别令人困惑。

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

feature_indices_

的文档中也提到了

feature_indices_ :
array of shape (n_features,)
Indices to feature ranges. Feature i in the original data is mapped to features from feature_indices_[i] to feature_indices_[i+1] (and then potentially masked by active_features_ afterwards)

这是什么意思,这里的mask是做什么用的?

谢谢!

OneHotEncoder 编码 categorical 特征,(特征值是分类的)例如特征 "vehicle" 可以具有来自集合 {"car"、"motorcycle"、[=24= 的值], ...}。当暗示您在这些值之间没有任何顺序时使用此功能类型,例如汽车无法与摩托车或卡车相提并论,尽管您使用整数对集合 "car"、"motorcycle"、"truck"} 进行编码,但您想要学习的估计量并不意味着分类特征。要将此特征类型转换为二进制或有理数,并仍然保持 属性 的无序值,您可以使用 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.]])