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


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


  • $\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
    Commented 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
    Commented May 10, 2020 at 18:52
  • 2
    $\begingroup$ No need to scale y. Not only in SVM but in any algorithm. $\endgroup$
    – 10xAI
    Commented May 11, 2020 at 4:16

2 Answers 2


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

  • $\begingroup$ I am following a tutorial were they scale the data (both the X and teh y) to get better results. $\endgroup$
    – MyName
    Commented 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$ Commented May 10, 2020 at 21:40

To answer your question:

  • Firstly, when separating data into X and Y, you need to drop the target column while selecting X and select only the target column as Y.
  • Secondly, you don't need to scale your target column. But if you have outliers in it, then you use Box-Cox or log1e or sqrt to transform your target column into Gaussian format. But when predicting, make sure that you reversed your data to its original format.
  • Finally, about the error, it doesn't look like the number of columns in train set and test set are matching. So make sure that you have the similar number of columns for prediction.
  • General/Best way to do scaling:

    from sklearn.preprocessing import StandardScaling
    X_train = df_train.drop('target', axis = 1)
    y_train = df_train['target']
    X_test = df_test # Assuming that there is no target column in test set
    sc = StandardScalar()
    X_train = sc.fit_transform(X_train)
    X_test = sc.transform(X_test)
# ===== Here comes your SVR model ===== #
    predict = model.predict(X_test)

I hope this works fine.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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