matrix normalisation between -1 and 1

Viewed 50

I am using python and I have a matrix and would like to normalize individual columns in it between [-1,1]

a = matrix
([[3, 2, 4, 6]
  [4, 5, 6, 5]
  [6, 4, 5, 3]
  [3, 5, 6, 7]])

I applied

a = a / np.linalg.norm(a, axis=0, keepdims=True)

and the a was normalized between [0,1]

however, I would like to do something like .apply(lambda x: np.where(x>0,x/x.max(),np.where(x<0,-x/x.min(),x))) so that it can get normalised between [-1,1] and the zero position is the same.

but it is not possible to use .apply in a matrix, how can I overcome it?

1 Answers
import numpy as np
matrix = np.array([[1, 2, 3],[-1, -2, -3]])

maxElement = np.amax(matrix)

new_matrix = matrix/maxElement
print(new_matrix)

Find the largest element in the matrix and divide it with all elements

Related