How to generate Random Matrix

How to generate Random Matrix

How to create matrix of random numbers in python

In this lesson you are going to learn how to generate a random matrix using numpy library meaning every time we run the codes we will be getting different kind of matrix. the kind of matrix we will be creating are as follows

  1. Matrix with floating Values
  2. Random Matrix with integers Values
  3. Random Matrix with a specific range of numbers
  4. Matrix with desired size

How to create Matrix with floating.

TO create any kind of random matrix firstly know that important library to used is numpy meaning you have to import the library before anything else. How do I import the library well to import the numpy is quit simple. just import the library us as any variable name you like just like the below codes.

import numpy as np.

here I am using 'np' as my variable you can use something else if you like just make sure you keep using it till the end of your codes.

Now before we proceed the generate a random matrix let me ask if you understand how to generate random number. if you don't know that you can click here to read that. to generate random matrix now we need a library called 'random'. then we can proceed to generate matrix.

Example

import numpy as np
import random
myMatrix=np.random.random(5)
print(myMatrix)

Result>>

[0.53654621 0.45231273 0.74970701 0.51179952 0.54350661]

Example2

import numpy as np
import random
myMatrix=np.random.random(10)
print(myMatrix)

Result>>

[0.21718582 0.2308477  0.84079285 0.31893167 0.43007458 0.48372693 0.84833598 0.11123525 0.07961326 0.29077513]

observe the example 1 and example 2 above you will realize that the number written inside the random function determine the column size of the matrix. also note that all the result range from 0 to 1. so,this is simple way to generate floating Matrix. Also note that the two matrix above are 1 dimensional matrix just like list. How about two dimensional matrix???

How to generate floating Number of two Dimensional Matrix

two dimensional matrix is nothing differ from 1 dimensional only that here we need to write the size argument inside the random() function and assign it to our choice of dimension.

Example 1

import numpy as np
import random
myMatrix=np.random.random(size=(4,5))
print(myMatrix)

Result>>

[[0.83664788 0.11158384 0.00666588 0.12442542 0.41824017]
 [0.77004787 0.87676491 0.02516419 0.50320987 0.09949669]
 [0.56015428 0.17386232 0.69205763 0.64390824 0.35018918]
 [0.00123015 0.97272091 0.05288541 0.5629424  0.74574819]]

Example 2

import numpy as np
import random
myMatrix=np.random.random(7,8)
print(myMatrix)

Result>>

[[0.92596764 0.7906281  0.60714552 0.52456868 0.04624857 0.92515589
  0.85977556 0.03854705]
 [0.56817265 0.93814353 0.02934177 0.12848377 0.35616172 0.45257818
  0.84124296 0.37260006]
 [0.04392383 0.6938886  0.61621649 0.22408471 0.04108214 0.14822871
  0.02830375 0.20350134]
 [0.1893726  0.42215194 0.82908719 0.30356027 0.98256338 0.60989141
  0.95088032 0.35713609]
 [0.49855577 0.88311795 0.87173614 0.79163634 0.1740845  0.33459581
  0.77837606 0.49552818]
 [0.58388942 0.57002196 0.20522334 0.74790762 0.67865546 0.95910833
  0.33196239 0.82861333]
 [0.29832986 0.29554488 0.73374855 0.21160131 0.41387691 0.02423003
  0.34106501 0.44083752]]

Can you see how we introduce the size argument??? the first matrix is 4 by 5 matrix i.e 4 rows and 5 columns while the second is 7 by 8 matrix i.e 7 rows and 8 columns. you can change it to your choice but the format remain the same.

How to generate three Dimensional Matrix(floating)

You can imagine how this will be done, not differ from two dimensional just provide your dimension assigning to the size argument and that is all.

Example

import numpy as np
import random
myMatrix=np.random.random(size=(3,4,5))
print(myMatrix)

Result>>

[[[0.54154068 0.54829801 0.93882275 0.15953545 0.10179355]
  [0.07384472 0.76306589 0.87465594 0.79322888 0.76857476]
  [0.70122571 0.55242313 0.81810615 0.12787815 0.37954906]
  [0.4897314  0.08573829 0.85814818 0.23100719 0.74702228]]

 [[0.82106186 0.01185665 0.54248084 0.72639434 0.66151619]
  [0.45101086 0.45863037 0.01554694 0.88225542 0.2831863 ]
  [0.84434743 0.11292512 0.67359394 0.62930478 0.37338811]
  [0.51421235 0.91985597 0.49666669 0.26384621 0.78536287]]

 [[0.72052056 0.15817463 0.32019991 0.42737295 0.49193572]
  [0.05054342 0.83017712 0.23861814 0.22547626 0.58286517]
  [0.29212408 0.09105362 0.66692966 0.55540159 0.93845912]
  [0.48235499 0.11692568 0.77737347 0.52378258 0.32062268]]]

