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

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.
  1. import numpy as np
  2. import numpy.matlib
  3. 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.

  1. import numpy as np
  2. import numpy.matlib
  3. 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.

  1. import numpy as np
  2. import numpy.matlib
  3. 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.

  1. import numpy as np
  2. import numpy.matlib
  3. 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

#numpy tutorials #function

NumPy Matrix Library and Operations - DataFlair
1.45 GEEK