Jurca Bg
Jurca Bg

Reputation: 27

How to normalize in numpy?

I have the following question: A numpy array Y of shape (N, M) where Y[i] contains the same data as X[i], but normalized to have mean 0 and standard deviation 1. I have mapped the array like this:

(X - np.mean(X)) / np.std(X) 

but it doesn't give me the correct answer.

Upvotes: 0

Views: 1265

Answers (2)

Gaston
Gaston

Reputation: 215

Use norm from linalg

https://numpy.org/doc/stable/reference/generated/numpy.linalg.norm.html

from numpy import linalg as LA

a = np.arange(9) - 4

LA.norm(a)
>>>7.745966692414834

Then you divide the array by the norm :

a/LA.norm(a)

Upvotes: 0

ShlomiF
ShlomiF

Reputation: 2895

You want to normalize along a specific dimension, for instance -

(X - np.mean(X, axis=0)) / np.std(X, axis=0) 

Otherwise you're calculating the statistics over the whole matrix, i.e. subtracting the global mean of all points/features and the same with the standard deviation.

Upvotes: 2

Related Questions