重塑数据以供Sklearn使用

我有一组颜色列表:

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']

这些颜色有对应的标签如下:

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]

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

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

from sklearn import preprocessingle = preprocessing.LabelEncoder()le.fit(initialColors)features_train = le.transform(initialColors)

转换后,我的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] 

最后,我执行以下操作:

from sklearn.naive_bayes import GaussianNBclf = GaussianNB()clf.fit(features_train, labels_train)

但我得到了一个错误:

/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]

我尝试了以下操作:

features_train = features_train.reshape(-1, 1)labels_train = labels_train.reshape(-1, 1)clf.fit(features_train, labels_train)

我得到了一个错误:

/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)

我也尝试了:

features_train = features_train.reshape(1, -1)labels_train = labels_train.reshape(1, -1)

但无论如何:

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)

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


回答:

快速回答:

  • 执行features_train = features_train.reshape(-1, 1);
  • 不要执行labels_train = labels_train.reshape(-1, 1)。保持labels_train不变。

一些细节:

看起来你对为什么估计器需要2D数据数组输入感到困惑。你的训练向量X具有形状(n_samples, n_features)。所以features_train.reshape(-1, 1)在你的情况下是正确的,因为你只有一个特征,并且希望让numpy推断出有多少样本。这确实解决了你的第一个错误。

你的目标值y具有形状(n_samples,),它期望一个1D数组。当你执行labels_train = labels_train.reshape(-1, 1)时,你将其转换为2D列向量。这就是你得到第二个警告的原因。请注意,这是一个警告,意味着fit()已经弄明白了并进行了正确的转换,即你的程序继续运行并且应该是正确的。

当你执行以下操作时:

features_train = features_train.reshape(1, -1)labels_train = labels_train.reshape(1, -1)

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

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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注