Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

Numpy.linalg.norm(): Find a Matrix or Vector Norm

  • 26 Sep, 2025
  • Com 0
Numpy.linalg.norm() Method in Python

The numpy.linalg.norm() method calculates the matrix or vector norm of an input array. Norms quantify the “size” or “magnitude” of vectors and matrices.

A vector norm measures the “length” or “magnitude” of a vector, while a matrix norm measures the size of a matrix in terms of rows, columns, or singular values.

numpy.linalg.norm() with a vector

Let’s calculate the vector norm (Euclidean distance L2). It measures straight-line distance.

import numpy as np

vector = np.array([2, 4])

vector_norm = np.linalg.norm(vector)

print(vector_norm)

# Output: 4.47213595499958

In the above code, we calculated the Euclidean distance between two points. 2 and 4.

Behind the scenes, it will calculate like this: sqrt(2^2 + 4^2) = sqrt(4 + 16) = sqrt(20) and square root of 20 is 4.4721.

So, concepts like Euclidean distance (L2 norm) or Manhattan distance (L1 norm) are vector norms.

Syntax

numpy.linalg.norm(x, ord=None, axis=None, keepdims=False)

Parameters

Argument Description
x (array_like, required) It represents an array or scalar. It must be 1D or 2D.
ord (optional, default=None) It specifies the norm order.

Vector norms:

  1. ord=None: 2-norm (Euclidean norm).
  2. ord=1: 1-norm (sum of absolute values).
  3. ord=2: 2-norm (Euclidean).
  4. ord=∞: max(abs(x)).
  5. ord=-∞: min(abs(x)).

Matrix norms:

  1. ord=None: Frobenius norm.
  2. ‘fro’: Frobenius norm.
  3. ‘nuc’: nuclear norm (sum of singular values).
  4. ord=1: max column sum.
  5. ord=∞: max row sum.
  6. ord=2: 2-norm (largest singular value).
axis (optional, default=None)

It represents an axis or axes over which to compute the norm.

keepdims (bool, optional, default=False) If it is True, it retains the reduced axes in the output as singleton dimensions.

1-norm (Manhattan), Infinity norm, General p-norm (p=3)

Let’s calculate the Manhattan norm, the infinity norm, and the general p-norm for vectors.

import numpy as np

vector = np.array([3, 4])

# 1-norm (Manhattan)
manhattan_norm = np.linalg.norm(vector, ord=1)
print(manhattan_norm)
# Output: 7.0 (|3| + |4| = 7)

# Infinity norm (max absolute value)
infinity_norm = np.linalg.norm(vector, ord=np.inf)
print(infinity_norm)
# Output: 4.0 (max(|3|, |4|) = 4)

# General p-norm (p=3)
pnorm = np.linalg.norm(vector, ord=3)
print(pnorm)
# Output: 4.497941445275415
# ∣1∣^3=1,∣2∣^3=8,∣3∣^3 = 27
# 1+8+27 = 36 (Sum)
# 36^(1/3) = 4.497941445275415 (Cube Root)

You can see that for different types of norms, we passed different ord values. For example, for 1-norm, we passed ord=1, for infinity norm, we passed ord=np.inf, and for p-norm, we passed ord=3.

Matrix norms

Let’s find the Frobenius (default for matrices) norm of the flattened matrix.

Finding Matrix norm in Numpy

import numpy as np

mat = np.array([[1, 2], [3, 4]])

matrix_norm = np.linalg.norm(mat)

print(matrix_norm)

# Output: 5.477225575051661

Here is the explanation for the output: sqrt(1^2 + 2^2 + 3^2 + 4^2) = 5.47722

1-norm (max column sum), Infinity norm (max row sum), and Nuclear norm

1-norm is the maximum absolute column sum of the matrix.

∞-norm is the maximum absolute row sum of the matrix.

The nuclear norm is the sum of singular values.

import numpy as np 

mat = np.array([[1, 2], [3, 4]])

# 1-norm (max column sum)
print(np.linalg.norm(mat, ord=1))   
# Output: max(|1|+|3|, |2|+|4|) = 6.0

# Infinity norm (max row sum)
print(np.linalg.norm(mat, ord=np.inf))  
# Output: max(|1|+|2|, |3|+|4|) = 7.0

# Nuclear norm (sum of singular values)
print(np.linalg.norm(mat, ord='nuc'))   
# Output: 5.8309518948453

By passing ord=1, we calculated the 1-norm of a matrix.

By passing ord=np.inf, we calculated the infinity norm of an input matrix.

And finally, for nuclear norm, we passed ord=nuc.

That’s all!

Post Views: 37
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Numpy.isnan(): Tests NaN Values Element-wise
JavaScript String indexOf() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend