0

在Python 2.7中使用(miniconda解释器)。下面的例子混淆了关于OneHotEncoder,困惑为什么enc.n_values_输出是[2, 3, 4]?如果有人能够帮助澄清,那将会很棒。scikit中的OneHotEncoder混淆学习

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

>>> from sklearn.preprocessing import OneHotEncoder 
>>> enc = OneHotEncoder() 
>>> enc.fit([[0, 0, 3], [1, 1, 0], [0, 2, 1], [1, 0, 2]]) 
OneHotEncoder(categorical_features='all', dtype=<... 'float'>, 
     handle_unknown='error', n_values='auto', sparse=True) 
>>> enc.n_values_ 
array([2, 3, 4]) 
>>> enc.feature_indices_ 
array([0, 2, 5, 9]) 
>>> enc.transform([[0, 1, 1]]).toarray() 
array([[ 1., 0., 0., 1., 0., 0., 1., 0., 0.]]) 

问候, 林

回答

1

n_values是每个特征值的数量。

在这个例子中,

X = 0 0 3 
    1 1 0 
    0 2 1 
    1 0 2 

(X的形状为[N_SAMPLES次,n_feature])

对于第一特征中,有2个值:0,1;

对于第二个特征中,有3个值:0,1,2

对于第三特征中,有4个值:0,1,2,3

因此,enc.n_values_[2, 3, 4]

+0

谢谢杨洁,所以3个样本是'[0,1,0,1]','[0,1,2,0]'和'[3,0,1,2]'? –

+0

也对'[n_samples,n_feature]'感到困惑,我认为它是'n_samples'行和'n_feature'列,但它似乎并非如此,如果你能清晰起来,那将会很棒。 :) –

+1

它是'n​​_samples'行和'n_feature'列。 X中有4个样本,每个样本有3个特征。 – yangjie