1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ベンフォードの法則に沿うかを調べグラフ化するコードPython

Posted at
main.py
a=""" """
#この間にエクセル等から数字の列をコピペ

import collections
import matplotlib.pyplot as plt

a=a.split("\n")
a = [x for x in a if x != '']
#nullを削除
l_head=list(map(lambda x: str(x)[0], a))
#最初の文字を取得
l_count=collections.Counter(l_head).most_common(9)
#0を含むことがあるため上から9番目まで
#l_count.sort(key=lambda x: x[0])
#ソート
#グラフにした時に、123456789の順か、出現頻度の順かにソート。外すと123ごとに。

l_count
l_rate=list(map(lambda x: x*100 /sum(list(zip(*l_count))[1]), list(zip(*l_count))[1]))
#割合算出
l_index=list(zip(*l_count))[0]
l_rate
plt.xticks([0.15, 1.15, 2.15, 3.15, 4.15, 5.15, 6.15, 7.15, 8.15], l_index)
plt.bar(range(9), l_rate, color='g', width=0.3, label='Selected_data', align="center")
ben={1:30.1, 2:17.6, 3:12.5, 4:9.7, 5:7.9, 6:6.7, 7:5.8, 8:5.1, 9:4.6}
ben_label= list(map(lambda x: ben[int(x)], l_index))
plt.bar([0.3,1.3,2.3,3.3,4.3,5.3,6.3,7.3,8.3], ben_label, color='b', width=0.3, label='Benford_law', align="center")
plt.legend(bbox_to_anchor=(0.5, 1),loc=2)
plt.show()

例:
日本の行政単位ごとの人口
Figure_1.png

日本の行政単位ごとの面積

Figure_menseki.png

日本の行政単位ごとの人口密度
Figure_mitsudo.png

日本の行政単位ごとの世帯数
Figure_setai.png

ここに映る全ての数字
bandicam 2020-09-01 15-12-32-599.jpg
Figure_zyoho.png

統計は統計局の数値から
https://www.stat.go.jp/data/index.html

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?