简体   繁体   English

Sklearn 用 StandardScaler 拟合 SVM

[英]Sklearn fitting SVM with StandardScaler

please am fitting svr on my dataset and am getting this error message.请在我的数据集上拟合 svr 并收到此错误消息。 it was working when I have not included standardscaler.当我没有包含标准缩放器时,它正在工作。 I have tried all means but still not working.我已经尝试了所有方法,但仍然无法正常工作。

from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
sc_y = StandardScaler()
X = sc_X.fit_transform(X)
y = sc_y.fit_transform(np.array(y).reshape(1,-1))

from sklearn.svm import SVR
regressor = SVR(kernel = 'rbf')
regressor.fit(X,y)`

    --------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-14-75416c35e495> in <module>
      2 from sklearn.svm import SVR
      3 regressor = SVR(kernel = 'rbf') # rbf means radial basis function
----> 4 regressor.fit(X,y)

C:\anconda\lib\site-packages\sklearn\svm\_base.py in fit(self, X, y, sample_weight)
    146         X, y = check_X_y(X, y, dtype=np.float64,
    147                          order='C', accept_sparse='csr',
--> 148                          accept_large_sparse=False)
    149         y = self._validate_targets(y)
    150 

C:\anconda\lib\site-packages\sklearn\utils\validation.py in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
    758                         dtype=None)
    759     else:
--> 760         y = column_or_1d(y, warn=True)
    761         _assert_all_finite(y)
    762     if y_numeric and y.dtype.kind == 'O':

C:\anconda\lib\site-packages\sklearn\utils\validation.py in column_or_1d(y, warn)
    795         return np.ravel(y)
    796 
--> 797     raise ValueError("bad input shape {0}".format(shape))
    798 
    799 

ValueError: bad input shape (1, 10)


You are feeding to the SVM a target vector with dimension (1,10) which means one row and ten columns, this is wrong and it's caused by you're using of reshaping in您正在向 SVM 提供一个尺寸为 (1,10) 的目标向量,这意味着一行十列,这是错误的,这是由于您使用了重塑

y = sc_y.fit_transform(np.array(y).reshape(1,-1))

Please note that this line is also conceptually wrong, the standardised should be applied only on the training features, not on the target vector, so you can avoid to define请注意,这条线在概念上也是错误的,标准化应该只应用于训练特征,而不是目标向量,所以你可以避免定义

sc_y = StandardScaler()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM