-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml_models.py
164 lines (129 loc) · 6.26 KB
/
ml_models.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
157
158
159
160
161
162
163
164
import os
from pickle import load
import pandas as pd # Import pandas for data manipulation
import numpy as np # Import numpy for numerical operations
from sklearn.neural_network import MLPRegressor
from sklearn.preprocessing import MinMaxScaler # Import MinMaxScaler for data normalization
from statsmodels.tsa.arima.model import ARIMA
from tensorflow.keras.models import load_model
time_steps = 1
target_variables = ['CO2', 'Methane', 'Nitrous_Oxide', 'CFCs',
'Hydrochlorofluorocarbons', 'Hydrofluorocarbons',
'Total_Heat_Absorbed_GHG', 'Total_Greenhouse_Gases',
'GHG_Increase', '%_Change_GHG', 'Surface_Temperature', 'CO2_Concentration']
# Get the absolute path of the CSV file
data_path = os.path.abspath('clean_data.csv')
# Load the data from the CSV file
data = pd.read_csv(data_path)
# Normalize the data
scaler = MinMaxScaler()
data_scaled = scaler.fit_transform(data[target_variables])
class ModelNN:
def __init__(self, model, data, scaler, is_tf_model=True):
self.model = model
self.data = data
self.scaler = scaler
self.is_tf_model = is_tf_model
def predict(self, input_year):
predicted_val = []
if input_year > 2021:
# Prepare the input data
input_data = self.data[self.data['Year'] <= 2021]
input_data_scaled = self.scaler.transform(input_data[target_variables])
# Prepare the input sequence
input_sequences = []
for i in range(len(input_data_scaled) - time_steps, len(input_data_scaled)):
input_sequences.append(input_data_scaled[i - time_steps:i])
for year in range(2022, input_year + 1):
input_sequences_array = np.array([sequence for sequence in input_sequences if len(sequence) == time_steps])
input_sequence_reshaped = np.reshape(input_sequences_array, (input_sequences_array.shape[0], input_sequences_array.shape[1], len(target_variables)))
# Make predictions for the input sequence
if self.is_tf_model:
predictions = self.model.predict(input_sequence_reshaped[-1].reshape(1, time_steps, len(target_variables)))
else:
predictions = self.model.predict(input_sequence_reshaped[-1].reshape(1, time_steps, len(target_variables)).reshape(1, 12))
# Inverse transform the predicted values
prediction = self.scaler.inverse_transform(predictions)
# Get the predicted values for the current year
predicted_values = prediction[0]
# Append the predicted values to the results
predicted_val.append(predicted_values)
# Update the input sequence for the next iteration
input_sequences[0][:-1] = input_sequences[0][1:] # Remove the first value
input_sequences[0][-1] = np.array(predictions) # Add predictions at the last index
else:
input_data = self.data[self.data['Year'] < input_year]
input_data_scaled = self.scaler.transform(input_data[target_variables])
# Prepare the input sequence
input_sequence = []
for i in range(len(input_data_scaled) - time_steps, len(input_data_scaled)):
input_sequence.append(input_data_scaled[i - time_steps:i])
input_sequence = np.array(input_sequence)
# Reshape the input sequence for LSTM (input_shape: [samples, time_steps, features])
if self.is_tf_model:
input_sequence = np.reshape(input_sequence, (input_sequence.shape[0], input_sequence.shape[1], len(target_variables)))
else:
input_sequence = input_sequence.reshape(1, 12)
# Make predictions for the input sequence
predictions = self.model.predict(input_sequence)
# Inverse transform the predicted values
predicted_val = self.scaler.inverse_transform(predictions)
return predicted_val[-1]
class ModelLSRM:
def __init__(self, coeff: np.ndarray, degree: int = 3):
self.coeff = coeff
self.degree = degree
def predict(self, year: int):
all_predictions = []
for c in range(12):
prediction = 0
coeff = self.coeff[c]
for index2 in range(self.degree+1):
prediction+=coeff[len(coeff)-index2-1]*float(year)**index2
all_predictions.append(prediction)
return all_predictions
class ModelARIMA:
def __init__(self, data, start_year=1979, train_year=2012):
self.data = data
self.start_year = start_year
self.train_year = train_year
def predict(self, end_year):
train_data = self.data.loc[self.data['Year']<self.train_year]
predictions = []
start = self.start_year - 1979
end = end_year - 1979
arima_order_dict = {
"CO2": (4, 1, 2),
"Methane": (0, 2, 2),
"Nitrous_Oxide": (6, 1, 2),
"CFCs": (10, 1, 0),
"Hydrochlorofluorocarbons": (1, 1, 1),
"Hydrofluorocarbons": (4, 2, 1),
"Total_Heat_Absorbed_GHG": (1, 1, 1), #(10, 0, 1),
"Total_Greenhouse_Gases": (1, 1, 1), #(4, 0, 1),
"GHG_Increase": (1, 1, 1),#(6, 0, 1),
"%_Change_GHG": (8, 0, 1),
"Surface_Temperature": (1, 1, 0),
"CO2_Concentration": (1, 1, 1)
}
for target in target_variables:
model = ARIMA(train_data[target], order=arima_order_dict[target])
model_fit = model.fit()
y_predict = model_fit.predict(strat=start, end=end)[end]
predictions.append(y_predict)
return predictions
# Load the trained models
def create_lstm():
model = load_model('Lstm.h5') # Load the ANN model
return ModelNN(model, data, scaler)
def create_ann():
model = load_model('ann_model.h5') # Load the ANN model
return ModelNN(model, data, scaler)
def create_mlp_regressor():
model = load(open('mlp_regressor.pickle', "rb"))
return ModelNN(model, data, scaler, is_tf_model=False)
def create_lsrm():
coeff = load(open('coefficients.pkl', 'rb'))
return ModelLSRM(coeff)
def create_arima():
return ModelARIMA(data)