# NumPy Norm of Vector

• Post author:
• Post category:NumPy / Python

NumPy norm of vector in python is used to get a matrix or vector norm we use `numpy.linalg.norm()` function. This function is used to calculate one of the eight different matrix norms or one of the vector norms, depending on the value of the ord parameter. In this article, I will explain how to use `numpy.linalg.norm()` function and using its syntax and parameters, and returns a norm of the matrix or vectors.

## 1. Quick Examples of Norm of Vector

If you are in a hurry, below are some quick examples of NumPy norm of vector.

``````
# Below are a quick examples

# Example 1: use numpy.linalg.norm() function
arr = np.arange(12)
arr2 = np.linalg.norm(arr)

# Example 2: Get the linalg.norm() with 1-D array
arr = np.array([2, 4, 6, 8, 10, 12, 14])
arr2 = np.linalg.norm(arr)

# Example 3: Get the linalg.norm() with 2-D array
arr = np.array([[3, 7, 9], [2, 6, 8]])
arr2 = np.linalg.norm(arr)

# Example 4: Get the linalg.norm() values over column
# for each of 2 rows
arr2 = np.linalg.norm(arr, axis = 1)

# Example 5: Get the linalg.norm() values over row
# for each of 3 columns
arr2 = np.linalg.norm(arr, axis = 0)

# Example 6: get numpy norm of vector with 2-d array along axis
arr2 = np.linalg.norm(arr,axis= (0,1))

# Example 7: use ord Parameter
arr2 = np.linalg.norm(arr, ord=1, axis=1)
``````

## 2. Syntax of NumPy linalg.norm()

Following is the syntax to create `numpy.linalg.norm()` function.

``````
# Syntax of numpy.linalg.norm()
linalg.norm(arr, ord=None, axis=None, keepdims=False)
``````

### 2.1 Parameters of linalg.norm()

Following are the parameters of linalg.norm().

• `arr` – Input array.
• `ord` – {non-zero int, inf, -inf, ‘fro’, ‘nuc’}, optional: This stands for the order of the norm.
• `axis` – None, int or 2-tuple of ints. Axis or axes is an integer, it specifies the axis of x along which to compute the vector norms. If an axis is a 2-tuple, it specifies the axes that hold 2-D matrices, and the matrix norms of these matrices are computed.
• `keepdims` – It is a boolean and optional. If this is set to True, the axes which are normed over are left in the result as dimensions with size one.

## 2.2 Return value of linalg.norm()

The linalg.norm() returns a norm of the matrix or vector(s).

## 3. Use numpy.linalg.norm() Function

We can use NumPy `linalg.norm()` function is used to calculate the norm of a vector or a matrix. This functions returns a float or an array of norm values accurately by passing the arr as a parameter.

``````
import numpy as np

# initialize vector
arr = np.arange(12)

# use numpy.linalg.norm() function
arr2 = np.linalg.norm(arr)
print(arr2)

# Output
# 22.494443758403985
``````

## 4. Get NumPy linalg.norm() With 1-D Array

Take a one-dimensional NumPy array and compute the norm of a vector or a matrix of the array using `numpy.linalg.norm()` function, for that let’s create an array using `numpy.array()`. For example,

``````
# Create 1-D array
arr = np.array([2, 4, 6, 8, 10, 12, 14])

# Get the linalg.norm() with 1-D array
arr2 = np.linalg.norm(arr)
print(arr2)

# Output
# 23.664319132398465
``````

## 5. Get NumPy linalg.norm() With 2-D Array

Let’s norm of vector the two-dimensional NumPy array using `numpy.linalg.norm()`. This function takes a 2-D array as input and returns a float or an array of norm values.

``````
# Create 2-D array
arr = np.array([[3, 7, 9], [2, 6, 8]])

# Get the linalg.norm() with 2-D array
arr2 = np.linalg.norm(arr)
print(arr2)

# Output
# 15.588457268119896
``````

## 6. Get NumPy Norm of Vector With 2-D Array Along Axis

We can also compute the matrix norm of a NumPy array along with a specified axis. If we want to compute the matrix norm of each row, we will pass the axis=0 parameter through the` linalg.norm()` function. Similarly, to compute the matrix norm of each column, use axis=1. We will pass the axis parameter as the 2- tuple of the integer value.

``````
# Get the linalg.norm() values over column
# for each of 2 rows
arr2 = np.linalg.norm(arr, axis = 1)
print(arr2)

# Output
# [11.78982612 10.19803903]

# Get the linalg.norm() values over row
# for each of 3 columns
arr2 = np.linalg.norm(arr, axis = 0)
print(arr2)

# Output
# [ 3.60555128  9.21954446 12.04159458]

# get numpy norm of vector with 2-d array along axis
arr2 = np.linalg.norm(arr,axis= (0,1))
print(arr2)

# Output
# 15.588457268119896
``````

## 7. Use ord Parameter

We can also compute the matrix norm of a NumPy array along with a specified `ord` Parameter and `axis`.

``````
# use ord Parameter
arr2 = np.linalg.norm(arr, ord=1, axis=1)
print(arr2)

# Output
# [19. 16.]
``````

## 8. Conclusion

In this article, I have explain the how to calculate the norm of a vector or a matrix of NumPy array along with the specified axis and multiple axes. Also explained how to use ord Parameter.

Happy Learning!!

### References  