-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweatherpred.py
53 lines (43 loc) · 1.55 KB
/
weatherpred.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
import pandas as pd
from statsmodels.tsa.statespace.sarimax import SARIMAX
import matplotlib.pyplot as plt
import pickle
# Load the data
df = pd.read_csv('/Users/moizibrar/Downloads/weatherAUS.csv')
# Filter data for Melbourne
melb = df[df['Location'] == 'Melbourne'].copy()
melb['Date'] = pd.to_datetime(melb['Date'])
# Plot initial data
plt.figure(figsize=(10, 5))
plt.plot(melb['Date'], melb['Temp3pm'])
plt.title('Temperature at 3pm in Melbourne')
plt.xlabel('Date')
plt.ylabel('Temp3pm')
plt.show()
# Filter by year and plot
melb = melb[melb['Date'].dt.year <= 2015].copy()
plt.figure(figsize=(10, 5))
plt.plot(melb['Date'], melb['Temp3pm'])
plt.title('Temperature at 3pm in Melbourne (Up to 2015)')
plt.xlabel('Date')
plt.ylabel('Temp3pm')
plt.show()
# Prepare data for SARIMA
data = melb[['Date', 'Temp3pm']].dropna().copy()
data.columns = ['ds', 'y'] # Ensure columns are named 'ds' and 'y'
data.set_index('ds', inplace=True)
# Ensure the data index has a daily frequency
data = data.asfreq('D')
# Define SARIMA model parameters
order = (1, 1, 1)
seasonal_order = (1, 1, 1, 12) # yearly seasonality (12 months)
# Fit the SARIMA model
model = SARIMAX(data['y'], order=order, seasonal_order=seasonal_order, enforce_stationarity=False, enforce_invertibility=False)
results = model.fit()
# Save the model parameters using pickle
try:
with open('/Users/moizibrar/Downloads/final/Weather_Predictions.pkl', 'wb') as f:
pickle.dump(results, f)
print("Model parameters saved successfully.")
except Exception as e:
print(f"Error saving model parameters: {e}")