# Scalling and unscalling of data for SVR prediction

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.

• What is the reason that you scale the var in the first place? I thought SVMs weren't necessarily susceptible to scaling issues. May 10 '20 at 17:35
• @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. May 10 '20 at 18:52
• No need to scale y. Not only in SVM but in any algorithm. May 11 '20 at 4:16

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,).