Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import matplotlib.pyplot as plt
- import numpy as np
- data = pd.read_csv("milwaukee_ward_votes.csv")
- def GetFirstDigit(num) -> int:
- first_digit = num
- while (first_digit >= 10):
- first_digit = first_digit // 10
- return (1 if first_digit == 0 else first_digit)
- biden = [0] * 10
- trump = [0] * 10
- # get occurance data
- for index, row in data.iterrows():
- biden_digit = GetFirstDigit(row['Joseph R. Biden / Kamala D. Harris'])
- trump_digit = GetFirstDigit(row['Donald J. Trump / Michael R. Pence'])
- biden[biden_digit] += 1
- trump[trump_digit] += 1
- for index in range(10):
- biden[index] = biden[index] / 478
- trump[index] = trump[index] / 478
- fig, ax = plt.subplots()
- plt.xlim((1, 9))
- benford = [0, .301, .176, .125, .097, .079, .067, .058, .051, 0.046]
- ax.plot(biden, label='Biden', color='b')
- ax.plot(trump, label='Trump', color='r')
- ax.plot(benford, label='True Benford', color='y')
- ax.set_ylabel('Probability of Occurance')
- ax.set_xlabel('Most Significant Integer')
- ax.set_title('2020 Votes for Major Candidates vs Benford\'s Law')
- ax.legend()
- plt.show()
Add Comment
Please, Sign In to add comment