The function you're after is numpy.linalg.norm. (I reckon it should be in base numpy as a property of an array -- say x.norm() -- but oh well).
import numpy as np
x = np.array([1,2,3,4,5])
np.linalg.norm(x)
You can also feed in an optional ord for the nth order norm you want. Say you wanted the 1-norm:
np.linalg.norm(x,ord=1)
And so on.
Answer from mathematical.coffee on Stack Overflow Top answer 1 of 8
340
The function you're after is numpy.linalg.norm. (I reckon it should be in base numpy as a property of an array -- say x.norm() -- but oh well).
import numpy as np
x = np.array([1,2,3,4,5])
np.linalg.norm(x)
You can also feed in an optional ord for the nth order norm you want. Say you wanted the 1-norm:
np.linalg.norm(x,ord=1)
And so on.
2 of 8
135
If you are worried at all about speed, you should instead use:
mag = np.sqrt(x.dot(x))
Here are some benchmarks:
>>> import timeit
>>> timeit.timeit('np.linalg.norm(x)', setup='import numpy as np; x = np.arange(100)', number=1000)
0.0450878
>>> timeit.timeit('np.sqrt(x.dot(x))', setup='import numpy as np; x = np.arange(100)', number=1000)
0.0181372
EDIT: The real speed improvement comes when you have to take the norm of many vectors. Using pure numpy functions doesn't require any for loops. For example:
In [1]: import numpy as np
In [2]: a = np.arange(1200.0).reshape((-1,3))
In [3]: %timeit [np.linalg.norm(x) for x in a]
100 loops, best of 3: 4.23 ms per loop
In [4]: %timeit np.sqrt((a*a).sum(axis=1))
100000 loops, best of 3: 18.9 us per loop
In [5]: np.allclose([np.linalg.norm(x) for x in a],np.sqrt((a*a).sum(axis=1)))
Out[5]: True
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.ndarray.size.html
numpy.ndarray.size — NumPy v2.1 Manual
Number of elements in the array · Equal to np.prod(a.shape), i.e., the product of the array’s dimensions
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.linalg.norm.html
numpy.linalg.norm — NumPy v2.1 Manual
If axis is an integer, it specifies the axis of x along which to compute the vector norms. If axis is a 2-tuple, it specifies the axes that hold 2-D matrices, and the matrix norms of these matrices are computed. If axis is None then either a vector norm (when x is 1-D) or a matrix norm (when x is 2-D) is returned.
Codegive
codegive.com › blog › numpy_length_of_vector.php
Numpy length of vector
For example, a 2D vector v = [x, ... irrespective of its direction. When working with NumPy arrays, a very common mistake for beginners is to use the built-in len() function to find the vector's mathematical length....
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-93.php
NumPy: Get the magnitude of a vector in NumPy - w3resource
August 29, 2025 - # Importing the NumPy library and aliasing it as 'np' import numpy as np # Creating a NumPy array 'x' containing integers x = np.array([1, 2, 3, 4, 5]) # Printing a message indicating the original array will be displayed print("Original array:") # Printing the original array 'x' with its elements print(x) # Printing a message indicating the calculation of the magnitude of the vector print("Magnitude of the vector:") # Calculating the magnitude (L2 norm) of the vector 'x' using np.linalg.norm() function magnitude = np.linalg.norm(x) # Printing the calculated magnitude of the vector 'x' print(ma
Codegive
codegive.com › blog › python_numpy_length_of_vector.php
Python numpy length of vector
It's the scalar value representing the geometric length of the vector. Number of Elements: The total count of items in the array (e.g., 3 elements in [1, 2, 3]). Number of Dimensions: How many axes the array has (e.g., a 1D array is a vector, a 2D array is a matrix).
CodeSignal
codesignal.com › learn › courses › fundamentals-of-vectors-and-matrices-with-numpy › lessons › vector-properties-and-norms-with-numpy
Vector Properties and Norms with NumPy
NumPy is the ideal choice for these operations due to its performance and simplicity, specifically designed for numerical computing tasks. ... L_2L2) measures the "straight-line" distance from the origin to the point represented by the vector, effectively the length of the vector.
w3resource
w3resource.com › python-exercises › numpy › basic › numpy-basic-exercise-23.php
NumPy: Create a vector of length 5 filled with arbitrary integers from 0 to 10 - w3resource
August 28, 2025 - NumPy Basic Exercises, Practice and Solution: Write a NumPy program to create a vector of length 5 filled with arbitrary integers from 0 to 10.
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.ma.size.html
numpy.ma.size — NumPy v2.1 Manual
Return the number of elements along a given axis.
IncludeHelp
includehelp.com › python › how-do-you-get-the-magnitude-of-a-vector-in-numpy.aspx
Python - How do you get the magnitude of a vector in NumPy?
October 7, 2023 - Numpy arrays can also be used to depict a vector. To get the magnitude of a vector in NumPy, we can either define a function that computes the magnitude of a given vector based on a formula or we can use the norm() method in linalg module of NumPy.
NumPy
numpy.org › doc › stable › reference › generated › numpy.ndarray.size.html
numpy.ndarray.size — NumPy v2.4 Manual
Number of elements in the array · Equal to np.prod(a.shape), i.e., the product of the array’s dimensions