This is how I work :

1- I get data

2- I scale those data :

def scale(to_scale):
    minimum = to_scale.min()
    maximum = to_scale.max()
    return (to_scale - minimum) / (maximum - minimum)

3- I run linear regression on those data and get the thetas (theta0, theta1). The problem is, those theta have a value that is not the one I want. I want those theta to be like if I hadn't scaled the datas before. How to do this ?

My code : https://gist.github.com/gbersac/a16c2191d58c20de03a9

  • $\begingroup$ Moebius, have you found a solution for this? I have this problem too. $\endgroup$ Mar 9, 2016 at 1:09
  • $\begingroup$ @Caaarlos I answered you in this thread. Not sure If my explications are clear. Don't be afraid to ask for a better answer ! $\endgroup$
    – Moebius
    Mar 9, 2016 at 13:46

1 Answer 1


The answer is to not normalize back the coeficients (thetas) after training. When you want to predict out of the theta, you must process the same normalization process to the data from which to predict as the one you processed on the training set.

Note that you don't normalize the final result. Only the datas.

First the normalization of datas before training :

# minmax normalization
Xs = X[:,1] # the dats
xmin = Xs.min()
xmax = Xs.max()
X = X.astype(float)
X[:,1] = (Xs.astype(float) - float(xmin)) / (float(xmax) - float(xmin))

Then the prediction function :

def predict(data, xmin, xmax, theta):
    #normalized data
    normd =  (float(data) - float(xmin)) / (float(xmax) - float(xmin))
    return theta[0, 0] + theta[0, 1] * normd

My full programs (very simple linear regression) : https://github.com/gbersac/linear_regression_42

  • $\begingroup$ Thanks for your reply. Now I understand what I have to do. Your code is relay easy to understand. Just for curiosity, is it possible to reverse normalized theta? And another thins, I dont if I should create another thread, but here it goes... I saw that you use the Max and Min difference. In my Machine Learning classes I use a function called std() instead MaxMin. It is on Octave. Do you know the difference between them? $\endgroup$ Mar 10, 2016 at 1:11
  • $\begingroup$ Is this the Coursera machine learning ? If it is, I did it long ago but don't remember what std is. For the minmax, you can't reverse normalized theta back to non nomalized. $\endgroup$
    – Moebius
    Mar 10, 2016 at 14:13
  • $\begingroup$ Yes, its from Cousera. Thanks for your attention, Moebius! $\endgroup$ Mar 18, 2016 at 1:25

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.