How to Create a Matrix in Python using Numpy ?

Numpy is the best libraries for doing complex manipulation on the arrays. It’s very easy to make a computation on arrays using the Numpy libraries. Array manipulation is somewhat easy but I see many new beginners or intermediate developers find difficulties in matrices manipulation. In this section of how to, you will learn how to create a matrix in python using Numpy.

What is a matrix?

Matrix is a two-dimensional array. In numpy, you can create two-dimensional arrays using the array() method with the two or more arrays separated by the comma. You can read more about matrix in details on Matrix Mathematics.

array1 = np.array([1,2,3])
array2 = np.array([4,5,6])
matrix1 = np.array([array1,array2])
matrix1

How to create a matrix in a Numpy?

There is another way to create a matrix in python. It is using the numpy matrix() methods. It is the lists of the list. For example, I will create three lists and will pass it the matrix() method.

list1 = [2,5,1]
list2 = [1,3,5]
list3 = [7,5,8]

matrix2 = np.matrix([list1,list2,list3])
matrix2

 

You can also find the dimensional of the matrix using the matrix_variable.shape. The matrix2 is of (3,3) dimension.

How to find the transpose of a matrix?

Transpose is a new matrix result from when all the elements of rows are now in column and vice -versa. You can find the transpose of a matrix using the matrix_variable .T. Like, in this case, I want to transpose the matrix2.

#transpose
matrix2.T

How to find the Inverse of a Matrix?

You can find the inverse of the matrix using the matrix_variable.I.

matrix2.I

To verify that this Inverse, you can multiply the original matrix with the Inverted Matrix and you will get the Identity matrix.

matrix2 * matrix2.I

How to solve the Simultaneous Linear Equations?

In a matrix, you can solve the linear equations using the matrix.  For example, you have the following three equations.

1) x + y − z = 4

2) x − 2y + 3z = −6

3) 2x + 3y + z = 7

First, you will create a matrix containing constants of each of the variable x,y,x or the left side. Then the matrix for the right side.

left_hand_side = np.matrix([[1,1,-1], # x + y − z = 4
                       [1,-2,3],      # x − 2y + 3z = −6
                       [2,3,1]])      #  2x + 3y + z = 7
left_hand_side

Right-hand Side

right_hand_side = np.matrix([[4],
                            [-6],
                            [7]])
right_hand_side

Solution

To find out the solution you have to first find the inverse of the left-hand side matrix and multiply with the right side.

left_hand_side_inverse = left_hand_side.I
left_hand_side_inverse
solution = left_hand_side_inverse*right_hand_side
solution

You can verify the solution is correct or not by the following

left_hand_side*solution - right_hand_side

Matrix is widely used by the data scientist for data manipulation. After reading this tutorial,  I hope you are able to manipulate the matrix. If you have any question regarding this then contact us we are always ready to help you.