[英]What can I do for this error in my SVR code in Python
I have the following code for SVR regressror:我有以下 SVR 回归器代码:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('E:\\/Position_Salaries.csv')
X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2].values
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
sc_y = StandardScaler()
X = sc_X.fit_transform(X)
y = sc_y.fit_transform(y)
from sklearn.svm import SVR
regressor = SVR(kernel = 'rbf')
regressor.fit(X, y)
y_pred = regressor.predict(6.5)
y_pred = sc_y.inverse_transform(y_pred)
But there's the following error:但是有以下错误:
ValueError: Expected 2D array, got scalar array instead:
array=6.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.
This is Position_Salaries.csv这是 Position_Salaries.csv
What can I do for that?我能为此做些什么? Thanks in advance.提前致谢。
The traceback really explains the issue: the predict
method expects a bidimensional array so you can either reshape your input as it suggests:回溯确实解释了这个问题: predict
方法需要一个二维数组,因此您可以按照它的建议重塑您的输入:
y_pred = regressor.predict(np.array([6.5]).reshape(1,1))
or even pass a 2D version of your argument already:甚至已经传递了您的论点的 2D 版本:
y_pred = regressor.predict(np.array([[6.5]]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.