-
Notifications
You must be signed in to change notification settings - Fork 0
/
Goolge Big Query Sandbox COVID Data_Forcast.py
84 lines (53 loc) · 1.84 KB
/
Goolge Big Query Sandbox COVID Data_Forcast.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
import os
import pandas as pd
from google.cloud import bigquery
from prophet import Prophet
from prophet.serialize import model_to_json, model_from_json
from prophet.plot import plot_plotly, plot_components_plotly
# Smust update the location of this path when downloading Key from the settings in big query
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = r"C:\Users\da046634\AppData\Local\Temp\MicrosoftEdgeDownloads\0edc4907-b6aa-48a7-af8c-4b7ced419855\vernal-tempo-410309-daa86de57214.json"
client = bigquery.Client()
query = """
WITH daily_counts AS (
SELECT
date,
IFNULL(positive - LAG(positive) OVER (ORDER BY date), positive) AS daily_positive
FROM
`bigquery-public-data.covid19_tracking.national_testing_and_outcomes`
WHERE
date > '2020-02-27'
ORDER BY
date ASC
)
SELECT
date,
daily_positive
FROM
daily_counts
WHERE
daily_positive IS NOT NULL;
"""
try:
query_job = client.query(query)
results = query_job.result()
df = results.to_dataframe()
df.rename(columns={'date': 'ds', 'daily_positive': 'y'}, inplace=True)
model = Prophet(holidays=pd.DataFrame({
'holiday': 'us_holidays',
'ds': pd.to_datetime([
'2020-01-01', '2020-07-04', '2020-12-25',
]),
'lower_window': 0,
'upper_window': 1,
}))
model.add_country_holidays(country_name='US')
model.fit(df)
future = model.make_future_dataframe(periods=30)
forecast = model.predict(future)
print(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']])
except Exception as e:
print(f"An error occurred: {e}")
fig1 = plot_plotly(model, forecast)
fig1.show()
fig2 = plot_components_plotly(model, forecast)
fig2.show()