Corona infection is widespread. If no measures are taken (or have no effect), the number of infected people generally increases along the exponential function.
The actual number of infections is announced daily on the Tokyo Corona Site, but no forecast has been made.
The exponential function curve_fit is used to calculate future forecast values and draw a graph here. (Of course, I don't want the result to be as good as possible, and I sincerely hope that the measures will be taken to converge as soon as possible.
Well, it's bad. Intuition that fits the exponential function quite well. The coefficient of determination will be calculated separately.
Data is from the official website. https://stopcovid19.metro.tokyo.lg.jp/
It seems that it is usually updated at night, but it is a prerequisite for graph drawing the next day. The number on Monday tends to be much smaller than the actual number because there are many places where the inspection agency is closed on the previous Sunday. It may be removed as a periodic factor, but here I will draw it as it is.
import pandas as pd
from pathlib import Path
from datetime import datetime
BASEDIR = Path('.')
FILE_PATH = 'https://stopcovid19.metro.tokyo.lg.jp/data/130001_tokyo_covid19_patients.csv'
df = pd.read_csv(str(FILE_PATH))
#Prediction interval
TO_PERIODS = 7
#Generates various things for graph drawing
target_columns = ['Published_date','patient_Age']
df_select = df[target_columns]
list_now = list(df_select.groupby('Published_date').count().index)
today = datetime.today()
today.strftime('%Y-%m-%d')
list_to = list(pd.date_range(today.strftime('%Y-%m-%d'), periods=TO_PERIODS, freq='D').strftime('%Y-%m-%d'))
list_total = list_now + list_to
Operate as an exponential function fit premise using curve_fit
from scipy.optimize import curve_fit
import numpy as np
import matplotlib.pyplot as plt
#Definition of approximate expression
def nonlinear_fit(x, a, b):
return a * x ** b
#Generate temporary x-axis data once
array_now_x = np.linspace(0, len(list_now)-1, len(list_now))
#Curve assuming an exponential function_fit run
param, cov = curve_fit(nonlinear_fit, array_now_x, array_now_y,maxfev=1000)
Calculate future date value with parameters obtained by exponential fit processing
list_total_y = []
for num, values in enumerate(list_total):
list_total_y.append(nonlinear_fit(num, param[0], param[1]))
#Calculate the number of infected people on a daily basis from published data
array_now_y = df_select.groupby('Published_date').count()['patient_Age'].values
Draw a graph.
import seaborn as sns
import matplotlib.pyplot as plt
sns.set()
sns.set_style('whitegrid')
fig, ax = plt.subplots(1, 1, figsize=(15, 10))
ax.bar(list_total, list_total_y, color='blue')
ax.bar(list_now, array_now_y, color='red',alpha=0.75)
plt.xticks(rotation=90)
plt.show()