Skip to content

Instantly share code, notes, and snippets.

@nbigot
Created May 10, 2016 08:15
Show Gist options
  • Save nbigot/9245d39ab88318ff5af78d6314ca9271 to your computer and use it in GitHub Desktop.
Save nbigot/9245d39ab88318ff5af78d6314ca9271 to your computer and use it in GitHub Desktop.
simple and small demo of python & numpy & matplotlib
#python
import random
import numpy
import matplotlib.pyplot as plt
# genere une liste de nombres aleatoires
# et affiche courbe de densite de probabilite
# http://www.science-emergence.com/Python/PythonFAQ/RandomNumberGaussPython/_source/
def demo0():
#x = [random.randint(0., 100.) for i in range(10000)]
x = [numpy.random.uniform(0., 100.) for i in range(10000)]
num_bins = 200
n, bins, patches = plt.hist(x, num_bins, normed=1, facecolor='green', alpha=0.5)
plt.show()
#demo0()
# genere une liste de nombres aleatoires avec une fonction gaussiene
# et affiche courbe de densite de probabilite
# http://www.science-emergence.com/Python/PythonFAQ/RandomNumberGaussPython/_source/
def demo1():
x = [random.gauss(100,15) for i in range(1000)]
num_bins = 50
n, bins, patches = plt.hist(x, num_bins, normed=1, facecolor='green', alpha=0.5)
plt.show()
#demo1()
# la meme avec une distribution gamma
# et affiche courbe de densite de probabilite
# https://fr.wikipedia.org/wiki/Loi_Gamma
# https://en.wikipedia.org/wiki/Gamma_distribution
def demo2():
x = numpy.random.gamma(2., 2., 10000)
#x = numpy.random.gamma(1., 2., 10000)
#x = numpy.random.gamma(3., 2., 10000)
#x = numpy.random.gamma(5., 1., 10000)
#x = numpy.random.gamma(9., .5, 10000)
num_bins = 50
n, bins, patches = plt.hist(x, num_bins, normed=1, facecolor='green', alpha=0.5)
plt.show()
#demo2()
def demo3():
# https://www.daniweb.com/programming/software-development/threads/199275/how-can-i-create-a-list-of-randomly-generated-tuples
#x = [(random.randint(low, high), ) for k in range(10)]
#x = [random.gauss(80,20) for i in range(1000)]
#x = numpy.random.gamma(2., 2., 10000)
x = [55 + (8. * random.gammavariate(3,2)) for i in range(10000)]
num_bins = 50
n, bins, patches = plt.hist(x, num_bins, normed=1, facecolor='green', alpha=0.5)
plt.show()
#demo3()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment