练习:Benford定律
# -*- coding:utf-8 -*-
# /usr/bin/python
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['axes.unicode_minus'] = False
mpl.rcParams['font.sans-serif'] = 'SimHei'
def top(number):
number -= int(number)
frequency[int(10 ** number) - 1] += 1
if __name__ == '__main__':
N = 100000
x = list(range(1, N+1))
frequency = np.zeros(9, dtype=np.int)
f = 1
y = np.cumsum(np.log10(x))
list(map(top, y))
plt.figure(facecolor='w')
t = np.arange(1, 10)
plt.plot(t, frequency, 'r-', t, frequency, 'go', lw=2, markersize=8)
for x,y in enumerate(frequency):
plt.text(x+1.1, y, frequency[x], verticalalignment='top', fontsize=13)
plt.title('%d!首位数字出现频率' % N, fontsize=18)
plt.xlim(0.5, 9.5)
plt.ylim(0, max(frequency)*1.03)
plt.grid(b=True)
plt.show()
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。