• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:14 mins read
You are currently viewing NumPy Norm of Vector

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.

Advertisements

1. Quick Examples of Norm of Vector

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


# Quick examples of norm of vector

# 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([[1, 2],[3, 4]])
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 – This parameter represents the input array. It could be a 1-D array (vector) or a 2-D array (matrix).
  • 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(optional) – If this parameter is set to True, the dimensions of the output will be the same as the input array. If set to False (default), the dimensions will be reduced as per NumPy’s broadcasting rules.

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

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


# Import numpy
import numpy as np
 
# Initialize vector
arr = np.arange(12)
 
# Use numpy.linalg.norm() function
arr2 = np.linalg.norm(arr)
print("After calculating the norm of a vector:\n",arr2)

Yields below output.

NumPy norm vector

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().

In the below example, np.linalg.norm(arr) calculates the Euclidean norm of the 1-D array [2, 4, 6, 8, 10, 12, 14]. The output will be the square root of the sum of the squares of its elements, which is sqrt(2^2 + 4^2 + 6^2 + 8^2 + 10^2 + 12^2 + 14^2), equal to sqrt(423), which is approximately 23.6643.


import numpy as np
 
# 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("After calculating the norm of a vector:\n",arr2)

# Output:
# After calculating the norm of a vector:
#  23.664319132398465

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

You can use the linalg.norm() function calculates the norm of a vector or matrix. When applied to a 2-D array, it computes the Frobenius norm, which is the square root of the sum of the absolute squares of its elements.

In the below example, np.linalg.norm(matrix) will calculate the Frobenius norm of the 2×2 matrix [[1, 2], [3, 4]]. The output will be the square root of the sum of the absolute squares of its elements, which is sqrt(1^2 + 2^2 + 3^2 + 4^2), equal to sqrt(30), which is approximately 5.4772.


import numpy as np

# Create a 2-D array (matrix)
arr = np.array([[1, 2],[3, 4]])
                   
# Get the linalg.norm() with 2-D array 
arr2 = np.linalg.norm(arr)
print("After calculating the norm of a vector:\n",arr2)

# Output:
# After calculating the norm of a vector:
#  5.477225575051661

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

You can also compute the matrix norm of a NumPy array along with a specified axis. If you want to compute the matrix norm of each row, You will pass the axis=0 parameter through the linalg.norm() function. Similarly, to compute the matrix norm of each column, use axis=1. You 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

You 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.]

Frequently Asked Questions

What is the NumPy norm function?


NumPy provides a function called numpy.linalg.norm() that computes the norm of a vector or a matrix. The norm of a vector is a measure of its length, and it can be calculated using different types of norms, such as L1 norm, L2 norm, etc.

How do I calculate the L2 norm (Euclidean norm) of a vector using NumPy?

To calculate the L2 norm (Euclidean norm) of a vector using NumPy, you can use the numpy.linalg.norm() function.

Can I calculate the norm along a specific axis of a multi-dimensional array?

You can calculate the norm along a specific axis of a multi-dimensional array by specifying the axis parameter in the numpy.linalg.norm() function.

Can I calculate the norm of a matrix using NumPy?

You can calculate the norm of a matrix using numpy.linalg.norm(). By default, it computes the Frobenius norm, which is the square root of the sum of the squared absolute values of its elements.

Can I normalize a vector using the norm in NumPy?

You can normalize a vector using the norm in NumPy. Normalizing a vector means scaling it to have a unit length, i.e., converting it to a unit vector. To normalize a vector using NumPy, you can divide the vector by its L2 norm (Euclidean norm). For example, to normalize a vector vector using the L2 norm.

Conclusion

In this article, I have explained 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 the ord Parameter.

Happy Learning!!

References