-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoronavirus Graph.py
160 lines (79 loc) · 2.65 KB
/
Coronavirus Graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
# coding: utf-8
# In[73]:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from sklearn.preprocessing import scale
from scipy.cluster.hierarchy import dendrogram,linkage
from sklearn.cluster import AgglomerativeClustering
from datetime import date
get_ipython().run_line_magic('matplotlib', 'inline')
# In[3]:
df = pd.read_csv('D:\\covid-19-data-master\\us-states.csv')
# In[4]:
df['date'] = pd.to_datetime(df['date'])
df['month'] = df['date'].map(lambda x: x.strftime('%B'))
# In[5]:
reference_dic = dict()
state_codes = (df.loc[: , ('state', 'fips')]).copy()
monthly_cases = dict()
for i, j, k in state_codes.itertuples():
reference_dic[k] = j
monthly_cases['Jan/f' + str(k)] = []
monthly_cases['Feb/f' + str(k)] = []
# In[6]:
new_df = df.reindex(columns=['fips', 'month', 'cases'])
# In[7]:
for i, j, k, m in new_df.itertuples():
if k == 'January':
monthly_cases['Jan/f' + str(j)].append(m)
elif k == 'February':
monthly_cases['Feb/f' + str(j)].append(m)
# In[8]:
df2 = pd.DataFrame(columns=['states', 'covid19_rate', 'unempl_rate'])
# In[9]:
avg_covid19_rate = dict()
for i, j in monthly_cases.items():
if i[:3] == 'Jan' and j == []:
avg_covid19_rate[i] = 0
elif i[:3] == 'Feb' and j == []:
avg_covid19_rate[i] = 0
elif i[:3] == 'Jan' and j != []:
rate = pd.Series(j).pct_change().mean()
avg_covid19_rate[i] = rate
elif i[:3] == 'Feb' and j != []:
rate = pd.Series(j).pct_change().mean()
avg_covid19_rate[i] = rate
# In[10]:
for i, j in avg_covid19_rate.items():
if i[:3] == 'Jan':
rate = j - avg_covid19_rate['Feb/' + i[4:]]
df2 = df2.append({'states': reference_dic[int(i[5:])], 'covid19_rate': rate}, ignore_index=True)
# In[11]:
df2 = df2.set_index('states')
# In[12]:
df3 = pd.read_html('https://www.bls.gov/web/laus/laumstcm.htm#laumstcm.f.p')
# In[13]:
df3 = df3[0].drop([51,52, 53])
# In[14]:
for i, j, k, l, m, n in df3.itertuples():
if j in df2.index:
df2.loc[j, 'unempl_rate'] = m
# In[15]:
df2.dropna(inplace=True)
# In[71]:
X = (scale(df2))
# In[74]:
dendrogram = dendrogram(linkage(X, method='complete'))
# In[57]:
model = AgglomerativeClustering(n_clusters=2, affinity='euclidean', linkage='complete')
model.fit(X)
labels = model.labels_
# In[58]:
labels
# In[76]:
plt.title('COVID-19 rate Vs. Unemployment rate within the U.S\n(for the month of Jan & Feb)')
plt.scatter(X[labels==0, 0], X[labels==0, 1], s=50, marker='+', color='red')
plt.scatter(X[labels==1, 0], X[labels==1, 1], s=50, marker='+', color='blue')
# In[ ]: