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:
Get the number of rows and number of columns in Pandas Dataframe
Next article icon

Find the number of rows and columns of a given matrix using NumPy

Last Updated : 05 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The shape attribute of a NumPy array returns a tuple representing the dimensions of the array. For a two-dimensional array, the shape tuple contains two values: the number of rows and the number of columns.

In this article, let’s discuss methods used to find dimensions of the matrix.

How to Find the Number of Rows and Columns of a Matrix?

We can find matrix dimension with three ways:

  • Using shape Attribute
  • Using Indexing
  • Using numpy.reshape()

Way 1: Using .shape Attribute

Here we are finding the number of rows and columns of a given matrix using Numpy.shape.

Python




import numpy as np
matrix = np.array([[9, 9, 9], [8, 8, 8]])
 
dimensions = matrix.shape
rows, columns = dimensions
 
print("Rows:", rows)
print("Columns:", columns)
 
 

Output:

Rows: 2 
Columns: 3

Way 2: Using Indexing

Here we are finding the number of rows and columns of a given matrix using Indexing.

Python




import numpy as np
 
matrix = np.array([[4, 3, 2], [8, 7, 6]])
rows = matrix.shape[0]
columns = matrix.shape[1]
 
print("Rows:", rows)
print("Columns:", columns)
 
 

Output:

Rows: 2 
Columns: 3

Way 3: Using numpy.reshape()

Here we are using numpy.reshape() to find number of rows and columns of a matrix, numpy.reshape in NumPy is used for changing the shape of an array without modifying the underlying data.

When using np.arange(start, stop), remember that the stop element is not included in the generated array. So, np.arange(1, 10) will create an array with values from 1 to 9 (inclusive).

Python




import numpy as np
matrix= np.arange(1,10).reshape((3, 3))
 
print(matrix) # Original matrix
print(matrix.shape) # Number of rows and columns of the said matrix
 
 

Output:

[[1 2 3]
[4 5 6]
[7 8 9]]
(3,3)


Next Article
Get the number of rows and number of columns in Pandas Dataframe
author
vipinyadav15799
Improve
Article Tags :
  • Python
  • Python numpy-Matrix Function
  • Python-numpy
Practice Tags :
  • python

Similar Reads

  • Compute the condition number of a given matrix using NumPy
    In this article, we will use the cond() function of the NumPy package to calculate the condition number of a given matrix. cond() is a function of linear algebra module in NumPy package. Syntax:  numpy.linalg.cond(x, p=None) Example 1: Condition Number of 2X2 matrix [GFGTABS] Python3 # Importing lib
    2 min read
  • How to get the number of dimensions of a matrix using NumPy in Python?
    In this article, we will discuss how to get the number of dimensions of a matrix using NumPy. It can be found using the ndim parameter of the ndarray() method. Syntax: no_of_dimensions = numpy.ndarray.ndim Approach: Create an n-dimensional matrix using the NumPy package.Use ndim attribute available
    3 min read
  • Number of rows and columns in a Matrix that contain repeated values
    Given a N x N square matrix arr[][] which contains only integers between 1 and N, the task is to compute the number of rows and the number of columns in the matrix that contain repeated values. Examples: Input: N = 4, arr[][] = {{1, 2, 3, 4}, {2, 1, 4, 3}, {3, 4, 1, 2}, {4, 3, 2, 1}} Output: 0 0 Exp
    6 min read
  • Calculate the QR decomposition of a given matrix using NumPy
    In this article, we will discuss QR decomposition of a matrix. QR factorization of a matrix is the decomposition of a matrix say ‘A’ into ‘A=QR’ where Q is orthogonal and R is an upper-triangular matrix. We can calculate the QR decomposition of a given matrix with the help of numpy.linalg.qr().  Syn
    2 min read
  • Get the number of rows and number of columns in Pandas Dataframe
    Pandas provide data analysts a variety of pre-defined functions to Get the number of rows and columns in a data frame. In this article, we will learn about the syntax and implementation of few such functions. Method 1: Using df.axes() Method axes() method in pandas allows to get the number of rows a
    3 min read
  • Compute the inverse of a matrix using NumPy
    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 variables. The inverse of a matrix is that matrix which when multiplied with the original matrix will give as an identity mat
    2 min read
  • Count the number of rows and columns of Pandas dataframe
    In this article, we'll see how we can get the count of the total number of rows and columns in a Pandas DataFrame. There are different methods by which we can do this. Let's see all these methods with the help of examples. Example 1: We can use the dataframe.shape to get the count of rows and column
    2 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
  • Return the infinity Norm of the matrix in Linear Algebra using NumPy in Python
    In this article, we will how to return the infinity Norm of the matrix in Linear Algebra in Numpy using Python. numpy.linalg.norm() method The numpy.linalg.norm() method returns the matrix's infinite norm in Python linear algebra. This function can return one of eight possible matrix norms or an inf
    3 min read
  • Compute the determinant of a given square array using NumPy in Python
    In Python, the determinant of a square array can be easily calculated using the NumPy package. This package is used to perform mathematical calculations on single and multi-dimensional arrays. numpy.linalg is an important module of NumPy package which is used for linear algebra. We can use det() fun
    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