gpt4 book ai didi

python - SVR 模型 --> 特征缩放 - 预期的二维数组,取而代之的是一维数组

转载 作者:行者123 更新时间:2023-11-28 17:06:49 25 4
gpt4 key购买 nike

我试图理解下面的代码有什么问题。我知道 Y 变量是 1D 数组,预计是 2D 数组,需要 reshape 结构,但该代码之前可以正常工作一个警告。

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

关于python - SVR 模型 --> 特征缩放 - 预期的二维数组,取而代之的是一维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50426857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com