Commit c7526068 authored by wenwen.tang's avatar wenwen.tang 😕

增加预测模型,增加8档预测

parent ed794b2e
...@@ -11,24 +11,23 @@ from ai.training_data_builder import TrainingDataBuilder ...@@ -11,24 +11,23 @@ from ai.training_data_builder import TrainingDataBuilder
from api import DataSync from api import DataSync
# 截止日期 # 截止日期
# max_date = None max_date = None
max_date = '2024-03-01' # max_date = '2024-03-20'
# max_date = '2024-01-11' # max_date = '2024-01-11'
toForecast = True # False means test, True means forecast toForecast = True # False means test, True means forecast
syncData = True # 开启会同步数据库指数及基金数据 syncData = True # 开启会同步数据库指数及基金数据
uploadData = False # 开启会上传预测结果 uploadData = True # 开启会上传预测结果
doReport = True # 开启会生成Excel报告 doReport = True # 开启会生成Excel报告
# 待预测指数 # 待预测指数
# PREDICT_LIST = [67, 121, 122, 123] # PREDICT_LIST = [67, 121, 122, 123]
PREDICT_LIST = [67, 121, 122, 123, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, PREDICT_LIST = [67, 121, 122, 123, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
174, 175, 177, 178] 164, 165, 166, 167, 168, 169, 170, 171, 174, 175, 177, 178]
eco = [65, 66, 74, 134] eco = [65, 66, 74, 134]
index = [67, 68, 69, 70, 71, 72, 73, 75, 76, 77, 105, 106, 116, 117, 138, 139, 142, 143, 140, 141, 144, 145, 146] index = [67, 68, 69, 70, 71, 72, 73, 75, 76, 77, 105, 106, 116, 117, 138, 139, 142, 143, 140, 141, 144, 145, 146]
fund = [121, 122, 123, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 174, 175, fund = [121, 122, 123, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165,
177, 166, 167, 168, 169, 170, 171, 174, 175, 177, 178]
178]
@autowired @autowired
...@@ -130,10 +129,15 @@ if __name__ == '__main__': ...@@ -130,10 +129,15 @@ if __name__ == '__main__':
rf_model = trainer.train_random_forest(X_train, y_train, X_test, y_test) rf_model = trainer.train_random_forest(X_train, y_train, X_test, y_test)
gbt_model = trainer.train_GBT(X_train, y_train, X_test, y_test) gbt_model = trainer.train_GBT(X_train, y_train, X_test, y_test)
svc_model = trainer.train_SVC(X_train, y_train, X_test, y_test) svc_model = trainer.train_SVC(X_train, y_train, X_test, y_test)
ensemble_model = trainer.ensemble_model(rf_model, gbt_model, svc_model, X_train, y_train, X_test, y_test) knn_model = trainer.train_nearest_neighbors(X_train, y_train, X_test, y_test)
ada_model = trainer.train_AdaBoost(X_train, y_train, X_test, y_test)
ensemble_model = trainer.ensemble_model(rf_model, gbt_model, svc_model,
knn_model, ada_model, X_train, y_train, X_test, y_test)
model_predict = {'forest': rf_model.predict(scaledX_forecast), model_predict = {'forest': rf_model.predict(scaledX_forecast),
'gbt': gbt_model.predict(scaledX_forecast), 'gbt': gbt_model.predict(scaledX_forecast),
'svc': svc_model.predict(scaledX_forecast), 'svc': svc_model.predict(scaledX_forecast),
'knn': knn_model.predict(scaledX_forecast),
'adaboost': ada_model.predict(scaledX_forecast),
'ensemble': ensemble_model.predict(scaledX_forecast)} 'ensemble': ensemble_model.predict(scaledX_forecast)}
print(f'预测结果:{model_predict}'.center(60, '+')) print(f'预测结果:{model_predict}'.center(60, '+'))
judgement(pid, infos_type[pid], model_predict) judgement(pid, infos_type[pid], model_predict)
......
...@@ -3,8 +3,10 @@ from abc import ABC ...@@ -3,8 +3,10 @@ from abc import ABC
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from lightgbm import LGBMClassifier from lightgbm import LGBMClassifier
from sklearn import svm from sklearn import svm
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import RandomForestClassifier, VotingClassifier from sklearn.ensemble import RandomForestClassifier, VotingClassifier
from sklearn.metrics import classification_report, confusion_matrix, ConfusionMatrixDisplay, accuracy_score from sklearn.metrics import classification_report, confusion_matrix, ConfusionMatrixDisplay, accuracy_score
from sklearn.neighbors import KNeighborsClassifier
class ModelTrainer(ABC): class ModelTrainer(ABC):
...@@ -61,9 +63,25 @@ class ModelTrainer(ABC): ...@@ -61,9 +63,25 @@ class ModelTrainer(ABC):
self.test_model('Support Vector Machines', classifierSVC, X_test, y_test) self.test_model('Support Vector Machines', classifierSVC, X_test, y_test)
return classifierSVC return classifierSVC
def ensemble_model(self, rf_model, gbt_model, svc_model, X_train, y_train, X_test, y_test): def train_nearest_neighbors(self, X_train, y_train, X_test, y_test):
classifier = KNeighborsClassifier()
classifier.fit(X_train, y_train)
if not self._toForecast:
self.test_model('K-Nearest Neighbors', classifier, X_test, y_test)
return classifier
def train_AdaBoost(self, X_train, y_train, X_test, y_test):
classifier = AdaBoostClassifier()
classifier.fit(X_train, y_train)
if not self._toForecast:
self.test_model('AdaBoost', classifier, X_test, y_test)
return classifier
def ensemble_model(self, rf_model, gbt_model, svc_model, knn_model,
ada_model, X_train, y_train, X_test, y_test):
# Create a dictionary of our models # Create a dictionary of our models
estimators = [('rf', rf_model), ('gbt', gbt_model), ('svc', svc_model)] estimators = [('rf', rf_model), ('gbt', gbt_model), ('svc', svc_model),
('knn', knn_model), ('AdaBoost', ada_model)]
# Create our voting classifier, inputting our models # Create our voting classifier, inputting our models
ensemble = VotingClassifier(estimators, voting='hard') ensemble = VotingClassifier(estimators, voting='hard')
# fit model to training data # fit model to training data
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment