Home > OS >  normalize the rows of numpy array based on a custom function
normalize the rows of numpy array based on a custom function

Time:03-17

I have an numpy array. I want to normalized each rows based on this formula

x_norm = (x-x_min)/(x_max-x_min)

, where x_min is the minimum of each row and x_max is the maximum of each row. Here is a simple example:

a = np.array(
         [[0, 1 ,2],
          [2, 4 ,7],
          [6, 10,5]
 ])

and desired output:

a = np.array([
          [0, 0.5 ,1],
          [0, 0.4 ,1],
          [0.2, 1 ,0]
    ])

Thank you

CodePudding user response:

IIUC, you can use raw numpy operations:

x = np.array(
         [[0, 1 ,2],
          [2, 4 ,7],
          [6, 10,5]
 ])

x_norm = ((x.T-x.min(1))/(x.max(1)-x.min(1))).T
# OR
x_norm = (x-x.min(1)[:,None])/(x.max(1)-x.min(1))[:,None]

output:

array([[0. , 0.5, 1. ],
       [0. , 0.4, 1. ],
       [0.2, 1. , 0. ]])

NB. if efficiency matters, save the result of x.min(1) in a variable as it is used twice

CodePudding user response:

You could use np.apply_along_axis


a = np.array(
         [[0, 1 ,2],
          [2, 4 ,7],
          [6, 10,5]
 ])

def scaler(x):
    return (x-x.min())/(x.max()-x.min())   
    
np.apply_along_axis(scaler, axis=1, arr=a)

Output:

array([[0. , 0.5, 1. ],
       [0. , 0.4, 1. ],
       [0.2, 1. , 0. ]])

CodePudding user response:

You can use this. However, it may not be efficeint when you have a large array.

a_new = np.zeros((a.shape[0], a.shape[1]))
for i in range(a.shape[0]):
    Max = a[i,:].max()
    Min = a[i,:].min()
    for j in range(a.shape[1]):
        a_new[i,j] = float((a[i,j]-Min)/(Max-Min))

CodePudding user response:

you can use this line of code, its not exactly same as the function, but it is close to it:

a/np.linalg.norm(a, ord=2, axis=1, keepdims=True)
  • Related