3

How to minmax normalize in the most efficient way, a XD-numpy array in "columns" of each 2D matrix of the array.

For example with a 3D-array :

a = np.array([[[  0,  10],
        [ 20,  30]],

       [[ 40,  50],
        [ 60,  70]],

       [[ 80,  90],
        [100, 110]]])

into the normalized array :

b = np.array([[[0., 0.],
      [1., 1.]],
     [[0., 0.],
      [1., 1.]],
     [[0., 0.],
      [1., 1.]]])

3 Answers 3

2

With sklearn.preprocessing.minmax_scale + numpy.apply_along_axis single applying:

from sklearn.preprocessing import minmax_scale

a = np.array([[[0, 10], [20, 30]], [[40, 50], [60, 70]], [[80, 90], [100, 110]]])
a_scaled = np.apply_along_axis(minmax_scale, 1, a)

# a_scaled
[[[0. 0.]
  [1. 1.]]

 [[0. 0.]
  [1. 1.]]

 [[0. 0.]
  [1. 1.]]]
Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks for your answer. This solve my problem and is scalable.
2
a_min = a.min(axis=-2, keepdims=True)
a_max = a.max(axis=-2, keepdims=True)
out = (a - a_min) / (a_max - a_min)

out:

array([[[0., 0.],
        [1., 1.]],

       [[0., 0.],
        [1., 1.]],

       [[0., 0.],
        [1., 1.]]])

1 Comment

I would add very small value to 'a_max - a_min' in case the evaluated column is like a constant vector. This would help to avoid division by zero. For ex: 'a_max - a_min + 1e-9'. The user also need to store the min and max values if they want to rescale the matrix
0

Broadcasting and simple list comprehension

f= lambda ar:(ar==ar.max(axis=0)[None,:]).astype(int)
b = np.array([f(x) for x in a], dtype=float)
print(b)

This can also be done using numpy.apply_along_axis as follows:

ar = np.array([[[0, 10], [20, 30]], [[40, 50], [60, 70]], [[80, 90], [100, 110]]])

def f(a):
   a = a.reshape(2,2)
   return (a==a.max(axis=0)[None,:]).astype(int)

ar = ar.reshape(3,4)
b = np.apply_along_axis(f, 1, ar)

output

array([[[0., 0.],
        [1., 1.]],

       [[0., 0.],
        [1., 1.]],

       [[0., 0.],
        [1., 1.]]])

1 Comment

Many thanks, useful example of a lambda function applied to an array.

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.