1
$\begingroup$

I'm trying to use SVR to predict a certain feature. I create the model with the following code:

from sklearn.svm import SVR
from sklearn.preprocessing import StandardScaler

X = data
# this is the outcome variable
y = data.iloc[:, 10].values

sc_X = StandardScaler()
sc_y = StandardScaler()

X2 = sc_X.fit_transform(X)
y = sc_y.fit_transform(y.reshape(-1, 1))

# my_custom_kernel looks at certain columns of X2 / scaled data
my_regressor = SVR(kernel=my_custom_kernel) 
my_regressor = regressor.fit(X2, y)

After creating the model, I want to test it to see if the prediction is good. The first thing the code does is scale the row that I want to test, using the same scaler as above (sc_X). Then I try to reverse the scaling of the prediction result (using sc_y). During this process, I get datatype errors. Here is the code:

line1 = X.iloc[0].as_matrix().reshape(1, -1)
line1_scaled = sc_X.fit_transform(line1)
res = regressor.predict (line1_scaled)
pred_line1 = sc_y.inverse_transform (res) # The error appears to be here

Error:

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

Thanks.

$\endgroup$
3
  • $\begingroup$ What is the reason that you scale the var in the first place? I thought SVMs weren't necessarily susceptible to scaling issues. $\endgroup$
    – mccurcio
    May 10, 2020 at 17:35
  • 1
    $\begingroup$ @oaxacamatt I am following a tutorial where they scale the data. Originally I did not scale it, but the "prediction" values were very high and very far away from the expected values, so I decided to try the scaling. $\endgroup$
    – MyName
    May 10, 2020 at 18:52
  • 1
    $\begingroup$ No need to scale y. Not only in SVM but in any algorithm. $\endgroup$
    – 10xAI
    May 11, 2020 at 4:16

1 Answer 1

0
$\begingroup$

First , I am not sure why you want to scale the "Y" value at all? Inverse transformation is to scale back features of X mainly. https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html#sklearn.preprocessing.MinMaxScaler.inverse_transform

Second, You are passing only 1 column of X now for prediction and your sc_y remembers the shape you passed it earlier. Your error is telling you the difference also. One has(24,) and another(1,).

$\endgroup$
2
  • $\begingroup$ I am following a tutorial were they scale the data (both the X and teh y) to get better results. $\endgroup$
    – MyName
    May 10, 2020 at 20:44
  • 1
    $\begingroup$ Okay. Couple of things. have you checked the corrected code the author provided?github.com/sametgirgin/Machine-Learning-Regression-Models/blob/…. It has scaling on target attribute. In addition, I am not able to reproduce your error.Could you attach the sample data you are using? $\endgroup$ May 10, 2020 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.