How to Create a Matrix in Python Using NumPy

How to Create a Matrix in Python Using NumPy | Here, we will discuss how to create a matrix in Python using NumPy. Matrix is a rectangular table arranged in the form of rows and columns, in the programming world we implement matrix by using arrays by classifying it as 1D and 2D arrays. In this section, we will develop some programs to create a matrix in Python using NumPy.

How to Create a Matrix in Python Using NumPy

NumPy is a Python library used for working with arrays. NumPy was created in 2005 by Travis Oliphant. It also has functions for working in the domain of linear algebra, Fourier transform, and matrices. 

We will see these below topics:

  1. Create a Matrix in Python using numpy.array()
  2. Create a Matrix in Python using numpy.matrix()
  3. Create a Matrix in Python using numpy.reshape()
  4. Create a Matrix in Python using numpy.append()

Make a Matrix in Python using NumPy.array()

Now let us see a simple program to make a matrix in Python using NumPy. As mentioned above NumPy is a library that has many functions. In this code, we use numpy.array() to make a matrix.

import numpy as np
num = np.array([[3, 1, 4], [8, 5, 1], [9, 0, 2]])
print(num)

Output:

[[3 1 4]
[8 5 1]
[9 0 2]]

In the shown code, we have imported NumPy as np, then in variable num, we stored an array by using numpy.array() however we can do this in a mathematical way also but it would be a long sequence. That is why using NumPy is a better way of coding.

Python Program to Create a Matrix using numpy.matrix()

The numpy.matrix() is also a function in NumPy that is used to return an array. It works similar to numpy.array().

import numpy as np
num = np.matrix(([2, 1, 9], [5, 3, 4], [0, 9, 8]))
print(num)

Output:-

[[2 1 9]
[5 3 4]
[0 9 8]]

Create a Matrix in Python Using numpy.reshape()

The numpy.reshape() is a function in NumPy that converts a 1D array to 2D.

import numpy as np
num = np.array([[1, 2, 3, 4, 5, 6]]).reshape(2, 3)
print(num)

Output:

[[1 2 3]
[4 5 6]]

reshape() takes 2 parameters that are row and column, it is basically used to alter the size of the array. In the above example reshape() takes 2 parameters 3 and 3 so it converts 1D array to 2D array 3X3 elements.

Make a Matrix in Python using numpy.append()

The numpy.append() is a function that appends an array into the row.

import numpy as np
num = np.array([[1, 7, 5]])
new = np.append(num, [[8, 0, 6], [2, 5, 9]], axis=0)
print(new)

Output:

[[1 7 5]
[8 0 6]
[2 5 9]]

In the above code, the array num is appended to new so the array gets altered. The axis parameter is set 0 as we wish to append the elements as rows.  We have also used shape as it displays the dimension of the matrix. In the above example, we have created a matrix with 3 rows and 3 columns hence dimension is (3,3).

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *