Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scikit-learn: How to normalize row values horizontally?

I would like to normalize the values below horizontally instead of vertically. The code read csv file provided after the code and output a new csv file with normalized values. How to make it normalize horizontally? Given the code as below:

Code

#norm_code.py
#normalization = x-min/max-min

import numpy as np
from sklearn import preprocessing
all_data=np.loadtxt(open("c:/Python27/test.csv","r"),
delimiter=",",
skiprows=0,
dtype=np.float64)

x=all_data[:]

print('total number of samples (rows):', x.shape[0])
print('total number of features (columns):', x.shape[1])
minmax_scale = preprocessing.MinMaxScaler(feature_range=(0, 1)).fit(x)

X_minmax=minmax_scale.transform(x)
with open('test_norm.csv',"w") as f:
    f.write("\n".join(",".join(map(str, x)) for x in (X_minmax)))

test.csv

1   2   0   4   3
3   2   1   1   0
2   1   1   0   1
like image 467
Xiong89 Avatar asked Jan 13 '16 06:01

Xiong89


2 Answers

You can simply operate on the transpose, and take a transpose of the result:

minmax_scale = preprocessing.MinMaxScaler(feature_range=(0, 1)).fit(x.T)

X_minmax=minmax_scale.transform(x.T).T
like image 105
Ami Tavory Avatar answered Sep 18 '22 22:09

Ami Tavory


Oneliner answer without use of sklearn:

X_minmax = np.transpose( (x-np.min(x,axis=1))/(np.max(x, axis=1)-np.min(x,axis=1)))

This is about 8x faster than using the MinMaxScaler from preprocessing.

like image 30
nxpnsv Avatar answered Sep 19 '22 22:09

nxpnsv