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 OverflowMedium
medium.com › @whyamit101 › understanding-vector-length-in-numpy-c05264f58ef2
Understanding Vector Length in NumPy | by why amit | Medium
February 26, 2025 - Lengths of Vectors: [ 5. 2.23606798 13. ] ... And just like that, NumPy handles multiple vectors with ease.
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
Numpy array of part of a vector
Your slice notation is off (also I'm not sure what's the type of your vector, is it a numpy array, or a regular list?). You get a slice from the 3rd element (inclusive) to the 2nd to last (exclusive) using: vector[2:-2] If vector is a list, you need to pass the above to numpy.array() to create a numpy array: arr = numpy.array(vector[2:-2]) If vector is a numpy array itself, the result of vector[2:-2] will also be an array, so you wouldn't need to do anything else. 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
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
Codegive
codegive.com › blog › numpy_length_of_vector.php
Numpy length of vector
Using len(vec): 2 As you can see, len(vec) gives 2, because there are two elements in the array. This is not the mathematical length of 5. So, len() is not what we want for vector magnitude. Let's implement the formula directly using NumPy's array operations.
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.ndarray.size.html
numpy.ndarray.size — NumPy v2.1 Manual
Equal to np.prod(a.shape), i.e., the product of the array’s dimensions.
CodeRivers
coderivers.org › blog › python-length-of-vector
Python Length of Vector: A Comprehensive Guide - CodeRivers
February 22, 2026 - In this example, the len() function returns the integer value 5, which is the number of elements in the vector_list. NumPy arrays have a size attribute that can be used to determine the number of elements in the array.
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
L_2L2) measures the "straight-line" distance from the origin to the point represented by the vector, effectively the length of the vector. ... import numpy as np # Defining a vector vector = np.array([3, 4, 5]) # Calculating the Euclidean Norm magnitude = np.linalg.norm(vector) # Display ...
APXML
apxml.com › courses › linear-algebra-essentials-ml › chapter-1-vectors-in-machine-learning › implementing-vector-operations-numpy
Implementing Vector Operations with NumPy
The default for np.linalg.norm ... as the norm of their difference, ... ||a - b||∣∣a−b∣∣. Using NumPy, this is a combination of subtraction and the norm function....
W3Schools
w3schools.com › python › gloss_python_array_length.asp
Python Array Length
Use the len() method to return the length of an array (the number of elements in an array).
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.
NumPy
numpy.org › doc › stable › reference › generated › numpy.ndarray.size.html
numpy.ndarray.size — NumPy v2.5 Manual
Equal to np.prod(a.shape), i.e., the product of the array’s dimensions.