SVR ValueError:形状为(1,1)的不可广播输出操作数与广播形状(1,9)不匹配

问题描述 投票:0回答:1

我正在使用SVR来预测NBA幻想得分。我的indep变量添加到我的dep变量中。

import numpy as np
import pandas as pd

X是58行9列的数组。 9列的总和=第十列(Y)

DK = pd.read_csv('NV.csv')
X = DK.iloc[:, :-1].values.astype(float)
Y = DK.iloc[:,9].values.astype(float).reshape(-1,1)


#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 data and creating regressors

from sklearn.svm import SVR
regressor1 = SVR(kernel='rbf')
regressor1.fit(X,Y)

运行上面的命令时,会收到数据警告和以下输出

DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the         shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)

SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1, gamma='auto',
kernel='rbf', max_iter=-1, shrinking=True, tol=0.001, verbose=False)

预测新结果将导致我的代码遇到障碍

y_pred = sc_y.inverse_transform((regressor1.predict(sc_X.transform(np.array([[6.5]])))))

这将返回以下错误。是我如何拟合/缩放数据或完全不同的问题?

ValueError                                Traceback (most recent call last)
<ipython-input-63-9c7f8b557a70> in <module>
----> 1 y_pred = sc_y.inverse_transform((regressor1.predict(sc_X.transform(np.array([[1.9]])))))

~\Anaconda\lib\site-packages\sklearn\preprocessing\data.py in transform(self, X, copy)
767         else:
768             if self.with_mean:
--> 769                 X -= self.mean_
770             if self.with_std:
771                 X /= self.scale_

ValueError: non-broadcastable output operand with shape (1,1) doesn't match the broadcast shape (1,9)
python numpy scikit-learn svm
1个回答
0
投票

首先了解错误本身:

X -= self.mean_

我可以通过以下方式重现错误:

In [234]: X = np.array([[1.23]])                                                                       
In [235]: X.shape                                                                                      
Out[235]: (1, 1)
In [236]: X -= np.array([[1,2,3]])                                                                     
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-236-6854fd7cd554> in <module>
----> 1 X -= np.array([[1,2,3]])

ValueError: non-broadcastable output operand with shape (1,1) doesn't match the broadcast shape (1,3)

我要写的是将回溯备份到您自己的代码可能需要一些工作。但我看到了您自己的代码:

sc_X.transform(np.array([[6.5]]))

[np.array([[6.5]])最有可能是(1,1)数组,X

© www.soinside.com 2019 - 2024. All rights reserved.