def normalize(a, axis=None):
tmp = a - np.min(a, axis=axis)
out = tmp / np.ptp(tmp, axis=axis)
return out
a = np.arange(25).reshape(5,5)
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
normalize(a, axis=0)
array([[0. , 0. , 0. , 0. , 0. ],
[0.25, 0.25, 0.25, 0.25, 0.25],
[0.5 , 0.5 , 0.5 , 0.5 , 0.5 ],
[0.75, 0.75, 0.75, 0.75, 0.75],
[1. , 1. , 1. , 1. , 1. ]])
normalize(a, axis=1)
array([[ 0. , -0.25, -0.5 , -0.75, -1. ],
[ 0.31, 0.06, -0.19, -0.44, -0.69],
[ 0.62, 0.38, 0.12, -0.12, -0.38],
[ 0.94, 0.69, 0.44, 0.19, -0.06],
[ 1.25, 1. , 0.75, 0.5 , 0.25]])
normalize(a, axis=None)
array([[0. , 0.04, 0.08, 0.12, 0.17],
[0.21, 0.25, 0.29, 0.33, 0.38],
[0.42, 0.46, 0.5 , 0.54, 0.58],
[0.62, 0.67, 0.71, 0.75, 0.79],
[0.83, 0.88, 0.92, 0.96, 1. ]])