Special Offer - Enroll Now and Get 2 Course at ₹25000/- Only Explore Now! # How to Implement Matrices in Python using NumPy?

January 9th, 2020

## What is a matrix?

A Matrix is a two-dimensional array or rectangular array of numbers where each of its data elements will be of the same size. Matrices are generally data structures that were used to perform math and scientific computations. In a matrix, data that is present horizontally are called as “rows” and data that were present vertically are called as “columns”. Say, for example, the number of elements in a matrix (or) the size of the matrix of (A) x (B) where A is the rows and B is the columns. There is no built-in type for a matrix in python and therefore, a conventional matrix with two or more lists as a matrix.
Let us try to understand the concept of matrix manipulation in python with an illustration. The following is a python code with a matrix:

 ```Print(‘nHi, Welcome to Matrix manipulation in Python!! n”) Print(“Here is a sample matrixn”) X=[[1,2,3.4.5], [-8,9,12,13,1], [-4,3,7,9,51]] Print(“X=”, X) Print(“nPrint the Second rown”) Print(“X=”, X) Print(“nPrint the 1st row, second elementn”) Print(“X=”, X) Print(“nPrint the last element of the third rown”) Print(“X=”, X) Column=[]; For row in X: Column.append(row) Print(“nDisplay the third column alonen”) Print(“third column=”, column) Print(“nThanks for learning matrices in python”)```

The output of the above python code for matrices is as follows:

 ```Hi, Welcome to Matrix manipulation in Python!! Here is a sample matrix X=[[1,2,3.4.5], [-8,9,12,13,1], [-4,3,7,9,51]] Print the Second row X = [-8,9,12,13,1] Print the 1st row, second element X = 2 Print the last element of the third row X = 51 Display the third column alone third column = [3,12,7] Thanks for learning matrices in python```

## How will you use the NumPy package for manipulating matrices in Python?

NumPy is a package available in Python library. Through this package, one can perform scientific computing. Also, it allows users to work with multi-dimensional arrays. Let us perform the addition of matrices using this NumPy package through the following illustration:

 ```//Matrices Addition Print(“nAddition of Matricesn”) import numpy as np X= np.array([[48,32], [49,-15]]) Y= np.array([23,-19], [35, 23]]) Z=X+Y Print(“Addition of matrices using NumPyn”) Print(Z) Print(“nThat’s it..!!”)```

### Output:

 ```Addition of Matrices Addition of matrices using NumPy [[ 71 13] [84 8]] That’s it..!!```

Let us perform Matrix multiplication using NumPy. Multiplication using NumPy is slightly different from the conventional multiplication using “*”. Let us learn it from the following example:

 ```//Multiplication of matrices Import numpy as np X=np.array([[3,6,5],[4,3,-2]]) Y=np.array([[4,5],[6,7], [8, 3]]) Z=X.dot(Y) Print(“nResult of the product of two matrices is n”) Print(Z) Print(“nCompleted!!n”)```

### Output:

Result of the product of two matrices is

 ```[[88   72] [18    35]] Completed!!```

The next topic in this study is “how to take the transpose of a matrix”. Transpose of a matrix is as simple in matrix manipulation. While we take the transpose of a matrix, the values in the columns will change to the values of rows and row values become column values. Let us see how to do it with the following illustration:

 ```//Transpose of matrices Import numpy as np X= np.array([[1,2], [2,3]. [3 -4]]) Print(“n The actual matrix isn”) Print(X) Print(The transpose of the matrix is”) Print(X.transpose()) Print(“nThats it..!!”)```

### Output:

 ```The actual matrix is [[ 1 2 2 3 3 -4]] The transpose of the matrix is [[1 2 3 2 3 -4]] That's it..!!```

### How will you add a new row?

