繁体   English   中英

我如何在python中的这个概率代码上使用直方图

[英]How can i use histogram graph on this probability code in python

有人可以告诉我如何从我的输出中创建直方图。

此代码的输出。

{89: 0.015, 51: 0.02, 71: 0.005, 70: 0.014, 8: 0.006, 12: 0.004, 53: 0.009, 2: 0.012, 58: 0.016, 59: 0.006, 83: 0.016, 95: 0.011, 74: 0.013, 79: 0.013, 55: 0.013, 93: 0.011, 78: 0.007, 90: 0.01, 33: 0.01, 72: 0.014, 18: 0.005, 61: 0.013, 22: 0.01, 37: 0.012, 92: 0.017, 98: 0.012, 23: 0.006, 14: 0.009, 38: 0.008, 94: 0.005, 65: 0.01, 76: 0.019, 20: 0.007, 80: 0.014, 68: 0.009, 46: 0.009, 26: 0.007, 15: 0.015, 28: 0.009, 3: 0.009, 85: 0.011, 82: 0.018, 57: 0.012, 45: 0.008, 48: 0.015, 60: 0.012, 43: 0.011, 77: 0.007, 47: 0.013, 10: 0.01, 34: 0.009, 42: 0.008, 17: 0.012, 13: 0.011, 39: 0.007, 31: 0.01, 5: 0.009, 41: 0.011, 9: 0.01, 81: 0.011, 29: 0.007, 36: 0.011, 1: 0.013, 30: 0.016, 50: 0.011, 62: 0.01, 87: 0.011, 63: 0.013, 67: 0.008, 19: 0.006, 11: 0.009, 44: 0.008, 64: 0.007, 96: 0.013, 97: 0.012, 69: 0.005, 6: 0.012, 52: 0.005, 16: 0.008, 73: 0.009, 75: 0.01, 35: 0.005, 84: 0.008, 21: 0.012, 24: 0.008, 49: 0.01, 86: 0.008, 99: 0.01, 40: 0.013, 27: 0.002, 56: 0.012, 32: 0.008, 4: 0.005, 54: 0.007, 88: 0.013, 7: 0.011, 66: 0.011, 91: 0.005, 25: 0.003}
from collections import Counter
import numpy as np

x = np.random.randint(low=1, high=100, size=1000)
counts = Counter(x)
total = sum(counts.values())
probability_mass = {k:v/total for k,v in counts.items()}
# Print each probability
print(str(probability_mass))

如果您使用的是 matplotlib,则不需要自己计算概率质量,因为hist函数已经为您完成了:

from collections import Counter
import numpy as np
import matplotlib.pyplot as plt

x = np.random.randint(low=1, high=100, size=1000)
# create a list of elements repeating as many times as its count
counts = list(Counter(x).elements())
plt.hist(counts, density=True, edgecolor='black')

归一化

如果您不想要密度(标准化版本),请不要传递density标志:

plt.hist(counts, edgecolor='black')

未标准化

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM