0

I have simple code below. I want to normalize all data together like one column. But I have so:

from sklearn.preprocessing import MinMaxScaler

data = [[-1, 2], [-0.5, 6], [0, 10], [1, 18]]
scaler = MinMaxScaler()
print(data)
print(scaler.fit_transform(data))

[[-1, 2],
 [-0.5, 6],
 [0, 10],
 [1, 18]]

[[0.   0.  ]
 [0.25 0.25]
 [0.5  0.5 ]
 [1.   1.  ]]

Python separate columns and separately normalize it. And so we have the same values in two columns. In principle I would able to merge first and second columns in one and split after. But I don't thing that idea are the best.

Any ideas are welcome...

UPDATE:

I don't want normalized data per column. I want so.

from sklearn.preprocessing import MinMaxScaler

data = [[-1], [-0.5], [0], [1], [2], [6], [10], [18]]
scaler = MinMaxScaler()
print(data)
print(scaler.fit_transform(data))

[[-1], [-0.5], [0], [1], [2], [6], [10], [18]]

[[0.        ]
 [0.02631579]
 [0.05263158]
 [0.10526316]
 [0.15789474]
 [0.36842105]
 [0.57894737]
 [1.        ]]

But I still want it will be two columns.

Like this:

[[0.        , 0.15789474]
 [0.02631579, 0.36842105]
 [0.05263158, 0.57894737]
 [0.10526316, 1.        ]]
Pro
  • 673
  • 2
  • 6
  • 15
  • can this answer your question: https://stackoverflow.com/questions/29661574/normalize-numpy-array-columns-in-python – gretal Nov 12 '21 at 05:22
  • @gretal This answer don't suite for me or I don't understand how It help me. Or you don't understand my question. I update post. – Pro Nov 12 '21 at 06:52

1 Answers1

1

In your case, instead of using sklearn module, you should simply apply the min-max scaler transformation formula :

import numpy as np

data = [[-1, 2], [-0.5, 6], [0, 10], [1, 18]]
print(data)
print((data-np.min(data))/(np.max(data)-np.min(data))) 

[[-1, 2], [-0.5, 6], [0, 10], [1, 18]]

[[0.         0.15789474]
 [0.02631579 0.36842105]
 [0.05263158 0.57894737]
 [0.10526316 1.        ]]
Lue Mar
  • 442
  • 7
  • 10
  • Thanks a lot for answer, I see in my case I have to write normalized function by myself and `sklearn` don't suit me - like in your answer. Or I have to use one column. – Pro Nov 12 '21 at 08:37
  • Yes, as sklearn's min-max-scaler is especially built to normalize following the columns (axis=0 when you look at the docs: https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html) – Lue Mar Nov 12 '21 at 08:55