简体   繁体   English

对于 Python 中的 SVR 代码中的此错误,我该怎么办

[英]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.

相关问题 我该怎么做才能在Python代码中修复NoSuchElementException错误? - What should i do to fix the NoSuchElementException error in my python code? 一段 Python 代码中的算法错误 - 我不能做我必须做的事 - Algorithmic error in a piece of Python code - I can not do what I have to do 运行我的机器人的python代码会返回语法错误,但我不知道该错误是什么 - Running the python code of my robot returned a syntax error and I do not know what the error is 我需要对我的python代码做什么才能让它成为一个模块? - What do I need to do to my python code to get it to be a module? 我的 Python 代码出现数学域错误,不知道该怎么做 - I'm getting a math domain error in my Python code and have no clue what to do 为什么我的python代码的运行时间这么长,我该怎么做才能让它运行得更快? - Why is the runtime of my python code so long, and what can I do to make it run faster? 我能做些什么来修复我的简单电脑游戏代码? 语法错误 - What can I do to fix my simple computer-player game code? Syntax Error 我的python代码有什么错误? - what is the error with my python code? 我的代码有什么问题? 我该如何处理这个错误? - What is the issue with my code? How do I deal with this error? 我的代码中的django错误,我该怎么办 - django error in my code ,what does i do
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM