NumPy Uniform Distribution (Python Tutorial)

Uniform Distribution

This is a detailed tutorial of NumPy Uniform Distribution. Learn to implement Uniform Distribution in NumPy and visualize using Seaborn.

Uniform Distribution

Uniform Distribution has a large use in the Random Numbers. This distribution is helpful where the chances of occurrence of every event are very much equal in all the aspects. It is very helpful in the generation of the random number. These are the set of number s that, may occur in an event with no specified condition but on its own.

For the generation of these distributions we need to pass three parameters as input:

  • a – It will act as the lower bound. And also this lower bound by default is set to 0.0
  • b – It will act as the upper bound. And also the default value for this bound is set to 1.0
  • size – this will help us specifying the size of the array.

Let us take an example to understand it better:

# here first we will import the numpy package with random module
from numpy import random
# we will use method
x=random.uniform(size=(3,6))
#now we will print the graph
print(x)

Output.

[[ 0.69646919  0.28613933  0.22685145  0.55131477  0.71946897  0.42310646]
 [ 0.9807642   0.68482974  0.4809319   0.39211752  0.34317802  0.72904971]
 [ 0.43857224  0.0596779   0.39804426  0.73799541  0.18249173  0.17545176]]

Here we have given just the size of the array.

Let us take another example where we will pass al the parameters.

# here first we will import the numpy package with random module
from numpy import random
# we will use method
x=random.uniform( size= 10)
#now we will print the graph
print(x)

Output.

[ 0.69646919  0.28613933  0.22685145  0.55131477  0.71946897  0.42310646
  0.9807642   0.68482974  0.4809319   0.39211752]

let us do the visualization of the following data:

# here first we will import the numpy package with random module
from numpy import random
#here we ill import matplotlib 
import matplotlib.pyplot as plt 
#now we will import seaborn 
import seaborn as sns 
#we will plot a displot here 
sns.distplot(random.uniform(size= 10), hist=False) 
# now we have the plot printed 
plt.show()

Output.

Uniform Distribution

Difference between Normal and Uniform

# here first we will import the numpy package with random module 
from numpy import random 
#here we ill import matplotlib 
import matplotlib.pyplot as plt 
#now we will import seaborn 
import seaborn as sns 
#we will plot a displot here 
sns.distplot(random.normal(loc=50,scale=4,size=500), hist=False, label='normal') 
#we will plot a displot here 
sns.distplot(random.uniform(size= 10), hist=False, label='uniform')  
# now we have the plot printed
plt.show()

Output.

Difference Between Normal And Uniform

Difference between Uniform and Binomial

# here first we will import the numpy package with random module 
from numpy import random 
#here we ill import matplotlib 
import matplotlib.pyplot as plt 
#now we will import seaborn 
import seaborn as sns 
#we will plot a displot here 
sns.distplot(random.binomial(n=50,p=0.5,size=500), hist=False, label='binomial')
#we will plot a displot here 
sns.distplot(random.uniform(size=10), hist=False, label='uniform')  
# now we have the plot printed 
plt.show()

Output.

Difference Between Uniform And Binomial

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 *