SVR模型->功能缩放-预期的2D阵列,取而代之的是1D阵列 [英] SVR Model -->Feature Scaling - Expected 2D array, got 1D array instead

查看:128
本文介绍了SVR模型->功能缩放-预期的2D阵列,取而代之的是1D阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解下面的代码出了什么问题.我知道Y变量是1D数组,应该是2D数组,需要重塑结构,但是该代码以前可以正常工作并带有警告.

I am trying to understand what is wrong with the code below. I know that the Y variable is 1D array and expected to be 2D array and need to reshape the structure but that code was working previously fine with a warning.

# Importing the libraries
  import numpy as np
  import matplotlib.pyplot as plt
  import pandas as pd

# Importing the dataset
  dataset = pd.read_csv('Position_Salaries.csv')
  X = dataset.iloc[:, 1:2].values
  y = dataset.iloc[:, 2].values



# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
sc_y = StandardScaler()
X = sc_X.fit_transform(X)
y = sc_y.fit_transform(y)


ValueError: Expected 2D array, got a 1D array instead:
array=[  45000.   50000.   60000.   80000.  110000.  150000.  200000.  300000.
  500000. 1000000.].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

推荐答案

解决方案在错误消息中:

The solution is in the error message:

Reshape your data either using array.reshape(-1, 1) if your data has
a single feature or array.reshape(1, -1) if it contains a single sample.

由于您要传递单个功能(而不是单个示例),请尝试:

Since you're passing in a single feature (not a single sample), try:

y = sc_y.fit_transform(y.reshape(-1, 1))

这篇关于SVR模型->功能缩放-预期的2D阵列,取而代之的是1D阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