Using NumPy package, we can add a new row as follows:

 ```From numpy import * i= array([[‘Jan’, 2,4,22, 25], [‘Feb’, 7,8,23, 24], [‘Mar’, 1,6,15, 17], [‘Apr’, 5,7,14, 17], [‘Jun’, 3,6,18, 23]]) i_j = append(i,[[ ‘Jul’, 6,8,25, 27]],0) print(i_j)```

We will get the result as follows once we execute the above code:

### Output:

 ```[[‘Jan’, 2,4,22, 25], [‘Feb’, 7,8,23, 24], [‘Mar’, 1,6,15, 17], [‘Apr’, 5,7,14, 17], [‘Jun’, 3,6,18, 23], [ ‘Jul’, 6,8,25, 27]]```

### How will you add a new column to a matrix?

Adding a new column to a matrix is possible through insert() option. To add a column, we must add the index in the place we need to add the new column and the array that contain the new values of the newly added column. In the following example, we add a new column in the fifth position as follows:

 ```From numpy import * i= array([[‘Jan’, 2,4,22, 25], [‘Feb’, 7,8,23, 24], [‘Mar’, 1,6,15, 17], [‘Apr’, 5,7,14, 17], [‘Jun’, 3,6,18, 23]]) i_j = insert(i,,[,,,,],1) print(i_j)```

We will get the result as follows while we execute the above code:

### Output:

 ```[[‘Jan’, 2,4,22, 25,12], [‘Feb’, 7,8,23, 24,13], [‘Mar’, 1,6,15, 17,14], [‘Apr’, 5,7,14, 17,15], [‘Jun’, 3,6,18, 23,16]]```

### How will you delete a row in a matrix?

Similar to adding a new row, deleting a row is also possible using delete() option. Similarly, we have to mention the index of that row and the axis value. The axis value will be ‘0’ in case of a row and ‘1’ for a column.

 ```From numpy import * i= array([[‘Jan’, 2,4,22, 25], [‘Feb’, 7,8,23, 24], [‘Mar’, 1,6,15, 17],  [‘Apr’, 5,7,14, 17], [‘Jun’, 3,6,18, 23]]) i = delete(i,,0) print(i)While we execute the above code, we will get the result as follows:```

### Output:

 ```[[‘Jan’, 2,4,22, 25], [‘Feb’, 7,8,23, 24], [‘Mar’, 1,6,15, 17], [‘Jun’, 3,6,18, 23]]```

### How will you delete a column from a matrix?

Similar to deleting a row, deleting a column from a matrix can be performed using delete() method. We have to mention the index of that row and the axis value. The axis value will be ‘0’ in case of a row and ‘1’ for a column.

 ```From numpy import * i= array([[‘Jan’, 2,4,22, 25], [‘Feb’, 7,8,23, 24], [‘Mar’, 1,6,15, 17], [‘Apr’, 5,7,14, 17], [‘Jun’, 3,6,18, 23]]) i = delete(i,j_,1) print(i)```

Once executing the above code, we will get the result as follows:

### Output:

 ```[[‘Jan’, 2, 22, 25], [‘Feb’, 7, 23, 24], [‘Mar’, 1, 15, 17], [‘Jun’, 3, 18, 23]]```

### How will you update a row in a matrix?

Updating values of a row in a matrix will replace the values present already with the new values. Let us do it with the following example:

 ```From numpy import * i= array([[‘Jan’, 2,4,22, 25], [‘Feb’, 7,8,23, 24], [‘Mar’, 1,6,15, 17], [‘Apr’, 5,7,14, 17], [‘Jun’, 3,6,18, 23]]) i =[‘Mar’, 3, 4, 5, 6] print(i)```

Once we execute the above script, the values present in the third row will be replaced with the new values as below:

### Output:

 ```[[‘Jan’, 2,4,22, 25], [‘Feb’, 7,8,23, 24], [‘Mar’, 3, 4, 5, 6], [‘Apr’, 5,7,14, 17], [‘Jun’, 3,6,18, 23]]```

With this topic, we conclude this tutorial. I hope this topic will be useful to you. See you on the next topic.