0

please am fitting svr on my dataset and am getting this error message. 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)


jwalton
  • 5,286
  • 1
  • 18
  • 36
  • You just want to fit X. Not X and y. Transformers aren't made to have a predictor argument (y). See more in this answer: https://stackoverflow.com/a/54899803/4590385 – jeffhale Apr 04 '20 at 22:08

1 Answers1

0

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

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()
Edoardo Guerriero
  • 1,210
  • 7
  • 16