In this Python tutorial, we will learn all about matrices using the NumPy package. We will see how to create a matrix using NumPy arrays and various methods/operations performed on the matrix, such as adding or removing a row/column.
A matrix is a data structure where data is stored in rows and columns, as shown in the figure below.
The above is a 3×3 or 3 by 3 matrix containing three rows and columns.
Do note that Python doesn’t support a built-in matrix data structure. However, we can treat matrices in Python as nested lists where each row represents a list data structure.
Also Read: How To Create An Empty Dictionary in Python
Python Matrix Using Numpy With Examples
NumPy makes working with matrices in Python a piece of cake as it supports multi-dimensional arrays and matrices. It also provides an extensive collection of high-level mathematical functions to operate on these arrays.
We can create an array in NumPy and reverse the array using some inbuilt functions available in NumPy.
Syntax:
import numpy
Creation:
We can create an array through the NumPy module.
Syntax:
numpy.array([elements])
A matrix is a two-dimensional data structure that contains rows and columns. So to create a matrix, we need to create a 2D NumPy array.
Also Read: How to Exit Python Program [4 Methods]
We can create this using the array() method in the NumPy module.
Syntax:
my_matrix = array([[elements],....................,[elements]])
Example:
In this example, we will create a matrix from a NumPy array with 4 rows and 3 columns.
#import the array from numpy module from numpy import array #create a matrix that has 4 rows and 3 columns my_matrix = array([[1,"php","web-technologies"], [2,"spark","bigdata"], [3,"html","web-technologies"], [4,"hadoop","web-technologies"]]) #display print(my_matrix)
Output:
[['1' 'php' 'web-technologies'] ['2' 'spark' 'bigdata'] ['3' 'html' 'web-technologies'] ['4' 'hadoop' 'web-technologies']]
Also Read: How to Convert Python Tuples to Lists
Accessing elements of a matrix using NumPy
We can get a particular row/column from the matrix using an index. Indexing starts with 0.
Syntax: To get a particular row
my_matrix[row_index]
Syntax: To get columns in a row
my_matrix[row_index][column_index]
Also Read: How to Handle String Index Out of Range Error In Python
Example: In this example, we will access different rows and columns.
#import the array from numpy module from numpy import array #create a matrix that has 4 rows and 3 columns my_matrix = array([[1,"php","web-technologies"], [2,"spark","bigdata"], [3,"html","web-technologies"], [4,"hadoop","web-technologies"]]) #access second row print(my_matrix[1]) #access forth row print(my_matrix[2]) #access second row, third element print(my_matrix[1][2]) #access forth row,first element print(my_matrix[2][0])
Output:
['2' 'spark' 'bigdata'] ['3' 'html' 'web-technologies'] bigdata 3
Also Read: Python For Loop Index With Examples
Adding a Row in a NumPy matrix
We can add a row by using the append() function.
Syntax:
append(my_matrix,[row],0)
where,
- my_matrix is the input matrix
- 0 represents the row axis.
Example:
In this example, we will add a row – [5,”IOT”,”python”] to the matrix.
from numpy import * #create a matrix that has 4 rows and 3 columns my_matrix = array([[1,"php","web-technologies"], [2,"spark","bigdata"], [3,"html","web-technologies"], [4,"hadoop","web-technologies"]]) #append - [5,"IOT","python"] to the matrix print(append(my_matrix,[[5,"IOT","python"]],0))
Output:
[['1' 'php' 'web-technologies'] ['2' 'spark' 'bigdata'] ['3' 'html' 'web-technologies'] ['4' 'hadoop' 'web-technologies'] ['5' 'IOT' 'python']]
Also Read: How to Reverse an Array In Python [Flip Array]
Adding a Column in a NumPy Matrix
We can add a column by using the insert() function.
Syntax:
insert(my_matrix,[column_position],[column_values],1)
where,
- my_matrix is the input matrix
- 1 represents the column axis.
- column_position represents the column index
- column_values represents the values
Example:
In this example, we will add a column – [[56],[67],[90],[89]] to the matrix.
from numpy import * #create a matrix that has 4 rows and 3 columns my_matrix = array([[1,"php","web-technologies"], [2,"spark","bigdata"], [3,"html","web-technologies"], [4,"hadoop","web-technologies"]]) #insert - [[56],[67],[90],[89]] column to the matrix print(insert(my_matrix,[3],[[56],[67],[90],[89]],1))
Output:
[['1' 'php' 'web-technologies' '56'] ['2' 'spark' 'bigdata' '67'] ['3' 'html' 'web-technologies' '90'] ['4' 'hadoop' 'web-technologies' '89']]
Also Read: How to Check If Dict Has Key in Python [5 Methods]
Deleting a Row in a NumPy Matrix
We can delete a row by using the delete() method.
Syntax:
delete(my_matrix,[row_index],0)
where,
- my_matrix is the input matrix
- row_index represents the row to be deleted
- 0 represents the row axis.
Example:
In this example, we will delete rows in a matrix.
from numpy import * #create a matrix that has 4 rows and 3 columns my_matrix = array([[1,"php","web-technologies"], [2,"spark","bigdata"], [3,"html","web-technologies"], [4,"hadoop","web-technologies"]]) #delete third row my_matrix=delete(my_matrix,[2],0) #delete first row my_matrix=delete(my_matrix,[0],0) #display the matrix print(my_matrix)
Output:
[['2' 'spark' 'bigdata'] ['4' 'hadoop' 'web-technologies']]
Also Read: How to Convert Binary to Decimal in Python [5 Methods]
Deleting a Column in a NumPy Matrix
We can delete a column by using the delete() method.
Syntax:
delete(my_matrix,[column_index],1)
where,
- my_matrix is the input matrix
- column_index represents the column to be deleted
- 1 represents the column axis.
Example:
In this example, we will delete columns in a matrix.
from numpy import * #create a matrix that has 4 rows and 3 columns my_matrix = array([[1,"php","web-technologies"], [2,"spark","bigdata"], [3,"html","web-technologies"], [4,"hadoop","web-technologies"]]) #delete third column my_matrix=delete(my_matrix,[2],1) #delete first column my_matrix=delete(my_matrix,[0],1) #display the matrix print(my_matrix)
Output:
[['php'] ['spark'] ['html'] ['hadoop']]
Also Read: Python Program To Display Fibonacci Series Up To N Terms
Updating a Row in a NumPy Matrix
We can update/modify the row using the row index.
Syntax:
my_matrix[row_index]=row
Example:
In this example, we will update some rows in a matrix.
from numpy import * #create a matrix that has 4 rows and 3 columns my_matrix = array([[1,"php","web-technologies"], [2,"spark","bigdata"], [3,"html","web-technologies"], [4,"hadoop","web-technologies"]]) #update third row my_matrix[2]=[10,"jsp","web"] #update forth row my_matrix[3]=[10,"jsp","web"] #display matrix print(my_matrix)
Output:
[['1' 'php' 'web-technologies'] ['2' 'spark' 'bigdata'] ['10' 'jsp' 'web'] ['10' 'jsp' 'web']]
Also Read: Python Program To Display Multiplication Table of Any Number
Wrapping Up
In this tutorial, we explored the Python matrix using NumPy with examples. We also saw how to create a matrix from a NumPy array and discussed adding and deleting rows and columns in a matrix. We also explored how to modify the rows in a matrix.
Next Read:
- Learn Python Online With These 12 Best Free Websites
- Sending Emails Using Python With Image And PDF Attachments
- How To Automate Google Search With Python
- 10 Best Udemy Python Courses for Beginners
- 7 Best Python IDE for Windows [Code Editors]
Hello Friends! I am Himanshu, a hobbyist programmer, tech enthusiast, and digital content creator.
With CodeItBro, my mission is to promote coding and help people from non-tech backgrounds to learn this modern-age skill!