Jelajahi Sumber

add: coef.dict

yq 1 bulan lalu
induk
melakukan
eca33dc807
1 mengubah file dengan 11 tambahan dan 0 penghapusan
  1. 11 0
      model/model_lr.py

+ 11 - 0
model/model_lr.py

@@ -4,6 +4,7 @@
 @time: 2024/11/1
 @desc: 
 """
+import json
 import os.path
 import pickle
 from os.path import dirname, realpath
@@ -30,6 +31,7 @@ class ModelLr(ModelBase):
         self._template_path = os.path.join(dirname(dirname(realpath(__file__))), "./template/模型开发报告模板_lr.docx")
         self.lr = None
         self.card = None
+        self.coef = None
 
     def get_report_template_path(self):
         return self._template_path
@@ -50,6 +52,8 @@ class ModelLr(ModelBase):
             if len(self.lr.coef_[0]) != len(data_x.columns):
                 raise GeneralException(ResultCodesEnum.SYSTEM_ERROR, message=f"lr模型coef系数长度与x_columns长度不一致。")
         self.card = sc.scorecard(woebin, self.lr, data_x.columns, points0=600, pdo=50, odds0=train_data.get_odds0())
+        coef_table = self.lr.summary2().tables[1]
+        self.coef = dict(zip(coef_table.index, coef_table['Coef.']))
 
     def prob(self, x: pd.DataFrame, *args, **kwargs) -> np.array:
         # scorecardpy高版本
@@ -74,6 +78,13 @@ class ModelLr(ModelBase):
         path = self.ml_config.f_get_save_path(f"model.pkl")
         self.lr.save(path)
         print(f"model save to【{path}】success. ")
+
+        path = self.ml_config.f_get_save_path(f"coef.dict")
+        with open(path, mode="w", encoding="utf-8") as f:
+            j = json.dumps(self.coef, ensure_ascii=False)
+            f.write(j)
+        print(f"model save to【{path}】success. ")
+
         df_card = pd.concat(self.card.values())
         path = self.ml_config.f_get_save_path(f"card.csv")
         df_card.to_csv(path)