How to create a constant matrix in Python with NumPy?
Last Updated :
17 Dec, 2020
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
Python3
# 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
Python3
# 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).
Python3
# 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.
Python3
# 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.
Python3
# 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.
Python3
# 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,
Python3
# 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.
Python3
# 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]
Similar Reads
How to create an empty matrix with NumPy in Python? In Python, an empty matrix is a matrix that has no rows and no columns. NumPy, a powerful library for numerical computing, provides various methods to create matrices with specific properties, such as uninitialized values, zeros, NaNs, or ones. Below are different ways to create an empty or predefin
3 min read
How to create constant in Python Python lacks built-in constant support, but constants are typically represented using all-uppercase names by convention. Although they can technically be reassigned, itâs best practice to avoid modifying their values. This convention helps indicate that such variables should remain unchanged through
2 min read
How to create a vector in Python using NumPy NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python. Numpy is basically used for creating array of n dimensions. Vector are built
4 min read
Flatten a Matrix in Python using NumPy Let's discuss how to flatten a Matrix using NumPy in Python. By using ndarray.flatten() function we can flatten a matrix to one dimension in python. Syntax:numpy_array.flatten(order='C') order:'C' means to flatten in row-major.'F' means to flatten in column-major.'A' means to flatten in column-major
1 min read
How to Convert NumPy Matrix to Array In NumPy, a matrix is essentially a two-dimensional NumPy array with a special subclass. In this article, we will see how we can convert NumPy Matrix to Array. Also, we will see different ways to convert NumPy Matrix to Array. Convert Python NumPy Matrix to an ArrayBelow are the ways by which we can
3 min read
How to Create Array of zeros using Numpy in Python numpy.zeros() function is the primary method for creating an array of zeros in NumPy. It requires the shape of the array as an argument, which can be a single integer for a one-dimensional array or a tuple for multi-dimensional arrays. This method is significant because it provides a fast and memory
4 min read
How to convert NumPy array to dictionary in Python? The following article explains how to convert numpy array to dictionary in Python. Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy, number of dimensions of the array is called rank of the array. A tuple of integers givi
3 min read
How to create a matrix of random integers in Python ? Prerequisites: numpy To create a matrix of random integers in Python, randint() function of the numpy module is used. This function is used for random sampling i.e. all the numbers generated will be at random and cannot be predicted at hand. Syntax : Â numpy.random.randint(low, high=None, size=None,
2 min read
How to get the number of dimensions of a matrix using NumPy in Python? In this article, we will discuss how to get the number of dimensions of a matrix using NumPy. It can be found using the ndim parameter of the ndarray() method. Syntax: no_of_dimensions = numpy.ndarray.ndim Approach: Create an n-dimensional matrix using the NumPy package.Use ndim attribute available
3 min read
Different Ways to Create Numpy Arrays in Python Creating NumPy arrays is a fundamental aspect of working with numerical data in Python. NumPy provides various methods to create arrays efficiently, catering to different needs and scenarios. In this article, we will see how we can create NumPy arrays using different ways and methods. Ways to Create
3 min read