machine-learning - OneHotEncoder 中的 active_features_ 属性

标签 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_ :
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)

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

谢谢!

最佳答案

OneHotEncoder 编码 categorical特征,(值是分类的特征)例如特征“车辆”可以具有来自集合{“汽车”,“摩托车”,“卡车”,...}的值。当暗示这些值之间没有任何顺序时,使用此功能类型,例如汽车与摩托车或卡车没有可比性,尽管您使用整数编码集“汽车”,“摩托车”,“卡车”},但您想要学习估计器,它并不暗示分类特征值之间的任何关系。要将这种特征类型转换为二进制或有理数,并仍然保持无序值的属性,您可以使用 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.]])

关于machine-learning - OneHotEncoder 中的 active_features_ 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33592034/

相关文章:

machine-learning - LSTM网络学习

python - 编码器一热混淆

machine-learning - 神经网络 - 我应该删除所有派生/计算变量吗?

r - R - S 和 F 计数中的 bestglm 问题不能 <0

python - sklearn SGDClassifier 无法使其确定性地训练或预测

python - 使用相似度函数对 scikit-learn 进行聚类

python - 机器学习: Does computing the accuracy score for binary labels always result in a low accuracy score?

python - np.mean() 导致内存不足错误

python - 回归模型 Pandas

python - 生成 3D 高斯数据