I am working on a data, for preding output, I used SVR by bellow code:

from sklearn.svm import SVR
regressor = SVR(kernel = 'linear')

from sklearn.metrics import r2_score
pred = regressor.predict(testX)

The answer is : [0.58439621 0.58439621 0.58439621 ... 0.81262134 0.81262134 0.81262134]. I'm trying to inverse the scaling to real amount.

I search it in StackOverflow and reach this: https://stackoverflow.com/questions/49330195/how-to-use-inverse-transform-in-minmaxscaler-for-a-column-in-a-matrix, I implement every 2 answers in my code, but I get error yet. Can anyone help me with this?

I write this from above source:

import sklearn
from sklearn.preprocessing import MinMaxScaler

but, I got same error as:

Blockquote Expected 2D array, got 1D array instead: array=[0.58439621 0.58439621 0.58439621 ... 0.81262134 0.81262134 0.81262134]. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample. Blockquote


1 Answer 1


From my understanding you are working on a regression task in which you have applied MainMaxScaler to your target variable y prior modeling.

If so you have two options:

  1. As the error message suggests, you can reshape the output with array.reshape(-1, 1)

  2. Scikit learn has implemented a class to work with transformations on target:

So just try

from sklearn.svm import SVR
from sklearn.compose import TransformedTargetRegressor
from sklearn.metrics import r2_score
from sklearn.preprocessing import MinMaxScaler

regressor = SVR(kernel = 'linear')

model = TransformedTargetRegressor(regressor= regressor,
                                        transformer = MinMaxScaler()

pred = model.predict(testX)

Every time you make a model.predict(X) it will apply an inverse transformation so that your predictions are in the same scale as prior MinMaxScaler


Working example of transformation without using Scikit-learn

# array example is between 0 and 1
array = np.array([0.58439621, 0.81262134, 0.231262134, 0.191])
#scaled from 100 to 250
minimo = 100
maximo = 250
array * minimo + (maximo - minimo)


array([208.439621 , 231.262134 , 173.1262134, 169.1])
  • $\begingroup$ Thanks. That was so helpful. I have a question, you know by normalization the pred scale is between 0 and 1. now, how could I transfer this scale to the data scale (real value). for example:[0.58439621 0.58439621 0.58439621 ... 0.81262134 0.81262134 0.81262134], the pred answer transfer to :[250 100 50 60 .....]. $\endgroup$
    – ramin
    Feb 18, 2021 at 8:20
  • $\begingroup$ thanks@Julio Jesus, you help me. $\endgroup$
    – ramin
    Feb 18, 2021 at 18:53

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.