Chr*_*hur 40 python scikit-learn deprecation-warning
在Ubuntu下全新安装Anaconda ...我在使用Scikit-Learn进行分类任务之前以各种方式预处理我的数据.
from sklearn import preprocessing
scaler = preprocessing.MinMaxScaler().fit(train)
train = scaler.transform(train)
test = scaler.transform(test)
Run Code Online (Sandbox Code Playgroud)
这一切都很好,但如果我有一个新的样本(温度低于),我想分类(因此我想以相同的方式预处理然后我得到
temp = [1,2,3,4,5,5,6,....................,7]
temp = scaler.transform(temp)
Run Code Online (Sandbox Code Playgroud)
然后我收到了弃用警告......
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.
Run Code Online (Sandbox Code Playgroud)
所以问题是我应该如何重新缩放这样的单个样本?
我想一个替代方案(不是很好的)会......
temp = [temp, temp]
temp = scaler.transform(temp)
temp = temp[0]
Run Code Online (Sandbox Code Playgroud)
但我确信有更好的方法.
Ami*_*ory 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)
Run Code Online (Sandbox Code Playgroud)
小智 31
只需听听警告告诉你的内容:
如果您的数据具有单个特征,则重塑您的数据X.reshape(-1,1);如果包含单个样本,则重塑X.reshape(1,-1).
对于您的示例类型(如果您有多个功能):
temp = temp.reshape(1,-1)
Run Code Online (Sandbox Code Playgroud)
一个功能:
temp = temp.reshape(-1,1)
Run Code Online (Sandbox Code Playgroud)