Scikit-learn: How to normalize row values horizontally?

Question:

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
Asked By: Xiong89

||

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
Answered By: 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.

Answered By: nxpnsv
from sklearn.preprocessing import MinMaxScaler
data = np.array([[1  , 2 ,  0 ,  4  , 3], 
                [3  , 2 ,  1,   1,   0],
                [2,   1 ,  1  , 0  , 1]])
scaler = MinMaxScaler()
print(data)
print(scaler.fit_transform(data.T).T)# row-wise transform
Answered By: Sharukh Rahman
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.