2

My data is numpy ndarray with shape(2,3,4) following this: I've try to normalize 0-1 scale for each column through sklearn normalization.

from sklearn.preprocessing import normalize  

x = np.array([[[1, 2, 3, 4],
      [2, 2, 3, 4],
      [3, 2, 3, 4]],
      [[4, 2, 3, 4],
      [5, 2, 3, 4],
      [6, 2, 3, 4]]])

x.shape ==> ( 2,3,4) 

x = normalize(x, norm='max', axis=0, ) 

However, I catch the error :

ValueError: Found array with dim 3. the normalize function expected <= 2.

How do I solve this problem?

Thank you.

0

1 Answer 1

4

It seems scikit-learn expects ndarrays with at most two dims. So, to solve it would be to reshape to 2D, feed it to normalize that gives us a 2D array, which could be reshaped back to original shape -

from sklearn.preprocessing import normalize  

normalize(x.reshape(x.shape[0],-1), norm='max', axis=0).reshape(x.shape)

Alternatively, it's much simpler with NumPy that works fine with generic ndarrays -

x/np.linalg.norm(x, ord=np.inf, axis=0, keepdims=True)
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you so much!! However, the code above does not apply to the column by column, but to the whole data. Which option should be applied?
@ChrisJoo Not sure what you mean by column to column. Maybe you meant using it along axis=1 instead of axis=0?
eg. 1st column [ [1, 2, 3], [4, 5, 6] ] should be [ [ 0.1667, 0.3333, 0.5000], [ 0.6667 , 0.8333, 1.0000 ] ] and 2nd column( 2, 2, 2, 2, 2, 2) should be [ 1, 1, 1, 1, 1, 1 ].
That doesn't look like l2 norm, but a max norm and also along the last axis. So, use : x/np.linalg.norm(x, ord=np.inf,axis=-1,keepdims=1) or norm='max', with the sklearn version.
I've solved the problem.. You're a great help to me.. Thank you!! normalize(x.reshape(x.shape[1],-1), norm='max', axis=0).reshape(x.shape)
|

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.