-
Pandas Basics 5
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
Lecture1.4
-
Lecture1.5
-
-
Data Transformations 6
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
Lecture2.4
-
Lecture2.5
-
Lecture2.6
-
-
Statistics 4
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
Lecture3.4
-
-
Reading and Writing Data 3
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
-
Joins 5
-
Lecture5.1
-
Lecture5.2
-
Lecture5.3
-
Lecture5.4
-
Lecture5.5
-
-
Grouping 4
-
Lecture6.1
-
Lecture6.2
-
Lecture6.3
-
Lecture6.4
-
-
Introduction to Numpy 4
-
Lecture7.1
-
Lecture7.2
-
Lecture7.3
-
Lecture7.4
-
-
Randomness 2
-
Lecture8.1
-
Lecture8.2
-
-
Numpy Data Functionality 1
-
Lecture9.1
-
Random Functions
Random Functions¶
There are quite a few functions which you can use from the random library. Here we will go through a few of them.
Uniform Random Numbers¶
Numpy will assign probabilities evenly to pick numbers between a and b given as arguments.
import matplotlib.pyplot as plt
#Set the seed to 0
np.random.seed(0)
#Pick 100000 random uniform numbers between 0 and 5
sample = np.random.uniform(0, 5, 100000)
plt.hist(sample, bins=50)
plt.xlabel("x")
plt.ylabel("Frequency")
plt.title("Uniform Random Numbers")
plt.show()
Normal Random Numbers¶
Numpy can pick numbers randomly from a normal distribution given the average and standard deviation of the distribution. For example, a distribution with mean of 10 and standard deviation of 5 is shown below.
#Set the seed to 0
np.random.seed(0)
#Pick 100000 random normal numbers
sample = np.random.normal(10, 5, 100000)
plt.hist(sample, bins=50)
plt.xlabel("x")
plt.ylabel("Frequency")
plt.title("Uniform Random Numbers")
plt.show()
Exponential Random Numbers¶
If you are familiar with exponential distribution, you can use it in numpy where the argument given is the scale or $\frac{1}{\lambda}$. Below we can show an example with scale of 10.
#Set the seed to 0
np.random.seed(0)
#Pick 100000 random exponential numbers
sample = np.random.exponential(10, 100000)
plt.hist(sample, bins=50)
plt.xlabel("x")
plt.ylabel("Frequency")
plt.title("Uniform Exponential Numbers")
plt.show()
Random Choice¶
Another method that is useful is the random choice function in numpy. This will allow you to choose one or multiple values randomly from a list with or without replacement. For example, if we had a range of numbers between 10, 20...100, and wanted to pick one of them.
#Create options to pick from
options = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
print(options)
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
#Set the seed
np.random.seed(0)
#Pick random numbers
print(np.random.choice(options))
print(np.random.choice(options))
print(np.random.choice(options))
60
10
40
We can pick multiple options by passing a second argument for number to pick. By default replacement is on so numbers can be picked again. Notice below that the number 40 gets picked twice.
#Set the seed
np.random.seed(0)
#Pick 5 random numbers
print(np.random.choice(options, 5))
[60 10 40 40 80]
If you set replace=False then you can turn this off.
#Set the seed
np.random.seed(0)
#Pick 5 random numbers
print(np.random.choice(options, 5, replace=False))
[ 30 90 50 100 20]