-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharima.py
More file actions
181 lines (116 loc) · 4.63 KB
/
Copy patharima.py
File metadata and controls
181 lines (116 loc) · 4.63 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import pandas as pd
import numpy as np
def arima(co_df,pm_df):
df = pd.read_csv("./content/station_day.csv")
df1 = df.dropna()
lstm_df = pd.read_excel("./content/cancer patient data sets.xlsx")
def remove_outliers(df1, column_name):
Q1 = df1["AQI"].quantile(0.25)
Q3 = df1["AQI"].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
return df1[(df1["AQI"] >= lower_bound) & (df1["AQI"] <= upper_bound)]
df1 = remove_outliers(df1, "AQI")
arima_df = df[["Date", "AQI"]]
arima_df["Date"] = pd.to_datetime(arima_df["Date"])
arima_df.AQI = arima_df.groupby(pd.PeriodIndex(arima_df["Date"], freq="M"))[
"AQI"
].apply(lambda x: x.fillna(x.mean()))
ts = arima_df.groupby(pd.PeriodIndex(arima_df["Date"], freq="M"))["AQI"].mean()
import statsmodels.api as sm
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.tsa.stattools import adfuller
from numpy import log
result = adfuller(ts)
print("ADF Statistic: %f" % result[0])
print("p-value: %f" % result[1])
ts_train = ts[:50]
ts_test = ts[50:]
from statsmodels.tsa.arima.model import ARIMA
import itertools
from sklearn.metrics import mean_squared_error
p = range(0, 8)
q = range(0, 8)
d = range(0, 2)
pqd_combination = list(itertools.product(p, d, q))
error = []
pqd = []
print(pqd_combination)
# exit()
for i in pqd_combination:
A_model = ARIMA(ts_train, order=i).fit()
predict = A_model.predict(len(ts_train), len(ts) - 1)
e = np.sqrt(mean_squared_error(ts_test, predict))
pqd.append(i)
error.append(e)
min = error[0]
index = 0
for i in range(1, len(error) - 1):
if min > error[i]:
min = error[i]
index = i
print(error[index], " => ", pqd[index])
model_ts = ARIMA(ts_train, order=pqd[index])
model_ts_fit = model_ts.fit()
print(model_ts_fit.summary())
arima_predict = model_ts_fit.predict(start=len(ts_train), end=len(ts))
import matplotlib.pyplot as plt
residuals = model_ts_fit.resid[1:]
# fig, ax = plt.subplots(1, 2)
# residuals.plot(title="Residuals", ax=ax[0])
# residuals.plot(title="Density", kind="kde", ax=ax[1])
# plt.show()
from sklearn.metrics import (
mean_absolute_error,
mean_absolute_percentage_error,
mean_squared_error,
)
forecast_test = model_ts_fit.forecast(len(ts_test))
rmse = np.sqrt(mean_squared_error(ts_test, forecast_test))
mae = mean_absolute_error(ts_test, forecast_test)
mape = mean_absolute_percentage_error(ts_test, forecast_test)
print(f"mae -: {mae}")
print(f"mape -: {mape}")
print(f"rmse -: {rmse}")
# plt.figure(figsize=(20, 10))
# ts_test.plot(label="Test")
# ts_train.plot(label="Train")
# predict.plot(label="Predict")
# plt.legend()
# plt.show()
# Assume avg_aqi is the average AQI for a month
avg_aqi = 150
# Convert the average AQI to a pandas Series
new_data = pd.Series([avg_aqi])
# Use the trained model to make a forecast for the next month
forecast_next_month = model_ts_fit.forecast(steps=1, exog=new_data)
## The forecast for the next month's AQI is the first element of the forecast
next_month_aqi = forecast_next_month[0]
# Convert the forecast for the next month to a pandas Series
next_month_aqi_series = pd.Series(
[next_month_aqi], index=[ts.index[-1] + pd.offsets.MonthEnd(1)]
)
# Append the forecast for the next month to the original time series
ts_extended = pd.concat([ts, next_month_aqi_series])
# Fit the ARIMA model to the extended time series
model_ts_extended = ARIMA(ts_extended, order=pqd[index])
model_ts_extended_fit = model_ts_extended.fit()
# Use the trained model to make a forecast for the next 12 months
forecast_next_year = model_ts_extended_fit.forecast(steps=12)
# Create a date range for the next 12 months
next_12_months = pd.date_range(
start=ts_extended.index[-1].to_timestamp() + pd.offsets.MonthEnd(1),
periods=12,
freq="M",
)
print([i for i in forecast_next_year])
# Convert the forecasts to a pandas Series with the date range as the index
forecast_series = pd.Series([i for i in forecast_next_year], index=next_12_months)
# Plot the original time series and the forecasts
# plt.figure(figsize=(20, 10))
# ts_extended.plot(label="Original")
# forecast_series.plot(label="Forecast")
# plt.legend()
# plt.show()
return forecast_series