# 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]]



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.    ]])


Answered by wwii on December 16, 2021

## Related Questions

### problem writing a recursive function in C (digits order in a nunber)

1  Asked on January 7, 2021

### How to migrate repos from SVN to Gerrit?

0  Asked on January 7, 2021 by fresher

### Text File Problems

1  Asked on January 7, 2021 by taco_attack

### Is there a way to make htaccess return a blank page on a matching URL?

1  Asked on January 7, 2021 by dan-goodspeed

### Is there an easy way to get out of this IF loop?

3  Asked on January 7, 2021 by matias

### How to proxy image with Netlify Lambda

0  Asked on January 6, 2021 by xaver-fleer

### a method that converts a daynumber into a date

4  Asked on January 6, 2021 by leenieke19

### What is the shape of output of the below model?

0  Asked on January 6, 2021 by mohan

### Rails devise cache refreshed at every request

2  Asked on January 6, 2021 by sparkle

### How to post data to web page form and get response using php

1  Asked on January 6, 2021 by abdulrahman-mohamed

### The canonical link points to the site root error

1  Asked on January 6, 2021 by charklewis

### React State value not updated in Arrow functional component

2  Asked on January 6, 2021

### Deep learning model not giving predictions as input layer is incompatible

1  Asked on January 6, 2021 by bashkash

### Groupby one column and find duplicates from another column then return indications in Python

2  Asked on January 6, 2021 by ahbon

### click() doesn’t work as enter the button by mouse manually

0  Asked on January 6, 2021 by user3051274

### How to get browser name using Selenium WebDriver with Java?

5  Asked on January 6, 2021 by swathi-chowdary

### How to replace text in a specific line between first appearence of a symbol and second appearence of it

3  Asked on January 5, 2021 by brun

### Use of enum and dictionary when search by value and search by key in equal proportions in python

1  Asked on January 5, 2021 by r2_d2

### Leaflet Map Tiles work on computer browsers, show only grey screen on phone and tablet screens

1  Asked on January 5, 2021 by shalaw-fatah

### i am making a ball rolling game and i added jumping but for some reason i can jump infinite times

1  Asked on January 5, 2021 by saif7899