|
@@ -18,7 +18,7 @@ import statsmodels.api as sm
|
|
|
from commom import f_df_to_image, f_display_images_by_side, GeneralException, f_display_title, \
|
|
|
f_image_crop_white_borders
|
|
|
from entitys import MetricFucResultEntity, DataSplitEntity, DataFeatureEntity
|
|
|
-from enums import ContextEnum, ResultCodesEnum, ConstantEnum
|
|
|
+from enums import ContextEnum, ResultCodesEnum, ConstantEnum, FileEnum
|
|
|
from init import context
|
|
|
from .model_base import ModelBase
|
|
|
from .model_utils import f_stress_test, f_calcu_model_ks, f_get_model_score_bin, f_calcu_model_psi, f_add_rules
|
|
@@ -31,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.card_cfg = None
|
|
|
self.coef = None
|
|
|
|
|
|
def get_report_template_path(self):
|
|
@@ -52,6 +53,7 @@ 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())
|
|
|
+ self.card_cfg = {"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.']))
|
|
|
|
|
@@ -75,28 +77,34 @@ class ModelLr(ModelBase):
|
|
|
GeneralException(ResultCodesEnum.NOT_FOUND, message=f"模型不存在")
|
|
|
if self.card is None:
|
|
|
GeneralException(ResultCodesEnum.NOT_FOUND, message=f"card不存在")
|
|
|
- path = self.ml_config.f_get_save_path(f"model.pkl")
|
|
|
+ path = self.ml_config.f_get_save_path(FileEnum.MODEL.value)
|
|
|
self.lr.save(path)
|
|
|
print(f"model save to【{path}】success. ")
|
|
|
|
|
|
- path = self.ml_config.f_get_save_path("coef.dict")
|
|
|
+ path = self.ml_config.f_get_save_path(FileEnum.COEF.value)
|
|
|
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")
|
|
|
+ path = self.ml_config.f_get_save_path(FileEnum.CARD.value)
|
|
|
df_card.to_csv(path)
|
|
|
print(f"model save to【{path}】success. ")
|
|
|
|
|
|
+ path = self.ml_config.f_get_save_path(FileEnum.CARD_CFG.value)
|
|
|
+ with open(path, mode="w", encoding="utf-8") as f:
|
|
|
+ j = json.dumps(self.card_cfg, ensure_ascii=False)
|
|
|
+ f.write(j)
|
|
|
+ print(f"model save to【{path}】success. ")
|
|
|
+
|
|
|
def model_load(self, path: str, *args, **kwargs):
|
|
|
if not os.path.isdir(path):
|
|
|
raise GeneralException(ResultCodesEnum.NOT_FOUND, message=f"【{path}】不是文件夹")
|
|
|
- path_model = os.path.join(path, "model.pkl")
|
|
|
+ path_model = os.path.join(path, FileEnum.MODEL.value)
|
|
|
if not os.path.isfile(path_model):
|
|
|
raise GeneralException(ResultCodesEnum.NOT_FOUND, message=f"模型文件【{path_model}】不存在")
|
|
|
- path_card = os.path.join(path, "card.csv")
|
|
|
+ path_card = os.path.join(path, FileEnum.CARD.value)
|
|
|
if not os.path.isfile(path_card):
|
|
|
raise GeneralException(ResultCodesEnum.NOT_FOUND, message=f"模型文件【{path_card}】不存在")
|
|
|
|