NumPy – Matrix Library

NumPy - Matrix Library

In this chapter, we will discuss about NumPy – Matrix Library. This package contains a Matrix library numpy.matlib. This module has functions that return matrices instead of ndarray objects.

NumPy – Matrix Library Are Use –

  • matlib.empty()
  • numpy.matlib.zeros()
  • numpy.matlib.ones()
  • numpy.matlib.eye()
  • numpy.matlib.identity()
  • numpy.matlib.rand()

matlib.empty()

The matlib.empty() function returns a new matrix without initializing the entries.

numpy.matlib.empty(shape, dtype, order)

Where,

Sr.No.Parameter & Description
1shapeint or tuple of int defining the shape of the new matrix
2DtypeOptional. Data type of the output
3orderC or F

Example

import numpy.matlib 
import numpy as np 

print np.matlib.empty((2,2)) 
# filled with random data

It will output −

[[ 2.12199579e-314,   4.24399158e-314] 
 [ 4.24399158e-314,   2.12199579e-314]] 

numpy.matlib.zeros()

This function returns the matrix filled with zeros.

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

It will output −

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

numpy.matlib.ones()

This function returns the matrix filled with 1s.

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

It will output −

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

numpy.matlib.eye()

This function returns a matrix with 1 along the diagonal elements and the zeros elsewhere. Th

numpy.matlib.eye(n, M,k, dtype)

Where,

Sr.No.Parameter & Description
1nThe number of rows in the resulting matrix
2MThe number of columns, defaults to n
3kIndex of diagonal
4dtypeData type of the output

Example

import numpy.matlib 
import numpy as np 
print np.matlib.eye(n = 3, M = 4, k = 0, dtype = float)

It will output −

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

numpy.matlib.identity()

The numpy.matlib.identity() function returns the Identity matrix of the given size. An identity matrix is a square matrix with all diagonal elements as 1.

import numpy.matlib 
import numpy as np 
print np.matlib.identity(5, dtype = float)

It will output −

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

numpy.matlib.rand()

The numpy.matlib.rand() function returns a matrix of the given size filled with random values.

Example

import numpy.matlib 
import numpy as np 
print np.matlib.rand(3,3)

It will output −

[[ 0.82674464  0.57206837  0.15497519] 
 [ 0.33857374  0.35742401  0.90895076] 
 [ 0.03968467  0.13962089  0.39665201]]

Note that a matrix is always two-dimensional, where as ndarray is an n-dimensional array. Both the objects are inter-convertible.

Example

import numpy.matlib 
import numpy as np  

i = np.matrix('1,2;3,4') 
print i 

It will output −

[[1  2] 
 [3  4]]

Example

import numpy.matlib 
import numpy as np  

j = np.asarray(i) 
print j 

It will output −

[[1  2] 
 [3  4]] 

Example

import numpy.matlib 
import numpy as np  

k = np.asmatrix (j) 
print k

It will output −

[[1  2] 
 [3  4]]

Next Topic – Click Here

This Post Has One Comment

Leave a Reply