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

时间:2018-05-19 15:30:45

标签: python pandas scikit-learn

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

# 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 个答案:

答案 0 :(得分:1)

解决方案在错误消息中:

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.

由于您传递了一个功能(不是单个样本),请尝试:

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