w3resource

NumPy: Get the magnitude of a vector in NumPy

NumPy: Array Object Exercise-93 with Solution

Write a NumPy program to get the magnitude of a vector in NumPy.

Sample Solution:-

Python Code:

import numpy as np
x = np.array([1,2,3,4,5])
print("Original array:")
print(x)
print("Magnitude of the vector:")
print(np.linalg.norm(x))

Sample Output:

Original array:                                                        
[1 2 3 4 5]                                                            
Magnitude of the vector:                                               
7.4161984871

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to select indices satisfying multiple conditions in a NumPy array.
Next: Write a NumPy program to count the frequency of unique values in NumPy array.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

List comprehension:

>>> m = [x ** 2 for x in range(5)]
>>> m
[0, 1, 4, 9, 16]