y\u预测=回归。预测(sc_X.transform(6.5))不工作

2024-06-01 03:49:30 发布

您现在位置:Python中文网/ 问答频道 /正文

每当我要预测的时候,我都会看到一个错误。 {I}在代码行中卡住了。在

我得到了一个错误:

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.

斯派德

# SVR

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

# Fitting SVR to the dataset
from sklearn.svm import SVR
regressor = SVR(kernel = 'rbf')
regressor.fit(X, y)

# Predicting a new result
y_pred = regressor.predict(6.5)

错误:y\u pred=回归。预测(超临界变换(6.5))

^{pr2}$

Tags: theimportyourdataifas错误array
2条回答

嗯,很明显,因为回归者.predit()需要一个值列表/数组来预测,而您将传递给它一个浮点,则它将不起作用:

# Predicting a new result
y_pred = regressor.predict(6.5)

至少:

^{pr2}$

但你可能有更多的东西想传递给它,所以更像:

# Predicting a new result
y_pred = regressor.predict(some_data_array)

编辑:

您需要排列传递给预测器的二维数组的形状,使其看起来像这样:

数据=[[1,0,0,1],[0,1,12,5],…]

其中[1,0,0,1]是要预测的一个数据点的一组参数。[0,1,12,5)它是另一个数据点。在

无论如何,它们都应该具有相同的特征(例如,在我的例子中是4个),并且它们应该具有与您用来训练预测者的数据相同的特征数量。在

y_pred = sc_Y.inverse_transform(regressor.predict(sc_X.transform(np.array([[6.5]]))))

相关问题 更多 >