Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Numpy exercise
  • pandas
  • Matplotlib
  • Data visulisation
  • EDA
  • Machin Learning
  • Deep Learning
  • NLP
  • Data science
  • ML Tutorial
  • Computer Vision
  • ML project
Open In App
Next Article:
Products of Vectors and Matrices in NumPy
Next article icon

Find a matrix or vector norm using NumPy

Last Updated : 06 Jun, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

To find a matrix or vector norm we use function numpy.linalg.norm() of Python library Numpy. This function returns one of the seven matrix norms or one of the infinite vector norms depending upon the value of its parameters.
 

Syntax: numpy.linalg.norm(x, ord=None, axis=None)
Parameters: 
x: input 
ord: order of norm 
axis: None, returns either a vector or a matrix norm and if it is an integer value, it specifies the axis of x along which the vector norm will be computed 
 


Example 1: 
 

Python3

# import library
import numpy as np
 
# initialize vector
vec = np.arange(10)
 
# compute norm of vector
vec_norm = np.linalg.norm(vec)
 
print("Vector norm:")
print(vec_norm)
                      
                       

Output:
 

Vector norm: 16.881943016134134


The above code computes the vector norm of a vector of dimension (1, 10)
Example 2: 
 

Python3

# import library
import numpy as np
 
# initialize matrix
mat = np.array([[ 1, 2, 3],
               [4, 5, 6]])
 
# compute norm of matrix
mat_norm = np.linalg.norm(mat)
 
print("Matrix norm:")
print(mat_norm)
                      
                       

Output:
 

Matrix norm: 9.539392014169456


Here, we get the matrix norm for a matrix of dimension (2, 3)
Example 3: 
To compute matrix norm along a particular axis – 
 

Python3

# import library
import numpy as np
 
 
mat = np.array([[ 1, 2, 3],
               [4, 5, 6]])
 
# compute matrix num along axis
mat_norm = np.linalg.norm(mat, axis = 1)
 
print("Matrix norm along particular axis :")
print(mat_norm)
                      
                       

Output:
 

Matrix norm along particular axis : [3.74165739 8.77496439]


This code generates a matrix norm and the output is also a matrix of shape (1, 2)
Example 4: 
 

Python3

# import library
import numpy as np
 
# initialize vector
vec = np.arange(9)
 
# convert vector into matrix
mat = vec.reshape((3, 3))
 
# compute norm of vector
vec_norm = np.linalg.norm(vec)
 
print("Vector norm:")
print(vec_norm)
 
# computer norm of matrix
mat_norm = np.linalg.norm(mat)
 
print("Matrix norm:")
print(mat_norm)
                      
                       

Output:
 

Vector norm: 14.2828568570857 Matrix norm: 14.2828568570857


From the above output, it is clear if we convert a vector into a matrix, or if both have same elements then their norm will be equal too.
 



Next Article
Products of Vectors and Matrices in NumPy

D

devanshigupta1304
Improve
Article Tags :
  • Python
  • Python numpy-Linear Algebra
  • Python-numpy
Practice Tags :
  • python

Similar Reads

  • Products of Vectors and Matrices in NumPy
    Working with vector and matrix operations is a fundamental part of scientific computing and data analysis. NumPy is a Python library that computes various types of vector and matrix products. Let's discuss how to find the inner, outer and cross products of matrices and vectors using NumPy in Python.
    3 min read
  • Divide each row by a vector element using NumPy
    The following article depicts how to Divide each row by a vector element using NumPy. The vector element can be a single element, multiple element, or array. The division operator ( / ) is employed to produce the required functionality.  We can divide rows of 1-D, 2-D, or even more types of arrays w
    2 min read
  • How to inverse a matrix using NumPy
    In this article, we will see NumPy Inverse Matrix in Python before that we will try to understand the concept of it. The inverse of a matrix is just a reciprocal of the matrix as we do in normal arithmetic for a single number which is used to solve the equations to find the value of unknown variable
    3 min read
  • Flatten a Matrix in Python using NumPy
    Let's discuss how to flatten a Matrix using NumPy in Python. By using ndarray.flatten() function we can flatten a matrix to one dimension in python. Syntax:numpy_array.flatten(order='C') order:'C' means to flatten in row-major.'F' means to flatten in column-major.'A' means to flatten in column-major
    1 min read
  • How to create a vector in Python using NumPy
    NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python. Numpy is basically used for creating array of n dimensions. Vector are built
    5 min read
  • How to Find cofactor of a matrix using Numpy
    In this article, we are going to see how to find the cofactor of a given matrix using NumPy. There is no direct way to find the cofactor of a given matrix using Numpy. Deriving the formula to find cofactor using the inverse of matrix in Numpy Formula to find the inverse of a matrix: A-1 = ( 1 / det(
    2 min read
  • numpy matrix operations | zeros() function
    numpy.matlib.zeros() is another function for doing matrix operations in numpy. It returns a matrix of given shape and type, filled with zeros. Syntax : numpy.matlib.zeros(shape, dtype=None, order='C') Parameters : shape : [int, int] Number of rows and columns in the output matrix.If shape has length
    2 min read
  • Parallel matrix-vector multiplication in NumPy
    In this article, we will discuss how to do matrix-vector multiplication in NumPy. Matrix multiplication with Vector For a matrix-vector multiplication, there are certain important points: The end product of a matrix-vector multiplication is a vector.Each element of this vector is obtained by perform
    2 min read
  • numpy matrix operations | ones() function
    numpy.matlib.ones() is another function for doing matrix operations in numpy. It returns a matrix of given shape and type, filled with ones. Syntax : numpy.matlib.ones(shape, dtype=None, order='C') Parameters : shape : [int, int] Number of rows and columns in the output matrix.If shape has length on
    2 min read
  • Find the memory size of a NumPy array
    In this post, we will see how to find the memory size of a NumPy array. So for finding the memory size of a NumPy array we are using following methods: Using size and itemsize attributes of NumPy array size: This attribute gives the number of elements present in the NumPy array. itemsize: This attri
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences