Python Matrix Using Numpy [With Examples]

101

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.

matrix in python

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 ReadHow 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 multidimensional 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 ReadHow to Exit Python Program [4 Methods]

We can create this by using the array() method available in the NumPy module.

Syntax:

my_matrix = array([[elements],....................,[elements]])

Example:

We will create a matrix from a NumPy array with 4 rows and 3 columns in this example.

#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 ReadHow to Convert Python Tuples to Lists

Accessing elements of a matrix using NumPy

We can get a particular row/column from the matrix by 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 ReadHow 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 ReadPython 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,

  1. my_matrix is the input matrix
  2. 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 ReadHow 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,

  1. my_matrix is the input matrix
  2. 1 represents the column axis.
  3. column_position represents the column index
  4. 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 ReadHow 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,

  1. my_matrix is the input matrix
  2. row_index represents the row to be deleted
  3. 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 ReadHow 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,

  1. my_matrix is the input matrix
  2. column_index represents the column to be deleted
  3. 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 ReadPython 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 ReadPython 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: