-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRegressionSV.py
199 lines (154 loc) · 7.75 KB
/
RegressionSV.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 27 16:44:10 2022
@author: Wesse
"""
from sklearn.model_selection import train_test_split
from General_functions import ML_database ,simple_database, make_IAST_database_Wessel_version
from sklearn.svm import SVR
import joblib
import time
from matplotlib import pyplot as plt
import numpy as np
from decimal import Decimal
"""inputs"""
training = False
testing = True
N_molecules = 2
Only_n = True
def Performance(name_model, amount_mols, rf_model, x_train, x_test, y_train, y_test):
"""
Performance function, to let it work properly, please let the inputs be of
the following format:
- name_model: enter the name of the model as a string, will be used in
titles of plots, for consistency hold the format like for example:
"Decision Tree" and "Neural Network"
- amount_mols: the amount of molecules in the mixture used to calculate the
loadings.
- rf_model: the variable where your model is stored, which is already being loaded in
so first load the joblib model, store it in a variable and put the variable here.
- x_train: the part of the data used to train the model
- x_test: the part of the data to be used to test, which is not the same as the
training data!
- y_train: the known output of the x_train data
- y_test: the known output of the y_train data
This function will make predictions of the x_test data, and times how long
it takes. Then it will calculate the absolute and relative error, the score
of the model which is a build-in function of sklearn, which is described as
the following:
######################################################################
Return the coefficient of determination R^2 of the prediction.
The coefficient R^2 is defined as (1 - u/v), where u is the residual
sum of squares ((y_true - y_pred) ** 2).sum() and v is the total
sum of squares ((y_true - y_true.mean()) ** 2).sum().
The best possible score is 1.0 and it can be negative (because the
model can be arbitrarily worse). A constant model that always
predicts the expected value of y, disregarding the input features,
would get a R^2 score of 0.0.
Parameters
----------
X : array-like of shape (n_samples, n_features)
Test samples. For some estimators this may be a
precomputed kernel matrix or a list of generic objects instead,
shape = (n_samples, n_samples_fitted),
where n_samples_fitted is the number of
samples used in the fitting for the estimator.
y : array-like of shape (n_samples,) or (n_samples, n_outputs)
True values for X.
######################################################################
Finally will this function give you the desired plots and will store them
as a pdf file.
"""
pred_time = 0
for i in range(1,10):
print(i)
start_time = time.time()
y_pred=rf_model.predict(x_test)
end_time = time.time()
pred_time_temp = end_time-start_time
pred_time = (pred_time + pred_time_temp)/i
print(pred_time)
abs_err = np.abs(y_pred-y_test)
rel_err = abs_err/y_test
nanIndex = np.isnan(rel_err)
rel_err = rel_err[~nanIndex] #to remove nan's
infIndex = np.isinf(rel_err)
rel_err = rel_err[~infIndex] #to remove zero's
abs_err = abs_err[~nanIndex] #to remove nan's
abs_err = abs_err[~infIndex] #to remove zero's
rel_err = rel_err.flatten()
abs_err = abs_err.flatten()
mean_rel_err = np.mean(rel_err)
mean_abs_err = np.mean(abs_err)
new_name = ""
for i in name_model.split(" "):
new_name += i
plt.figure()
plt.title(f"Relative error {name_model}, {amount_mols} molecules mixture\nMean relative error = {'%.0e' %Decimal(mean_rel_err)}")
plt.scatter(range(len(rel_err)), rel_err, s=4,label="Relative error point i")
plt.hlines(mean_rel_err, xmin = 0, xmax = len(rel_err), color="red", label="Mean relative error")
plt.yscale("log")
plt.xlabel("Index of datapoint in array")
plt.ylabel("Relative error of predicted point wrt to known point")
plt.legend()
plt.savefig(f"{new_name}_{amount_mols}molsmix_RelErrPlot")
plt.show()
plt.figure()
plt.title(f"Absolute error {name_model}, {amount_mols} molecules mixture\nMean absolute error = {'%.0e' %Decimal(mean_abs_err)}")
plt.scatter(range(len(abs_err)), abs_err, s=4, label="Absolute error point i")
plt.hlines(mean_abs_err, xmin = 0, xmax = len(abs_err), color="red", label="Mean absolute error")
plt.yscale("log")
plt.xlabel("Index of datapoint in array")
plt.ylabel("Absolute error of predicted point wrt to known point")
plt.legend()
plt.savefig(f"{new_name}_{amount_mols}molsmix_AbsErrPlot")
plt.show()
plt.figure()
plt.title(f"Performance {name_model}, {amount_mols} molecules mixture")
plt.scatter(y_test, y_pred, s=10)
plt.xlabel("calculated loading by IAST (mol/kg)")
plt.ylabel(f"Predicted loading {name_model} (mol/kg)")
plt.savefig(f"{new_name}_{amount_mols}molsmix_PlotCompPredTrue")
plt.show()
print(f"\nMean relative error = {mean_rel_err}")
print("Formula relative error: np.abs(y_pred-y_test)/y_test\n")
print(f"Mean absolute error = {mean_abs_err}")
print("Formula absolute error: np.abs(y_pred-y_test)\n")
print(f"Score model (based on test data) = {rf_model.score(x_test,y_test)}")
print(f"Score model (based on train data) = {rf_model.score(x_train,y_train)}\n")
print(f"Total time to predict {len(y_pred)} amount of molmixes (mixture of {amount_mols} mols): {'%.2e' %Decimal(pred_time)}")
print(f"Time to predict loading 1 molmix (mixture of {amount_mols} mols): {'%.2e' %Decimal((pred_time)/len(y_pred))}")
f = open(f"{new_name}_{amount_mols}molsmix_performance.txt","w+")
f.write(f"\nMean relative error = {mean_rel_err}\n")
f.write("Formula relative error: np.abs(y_pred-y_test)/y_test\n")
f.write(f"Mean absolute error = {mean_abs_err}\n")
f.write("Formula absolute error: np.abs(y_pred-y_test)\n")
f.write(f"Score model (based on test data) = {rf_model.score(x_test,y_test)}\n")
f.write(f"Score model (based on train data) = {rf_model.score(x_train,y_train)}\n")
f.write(f"Total time to predict {len(y_pred)} amount of molmixes (mixture of {amount_mols} mols): {'%.2e' %Decimal(pred_time)}\n")
f.write(f"Time to predict loading 1 molmix (mixture of {amount_mols} mols): {'%.2e' %Decimal((pred_time)/len(y_pred))}\n")
f.close()
return 0;
#making database of molecules representation
selfies_database = ML_database()
easy_database = simple_database()
#getting IAST combined with the molecule representaion
x_data, y_data = make_IAST_database_Wessel_version(easy_database,N_molecules, only_max_combinations= Only_n)
x_train, x_test, y_train, y_test = train_test_split(x_data,y_data,test_size= 0.3)
modelname = "SupportVectorModel"+ str(N_molecules) + "molecules"
if training:
print("Trainig the model")
model = SVR(kernel= "poly", tol=1e-6, epsilon = 1e-3)
t0 = time.perf_counter()
model.fit(x_train,y_train[:,0])
t1 = time.perf_counter()
print("time to train model= ", t1-t0 , "seconds" )
joblib.dump(model,modelname)
if testing:
model = joblib.load(modelname)
print("Model loaded!")
y_predict = model.predict(x_test)
plt.scatter(y_test[:,0],y_predict)
plt.xlabel("Iast-data")
plt.ylabel("SVR predictions")
Performance("Support Vector Regression" , 2, model , x_train, x_test, y_train[:,0], y_test[:,0])