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
Vector angles with numpy
There's two issues: If both X and Y are shape (n, m), np.inner(X, Y) will return a shape (n, n) array containing the inner product for each possible combination of vector from X and Y. In general, for batched operations, it's better to stay away from the standard linear algebra functions like inner, dot, matmul, etc., which all behave kind of weirdly for higher-dimensional inputs, and instead implement them manually or by using np.einsum, that makes it a lot more consistent. Batched inner product can be implemented using an element-wise multiplication followed by a sum-reduction of the second axis: inner = np.sum(X*Y, axis=1) You're missing the conversion to degree at the end (NumPy and in fact most libraries/languages operate with radians by default). You can use the function np.rad2deg for that. Clipping the cosine to between -1 and 1 should be redundant. Edit: Actually, there's a third, since there's a similar problem with np.linalg.norm. You need to specify axis=1, otherwise it calculates the norm by treating the entire flattened array as one large vector: x_u = np.linalg.norm(X, axis=1) y_u = np.linalg.norm(Y, axis=1) More on reddit.com
Struggled with Vector Magnitude? Here’s the Easiest Way I Found to Understand It (with visuals + NumPy)
Your blog post was a good read, especially the question regarding 3d Vector, it helped me internalize the text that I had just read. Keep up the good work👍 More on reddit.com
Find the length of a vector (Vector3D?)
An manim Vector3D is just a numpy NDArray. You could import numpy and use its linalg functions to get the norm. Or you could make use of the per-element multiplication to make your own length function: def vec_norm(vec): return math.sqrt((vec*vec).sum()) More on reddit.com
How should I initialize a numpy array of NaN values?
>>> np.full(3, np.nan) But the bigger question is why would you want to? Edit: as an explanation, your example does not work because you initialized an array of ints. ints have no "NaN" value, only floats do. So your method would work if you initialized an array of floats: >>> x = np.array([0.0,0.0,0.0]) >>> x.fill(np.nan) >>> x array([ nan, nan, nan]) Or converted the ints to floats: >>> x = np.array([0,0,0], dtype=np.float) >>> x.fill(np.nan) >>> x array([ nan, nan, nan]) But the np.full() method is much better. More on reddit.com
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.
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 › 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....
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