-
Notifications
You must be signed in to change notification settings - Fork 0
/
forcasting_minmax_temp_using_arima.py
142 lines (102 loc) · 3.7 KB
/
forcasting_minmax_temp_using_arima.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
# -*- coding: utf-8 -*-
"""Forcasting_minmax_temp_using_ARIMA.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1GnMBc1Pv5vUW53TfPoyx6KuZURDIMVNn
#Time Series Analysis and Temperature Forcast
## Mounting Google Drive
"""
from google.colab import drive
drive.mount('/content/drive')
"""## Importing Required Libraries
"""
# Commented out IPython magic to ensure Python compatibility.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from statsmodels.tsa.arima_model import ARIMA
from sklearn.metrics import mean_squared_error, mean_absolute_error
# %matplotlib inline
import warnings
warnings.filterwarnings('ignore')
"""## Reading Data"""
df = pd.read_csv('/content/drive/MyDrive/Timeseries Forcast/timeseriesdatadate.csv')
df.head()
df.dtypes
df.shape
"""### Renamed Columns"""
df.rename(columns ={'Date':'date','Max':'temp_max','Min':'temp_min'},inplace=True)
df.tail()
"""### Plotting Data of Maximum and Minimum Temperature """
df.plot(subplots=True)
plt.show()
"""### Splitting the dataset into Testing and Training Data"""
train_temp_max = list(df[0:1800]['temp_max'])
test_temp_max = list(df[1800:]['temp_max'])
train_temp_min = list(df[0:1800]['temp_min'])
test_temp_min = list(df[1800:]['temp_min'])
"""### Plotting the training and testing data"""
plt.figure(figsize=(12,6))
plt.grid(True)
plt.xlabel('Dates')
plt.ylabel('temp_max')
plt.plot(df[0:1800]['temp_max'],'blue',label='Train Data')
plt.plot(df[1800:]['temp_max'],'red',label='Test Data')
plt.legend()
model_prediction_max = []
n_test_obser_tmax = len(test_temp_max)
plt.figure(figsize=(12,6))
plt.grid(True)
plt.xlabel('Dates')
plt.ylabel('temp_min')
plt.plot(df[0:1800]['temp_min'],'green',label='Train Data')
plt.plot(df[1800:]['temp_min'],'orange',label='Test Data')
plt.legend()
model_prediction_min = []
n_test_obser_tmin = len(test_temp_min)
"""## Fitting the Model"""
for i in range(n_test_obser_tmax):
modelmax = ARIMA(train_temp_max,order =(4,1,0))
model_max_fit =modelmax.fit()
output_max = model_max_fit.forecast()
yhat_max = list(output_max[0])[0]
model_prediction_max.append(yhat_max)
actual_test_value_max = test_temp_max[i]
train_temp_max.append(actual_test_value_max)
for i in range(n_test_obser_tmin):
modelmin = ARIMA(train_temp_min,order =(4,1,0))
model_min_fit =modelmin.fit()
output_min = model_min_fit.forecast()
yhat_min = list(output_min[0])[0]
model_prediction_min.append(yhat_min)
actual_test_value_min = test_temp_min[i]
train_temp_min.append(actual_test_value_min)
"""### Model Summary"""
print(model_max_fit.summary2())
print(model_min_fit.summary2())
"""## Plotting the Prediction"""
plt.figure(figsize=(20,9))
plt.grid(True)
date_range = df[1800:].index
plt.plot(date_range, model_prediction_max[:],'blue',marker= 'o',linestyle = 'dashed',label='Predicted Temp_Max')
plt.plot(date_range, test_temp_max, 'red', label='Actual Temp_Max')
plt.title('Maximum Temperature Prediction')
plt.xlabel('Dates')
plt.ylabel('Temp_max')
plt.legend()
plt.show()
plt.figure(figsize=(20,9))
plt.grid(True)
date_range = df[1800:].index
plt.plot(date_range, model_prediction_min[:],'blue',marker= 'o',linestyle = 'dashed',label='Predicted Temp_Min')
plt.plot(date_range, test_temp_min, 'orange', label='Actual Temp_Min')
plt.title('Minimum Temperature Prediction')
plt.xlabel('Dates')
plt.ylabel('Temp_min')
plt.legend()
plt.show()
"""## Mean Absolute Percentage Error (MAPE)"""
mape = np.mean(np.abs(np.array(model_prediction_max[:]) - np.array(test_temp_max))/np.abs(test_temp_max))*100
print('MAPE :'+str(mape))
mape = np.mean(np.abs(np.array(model_prediction_min[:]) - np.array(test_temp_min))/np.abs(test_temp_min))*100
print('MAPE :'+str(mape))