librosa.util.normalize¶
- librosa.util.normalize(S, norm=inf, axis=0)¶
Normalize the columns or rows of a matrix
Note
Columns/rows with length 0 will be left as zeros.
Parameters: S : np.ndarray [shape=(d, n)]
The matrix to normalize
norm : {np.inf, -np.inf, 0, float > 0, None}
np.inf : maximum absolute value
-np.inf : mininum absolute value
0 : number of non-zeros
- float : corresponding l_p norm.
See scipy.linalg.norm for details.
None : no normalization is performed
axis : int [scalar]
Axis along which to compute the norm. axis=0 will normalize columns, axis=1 will normalize rows. axis=None will normalize according to the entire matrix.
Returns: S_norm : np.ndarray [shape=S.shape]
Normalized matrix
Raises: ParameterError
If norm is not among the valid types defined above
Examples
>>> # Construct an example matrix >>> S = np.vander(np.arange(-2.0, 2.0)) >>> S array([[-8., 4., -2., 1.], [-1., 1., -1., 1.], [ 0., 0., 0., 1.], [ 1., 1., 1., 1.]]) >>> # Max (l-infinity)-normalize the columns >>> librosa.util.normalize(S) array([[-1. , 1. , -1. , 1. ], [-0.125, 0.25 , -0.5 , 1. ], [ 0. , 0. , 0. , 1. ], [ 0.125, 0.25 , 0.5 , 1. ]]) >>> # Max (l-infinity)-normalize the rows >>> librosa.util.normalize(S, axis=1) array([[-1. , 0.5 , -0.25 , 0.125], [-1. , 1. , -1. , 1. ], [ 0. , 0. , 0. , 1. ], [ 1. , 1. , 1. , 1. ]]) >>> # l1-normalize the columns >>> librosa.util.normalize(S, norm=1) array([[-0.8 , 0.667, -0.5 , 0.25 ], [-0.1 , 0.167, -0.25 , 0.25 ], [ 0. , 0. , 0. , 0.25 ], [ 0.1 , 0.167, 0.25 , 0.25 ]]) >>> # l2-normalize the columns >>> librosa.util.normalize(S, norm=2) array([[-0.985, 0.943, -0.816, 0.5 ], [-0.123, 0.236, -0.408, 0.5 ], [ 0. , 0. , 0. , 0.5 ], [ 0.123, 0.236, 0.408, 0.5 ]])