Some Python code examples using Spyder5
One of my projects is to use Benford's Law to evaluate covid daily new cases and daily new deaths. Here's the code of my short program, which I run in Spyder5:
When using Spyder5 the graphs generated appear in separate windows. Each window has menu of controls which allow you to zoom in or out, pan the graph, edit the elements of the graph and save the graph in one of several formats. In the code below I save the graphs via code and not via the graph menus.
And, here is the longer version of that code, which I run in Jupyter notebook. The asterisks separate the cells:
One of my projects is to use Benford's Law to evaluate covid daily new cases and daily new deaths. Here's the code of my short program, which I run in Spyder5:
Code:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat Apr 24 20:12:51 2021 @author: jerry """ import pandas as pd from benfordslaw import benfordslaw nehist = pd.read_csv("https://covidtracking.com/data/download/nebraska-history.csv") ne_clean = nehist.drop(['state', 'deathConfirmed', 'deathProbable', 'hospitalized', 'hospitalizedCumulative', 'hospitalizedCurrently', 'hospitalizedIncrease', 'negative', 'negativeIncrease', 'negativeTestsViral', 'onVentilatorCumulative', 'onVentilatorCurrently', 'positiveCasesViral', 'totalTestEncountersViralIncrease', 'positiveScore', 'totalTestsAntibody', 'totalTestEncountersViral', 'totalTestsAntigen', 'positiveTestsAntigen', 'negativeTestsAntibody', 'inIcuCurrently', 'positiveTestsPeopleAntigen', 'totalTestsPeopleAntigen', 'negativeTestsPeopleAntibody', 'positiveTestsAntibody', 'positiveTestsPeopleAntibody', 'inIcuCumulative'], axis=1) ne = ne_clean.sort_values('date') maxdate = ne['date'].max() mindate = ne['date'].min() # print(maxdate) # print(mindate) numdays = (pd.to_datetime(maxdate) - pd.to_datetime(mindate)).days # print(numdays) ne.head(-5) posinc = ne['positiveIncrease'] bl = benfordslaw(alpha=0.05) df = bl.fit(posinc) bl.plot('New Cases Analysis over '+str(numdays)+' days') # Bradfordlaw's own plot function. totaldeaths = ne['death'] bl2 = benfordslaw(alpha=0.05) df2 = bl2.fit(totaldeaths) bl2.plot('New Deaths Analysis over '+str(numdays)+' days')
And, here is the longer version of that code, which I run in Jupyter notebook. The asterisks separate the cells:
Code:
%matplotlib inline from matplotlib import pyplot as plt import numpy as np import pandas as pd from benfordslaw import benfordslaw ******************* nehist = pd.read_csv("https://covidtracking.com/data/download/nebraska-history.csv") ne_clean = nehist.drop(['state', 'deathConfirmed', 'deathProbable', 'hospitalized', 'hospitalizedCumulative', 'hospitalizedCurrently', 'hospitalizedIncrease', 'negative', 'negativeIncrease', 'negativeTestsViral', 'onVentilatorCumulative', 'onVentilatorCurrently', 'positiveCasesViral', 'totalTestEncountersViralIncrease', 'positiveScore', 'totalTestsAntibody', 'totalTestEncountersViral', 'totalTestsAntigen', 'positiveTestsAntigen', 'negativeTestsAntibody', 'inIcuCurrently', 'positiveTestsPeopleAntigen', 'totalTestsPeopleAntigen', 'negativeTestsPeopleAntibody', 'positiveTestsAntibody', 'positiveTestsPeopleAntibody', 'inIcuCumulative'], axis=1) ******************** ne = ne_clean.sort_values('date') maxdate = ne['date'].max() mindate = ne['date'].min() # print(maxdate) # print(mindate) numdays = (pd.to_datetime(maxdate) - pd.to_datetime(mindate)).days # print(numdays) ne.head(-5) ************************ plt.close('all') plt.figure(figsize=(30, 12)) plt.title("Nebraska Daily New Deaths vs Daily New Cases from " + mindate + " to " + maxdate + " for " + str(numdays) + " days") plt.xlabel = "Date" plt.ylabel = "New Cases v New Deaths" plt.xticks(rotation='vertical') plt.plot(ne['date'], ne["deathIncrease"], label='Daily New Deaths', marker='.') plt.plot(ne['date'], ne['positiveIncrease'], label='Daily New Cases', marker=".") plt.tight_layout() plt.gca().xaxis.set_major_locator(plt.MultipleLocator(2)) plt.gcf() plt.legend() plt.grid() plt.savefig("dailynewcases_v_newdeaths_" + maxdate + ".svg", format='svg') plt.draw() plt.show() ******************************** posinc = ne['positiveIncrease'] bl = benfordslaw() df = bl.fit(posinc) bl.plot() ******************************** benlaw = np.array([30.1, 17.6, 12.5, 9.7, 7.9, 6.7, 5.8, 5.1, 4.6]) fig = plt.figure(figsize=(20, 16)) ax = fig.add_subplot(111) ax.text(3, 24, '[chi2] Anomaly detected! P=0.00417, Tstat=22.435', fontsize=18, color='red') plt.xticks(np.arange(len(benlaw)), np.arange(1, len(benlaw) + 1)) ax.set_title("NE Daily New Cases Increases vs Benford's Law over the last " + str(numdays) + " days", fontsize=16, fontweight='bold') ax.set_xlabel('First Digit', fontsize=14) ax.set_ylabel('Percentage', fontsize=14) ax.plot(df['percentage_emp'][:, 1], label="Daily New Cases") ax.plot(benlaw, label="Benford's Law") plt.legend() plt.grid() plt.savefig("BL_v_NE_DailyNewCaseIncrease" + maxdate + ".svg", format='svg') plt.draw() plt.show() *********************************** plt.figure(figsize=(11, 8)) digits = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) plt.bar(digits, df['percentage_emp'][:, 1], width=0.4, label="Daily New Cases") plt.bar(digits, benlaw, width=0.2, label="Benford's Law") plt.xlabel = "First Digit" plt.ylabel = "Percentage" plt.savefig("BL_v_NE_Bar_DailyNewCaseIncreases" + maxdate + ".svg", format='svg') plt.xticks(digits) plt.title("Bar Graph of Benford Law analysis of Daily New Cases - Anomoly") plt.legend() plt.show() ********************************* totaldeaths = ne["death"] bl2 = benfordslaw() df2 = bl2.fit(totaldeaths) bl2.plot() ************************************* plt.figure(figsize=(11, 8)) plt.bar(digits, df['percentage_emp'][:, 1], width=0.4, label="Total Deaths") plt.bar(digits, benlaw, width=0.2, label="Benford's Law") plt.xlabel = "First Digit" plt.ylabel = "Percentage" plt.savefig("Bar_BL_v_NE_Bar_TotalDeaths" + maxdate + ".svg", format='svg') plt.xticks(digits) plt.title("Bar Graph of Benford Law analysis of Total Deaths - No Anomoly") plt.legend() plt.show()
Comment