You can see from the result that the last number represent the number of column i.e 5 while the middle is the row i.e 4 while 3 do the format in 3 places. i.e formulating 2 dimensional matrix 3 times. How about 4 dimensional Matrix?? well let me say most of the time you don't always need it but that doesn't means it doesn't exist you can do for as much as dimension you want. just follow the same pattern and input your dimension inside the random() function.

Example of 4 dimensional Matrix

import numpy as np
import random
myMatrix=np.random.random(size=(2,3,4,5))
print(myMatrix)

Result>>

[[[[0.53418766 0.09797774 0.54653956 0.66342903 0.21339255]
   [0.32622721 0.37503511 0.51773339 0.24569951 0.47865251]
   [0.17531156 0.90776719 0.07562029 0.92898288 0.10245499]
   [0.38429552 0.07907702 0.2783116  0.50199012 0.71392349]]

  [[0.33460356 0.07623499 0.2873998  0.18179982 0.4144279 ]
   [0.30848727 0.89410334 0.23325824 0.73643898 0.66153579]
   [0.61537296 0.89491465 0.00811938 0.96678494 0.20806986]
   [0.79238113 0.67048095 0.30134586 0.44888957 0.33379819]]

  [[0.72251534 0.43202525 0.96407891 0.98649374 0.14528289]
   [0.56845872 0.04631825 0.00839899 0.81924306 0.65134966]
   [0.94956847 0.69794012 0.86264589 0.0197176  0.60050456]
   [0.04422539 0.17490321 0.57000959 0.96761872 0.193121  ]]]


 [[[0.35469213 0.3524116  0.20441285 0.60105324 0.24138903]
   [0.00898076 0.04745275 0.24792945 0.95475473 0.88687222]
   [0.3594462  0.46355087 0.02392699 0.23517865 0.35177281]
   [0.53119961 0.30085628 0.68822892 0.09000496 0.02525243]]

  [[0.62606509 0.13560369 0.68865443 0.79040089 0.83535603]
   [0.90667341 0.16918656 0.0340416  0.25963479 0.59328857]
   [0.7451594  0.9574463  0.05146664 0.25810624 0.43270705]
   [0.62638472 0.17131787 0.33523104 0.42145385 0.68404432]]

  [[0.4680146  0.25403458 0.05934939 0.93939989 0.96745228]
   [0.53734234 0.12862929 0.75995028 0.81294553 0.83203757]
   [0.08757136 0.82775334 0.74139344 0.56213748 0.23160865]
   [0.5902586  0.89731232 0.14971653 0.60398504 0.39872987]]]]

can you see the result?? you can generate as much as you want. But all of this are just floating number how about integers??

How to generate integer of 1 Dimensional Matrix.

what are integers?? integers are counting numbers, so how do we make a matrix of integers?? calm down I get your back. to do this is similar to that of floating what make the difference is that here we need to use random.randint() function instead of random.random() function that will use the other time.

Example 1

import numpy as np
import random
myMatrix=np.random.randint(4,size=5)
print(myMatrix)

Result>>>

[3,2,2,2,0]

Example 2

import numpy as np
import random
myMatrix=np.random.randint(6,size=10)
print(myMatrix)

Result>>

[5 5 4 4 4 1 5 1 4 5]

Note: the first value represent the range of random number which start from zero but exclude the value given.i.e for 4 means 0,1,2,3 and 6 means random numbers from 0,1,2,3,4,5. so you change the value to whatever you like. let's try 2 Dimensional Matrix

How to generate integer of 2 Dimensional Matrix.

this is just like what we did in floating just assign the dimension to size and you are done.

import numpy as np
import random
myMatrix=np.random.randint(4,size=(3,4))
print(myMatrix)

Result>>>

[[1 2 3 3]
 [3 2 2 1]
 [3 1 3 0]]

How about for 3 Dimensional Matrix?? just use the same trick even if you need to do 4 dimensional Matrix.

Example 2

import numpy as np
import random
myMatrix=np.random.randint(6,size=(2,3,4))
print(myMatrix)

Result>>

[[[1 4 3 5]
  [5 4 1 2]
  [3 1 2 4]]

 [[2 2 4 2]
  [4 2 5 3]
  [0 1 0 0]]]

How to generate random Matrix of specific range

well, sometime you might need to generate random integer of specific range which the last example might not suit so what is the solution?? let say I need to generate a 2 by 3 matrix range from 2 to 5. my code will look like the following: Codes>>

import numpy as np
import random
myMatrix=np.random.randint(2,6,size=(3,4))
print(myMatrix)

Result>>

[[2 2 2 2]
 [4 2 5 2]
 [5 5 3 3]]

Did you understand the codes?? all we need to do is to specify the range before the size. 2 is the beginning while 6 is the end but note the 6 is exclude. that is how to generate matrix of different kind consider to like and share if you find this helpful. feel free to ask me any question on this. you can also chat me up on 09153036869 should have any personal question or correction. thank you for reading. Enjoy!