Open In App

How to create a constant matrix in Python with NumPy?

A matrix represents a collection of numbers arranged in the order of rows and columns. It is necessary to enclose the elements of a matrix in parentheses or brackets. A constant matrix is a type of matrix whose elements are the same i.e. the element does not change irrespective of any index value thus acting as a constant.

Examples:



M = [[ x, x, x ]

       [ x ,x ,x]



      [ x, x, x]]

Here M is the constant matrix and x is the constant element.

Below are some examples of Constant Matrix:

A = [[ 5 , 5]

       [ 5, 5]]

B = [[ 12, 12, 12, 12, 12, 12]]

There are various methods in numpy module, which can be used to create a constant matrix such as numpy.full(), numpy.ones(), and numpy.zeroes().

Using numpy.full() method

Syntax:

numpy.full(shape, fill_value, dtype = None, order = ‘C’) 

Parameters:

  • shape: Number of rows
     
  • order: C_contiguous or F_contiguous
     
  • dtype: [optional, float(by Default)] Data type of returned array.  
     
  • fill_value: [bool, optional] Value to fill in the array.

Returns:  ndarray of a given constant having given shape, order and datatype.

Example 1: 

Here, we will create a constant matrix of size (2,2) (rows = 2, column = 2) with a constant value of 6.3




# import required module
import numpy as np
  
# use full() with a
# constant value of 6.3
array = np.full((2, 2), 6.3)
  
# display matrix
print(array)

Output:

[[6.3 6.3]
 [6.3 6.3]]

Example 2:

A similar example to the one showed above




# import required module
import numpy as np
  
# use full() with a
# constant value of 60
array = np.full((4, 3), 60)
  
# display matrix
print(array)

Output:

[[60 60 60]
 [60 60 60]
 [60 60 60]
 [60 60 60]]

Using numpy.ones() method

Syntax:

numpy.ones(shape, dtype = None, order = ‘C’) 

Parameters:

  • shape: integer or sequence of integers
  • order: C_contiguous or F_contiguous
  • dtype: Data type of returned array.

Returns: ndarray of ones having given shape, order and datatype.

Example 1:

Now, suppose we want to print a matrix consisting of only ones(1s).




# import required module
import numpy as np
  
# use ones() 
array = np.ones((2,2)) 
  
# display matrix
print(array)

Output: 

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

Here by-default, the data type is float, hence all the numbers are written as 1. An alteration, to the above code. Now, we want the data type to be of an integer.




# import required module
import numpy as np
  
# use ones() with integer constant
array = np.ones((2, 2), dtype=np.uint8)
  
# display matrix
print(array)

Output:

[[1 1]
 [1 1]]

Notice the change in the last two outputs, one of them shows, 1. And the other is showing 1 only, which means we converted the data type to integer in the second one. uint8 stands for an unsigned 8-bit integer which can represent values ranging from 0 to 255.

Example 2:

Here we create a one-dimensional matrix of only 1s.




# import required module
import numpy as np
  
# use ones() with integer constant
array = np.ones((5), dtype=np.uint8)
  
# display matrix
print(array)

Output:

[1 1 1 1 1]

Using numpy.zeroes() method

Syntax:

numpy.zeros(shape, dtype = None, order = ‘C’)

Parameters:

  • shape: integer or sequence of integers
  • order: C_contiguous or F_contiguous
  • dtype: Data type of returned array.

Returns: ndarray of zeros having given shape, order and datatype.

Example 1:

Now that we made a matrix of ones, let’s make one for zeroes.




# import required module
import numpy as np
  
# use zeroes()
array = np.zeros((2,2))
  
# display matrix
print(array)

Output:

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

To change it to an integer type,




# import required module
import numpy as np
  
# use zeroes() with integer constant
array = np.zeros((2,2), dtype=np.uint8)
  
# display matrix
print(array)

Output:

[[0 0]
 [0 0]]

Example 2:

Here is another example to create a constant one-dimensional matrix of zeroes.




# import required module
import numpy as np
  
# use zeroes() with integer constant
array = np.zeros((5), dtype=np.uint8)   
  
# display matrix
print(array)

Output:

[0 0 0 0 0]

Article Tags :