Skip to main content

Zipf Distribution in Python


In this tutorial you will learn:

  • What is Zipf Distribution?
  • Zipf Distribution Implementation in python
  • Visualization of Zipf Distribution

Zipf Distribution

Zipf Distribution is a discrete pareto distribution also known as Riemann zeta distribution. It is specified by probability mass function. Zipf distribution samples the data based on Zipf’s law which refer to the fact that many types of data studied in the physical and social sciences can be approximated with a Zipfian distribution. Zipfian distribution belongs to the family of discrete power law probability distributions commonly used in linguistics, insurance and the modelling of rare events. The graphical pattern of Zipf Distribution follows a straight line when it is plotted on a double-logarithmic diagram.

Zipf Distribution in Python

In order to implement the Zipf Distribution the random module of python’s NumPy library function provides an inbuilt function ”zipf()”. It takes in 2 mandatory parameter. The first parameter is the “size”, it is the size of array which is desired as the output from the zipf() function, it could be 1D, 2D or n-dimensional array as required by the programmer. The second parameter is the distribution parameter defined by ‘a’, it must be a unsigned float or int and must greater than 1.
In order to observe the results of Zipf Distribution, lets take an example. Here we will generate a 1D array of Zipf distribution having size 4 with distribution 1.5. In the code below we are importing the random module in the second line of the code and in the fourth line we are applying the Zipf distribution with size of output array 4 and distribution parameter ‘a’ equal to 1.5.

  1. #importing the random module
  2. from numpy import random
  3. #applying the Zipf function
  4. res_arr= random.zipf(size=4,a=1.5)
  5. #printing the results
  6. print('1D array of size 4 having Zipf distribution with distribution parameter 1.5 :\n')
  7. print(res_arr)

Lets take another example, in this example we will generate a 2D array of Zipf distribution having size (5,2) with value of distribution parameter equal to 2.5.
  1. #importing the random module
  2. from numpy import random
  3. #here we are using Zipf function to generate Zipf distribution of size 5 x 2 with distribution parameter 2.5
  4. res_arr = random.zipf(size=(5,2),a=2.5)
  5. print('2D Zipf Distribution as output from Zipf() function:\n')
  6. #printing the result
  7. print(res_arr)

In this example we will generate a 3D array of Zipf distribution of the size(2,3,4) with distribution parameter equal to 1.1.
  1. #importing the random module
  2. from numpy import random
  3. #here we are using Zipf function to generate Zipf distribution of size 2 x 3 x 4
  4. res = random.zipf(size=(2,3,4), a=1.1)
  5. print('3D Zipf Distribution as output from Zipf() function:\n')
  6. #printing the result
  7. print(res)

Visualization of Zipf Distribution

In this example we will visualize the Zipf Distribution with distribution parameter 2. Here we will be using the displot function of seaborn library to plot and visualize a one dimensional discrete Zipf distribution

  1. #importing all the required modules and packages
  2. from numpy import random
  3. import matplotlib.pyplot as mpl
  4. import seaborn as sb
  5. #here we are using Zipf function to generate distributions of size 3000 with distribution parameter 2
  6. sb.distplot(random.zipf(size=3000,a=2), hist=False, label='Zipf Distribution')
  7. #plotting the graph
  8. mpl.show()

Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.