Normalized back theta after linear regression

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 ?

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

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

• 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? Mar 10, 2016 at 1:11
• 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. Mar 10, 2016 at 14:13
• Yes, its from Cousera. Thanks for your attention, Moebius! Mar 18, 2016 at 1:25