- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Return the Norm of the matrix over axis in Linear Algebra in Python

To return the Norm of the matrix or vector in Linear Algebra, use the LA.norm() method in Python Numpy. The 1st parameter, x is an input array. If axis is None, x must be 1-D or 2-D, unless ord is None. If both axis and ord are None, the 2-norm of x.ravel will be returned. The 2nd parameter, ord is the order of the norm. The inf means numpy’s inf object. The default is None.

The 3rd parameter axis, if an integer, 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. The default is None.

The 4th parameter, keepdims, if set to True, the axes which are normed over are left in the result as dimensions with size one. With this option the result will broadcast correctly against the original x.

## Steps

At first, import the required library −

import numpy as np from numpy import linalg as LA

Create an array −

arr = np.arange(8).reshape(2,2,2)

Display the array −

print("Our Array...\n",arr)

Check the Dimensions −

print("\nDimensions of our Array...\n",arr.ndim)

Get the Datatype −

print("\nDatatype of our Array object...\n",arr.dtype)

Get the Shape −

print("\nShape of our Array object...\n",arr.shape)

To return the Norm of the matrix or vector in Linear Algebra, use the LA.norm() method −

print("\nResult...\n",LA.norm(arr, axis = (1, 2)))

## Example

import numpy as np from numpy import linalg as LA # Create an array arr = np.arange(8).reshape(2,2,2) # Display the array print("Our Array...\n",arr) # Check the Dimensions print("\nDimensions of our Array...\n",arr.ndim) # Get the Datatype print("\nDatatype of our Array object...\n",arr.dtype) # Get the Shape print("\nShape of our Array object...\n",arr.shape) # To return the Norm of the matrix or vector in Linear Algebra, use the LA.norm() method in Python Numpy print("\nResult...\n",LA.norm(arr, axis = (1, 2)))

## Output

Our Array... [[[0 1] [2 3]] [[4 5] [6 7]]] Dimensions of our Array... 3 Datatype of our Array object... int64 Shape of our Array object... (2, 2, 2) Result... [ 3.74165739 11.22497216]

- Related Articles
- Return the Norm of the vector over given axis in Linear Algebra in Python
- Return the Norm of the vector over axis 1 in Linear Algebra in Python
- Return the Norm of the vector over axis 0 in Linear Algebra in Python
- Return the infinity Norm of the matrix in Linear Algebra in Python
- Return the Frobenius Norm of the matrix in Linear Algebra in Python
- Return the Nuclear Norm of the matrix in Linear Algebra in Python
- Return the negative infinity Norm of the matrix in Linear Algebra in Python
- Return the Norm of the matrix or vector in Linear Algebra in Python
- Return the Norm of the matrix or vector in Linear Algebra and also set the order in Python
- Compute the condition number of a matrix in linear algebra using Frobenius norm in Python
- Compute the condition number of a matrix in linear algebra using 2 norm in Python
- Compute the condition number of a matrix in linear algebra using Infinity norm in Python
- Compute the condition number of a matrix in linear algebra using negative 2 norm in Python
- Compute the condition number of a matrix in linear algebra using Negative Infinity norm in Python
- Return the Cholesky decomposition in Linear Algebra in Python