scikit中的预处理学习 - 单个样本 - 折旧警告

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)

  • @Tajchert抱歉-将numpy导入为np。 (2认同)
  • 这是否意味着 sklearn 决定不鼓励 Python 原生列表?还有办法不使用numpy吗? (2认同)

小智 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)

  • 不要重塑X!它关于 y(目标)!!y 显然只有一列。前几天有同样的问题。我花了一个小时试图重塑 X ;-) (3认同)
  • 我不明白单个样本的含义。X.shape返回(891,158)。他们提出的2个解决方案中的任何一个都给出了错误,但是如果不重塑它,我仍然会收到警告。 (2认同)

Flo*_*oor 7

这可能有所帮助

temp = ([[1,2,3,4,5,6,.....,7]])
Run Code Online (Sandbox Code Playgroud)