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.

Topic svr reshape jupyter feature-scaling python

Category Data Science


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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.