Last active
November 7, 2017 12:31
-
-
Save fedden/bf5a8d2b118aa9c13637263669f6d61b to your computer and use it in GitHub Desktop.
NumPy Norms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> import numpy as np | |
>>> # Create five vectors of size two to compute the norms for. | |
>>> values = np.array([[v, v] for v in np.arange(0.0, 1.0, 0.2)]) | |
>>> values | |
array([[ 0. , 0. ], | |
[ 0.2, 0.2], | |
[ 0.4, 0.4], | |
[ 0.6, 0.6], | |
[ 0.8, 0.8]]) | |
>>> # For each row, compute the L2 norm. | |
>>> l2 = np.linalg.norm(values, ord=2, axis=1, keepdims=True) | |
>>> l2 | |
array([[ 0. ], | |
[ 0.28284271], | |
[ 0.56568542], | |
[ 0.84852814], | |
[ 1.13137085]]) | |
>>> # For each row, compute the L1 norm. | |
>>> l1 = np.linalg.norm(values, ord=1, axis=1, keepdims=True) | |
>>> l1 | |
array([[ 0. ], | |
[ 0.4], | |
[ 0.8], | |
[ 1.2], | |
[ 1.6]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment