为 Sklearn 重塑数据

rel*_*l1x 2 machine-learning python-2.7 scikit-learn

我有一个颜色列表:

initialColors = [u'black' u'black' u'black' u'white' u'white' u'white' u'powderblue'
 u'whitesmoke' u'black' u'cornflowerblue' u'powderblue' u'powderblue'
 u'goldenrod' u'white' u'lavender' u'white' u'powderblue' u'powderblue'
 u'powderblue' u'powderblue' u'powderblue' u'powderblue' u'powderblue'
 u'powderblue' u'white' u'white' u'powderblue' u'white' u'white']
Run Code Online (Sandbox Code Playgroud)

我有这些颜色的标签,如下所示:

labels_train = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
Run Code Online (Sandbox Code Playgroud)

0表示颜色由女性选择,1表示颜色由男性选择。我将使用另一组颜色来预测性别。

因此,对于我的初始颜色,我将名称转换为数字特征向量,如下所示:

from sklearn import preprocessing
le = preprocessing.LabelEncoder()
le.fit(initialColors)
features_train = le.transform(initialColors)
Run Code Online (Sandbox Code Playgroud)

之后我的features_train样子:

[0 0 0 5 5 5 4 6 0 1 4 4 2 5 3 5 4 4 4 4 4 4 4 4 5 5 4 5 5] 
Run Code Online (Sandbox Code Playgroud)

最后,我这样做:

from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
clf.fit(features_train, labels_train)
Run Code Online (Sandbox Code Playgroud)

但我有一个错误:

/Library/Python/2.7/site-packages/sklearn/utils/validation.py:395: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
  DeprecationWarning)
Traceback (most recent call last):
  File "app.py", line 36, in <module>
    clf.fit(features_train, labels_train)
  File "/Library/Python/2.7/site-packages/sklearn/naive_bayes.py", line 182, in fit
    X, y = check_X_y(X, y)
  File "/Library/Python/2.7/site-packages/sklearn/utils/validation.py", line 531, in check_X_y
    check_consistent_length(X, y)
  File "/Library/Python/2.7/site-packages/sklearn/utils/validation.py", line 181, in check_consistent_length
    " samples: %r" % [int(l) for l in lengths])
ValueError: Found input variables with inconsistent numbers of samples: [1, 70]
Run Code Online (Sandbox Code Playgroud)

我做了:

features_train = features_train.reshape(-1, 1)
labels_train = labels_train.reshape(-1, 1)
clf.fit(features_train, labels_train)
Run Code Online (Sandbox Code Playgroud)

我有一个错误:

/Library/Python/2.7/site-packages/sklearn/utils/validation.py:526: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

features_train = features_train.reshape(1, -1)
labels_train = labels_train.reshape(1, -1)
Run Code Online (Sandbox Code Playgroud)

但不管怎么说:

Traceback (most recent call last):
  File "app.py", line 36, in <module>
    clf.fit(features_train, labels_train)
  File "/Library/Python/2.7/site-packages/sklearn/naive_bayes.py", line 182, in fit
    X, y = check_X_y(X, y)
  File "/Library/Python/2.7/site-packages/sklearn/utils/validation.py", line 526, in check_X_y
    y = column_or_1d(y, warn=True)
  File "/Library/Python/2.7/site-packages/sklearn/utils/validation.py", line 562, in column_or_1d
    raise ValueError("bad input shape {0}".format(shape))
ValueError: bad input shape (1, 29)
Run Code Online (Sandbox Code Playgroud)

我的问题是我不明白在我的情况下重塑数据的最佳方法是什么。您能帮我选择一种重塑数据的方法吗?

Y. *_*Luo 5

快速回答:

  • features_train = features_train.reshape(-1, 1);
  • 不要做labels_train = labels_train.reshape(-1, 1)。保持labels_train原样。

一些细节:

您似乎对估算器需要二维数据数组输入的原因感到困惑。您的训练向量的X形状为 (n_samples, n_features)。对于您的情况来说,这features_train.reshape(-1, 1)是正确的,因为您只有 1 个特征,并且想要numpy推断有多少个样本。这确实解决了您的第一个错误。

您的目标值y具有形状 (n_samples,),它需要一个一维数组。当您这样做时labels_train = labels_train.reshape(-1, 1),您将其转换为二维列向量。这就是你收到第二次警告的原因。请注意,这是一个警告,意味着fit()已弄清楚并进行了正确的转换,即您的程序继续运行并且应该是正确的。

当你这样做时:

features_train = features_train.reshape(1, -1)
labels_train = labels_train.reshape(1, -1)
Run Code Online (Sandbox Code Playgroud)

首先,对于features_train您的情况来说,这是一个错误的转换,因为X.reshape(1, -1)这意味着您有 1 个样本,并且想要numpy推断有多少个特征。它不是你想要的但fit()不知道并且会相应地处理它,给你错误的结果。

话虽如此,您的最后一个错误并非来自features_train = features_train.reshape(1, -1). 它来自labels_train = labels_train.reshape(1, -1). 现在你的labels_train形状 (1, 29) 既不是行向量也不是列向量。尽管我们可能知道它应该被解释为目标值的一维数组,但fit()还不是那么聪明,也不知道如何处理它。