In this tutorial we will learn about Python Matrix. In our previous tutorial we learnt about Python JSON operations.

## Python Matrix

To work with Python Matrix, we need to import Python **numpy** module. If you do not have any idea about numpy module you can read python numpy tutorial. Python matrix is used to do operations regarding matrix, which may be used for scientific purpose, image processing etc.

### Create Matrix Python

In this section we will learn how to create a matrix in python.

According to wikipedia, a matrix is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns. So, in the following code we will be initializing various types of matrices.

Generally a matrix is created using `numpy.matix()`

function. We can use `numpy.shape`

to know the dimension of the matrix. See the following python matrix example code.

```
import numpy as np
# create 2x2 matrix
a = np.matrix([[1, 2], [3, 4]]) # using array of array
print('2x2 matrix is:n', a)
# using shape attribute to get the tuple describing matrix shape
print('The dimension of the matrix is :', a.shape)
# using MatLab syntax in string
b = np.matrix('[1,2;3,4;5,6]', dtype=np.int32) # limiting the data-type to int
print('n3x2 matrix is:n', b)
# using shape attribute to get the tuple describing matrix shape
print('The dimension of the matrix is :', b.shape)
# using numpy.random.rand(row, column) to generate array of random element
c = np.matrix(np.random.rand(3, 3), dtype=np.float32) # considering the data-type as float
print('n3x3 random element matrix is:n', c)
# using shape attribute to get the tuple describing matrix shape
print('The dimension of the matrix is :', c.shape)
```

You will get output like the following image.

### Python Matrix Addition

The manual code for matrix addition is complex enough to write! Thanks to numpy module, we can simply use **+** operator to for matrix addition. So, in the following example code we will see both to write the addition code manually and also by using plus operator.

```
import numpy as np
# create two 2x2 matrix
a = np.matrix([[1, 2], [3, 4]]) # using array of array
b = np.matrix([[5, 6], [7, 8]]) # using array of array
result = np.matrix(np.zeros((2,2))) # result matrix
print('A matrix :n', a)
print('nB matrix :n', b)
# traditional code
for i in range(a.shape[1]):
for j in range(a.shape[0]):
result[i, j] = a[i, j] + b[i, j]
print('nManually calculated result :n', result)
# get the result by simply using + operator
resultB = a + b
print('nCalculated using matrix + operator :n', resultB)
```

The output of the python matrix addition code is following.

```
A matrix :
[[1 2]
[3 4]]
B matrix :
[[5 6]
[7 8]]
Manually calculated result :
[[ 6. 8.]
[ 10. 12.]]
Calculated using matrix + operator :
[[ 6 8]
[10 12]]
```

### Python Matrix Multiplication, Inverse Matrix, Matrix Transpose

In the previous section we have discussed about the benefit of Python Matrix that it just makes the task simple for us. Like that, we can simply Multiply two matrix, get the inverse and transposition of a matrix.

As we have seen before that **+** operator adds two matrix, here we can simply use ***** operator to multiply matrices.

For matrix multiplication, number of columns in first matrix should be equal to number of rows in second matrix.

We can get the inverse of a matrix by using **getI()** function. We can use `getT()`

to get the transpose of matrix. Let’s have a look at matrix multiplication example.

```
import numpy as np
# initialize a 3x2 matrix of random values
matA = np.matrix(np.random.rand(3, 2))
# print the first matrix
print('The first matrix is :n', matA)
# initialize a 2x3 matrix of random values
matB = np.matrix(np.random.rand(2, 3))
# print the second matrix
print('nThe second matrix is :n', matB)
# multiply two matrix using * operator
result = matA * matB
# print the resultant matrix
print('nMatrix multiplication result :n', result)
# get the inverse of the first matrix
inverseMatA = matA.getI()
print('nThe inverse of the first matrix is :n', inverseMatA)
# get the transpose matrix of the second matrix
transposeMatB = matB.getT()
print('nThe transpose of the second matrix is :n', transposeMatB)
```

As we have used random values. So the elements of the matrix will vary. However the output of the above code is given below for a sample run in my computer.

```
The first matrix is :
[[ 0.88847844 0.01832413]
[ 0.08538396 0.20208474]
[ 0.92615527 0.8963927 ]]
The second matrix is :
[[ 0.03454971 0.89908281 0.08825769]
[ 0.46224998 0.63173062 0.91734146]]
Matrix multiplication result :
[[ 0.039167 0.81039161 0.09522454]
[ 0.09636365 0.20443036 0.1929165 ]
[ 0.44635589 1.398969 0.90403851]]
The inverse of the first matrix is :
[[ 1.12771189 -0.15722127 0.01239153]
[-1.13143853 0.40000541 1.04853336]]
The transpose of the second matrix is :
[[ 0.03454971 0.46224998]
[ 0.89908281 0.63173062]
[ 0.08825769 0.91734146]]
```

So, that’s all for python matrix operations. To know more about python matrix, you may read the official documentation.