【问题标题】:y_pred = regressor.predict(sc_X.transform(6.5)) not workingy_pred = regressor.predict(sc_X.transform(6.5)) 不工作
【发布时间】:2019-12-06 02:15:26
【问题描述】:

每当我要预测时,我都会看到一个错误。 我被代码中的y_pred = regressor.predict(6.5) 卡住了。

我收到错误:

ValueError: 预期的二维数组,得到的是标量数组: 数组=6.5。 如果您的数据具有单个特征,则使用 array.reshape(-1, 1) 重塑您的数据,如果数据包含单个样本,则使用 array.reshape(1, -1)。

间谍

# 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_pred = regressor.predict(sc_X.transform(6.5))

Traceback (most recent call last):

  File "<ipython-input-11-64bf1bca4870>", line 1, in <module>
    y_pred = regressor.predict(sc_X.transform(6.5))

  File "C:\Users\achiever\Anaconda3\lib\site-packages\sklearn\preprocessing\data.py", line 758, in transform
    force_all_finite='allow-nan')

  File "C:\Users\achiever\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 514, in check_array
    "if it contains a single sample.".format(array))

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.

【问题讨论】:

  • 伙计,没有数据样本,所有这些努力都是徒劳的。 X的尺寸是多少?当然,它应该是一个二维数组。
  • 您在代码中的行是y_pred = regressor.predict(6.5),但错误显示为y_pred = regressor.predict(sc_X.transform(6.5))

标签: python spyder


【解决方案1】:

很明显,因为 regressor.preedit() 需要一个值列表/数组来进行预测,并且你传递给它一个浮点数,所以它不起作用:

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

至少:

# Predicting a new result
y_pred = regressor.predict(np.array([6.5]))

但大概你有更多的东西想要传递给它,所以更像:

# 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 = regressor.predict(np.array([6.5]).reshape(-1,1)
  • 它仍然给我错误:y_pred = regressor.predict(np.array([6.5]).reshape(-1,1) File "", line 1 y_pred = regressor.predict(np.array([6.5]).reshape(-1,1) ^ SyntaxError: unexpected EOF while parsing
  • @SOUVIKDATTA:我们没有你的数据。我不知道你的数据中有多少特征(参数),所以我不能准确地告诉你你需要如何格式化你的代码。要么发布示例数据,要么自己弄清楚如何格式化。但基本上 some_data_array 的形状需要是 (number_of_sample_to_predict, number_of_parameters_in_data)
  • 最后一个错误意味着您的文件格式错误。那是另一个问题/不相关的事情。我建议您独立测试这些东西(例如打开文件并正确阅读等)。否则我们只是试图在一堆错误中导航,我们无法判断是什么导致了什么问题。
【解决方案2】:
y_pred = sc_Y.inverse_transform(regressor.predict(sc_X.transform(np.array([[6.5]]))))

【讨论】:

    【解决方案3】:

    使用整形功能:

    sc_y.inverse_transform(regressor.predict(sc_X.transform([[6.5]])).reshape(1,-1))
    

    【讨论】:

      猜你喜欢
      • 2021-09-03
      • 2022-06-15
      • 2018-06-18
      • 1970-01-01
      • 2014-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-11
      相关资源
      最近更新 更多