VIDEO
print('抓covid-19資料畫4月本土病例日線與回歸分析')
url='https://od.cdc.gov.tw/eic/Day_Confirmation_Age_County_Gender_19CoV.json'
匯入套件
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
pandas讀資料
df=pd.read_json(url)
df2022April=df[('2022/04/22'>=df['個案研判日']) & (df['個案研判日']>='2022/04/') & (df['是否為境外移入']=='否')]
完整列印
with pd.option_context('display.max_rows', None, 'display.max_columns', None):
print(df2022April)
篩去不用欄位
df2=df2022April.drop(columns=['確定病名','縣市', '性別',\
'是否為境外移入','年齡層'])
array=df2.to_numpy()
整理資料用dict存
dic={}
n=0
for x in array:
z=x[0].split('/')[2]
key=int(z)
value=x[2]#因有加key '鄉鎮'
n+=value
if key not in dic:
dic[key]=value
else:
dic[key]+=value
print('2022/04本土病例=',n)
print(dic)
迴歸分析
data=np.array(list(dic.items()))
coef=np.polyfit(data[:,0], data[:,1], 5)
reg=np.poly1d(coef)
print(reg)
畫統計圖
plt.rcParams['font.family'] = ['Microsoft JhengHei']
plt.scatter(data[:,0], data[:,1])
plt.plot(data[:,0],reg(data[:,0]), color='r')
plt.title('這是covid-19最近日線')
plt.savefig('covid-19-April-days.jpg')
plt.show()
沒有留言:
張貼留言