Browse Source

预测代码

罗俊辉 1 year ago
parent
commit
eaee498d3a
1 changed files with 6 additions and 2 deletions
  1. 6 2
      main.py

+ 6 - 2
main.py

@@ -122,6 +122,7 @@ class LightGBM(object):
         评估模型性能
         :return:
         """
+        fw = open("summary.txt", "a+", encoding="utf-8")
         # 测试数据
         with open("whole_data/x_data_total_return_prid.json") as f1:
             x_list = json.loads(f1.read())
@@ -138,21 +139,24 @@ class LightGBM(object):
             X_test[key] = pd.to_numeric(X_test[key], errors='coerce')
         bst = lgb.Booster(model_file=self.model)
         y_pred = bst.predict(X_test, num_iteration=bst.best_iteration)
+        y_pred_binary = [0 if i <= 0.1613 else 1 for i in list(y_pred)]
         # 转换为二进制输出
         score_list = []
         for index, item in enumerate(list(y_pred)):
             real_label = Y_test[index]
             score = item
-            print(real_label, "\t", score)
+            prid_label = y_pred_binary[index]
+            print(real_label, "\t", prid_label, "\t", score)
+            fw.write("{}\t{}\t{}\n".format(real_label, prid_label, score))
             score_list.append(score)
         print("预测样本总量: {}".format(len(score_list)))
         data_series = pd.Series(score_list)
         print("统计 score 信息")
         print(data_series.describe())
-        y_pred_binary = np.where(y_pred > 0.1613, 1, 0)
         # 评估模型
         accuracy = accuracy_score(Y_test, y_pred_binary)
         print(f'Accuracy: {accuracy}')
+        fw.close()
 
 
 if __name__ == '__main__':