# Normalize matrix in Python numpy

I've an array like this:

array([[ 0,  1],
[ 2,  3],
[ 4,  5],
[ 6,  7],
[ 8,  9],
[10, 11],
[12, 13],
[14, 15]])

I want to make normalize this array between -1 and 1. I'm currently using numpy as a library.

nazz's answer doesn't work in all cases and is not a standard way of doing the scaling you try to perform (there are an infinite number of possible ways to scale to [-1,1] ). I assume you want to scale each column separately:

1) you should divide by the absolute maximum:

arr = arr - arr.mean(axis=0)
arr = arr / np.abs(arr).max(axis=0)

2) But if the maximum of one column is 0 (which happens when the column if full of zeros) you'll get an error (you can't divide by 0).

arr = arr - arr.mean(axis=0)
safe_max = np.abs(arr).max(axis=0)
safe_max[safe_max==0] = 1
arr = arr / safe_max

Still, this is not the standard way to do this. You're trying to do some "Feature Scaling" see here

Then the formula is:

import numpy as np

def scale(X, x_min, x_max):
nom = (X-X.min(axis=0))*(x_max-x_min)
denom = X.max(axis=0) - X.min(axis=0)
denom[denom==0] = 1
return x_min + nom/denom

X = np.array([
[ 0,  1],
[ 2,  3],
[ 4,  5],
[ 6,  7],
[ 8,  9],
[10, 11],
[12, 13],
[14, 15]
])
X_scaled = scale(X, -1, 1)
print(X_scaled)

Result:

[[-1.         -1.        ]
[-0.71428571 -0.71428571]
[-0.42857143 -0.42857143]
[-0.14285714 -0.14285714]
[ 0.14285714  0.14285714]
[ 0.42857143  0.42857143]
[ 0.71428571  0.71428571]
[ 1.          1.        ]]

If you want to scale the entire matrix (not column wise), then remove the axis=0 and change the lines denom[denom==0] = 1 for denom = denom + (denom is 0).

Suppose you have an array arr. You can normalize it like this:

arr = arr - arr.mean()
arr = arr / arr.max()

You first subtract the mean to center it around $$0$$, then divide by the max to scale it to $$[-1, 1]$$.