TransWikia.com

How to normalize my array between 0 and 1 by column and by line

Stack Overflow Asked by user13984029 on December 16, 2021

I have an array and need to normalize it in a way that the results will be numbers between 0 and 1. I already normalized the entire array as follows:

C = A / A.max(axis=0)

print(C)
____________________________________________________________________
[[0.         0.05263158 0.1        0.14285714 0.18181818 0.2173913 ]
 [0.33333333 0.36842105 0.4        0.42857143 0.45454545 0.47826087]
 [0.66666667 0.68421053 0.7        0.71428571 0.72727273 0.73913043]
 [1.         1.         1.         1.         1.         1.        ]]

But now I have to normalize by column and by line. How can I do that with axis reduction? If there is a better way to what I did, suggest me alterations.

My expected result is two arrays with the values normalized. One considering the columns and the other by the lines

This is my data

A = [[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]

2 Answers

You skip the minimum part. Normally a 0-1 normalization demanding subtracting a minimum value from denominator and numerator. https://stats.stackexchange.com/questions/70801/how-to-normalize-data-to-0-1-range

import numpy as np

A = np.matrix([[ 0,  1,  2,  3,  4,  5],
 [ 6,  7,  8,  9, 10, 11],
 [12, 13, 14, 15, 16, 17],
 [18, 19, 20, 21, 22, 23]])

(A-A.min(axis=1))/(A.max(axis=1)-A.min(axis=1))
(A-A.min(axis=0))/(A.max(axis=0)-A.min(axis=0))

Answered by polkas on December 16, 2021

My expected result is two arrays with the values normalized. One considering the columns and the other by the lines

a = np.array([[ 0,  1,  2,  3,  4,  5],
              [ 6,  7,  8,  9, 10, 11],
              [12, 13, 14, 15, 16, 17],
              [18, 19, 20, 21, 22, 23]])

If

c = a / a.max(axis=0)

gives you what you want for the columns then

d = a / a.max(axis=1)[:,None]  

will suffice for the rows.

>>> d.round(4)
array([[0.    , 0.2   , 0.4   , 0.6   , 0.8   , 1.    ],
       [0.5455, 0.6364, 0.7273, 0.8182, 0.9091, 1.    ],
       [0.7059, 0.7647, 0.8235, 0.8824, 0.9412, 1.    ],
       [0.7826, 0.8261, 0.8696, 0.913 , 0.9565, 1.    ]])

https://numpy.org/doc/stable/user/basics.broadcasting.html

Answered by wwii on December 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP