Skip to content

Instantly share code, notes, and snippets.

@XertroV

XertroV/benfords-lots.py Secret

Last active Nov 13, 2020
Embed
What would you like to do?
import json
from collections import Counter, defaultdict
import requests
import sys
import matplotlib.pyplot as plt
def loads_and_parse(nums):
parse_f = lambda s : int(s.replace(',', ''))
return list(map(parse_f, json.loads(nums)))
def get_data(state_name):
ret = defaultdict(list)
counties = requests.get(f"https://static01.nyt.com/elections-assets/2020/data/api/2020-11-03/state-page/{state_name}.json").json()['data']['races'][0]['counties']
for county in counties:
for cand, votes in county['results'].items():
ret[cand].append(votes)
return ret
def first_digits_kvs(kvs):
k, vs = kvs
# return (k, Counter(list(map(lambda v: int(str(v)[0]), vs))))
return (k, list(map(lambda v: int(str(v)[0]), vs)))
# from https://realpython.com/python-histograms/
def ascii_histogram(seq) -> None:
"""A horizontal frequency-table/histogram plot."""
for k in sorted(seq):
print('{0:5d} {1}'.format(k, '+' * seq[k]))
def heading(h):
print(f"\n\n{h} histogram")
def histogram_state(state, counts, only_two=True):
if only_two:
counts = dict(bidenj=counts['bidenj'], trumpd=counts['trumpd'])
plt.figure(figsize=([16,9]))
leg = list(counts.keys())
print(leg)
zipped_cs = list(map(list, counts.values()))
print(zipped_cs)
n, bins, patches = plt.hist(zipped_cs, 9, density=True, histtype='bar', label=leg, color=['blue', 'red'])
plt.legend(prop=dict(size=10))
# plt.grid(axis='y', alpha=0.75)
plt.xlabel('leading_digit')
plt.ylabel('frequency')
plt.title(f'benford histogram for {state}')
plt.savefig(f"b-out-{state}.png")
def print_graph(h, seq):
heading(h)
ascii_histogram(seq)
for state in ['illinois', 'nevada', 'wisconsin', 'washington', 'new-york', 'florida', 'arizona', 'georgia', 'wyoming', 'california', 'pennsylvania', 'ohio']:
try:
print(f"\n\n ## {state} ##\n")
cand_votes = get_data(state)
pairs = dict(map(first_digits_kvs, cand_votes.items()))
histogram_state(state, pairs)
# for k, v in pairs.items():
# print_graph(k, v)
except Exception as e:
print(f'exception during state: {state}')
raise e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.