# Python Matrix

In this tutorial, we will learn about Python matrices. In Python, a matrix object is similar to nested lists as they are multi-dimensional. We will see how to create a matrix using Numpy arrays. Following this, we will see various matrix operations methods and examples for better understanding.

## What is a Matrix in Python?

A matrix in Python is a rectangular Numpy array. This array has to be two-dimensional. It contains data stored in the array's rows and columns. In a Python matrix, the horizontal series of items are referred to as "rows," while the vertical series of items are referred to as "columns." The rows and columns are stacked over each other just like a nested list. If a matrix contains r number of rows and c number of columns, where r and c are positive integers, then r x c determines the order of this matrix object.

We can store strings, integers, and objects of other data types in a matrix. Data is stored in the stacks of rows and columns in a matrix. The matrix is a crucial data structure for calculations in mathematics and science. In Python, we consider a list of lists or a nested list as a matrix since Python doesn't include any built-in type for a matrix object.

In the course of this tutorial, we will go through the following list of matrix operation methods.

• Matrix multiplication
• Matrix multiplication operator
• Matrix multiplication without Numpy
• Matrix inverse
• Matrix transpose
• Matrix to array

## How do Matrices in Python work?

We write data in a two-dimensional array to create a matrix. It is done as follows:

Example

It displays a matrix having 3 rows and 5 columns, so its dimension is 3×5. Integer data type objects make up the data in this matrix. Row1, the first row, has values (2, 3, 5, 7, 6), while Row2 has values (3, 2, 6, 7, 2) and Row3 has values 5, 7, 2, 6, 1. Regarding columns, Column1 has values (2, 3, 5), Column2 has values (3, 2, 7), and so on.

Example

It displays a matrix having 3 rows and 3 columns, so its dimension is 3×3. Such matrices having equal rows and columns are called square matrices.

Similarly, Python allows users to store their data within an m x n dimensional matrix. We can perform the addition of matrices, multiplication, transposition, and other operations on a matrix-like structure.

The implementation of a matrix object in Python is not straightforward. We can create a Python matrix by using arrays and similarly use them.

## NumPy Array

The scientific computing software NumPy supports a robust N-dimensional array object. Installing NumPy is a prerequisite to using it in our program.

NumPy can be used and imported after installation. Knowing the basics of Numpy Array will be helpful in understanding matrices.

Arrays having multiple dimensions of items are provided by NumPy. Here's an illustration:

Code

Output:

```['4' '6' 'Harry']
Data type of array object:  <class 'numpy.ndarray'>
```

As we can see, Numpy arrays belong to the ndarray class.

### Example to Create a Matrix using Numpy Array

Think about the scenario where we create a record of students' marks. We will record the student's name and marks in two subjects, Python programming, and Matrix. We will create a two-dimensional matrix using a numpy array and then reshape it.

Code

Output:

```The matrix is:
[['Itika' '89' '91']
['Harry' '91' '81']
['Andrew' '87' '91']
['Peter' '72' '79']]
```

### Example to Create a Matrix using Numpy Matrix Method

We can use the numpy.matrix to create a 2D matrix.

Code

Output:

```[[3 4]
[5 6]]
```

## Accessing Values of a Matrix

The indices of a matrix can be used to access the elements stored in it. Data stored in a matrix is accessible using the same approach we use for a two-dimensional array.

Code

Output:

```['Itika' '89' '91']
Andrew's marks in Matrix subject:  91
```

## Methods to Create a 2-D Numpy Array or a Matrix

There are several methods to create a two-dimensional NumPy array and hence a matrix. Providing entries for the rows and columns

We can provide integers, floats, or even complex numbers. Using the dtype attribute of the array method, we can specify the data type we want.

Code

Output:

```Array of data type integers:
[[4 2 7 3]
[2 8 5 2]]
Array of data type float:
[[1.5 2.2 3.1]
[3.  4.4 2. ]]
Array of data type complex numbers:
[[5.+0.j 3.+0.j 6.+0.j]
[2.+0.j 5.+0.j 7.+0.j]]
```

### Array having Zeros and Ones

Code

Output:

```[[0. 0.]
[0. 0.]
[0. 0.]]
[[1 1 1 1]
[1 1 1 1]]
```

Here, we have specified dtype to 64 bits.

### Using the arange() and shape() Methods

Code

Output:

```[0 1 2 3 4]
[[0 1 2]
[3 4 5]]
```

## Python Matrix Operations

We will add the two matrices and use the nested for loop through the given matrices.

Code

Output:

```The sum of the matrices is =  [[27, 46, 11], [49, 17, 21], [23, 8, 17]]
```

### Python Matrix Multiplication

Python Matrix Multiplication Operator

In Python @ is known as the multiplication operator. Let us see an example where we will use this operator to multiply two matrices.

Code

Output:

```[[44 26]
[68 42]]
```

### Python Matrix Multiplication without using Numpy

Another way of multiplying two matrices is using nested loops. Here is an example to show.

Code

Output:

```[66, 60, 64, 62]
[108, 86, 124, 90]
[85, 67, 103, 68]
```

### Python Matrix Inverse

When an equation needs to be solved to get the value of an unknown variable that satisfies the equations, the inverse of a matrix, which is just the reciprocal of the matrix as we would in regular mathematics, is calculated. A matrix's inverse is the matrix that gives the identity matrix when we multiply with the original matrix. Only a non-singular matrix can have an inverse. A non-singular matrix has a non-zero determinant.

Code

Output:

```[[-3.33333333e-01 -7.40148683e-17  3.33333333e-01]
[ 1.25000000e-01 -3.75000000e-01  3.12500000e-01]
[ 2.50000000e-01  2.50000000e-01 -3.75000000e-01]]
```

### Python Matrix Transpose

Python Matrix Transpose without Numpy

A matrix's transposition involves switching the rows and columns. It has the symbol X'. We will put the object in row i and column j of matrix X in row j and column i of matrix X'. Consequently, X' will become a 4x3 matrix if the original matrix X is a 3x4 matrix.

Code

Output:

```[4, 3, 7]
[6, 7, 3]
[7, 2, 7]
[8, 7, 5]
```

Python Matrix Transpose using Numpy

We can use the matrix.transpose() method in Numpy to get the matrix's transpose.

Code

Output:

```[[5 4]
[7 2]
[6 4]]
```

### Converting Python Matrix to Array

We can use ravel and flatten functions to convert a Python matrix to a Python array.

Code

Output:

```[[4 6 7 5 2 6 6 3 6]]
[4 6 7 5 2 6 6 3 6]
[4 6 7 5 2 6 6 3 6]
```

### Feedback   