In this tutorial, we will introduce how to create a randomized matrix using python numpy.
In order to create a matrix with random numbers, we can use numpy.random.rand() function.
1. Import numpy library
import numpy as np
2. Create a 1D random matrix
random_matrix_array = np.random.rand(3) print(random_matrix_array)
The matrix may be:
[0.13972036 0.58100399 0.62046278]
2. Create a 2D random matrix
random_matrix_array = np.random.rand(3, 4) print(random_matrix_array)
The shape if random_matrix_array is (3, 4), it may be:
[[0.43189018 0.0903101 0.2664645 0.37512746] [0.63474244 0.91995859 0.84270619 0.97062349] [0.19307901 0.29623444 0.30945273 0.93585395]]
3. Create a 3D random matrix
random_3d_matrix_array = np.random.rand(3, 4, 2) print(random_3d_matrix_array)
The shape of random_3d_matrix_array is (3, 4, 2). It may be:
[[[0.55267301 0.95526256] [0.92689674 0.86599548] [0.87304883 0.32868337] [0.14190636 0.92375264]] [[0.22447201 0.00706627] [0.60944606 0.71169812] [0.371652 0.48960865] [0.77221671 0.30692933]] [[0.11237068 0.99828592] [0.1608211 0.47616887] [0.5892122 0.52634281] [0.10034398 0.36586993]]]