NumPy Matrix Library and Operations

Free NumPy course with real-time projects Start Now!!

The NumPy module consists of a matrix library. The numpy.matlib()is used in NumPy for matrix functions. These functions return matrix values as output. It uses the array elements as input. Let us learn about NumPy Matrix Library and various functions in it.

NumPy Matrix Library

1. np.matlib.empty()Function

We use this function to return a new matrix. The entries of the matrix are uninitialized. This function takes three parameters.

Syntax- np.matlib.empty(shape,dtype,order)

parameters and description

  • shape- It is a tuple value that defines the shape of the matrix.
  • dtype- It defines the data type of the matrix.
  • order- It is used to define the order.
import numpy as np
import numpy.matlib
print(np.matlib.empty((5,3)))  

Output

[[1.21713744e-316 1.01855798e-312 1.01855798e-312]
[9.54898106e-313 1.14587773e-312 1.01855798e-312]
[1.23075756e-312 1.10343781e-312 1.10343781e-312]
[9.76118064e-313 1.08221785e-312 1.10343781e-312]
[1.20953760e-312 5.73572782e+169 8.32980114e+151]]

2. np.matlib.zeros()Function

We use this function to initialize a new matrix. All the matrix elements are set to be zero.

import numpy as np  
import numpy.matlib  
print(np.matlib.zeros((2,3))) 
 

Output

[[0. 0. 0.]
[0. 0. 0.]]

3. np.matlib.ones()Function

We use this function to initialize a new matrix. All the matrix elements are filled with 1 as its value.

import numpy as np  
import numpy.matlib  
print(np.matlib.ones((2,4)))  

Output

[[1. 1. 1. 1.]
[1. 1. 1. 1.]]

4. np.matlib.identity()Function

An identity matrix is a matrix with all its diagonal elements as 1 and all the other elements as zero. This function returns an identity matrix of a given size.

import numpy as np  
import numpy.matlib  
print(np.matlib.identity(3))

Output

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

5. np.matlib.eye()Function

We use this function to initialize a matrix with 1 as the diagonal elements and 0 otherwise. It has the following parameters

Syntax- np.matlib.eye(n,m,k,dtype)

parameters and description

  • n: It represents the number of rows
  • m: It represents the number of columns
  • k: It denotes the index of the diagonal
  • dtype: It defines the data type of output matrix
import numpy as np  
import numpy.matlib  
print(numpy.matlib.eye(n = 4, M = 4, k = 0, dtype = float))  

Output

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

6. np.matlib.rand()Function

We use it to initialize a matrix filled with random values. The size of the matrix is given as input.

import numpy as np  
import numpy.matlib  
print(numpy.matlib.rand(4,2)) 
 

Output

[[0.77051087 0.69406467]
[0.67043961 0.47559031]
[0.17870651 0.82540777]
[0.79233063 0.77516969]]

7. np.matlib.repmat()Function

We use this function to return an array with its element is repeated. We define the axis of repetition

Syntax- np.matlib.repmat(array,m,n)
We take three arguments. The first one defines the array and the other two the axes.

import numpy as np
import numpy.matlib
arr = np.array(1)
np.matlib.repmat(arr, 4, 2)

Output

array([[1, 1],
[1, 1],
[1, 1],
[1, 1]])

8. np.matlib.randn()Function

With the use of this function, we can generate an array having random values from a standard normal distribution. We can specify the number of elements as a parameter.

import numpy as np
import numpy.matlib
np.matlib.randn(3)

Output

matrix([[ 1.81789638, 0.71182264, -0.34353199]])

9. np.matmul() Function

The matrix multiplication function gives the multiplication of two matrices of the same shape. If the shape is not the same, then it gives an error.

import numpy as np  
a = np.array([4,5,6])  
b = np.array([7,8,9])  
#matrix multiplication function
i=np.matmul(a,b)
print(i)

Output

122

Summary

The matlib library is very useful for working with NumPy matrix. It takes in the ndarray object as input and returns the appropriate matrix. These functions have additional functionality due to its input parameters. We can define the size, data type, shape, and order of the resultant matrices

Your opinion matters
Please write your valuable feedback about DataFlair on Google

follow dataflair on YouTube

Leave a Reply

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