Code Python to check and graph if it follows Benford's law

main.py


a=""" """
#During this time, copy the sequence of numbers from Excel etc.

import collections
import matplotlib.pyplot as plt

a=a.split("\n")
a = [x for x in a if x != '']
#Remove null
l_head=list(map(lambda x: str(x)[0], a))
#Get the first character
l_count=collections.Counter(l_head).most_common(9)
#9th from the top because it may contain 0
#l_count.sort(key=lambda x: x[0])
#sort
#When graphed, it is sorted in the order of 123456789 or the frequency of appearance. Every 123 when removed.

l_count
l_rate=list(map(lambda x: x*100 /sum(list(zip(*l_count))[1]), list(zip(*l_count))[1]))
#Percentage calculation
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()

Example: Population by administrative unit in Japan Figure_1.png

Area for each administrative unit in Japan

Figure_menseki.png

Population density by administrative unit in Japan Figure_mitsudo.png

Number of households per administrative unit in Japan Figure_setai.png

All the numbers reflected here bandicam 2020-09-01 15-12-32-599.jpg Figure_zyoho.png

Statistics are from the statistics bureau figures https://www.stat.go.jp/data/index.html

Recommended Posts

Code Python to check and graph if it follows Benford's law
I want to format and check Python code to my liking on VS Code
Create Python folder Check if it already exists
List of Python code to move and remember
If you try to install Python2 pip after installing Python3 pip and it is rejected
I want to write in Python! (1) Code format check
Rewrite Python2 code to Python3 (2to3)
Write tests in Python to profile and check coverage
Python # How to check type and type for super beginners
[Python / Ruby] Understanding with code How to get data from online and write it to CSV
Try to make it using GUI and PyQt in Python
[python] How to check if the Key exists in the dictionary
[pyqtgraph] Add region to the graph and link it with the graph region
The process of making Python code object-oriented and improving it
PyArmor ~ Easy way to encrypt and deliver python source code ~
Overview of Python virtual environment and how to create it
Convert python 3.x code to python 2.x
Operate Jupyter with REST API to extract and save Python code
Don't write Python if you want to speed it up with Python
Join csv normalized by Python pandas to make it easier to check
How to install OpenCV on Cloud9 and run it in Python
[PEP8] Take over the Python source code and write it neatly
Difference in how to write if statement between ruby ​​and python
If Python code written by someone else is hard to decipher (Python)
Convert the result of python optparse to dict and utilize it
Check if you can connect to a TCP port in Python
Have python check if the string can be converted / converted to int
How to build Python and Jupyter execution environment with VS Code
How to automatically check if the code you wrote in Google Colaboratory corresponds to the python coding standard "pep8"
Python 3.6 on Windows ... and to Xamarin.
[Introduction to Python3 Day 1] Programming and Python
[Code] Module and Python version output
Check and move directories in Python
Check python code styles using pep8
Python logging and dump to json
Selenium and python to open google
What to do if ipython and python start up with different versions
[Introduction to Python] How to judge authenticity with if statement (True and None)
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
Try to write python code to generate go code --Try porting JSON-to-Go and so on
If pip stops due to SSL, it is better to re-insert python itself
Python code to train and test with Custom Vision of Cognitive Service
Read CSV file with Python and convert it to DataFrame as it is
From re-environment construction of Python to graph drawing (on visual studio code)
[python] Send the image captured from the webcam to the server and save it
How to generate a QR code and barcode in Python and read it normally or in real time with OpenCV
A Python program that checks if any content has been updated and git commits & pushes it to GitLab if it has been updated.
If you use Pandas' Plot function in Python, it is really seamless from data processing to graph creation