46

在 Ubuntu 下全新安装 Anaconda... 在使用 Scikit-Learn 进行分类任务之前,我正在以各种方式预处理我的数据。

from sklearn import preprocessing

scaler = preprocessing.MinMaxScaler().fit(train)
train = scaler.transform(train)    
test = scaler.transform(test)

这一切都很好,但如果我有一个我想要分类的新样本(下面的温度)(因此我想以同样的方式进行预处理,那么我得到

temp = [1,2,3,4,5,5,6,....................,7]
temp = scaler.transform(temp)

然后我收到弃用警告...

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. 

所以问题是我应该如何重新调整这样的单个样本?

我想另一种选择(不是很好)是......

temp = [temp, temp]
temp = scaler.transform(temp)
temp = temp[0]

但我确信有更好的方法。

4

6 回答 6

47

只需听一下警告告诉您的内容:

如果您的数据具有单个特征/列,则重塑您的数据 X.reshape(-1, 1) 和 X.reshape(1, -1) 如果它包含单个样本。

对于您的示例类型(如果您有多个功能/列):

temp = temp.reshape(1,-1) 

对于一个特征/列:

temp = temp.reshape(-1,1)
于 2016-07-12T06:40:54.633 回答
33

好吧,实际上看起来警告是在告诉你该怎么做。

作为sklearn.pipeline阶段统一接口的一部分,根据经验:

  • 当你看到时X,它应该是一个np.array二维的

  • 当你看到 时y,它应该是np.array一个单一维度的。

因此,在这里,您应该考虑以下事项:

temp = [1,2,3,4,5,5,6,....................,7]
# This makes it into a 2d array
temp = np.array(temp).reshape((len(temp), 1))
temp = scaler.transform(temp)
于 2016-01-29T10:32:16.027 回答
9

这可能会有所帮助

temp = ([[1,2,3,4,5,6,.....,7]])
于 2017-04-30T10:12:13.013 回答
3

.values.reshape(-1,1)将在没有警报/警告的情况下被接受

.reshape(-1,1)将被接受,但有弃用战争

于 2017-12-25T02:19:35.357 回答
0

你总是可以像这样重塑:

temp = [1,2,3,4,5,5,6,7]

temp = temp.reshape(len(temp), 1)

因为,主要问题是当你的 temp.shape 是:(8,)

你需要 (8,1)

于 2019-06-05T10:55:34.723 回答
0

我遇到了同样的问题并得到了同样的弃用警告。当我收到消息时,我正在使用 [23, 276] 的 numpy 数组。我尝试根据警告对其进行重塑,但最终无济于事。然后我从 numpy 数组中选择每一行(因为我一直在迭代它)并将其分配给一个列表变量。它在没有任何警告的情况下工作。

array = []
array.append(temp[0])

然后,您可以使用 python 列表对象(此处为“数组”)作为 sk-learn 函数的输入。不是最有效的解决方案,但对我有用。

于 2017-07-19T07:35:08.483 回答