1
1
import os , sys
2
2
import inspect , types
3
3
from datetime import timedelta , date , datetime
4
+ from array import *
4
5
5
6
import matplotlib .pyplot as plt
6
7
7
8
import sklearn .utils ._cython_blas
9
+ from sklearn import linear_model
8
10
from sklearn .linear_model import LinearRegression
11
+ from sklearn .preprocessing import PolynomialFeatures
12
+ from sklearn .pipeline import Pipeline
9
13
from sklearn .model_selection import train_test_split
10
14
import joblib
11
15
from pathlib import Path
@@ -106,12 +110,13 @@ def display_graph(self, listdata):
106
110
plt .show ()
107
111
108
112
109
- def training_linear_model (self , listdata ):
113
+ def training_linear_model (self , listdata , valueNeedPredict ):
110
114
date = [[x .OnDate ] for x in listdata ]
111
115
rate = [[x .RateValue ] for x in listdata ]
112
116
113
117
#Use 80% of data as training, rest 20% to Test model
114
118
x_train , x_test , y_train , y_test = train_test_split (date , rate , test_size = 0.2 )
119
+
115
120
# training model
116
121
linear = LinearRegression ()
117
122
linear .fit (x_train , y_train )
@@ -125,23 +130,67 @@ def training_linear_model(self, listdata):
125
130
joblib .dump (linear , modelPathDir )
126
131
127
132
# loading model
133
+ not valueNeedPredict and x_test or x_test .insert (0 , [valueNeedPredict ])
128
134
clf = joblib .load (modelPathDir )
129
135
predicted = clf .predict (x_test )#linear.predict(x_test)
136
+ print ("###Predicted by Linear Model" )
137
+ print ("Predicted Max:" , predicted .max ())
138
+ print ("Predicted Min:" , predicted .min ())
139
+ print ("Predicted: " , predicted )
140
+
141
+ return predicted
142
+
143
+
144
+ def training_polynomial_model (self , listdata , valueNeedPredict ):
145
+ date = [[x .OnDate ] for x in listdata ]
146
+ rate = [[x .RateValue ] for x in listdata ]
147
+
148
+ #Use 80% of data as training, rest 20% to Test model
149
+ x_train , x_test , y_train , y_test = train_test_split (date , rate , test_size = 0.2 )
150
+
151
+ # training model
152
+ poly = Pipeline ([('poly' , PolynomialFeatures (interaction_only = True , degree = 2 )),
153
+ ('linear' , linear_model .LinearRegression (fit_intercept = False ))])
154
+ poly .fit (x_train , y_train )
155
+
156
+ # evaluating model
157
+ score_trained = poly .score (x_test , y_test )
158
+ print ("Model scored:" , score_trained )
159
+
160
+ # saving model
161
+ modelPathDir = Path (ROOT_DIR + r'/model_trained/poly_model.pkl' )
162
+ joblib .dump (poly , modelPathDir )
163
+
164
+ # loading model
165
+ not valueNeedPredict and x_test or x_test .insert (0 , [valueNeedPredict ])
166
+ clf = joblib .load (modelPathDir )
167
+ predicted = clf .predict (x_test )#poly.predict(x_test)
168
+ print ("###Predicted by Polynomial Model" )
130
169
print ("Predicted Max:" , predicted .max ())
131
170
print ("Predicted Min:" , predicted .min ())
132
171
print ("Predicted: " , predicted )
133
172
134
173
return predicted
135
174
136
175
137
- def calculate_exrate_forSpecificDate (self , listdata , checkedDate ):
176
+ def calculate_exrate_bypurelinearmodel (self , listdata , checkedDate ):
138
177
date = [x .OnDate for x in listdata ]
139
178
rate = [x .RateValue for x in listdata ]
140
179
chkDate = datetime .timestamp (checkedDate )
141
180
142
181
predictedRate = self .util_logic .calLinearRegressionOfY (lstSampleValueX = date , lstSampleValueY = rate , xVal = chkDate )
143
182
144
183
return predictedRate
184
+
185
+
186
+ def calculate_exrate_bypurepolynomialmodel (self , listdata , checkedDate ):
187
+ date = [x .OnDate for x in listdata ]
188
+ rate = [x .RateValue for x in listdata ]
189
+ chkDate = datetime .timestamp (checkedDate )
190
+
191
+ predictedRate = self .util_logic .calPolynomialRegressionOfY (lstSampleValueX = date , lstSampleValueY = rate , xVal = chkDate )
192
+
193
+ return predictedRate
145
194
146
195
147
196
def predicted_quick_exrate_bytrainnedmodel (self , apiConfig , strPredictedDate , baseCurrency , toCurrency ):
@@ -154,7 +203,8 @@ def predicted_quick_exrate_bytrainnedmodel(self, apiConfig, strPredictedDate, ba
154
203
155
204
data = self .get_specific_exrate_byDateRange (apiConfig , dateLastMonth , dateLastYear , checkedDate , baseCurrency , toCurrency )
156
205
157
- self .training_linear_model (data )
206
+ self .training_linear_model (data , datetime .timestamp (predictedDate ))
207
+ self .training_polynomial_model (data , datetime .timestamp (predictedDate ))
158
208
159
209
return data
160
210
@@ -170,7 +220,8 @@ def predicted_long_exrate_bytrainnedmodel(self, apiConfig, strPredictedDate, bas
170
220
171
221
data = self .get_specific_exrate_byDateRange (apiConfig , dateLastMonth , dateLastYear , checkedDate , baseCurrency , toCurrency )
172
222
173
- self .training_linear_model (data )
223
+ self .training_linear_model (data , datetime .timestamp (predictedDate ))
224
+ self .training_polynomial_model (data , datetime .timestamp (predictedDate ))
174
225
175
226
return data
176
227
@@ -184,8 +235,10 @@ def predicted_basic_exrate(self, apiConfig, strPredictedDate, baseCurrency, toCu
184
235
185
236
data = self .get_specific_exrate_byDateRange (apiConfig , dateLastMonth , dateLastYear , checkedDate , baseCurrency , toCurrency )
186
237
187
- predictedRate = self .calculate_exrate_forSpecificDate (data , predictedDate )
188
- print ("Predicted: " , predictedRate )
238
+ predictedRate = self .calculate_exrate_bypurelinearmodel (data , predictedDate )
239
+ print ("Predicted with Linear Regression Model: " , predictedRate )
240
+ predictedRate2 = self .calculate_exrate_bypurepolynomialmodel (data , predictedDate )
241
+ print ("Predicted with Polynomial Regression Model: " , predictedRate2 )
189
242
190
243
return data
191
244
0 commit comments