Home »
Python »
Linear Algebra using Python
randomisation_matrix() Function to generate Random Matrix | Linear Algebra using Python
Linear Algebra using Python | randomisation_matrix() Function to generate Random Matrix: Here, we are going to learn about the randomisation_matrix() Function in Python?
Submitted by Anuj Singh, on May 22, 2020
Prerequisite: numpy.random.random( ) function with no input parameter
Numpy is the library of function that helps to construct or manipulate matrices and vectors. The function numpy.random.random() is a function used for generating a random value between 0 and 1. Now we are going to use this function to create a matrix of elements having a random value between 0 and 1. In this article, we are defining a function called randomisation_matrix() which returns a vector.
Syntax:
random_mat = randomisation_matrix(rows, columns)
Input parameter(s):
- rows, columns – represent the number of rows and columns.
Return value:
A Matrix of dimension (rows x columns) with random values (between 0 and 1) at each entry
Applications:
- Machine Learning
- Neural Network
- Probability - (PMF specifically)
- Statistics and Inference
- Markov Matrix
Python code to demonstrate example of randomisation_matrix() function
# Linear Algebra Learning Sequence
# randomisation_matrix() Function
# to generate Random Matrix
import numpy as np
# defining a function in input arguements
# as row and column numbers
def randomization_matrix(m,n):
x = np.random.random([m,n])
return x
m = int(input('Number of Rows: '))
n = int(input('Number of Columns: '))
# printing the random matrix
print(randomization_matrix(m,n))
Output:
RUN 1:
Number of Rows: 3
Number of Columns: 3
[[0.78101157 0.78051788 0.36688224]
[0.61297243 0.95915371 0.10535508]
[0.80270319 0.78350465 0.01176104]]
RUN 2:
Number of Rows: 9
Number of Columns: 4
[[0.35716108 0.33297787 0.66591572 0.54160728]
[0.53952926 0.84150531 0.49277988 0.21231327]
[0.63708892 0.12742805 0.80148422 0.78332466]
[0.47448847 0.27764141 0.81580429 0.15018078]
[0.65042576 0.51910242 0.42151608 0.5270826 ]
[0.72498296 0.95011068 0.0472314 0.19193858]
[0.18470833 0.90463529 0.44257893 0.46191247]
[0.40482905 0.95049498 0.61134395 0.80097087]
[0.50008522 0.99844731 0.90253853 0.42075036]]