Python scikit learn 中的预处理 - 单个样本 - 折旧警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35082140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Preprocessing in scikit learn - single sample - Depreciation warning
提问by Chris Arthur
On a fresh installation of Anaconda under Ubuntu... I am preprocessing my data in various ways prior to a classification task using Scikit-Learn.
在 Ubuntu 下全新安装 Anaconda 时……我在使用 Scikit-Learn 进行分类任务之前以各种方式预处理我的数据。
from sklearn import preprocessing
scaler = preprocessing.MinMaxScaler().fit(train)
train = scaler.transform(train)
test = scaler.transform(test)
This all works fine but if I have a new sample (temp below) that I want to classify (and thus I want to preprocess in the same way then I get
这一切都很好,但是如果我有一个我想要分类的新样本(下面的温度)(因此我想以相同的方式进行预处理,那么我得到
temp = [1,2,3,4,5,5,6,....................,7]
temp = scaler.transform(temp)
Then I get a deprecation warning...
然后我收到一个弃用警告......
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.
So the question is how should I be rescaling a single sample like this?
所以问题是我应该如何重新调整这样的单个样本?
I suppose an alternative (not very good one) would be...
我想另一种选择(不是很好)是......
temp = [temp, temp]
temp = scaler.transform(temp)
temp = temp[0]
But I'm sure there are better ways.
但我相信有更好的方法。
采纳答案by Mike
Just listen to what the warning is telling you:
听听警告告诉你什么:
Reshape your data either X.reshape(-1, 1) if your data has a single feature/column and X.reshape(1, -1) if it contains a single sample.
如果您的数据具有单个特征/列,则重塑您的数据 X.reshape(-1, 1) 和 X.reshape(1, -1) 如果它包含单个样本。
For your example type(if you have more than one feature/column):
对于您的示例类型(如果您有多个功能/列):
temp = temp.reshape(1,-1)
For one feature/column:
对于一个特征/列:
temp = temp.reshape(-1,1)
回答by Ami Tavory
Well, it actually looks like the warning is telling you what to do.
好吧,实际上看起来警告是在告诉您该怎么做。
As part of sklearn.pipeline
stages' uniform interfaces, as a rule of thumb:
作为sklearn.pipeline
阶段统一接口的一部分,根据经验:
when you see
X
, it should be annp.array
with two dimensionswhen you see
y
, it should be annp.array
with a single dimension.
当你看到时
X
,它应该是一个np.array
有两个维度的当你看到时
y
,它应该是np.array
一个单一维度的。
Here, therefore, you should consider the following:
因此,在这里,您应该考虑以下几点:
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)
回答by Bharath
This might help
这可能有帮助
temp = ([[1,2,3,4,5,6,.....,7]])
回答by shan89
I faced the same issue and got the same deprecation warning. I was using a numpy array of [23, 276] when I got the message. I tried reshaping it as per the warning and end up in nowhere. Then I select each row from the numpy array (as I was iterating over it anyway) and assigned it to a list variable. It worked then without any warning.
我遇到了同样的问题并得到了同样的弃用警告。收到消息时,我使用的是 [23, 276] 的 numpy 数组。我尝试按照警告重塑它,但最终无处可去。然后我从 numpy 数组中选择每一行(因为我一直在迭代它)并将它分配给一个列表变量。它在没有任何警告的情况下工作。
array = []
array.append(temp[0])
Then you can use the python list object (here 'array') as an input to sk-learn functions. Not the most efficient solution, but worked for me.
然后您可以使用 python 列表对象(此处为“数组”)作为 sk-learn 函数的输入。不是最有效的解决方案,但对我有用。
回答by Analytics
.values.reshape(-1,1)
will be accepted without alerts/warnings
.values.reshape(-1,1)
将在没有警报/警告的情况下被接受
.reshape(-1,1)
will be accepted, but with deprecation war
.reshape(-1,1)
将被接受,但有弃用War
回答by Francisco Pereira
You can always, reshape like:
你总是可以像这样重塑:
temp = [1,2,3,4,5,5,6,7]
temp = temp.reshape(len(temp), 1)
Because, the major issue is when your, temp.shape is: (8,)
因为,主要问题是当你的 temp.shape 是:(8,)
and you need (8,1)
你需要 (8,1)