SVR模型->特征缩放-预期是2D阵列,得到的是1D阵列。[英] SVR Model -->Feature Scaling - Expected 2D array, got 1D array instead

本文是小编为大家收集整理的关于SVR模型->特征缩放-预期是2D阵列,得到的是1D阵列。的处理方法,想解了SVR模型->特征缩放-预期是2D阵列,得到的是1D阵列。的问题怎么解决?SVR模型->特征缩放-预期是2D阵列,得到的是1D阵列。问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

我试图了解下面的代码有什么问题.我知道 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.
解决方案

解决方法在报错信息中:

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

本文地址:https://www.itbaoku.cn/post/1728117.html