NumPy Matrix Library

NumPy contains a matrix library, i.e. numpy.matlib which is used to configure matrices instead of ndarray objects.

numpy.matlib.empty() function

This function is used to return a new matrix with the uninitialized entries. The syntax to use this function is given below.

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

It accepts the following parameter.

  1. shape: It is the tuple defining the shape of the matrix.
  2. dtype: It is the data type of the matrix.
  3. order: It is the insertion order of the matrix, i.e. C or F.

Consider the following example.

Example

snippet
import numpy as np

import numpy.matlib

print(numpy.matlib.empty((3,3)))

Output:

Output
[[6.90262230e-310 6.90262230e-310 6.90262304e-310] [6.90262304e-310 6.90261674e-310 6.90261552e-310] [6.90261326e-310 6.90262311e-310 3.95252517e-322]]

numpy.matlib.zeros() function

This function is used to create the matrix where the entries are initialized to zero.

Consider the following example.

Example

snippet
import numpy as np

import numpy.matlib

print(numpy.matlib.zeros((4,3)))

Output:

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

numpy.matlib.ones() function

This function returns a matrix with all the elements initialized to 1.

Consider the following example.

Example

snippet
import numpy as np

import numpy.matlib

print(numpy.matlib.ones((2,2)))

Output:

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

numpy.matlib.eye() function

This function returns a matrix with the diagonal elements initialized to 1 and zero elsewhere. The syntax to use this function is given below.

snippet
numpy.matlib.eye(n, m, k, dtype)

It accepts the following parameters.

  1. n: It represents the number of rows in the resulting matrix.
  2. m: It represents the number of columns, defaults to n.
  3. k: It is the index of diagonal.
  4. dtype: It is the data type of the output

Consider the following example.

Example

snippet
import numpy as np

import numpy.matlib

print(numpy.matlib.eye(n = 3, M = 3, k = 0, dtype = int))

Output:

Output
[[1 0 0] [0 1 0] [0 0 1]]

numpy.matlib.identity() function

This function is used to return an identity matrix of the given size. An identity matrix is the one with diagonal elements initializes to 1 and all other elements to zero.

Consider the following example.

Example

snippet
import numpy as np

import numpy.matlib

print(numpy.matlib.identity(5, dtype = int))

Output:

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() function

This function is used to generate a matrix where all the entries are initialized with random values.

Consider the following example.

Example

snippet
import numpy as np

import numpy.matlib

print(numpy.matlib.rand(3,3))

Output:

Output
[[0.86201511 0.86980769 0.06704884] [0.80531086 0.53814098 0.84394673] [0.85653048 0.8146121 0.35744405]]
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents