SVR模型 - >特征缩放 - 预期的2D数组,取而代之的是1D数组

时间:2021-07-28 19:01:12

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.

我试图了解下面的代码有什么问题。我知道Y变量是一维数组,并且预计是二维数组,需要重新整形结构,但该代码之前的工作正常,并带有警告。

# 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.

1 个解决方案

#1


0  

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

#1


0  

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