1

I have the following numpy array:

from sklearn.decomposition import PCA
from sklearn.preprocessing import normalize
import numpy as np

# Tracking 4 associate metrics
# Open TA's, Open SR's, Open SE's
associateMetrics = np.array([[111,  28,  21],
   [ 27,  17,  20],
   [ 79,  23,  17],
   [185, 125,  50],
   [155,  76,  32],
   [ 82,  24,  17],
   [127,  63,  33],
   [193,  91,  63],
   [107,  24,  17]])

Now, I want to normalize every 'column' so that the values are between 0 and 1. What I mean is that the values in the 1st column for example should be between 0 and 1.

How do i do this?

normed_matrix = normalize(associateMetrics, axis=1, norm='l1')

the above gives me rowwise normalization

3
  • "will the above work?" Did you try? Commented Feb 6, 2016 at 23:29
  • Do you want to divide each column by its maximum? Commented Feb 6, 2016 at 23:29
  • I think i have my answer.I tried putting axis=0 and it worked. the documentation wasn't clear for me i guess...scikit-learn.org/stable/modules/generated/… Commented Feb 6, 2016 at 23:30

1 Answer 1

2

I was able to do this using the following:

normalized_metrics = normalize(associateMetrics, axis=0, norm='l1')
Sign up to request clarification or add additional context in comments.

4 Comments

An axis in scipy specifies the dimension you are working on. For 2D data (matrxi), axis = 0 means across rows and axis = 1 means across columns. docs.scipy.org/doc/numpy-1.10.1/glossary.html
thank you! If I may ask an additional question. How would I multiply every column by different scalar values? What I want to do is assign different weights to each column. Should i do this after normalizaiotn? If yes, how? sorry I am a bit new to python
You can use numpy.multiply for element-wise multiplication of the weight vector by your matrix. Perhaps np.multiply(v, m) where v is your vector and m is your matrix. docs.scipy.org/doc/numpy-1.10.1/reference/generated/…
Sorry, for column-wise multiplication you might need np.multiply(v, m.transpose()).transpose()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.