Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo L2 Normalization in sklearn python

Once I normalized my data with an sklearn l2 normalizer and use it as training data: How do I turn the predicted output back to the "raw" shape?

In my example I used normalized housing prices as y and normalized living space as x. Each used to fit their own X_ and Y_Normalizer.

The y_predict is in therefore also in the normalized shape, how do I turn in into the original raw currency state?

Thank you.

like image 663
Herka Avatar asked Apr 13 '16 09:04

Herka


2 Answers

If you are talking about sklearn.preprocessing.Normalizer, which normalizes matrix lines, unfortunately there is no way to go back to original norms unless you store them by hand somewhere.

If you are using sklearn.preprocessing.StandardScaler, which normalizes columns, then you can obtain the values you need to go back in the attributes of that scaler (mean_ if with_mean is set to True and std_)

If you use the normalizer in a pipeline, you wouldn't need to worry about this, because you wouldn't modify your data in place:

from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import Normalizer

# classifier example
from sklearn.svm import SVC

pipeline = make_pipeline(Normalizer(), SVC())
like image 145
eickenberg Avatar answered Oct 17 '22 06:10

eickenberg


Thank you very much for your answer, I didn't know about the pipeline feature before

For the case of L2 normalization turns out you can do it manually. Here is one example for a small array:

x = np.array([5, 8 , 12, 15])

#Using Sklearn
normalizer_x = preprocessing.Normalizer(norm = "l2").fit(x)
x_norm = normalizer_x.transform(x)[0]
print x_norm

>array([ 0.23363466,  0.37381545,  0.56072318,  0.70090397])

Or do it manually with the weight of the squareroot of the squaresum:

#Manually
w = np.sqrt(sum(x**2))
x_norm2 = x/w
print x_norm2

>array([ 0.23363466,  0.37381545,  0.56072318,  0.70090397])

So turning them "back" to the raw formate is simple by multiplying with "w".

like image 30
Herka Avatar answered Oct 17 '22 06:10

Herka