-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhue06_mlp.py
85 lines (70 loc) · 2.91 KB
/
hue06_mlp.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
import pickle
import pandas as pd
import numpy as np
import StatsFunctions as stats
from tensorflow import keras
from sklearn.preprocessing import StandardScaler
class hue06_mlp():
"""
This class represents the best model/configuration from Loja (RIA station) - aridity index = 0.3162.
It uses a SVM model and the following input configuration (being rs the predicted value):
['tx', 'tn', 'ra', 'ea', 'et0']
"""
def __init__(self):
# import model
filename = "savedModels/MLP_Aroche.h5"
self.model = keras.models.load_model(filename)
# define required inputs
self.parameters = ['tx', 'tn', 'ra', 'ea', 'et0']
def import_dataset(self, fileLocation, fileType):
"""
This function import a dataset and convert it into a pandas Dataframe
Inputs:
fileLocation: "data/dataSet.csv"
fileType: string with csv, excel or txt
"""
if fileType == 'csv' or fileType == 'txt':
self.dfData = pd.read_csv(fileLocation)
elif fileType == 'excel':
self.dfData = pd.read_excel(fileLocation)
else:
AssertionError('this fileType does not exit, use excel, csv and txt instead')
# filter nan values
self.dfData.dropna(inplace=True)
self.dfData.reset_index(drop=True, inplace=True)
# filter the parameters
self.dfData = self.dfData.filter(self.parameters)
def getStandardDataTest(self):
"""
Split data to train and test
"""
# from training original dataset
mean_list = [23.20, 9.10, 28.87, 1.15]
std_list = [66.30, 32.39, 93.60, 0.18]
# we have the input data as x, and the output as y
self.x_test = np.array(self.dfData.iloc[:, :-1])
self.y_test = np.array(self.dfData.iloc[:, -1])
# standarization
scaler = StandardScaler()
scaler.mean_ = mean_list
scaler.scale_ = std_list
# x_train and x_test
self.x_test = np.array(scaler.transform(self.x_test))
return self.x_test, self.y_test
def predictValues(self):
self.y_pred = np.ravel(np.array(self.model.predict(self.x_test)))
return self.y_pred
def statAnalysis(self):
rmse = stats.get_root_mean_square_error(self.y_test, self.y_pred)
rrmse = stats.get_root_mean_square_error(self.y_test, self.y_pred) / np.mean(self.y_test)
mbe = stats.get_mean_bias_error(self.y_test, self.y_pred)
r2 = stats.get_coefficient_of_determination(self.y_test, self.y_pred)
nse = stats.get_nash_suteliffe_efficiency(self.y_test, self.y_pred)
return rmse, rrmse, mbe, r2, nse
if __name__ == '__main__':
mlModel = hue06_mlp()
mlModel.import_dataset("data/HUE06.csv", 'csv')
mlModel.getStandardDataTest()
mlModel.predictValues()
rmse, rrmse, mbe, r2, nse = mlModel.statAnalysis()
print(rmse, rrmse, mbe, r2, nse)