# Python Matrix Using Numpy [With Examples]

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.

## 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"],

#display
print(my_matrix)
```

Output:

```[['1' 'php' 'web-technologies']
['2' 'spark' 'bigdata']
['3' 'html' '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]`

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"],

#access second row
print(my_matrix)

#access forth row
print(my_matrix)

#access second row, third element
print(my_matrix)

#access forth row,first element
print(my_matrix)```

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"],

#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']
['5' 'IOT' 'python']]```

## 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 – [,,,] 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"],

#insert - [,,,] column to the matrix
print(insert(my_matrix,,[,,,],1))```

Output:

```[['1' 'php' 'web-technologies' '56']
['2' 'spark' 'bigdata' '67']
['3' 'html' 'web-technologies' '90']

## 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"],
#delete third row
my_matrix=delete(my_matrix,,0)

#delete first row
my_matrix=delete(my_matrix,,0)

#display the matrix
print(my_matrix)```

Output:

```[['2' 'spark' 'bigdata']

## 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"],
#delete third column
my_matrix=delete(my_matrix,,1)

#delete first column
my_matrix=delete(my_matrix,,1)

#display the matrix
print(my_matrix)```

Output:

```[['php']
['spark']
['html']

## 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"],

#update  third row
my_matrix=[10,"jsp","web"]

#update  forth row
my_matrix=[10,"jsp","web"]

#display matrix
print(my_matrix)```

Output:

```[['1' 'php' 'web-technologies']
['2' 'spark' 'bigdata']
['10' 'jsp' 'web']
['10' 'jsp' 'web']]```

### 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.