NumPy Random Data Distribution (Python Tutorial)

NumPy Random Data Distribution

This is a detailed tutorial of NumPy Random Data Distribution. Learn the concept of distributing random data in NumPy Arrays with examples.

Random Data Distribution

This distribution is a sort of list of all the values that we could have possibly due to distribution. In a data distribution, we depend on how often a value will occur in a sequence. These lists have all sort of random data that is quite useful in case of any studies.

When we work with statics and also in the field of data science, we need these data distributions. With the help of these distributions, we can carry out any sort of experimental study in any filed. We can use this data in various algorithms to get to the results.

In this, we have modules that offer us to generate random data so we could use it for our research work. These modules return us a lot of useful data distributions.

Random Distribution

These distributions contain a set of a random number that follows a certain function. This function is known as a probability density function. In this function, a continuous probability is given, which means it will give us a probability that if a number will appear in an array.

We have various methods with which we can generate random numbers.  One such method is choice(), the method which is part of the random module. This method will allow us to specify that with what probability will a number in an array.

The process of defining a probability for a number to appear in an array is set by giving 0 and 1. Where 0 will stand for values that will never come in the array and one stand for those numbers that will come in the array.

Let us go through an example for this to understand it better:

#Importing the numpy package and also the random module
from numpy import random
#Now we assign probability to the numbers by using choice()
a = random.choice([11,12,13,14,15],p=[0.2,0.1,0.3,0.4,0.0],size=(50))
# we will print the array
print(a)

Output.

[14 12 12 13 14 13 14 14 13 13 13 14 13 11 13 14 11 11 13 13 14 14 14 14 14
 13 13 12 12 14 11 13 13 13 13 13 13 14 14 13 14 11 13 13 14 12 13 14 13 14]

Here we get a set random number with assigned probability. So as we have given the number 15 as 0 so it will never occur in the whole array.

Let us make a 2-d array by giving the shape of the array:

#Importing the numpy package and also the random module 
from numpy import random 
#Now we assign probability to the numbers by using choice() 
a = random.choice([9,8,7,6,5],p=[0.2,0.1,0.3,0.4,0.0],size=(3,6)) 
# we will print the array 
print(a)

Output.

[[6 8 8 7 6 7]
 [6 6 7 7 7 6]
 [7 9 7 6 9 9]]

Here we get a two-dimensional array with all the probable numbers.

Let us go through another example:

#Importing the numpy package and also the random module 
from numpy import random 
#Now we assign probability to the numbers by using choice() 
a = random.choice([9,8,7,6,5],p=[0.2,0.1,0.3,0.4,0.0],size=(2,4)) 
# we will print the array 
print(a)

Output.

[[6 8 8 7]
 [6 7 6 6]]

Here we have an array with two layers and random numbers as per the probability.

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *