9951 explained code solutions for 126 technologies


python-scipyHow do I calculate the norm of a numpy array in Python?


The norm of a numpy array is the length of the array, which can be calculated using the numpy.linalg.norm() function.

import numpy as np

arr = np.array([1, 2, 3])

norm = np.linalg.norm(arr)

print(norm)

Output example

3.7416573867739413

The code above:

  • import numpy as np: imports the numpy library into the script
  • arr = np.array([1, 2, 3]): creates a numpy array from the list of numbers
  • norm = np.linalg.norm(arr): calculates the norm of the numpy array
  • print(norm): prints the result

Helpful links

Edit this code on GitHub