简体   繁体   中英

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.

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())

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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM