SVR 模型 --> 特征缩放 - 预期的 2D 阵列,而是得到 1D 阵列

Ram*_*yal 8 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)
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)

Pet*_*ler 5

解决方法在错误信息中:

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.
Run Code Online (Sandbox Code Playgroud)

由于您传入的是单个功能(不是单个示例),请尝试:

y = sc_y.fit_transform(y.reshape(-1, 1))
Run Code Online (Sandbox Code Playgroud)

  • 您介意解释一下为什么需要二维数组吗?毕竟,数据只是“标准化”,不需要二维数组。 (2认同)