[ad_1]

Numpy is a python package that allows any developer to create an array and make complex calculations on it. There are many inbuilt methods in it and all make efficient and quick mathematical calculations. Suppose you want to find the magnitude of a vector in NumPy, then there are different methods for it.

In this entire tutorial, you will learn how to calculate the magnitude of vectors in NumPy using various method.

But before going to the coding part let’s know the concept of magnitude and vector.

Table of Contents

**What is a vector?**

A vector is a quantity that has two independent properties. One is the magnitude and the other is direction. Any quantity that has magnitude but no direction is a scalar.

**How to calculate the magnitude of a vector?**

The magnitude of a vector represents the length of a vector. It is represented by the mode of the vector. The formulae for calculating the magnitude of the vector is the below.

## How to find the magnitude of a vector using NumPy?

Numpy provided you to find the magnitude of a numpy very easily. There are different methods to find the magnitude of a vector. We will discuss each of them.

### Example 1: Using the Formulae

The first method to find the magnitude of a vector is using the formulae. In this example, I will first create a function that will take each element of a NumPy array and calculate the magnitude.

Execute the below lines of code.

```
import math
import numpy as np
vector = np.array([0, 2])
def magnitude(x):
return math.sqrt(sum(i ** 2 for i in x))
mag = magnitude(vector)
print(mag)
```

**Output**

### Example 2: Find the magnitude of the vector using the NumPy method

The second method for calculating the magnitude of a vector is the NumPy np.linalg.norm() method. It will normalize the elements of the NumPy array. The normalization formula is the same as the direct formulae.

Run the below lines of code and you will get the same output as example 1.

```
import numpy as np
vector = np.array([0, 2])
mag = np.linalg.norm(vector)
print(mag)
```

**Output**

### Example 3: Using the NumPy dot() and sqrt() method

Numpy provides other inbuilt methods np.dot() and np.sqrt() that will be used to find the magnitude of a vector in NumPy. The * np.dot() *will calculate the dot product of the elements. After that its result will be passed as an argument for the

*method.*

**numpy.sqrt()**You will get the same result as the above when you will run the below lines of code.

```
import numpy as np
vector = np.array([0, 2])
mag = np.sqrt(vector.dot(vector))
print(mag)
```

**Output**

## Conclusion

Vector uses to represent physical quantities. In Machine learning, it uses to represent numeric and symbolic characteristics of an object in a mathematical way. In fact, It is called features. The magnitude of a vector is very useful in the normalization of the datasets.

These are the method to find the Magnitude of a Vector in Numpy. I hope you have liked this tutorial. Even If you have any queries then you can contact us for more information.

#### Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

[ad_2]

Source link