powelaw.py 965 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import powerlaw
  2. from numpy import genfromtxt
  3. import matplotlib.pyplot as plt
  4. mydata = genfromtxt('power_law.csv', delimiter=',', dtype = 'int')
  5. print(len(mydata))
  6. print(mydata)
  7. ## Build and print probability distribution, bins per 10
  8. distr = dict()
  9. for i in mydata:
  10. bins = i // 10
  11. if bins in distr:
  12. distr[bins] += 1
  13. else:
  14. distr[bins] = 1
  15. for i in distr:
  16. print(str(i) + ', ' + str(distr[i]))
  17. results=powerlaw.Fit(mydata, True, xmin=1, xmax = 2000)
  18. print(results.power_law.alpha)
  19. print(results.truncated_power_law.alpha)
  20. print(results.power_law.xmin)
  21. print(results.truncated_power_law.xmin)
  22. print(results.truncated_power_law.xmax)
  23. print(results.power_law.discrete)
  24. print(results.lognormal.mu)
  25. results.plot_pdf(color = 'blue')
  26. results.power_law.plot_pdf(color = 'green')
  27. results.truncated_power_law.plot_pdf(color = 'red')
  28. #plt.plot(results.data)
  29. plt.show()
  30. R, p=results.distribution_compare('power_law','exponential')
  31. print(R,p)