Commit 085d706c authored by 王家华's avatar 王家华

仍然没有加表格 好气呀

parent 9b10189a
This diff is collapsed.
...@@ -57,7 +57,7 @@ def plot_table_list(datalist, auc, datalist_description=None, title='untitled', ...@@ -57,7 +57,7 @@ def plot_table_list(datalist, auc, datalist_description=None, title='untitled',
# 每个table需要只有一个index,一个values # 每个table需要只有一个index,一个values
x = range(len(datalist[table_index].index)) x = range(len(datalist[table_index].index))
y = datalist[table_index].values y = datalist[table_index].values
axs.plot(x, y, label=datalist_description[table_index]) axs.plot(x, y, label=datalist_description[table_index] + "AUC: "+ str(round(auc[table_index],3)))
if len(x) == 1: if len(x) == 1:
plot_tab = False plot_tab = False
if plot_tab: if plot_tab:
...@@ -100,7 +100,7 @@ def plot_table_list(datalist, auc, datalist_description=None, title='untitled', ...@@ -100,7 +100,7 @@ def plot_table_list(datalist, auc, datalist_description=None, title='untitled',
plt.xticks([]) plt.xticks([])
# otherwise, nothing to do here # otherwise, nothing to do here
the_table.auto_set_font_size(False) the_table.auto_set_font_size(False)
the_table.set_fontsize(8) the_table.set_fontsize(6)
fig.subplots_adjust(bottom=0.2) fig.subplots_adjust(bottom=0.2)
plt.grid() plt.grid()
if y_label is not None: if y_label is not None:
......
File added
...@@ -39,24 +39,26 @@ def topN_feature_importance_list(features, clf, topN=3): ...@@ -39,24 +39,26 @@ def topN_feature_importance_list(features, clf, topN=3):
return importanct_feat return importanct_feat
def model_selection(algorthm,clf,df_train,df_val,df_test,target,score,optimal_model,model_obj): #TODO here
# model matrix 存储不同模型指标的矩阵
model_matrix_index = ['name', 'Params', 'trainAUC', 'validationAUC']
model_matrix = pd.DataFrame(['NULL', 'NULL', roc_auc_score(df_train[target], df_train[score]),
roc_auc_score(df_train[target], df_train[score])], index=model_matrix_index,
columns=['线上模型'])
# 定义最优参指针 # def model_selection(algorthm,clf,df_train,df_val,df_test,target,score,optimal_model,model_obj):
pointer = 0 # # model matrix 存储不同模型指标的矩阵
# 遍历最优参组合 # model_matrix_index = ['name', 'Params', 'trainAUC', 'validationAUC']
for param in optimal_para: # model_matrix = pd.DataFrame(['NULL', 'NULL', roc_auc_score(df_train[target], df_train[score]),
if algorthm == "lightGBM": # roc_auc_score(df_train[target], df_train[score])], index=model_matrix_index,
train_auc, val_auc, lgbm = lightgbm.train_lgbm(lightgbm.params_lgb, df_train, df_val, model_obj.features, # columns=['线上模型'])
adds_on=param, target=target) #
model_matrix = pd.concat([model_matrix, # # 定义最优参指针
pd.DataFrame(['lightGBM', param, train_auc, val_auc], index=model_matrix_index, # pointer = 0
columns=[pointer])], axis=1) # # 遍历最优参组合
pointer += 1 # for param in optimal_para:
# if algorthm == "lightGBM":
# 简单选取一下validation set auc 最高的 params # train_auc, val_auc, lgbm = lightgbm.train_lgbm(lightgbm.params_lgb, df_train, df_val, model_obj.features,
best_params = model_matrix.T.sort_values(by='validationAUC', ascending=False).iloc[0, :].loc['Params'] # adds_on=param, target=target)
\ No newline at end of file # model_matrix = pd.concat([model_matrix,
# pd.DataFrame(['lightGBM', param, train_auc, val_auc], index=model_matrix_index,
# columns=[pointer])], axis=1)
# pointer += 1
#
# # 简单选取一下validation set auc 最高的 params
# best_params = model_matrix.T.sort_values(by='validationAUC', ascending=False).iloc[0, :].loc['Params']
\ No newline at end of file
...@@ -62,11 +62,11 @@ df_train, df_val, df_test = datacal.train_test_split_general(df_sample, val_size ...@@ -62,11 +62,11 @@ df_train, df_val, df_test = datacal.train_test_split_general(df_sample, val_size
del df_sample del df_sample
# 模型refit # 模型refit
model_matrix, lgbm = refit.model_fit(df_sample, dhb, target, score) model_matrix, lgbm = refit.model_fit(df_train, df_val, df_test, dhb, target, score)
print(model_matrix) print(model_matrix)
# 生成报告 # 生成报告
status = refit.model_report(lgbm, df_train, df_val, df_test, dhb, target, status = refit.model_report(lgbm, df_train, df_val, df_test, dhb, target, model_matrix,
score, prediction, report_path, report_name, applied_from, applied_type, topN=3) score, prediction, report_path, report_name, applied_from, applied_type, topN=3)
......
...@@ -83,6 +83,9 @@ def model_report(clf, df_train, df_val, df_test, model_obj, target,model_matrix, ...@@ -83,6 +83,9 @@ def model_report(clf, df_train, df_val, df_test, model_obj, target,model_matrix,
:return: :return:
status : 返回1表示执行完成 status : 返回1表示执行完成
''' '''
# 样本分布
df_train_ = len(df_train)
# 用新模型预测结果 xgb还需要加一个proba (TODO here) # 用新模型预测结果 xgb还需要加一个proba (TODO here)
predictions ,test_auc = lightgbm.predict(clf,df_test,model_obj.features,target) predictions ,test_auc = lightgbm.predict(clf,df_test,model_obj.features,target)
...@@ -126,7 +129,7 @@ def model_report(clf, df_train, df_val, df_test, model_obj, target,model_matrix, ...@@ -126,7 +129,7 @@ def model_report(clf, df_train, df_val, df_test, model_obj, target,model_matrix,
#univarChart = matplot.plot_table_list([univar_train,univar_val,univar_test], [1,2,3], datalist_description=None, title= i +' univar Chart', X_label=None, y_label=None, #univarChart = matplot.plot_table_list([univar_train,univar_val,univar_test], [1,2,3], datalist_description=None, title= i +' univar Chart', X_label=None, y_label=None,
# tab_df_list=tab_df_list, plot_tab=False, # tab_df_list=tab_df_list, plot_tab=False,
# saved_path='./mvp/plots/cache/') # saved_path='./mvp/plots/cache/')
document.add_picture(univar_chart,width=Inches(8)) document.add_picture(univar_chart,width=Inches(7))
# 新增pdp段 # 新增pdp段
document.add_paragraph('PDP_chart') document.add_paragraph('PDP_chart')
...@@ -136,45 +139,65 @@ def model_report(clf, df_train, df_val, df_test, model_obj, target,model_matrix, ...@@ -136,45 +139,65 @@ def model_report(clf, df_train, df_val, df_test, model_obj, target,model_matrix,
# pdpChart = matplot.plot_table_df(pdp, ['1'], title=i + ' PDP Chart', X_label=None, y_label=None, # pdpChart = matplot.plot_table_df(pdp, ['1'], title=i + ' PDP Chart', X_label=None, y_label=None,
# tab_df=None, plot_tab=True, saved_path='./mvp/plots/cache/') # tab_df=None, plot_tab=True, saved_path='./mvp/plots/cache/')
pdpChart = matplot.pdpCharts9(clf, df_test, importanct_feat, model_obj.features, n_bins=10, dfltValue=-99999, maxValRatio=1, saved_path="./mvp/plots/cache/") pdpChart = matplot.pdpCharts9(clf, df_test, importanct_feat, model_obj.features, n_bins=10, dfltValue=-99999, maxValRatio=1, saved_path="./mvp/plots/cache/")
document.add_picture(pdpChart,width=Inches(8)) document.add_picture(pdpChart,width=Inches(7))
# 新增liftchart段 # 新增liftchart段
document.add_paragraph('lift_chart') document.add_paragraph('lift_chart')
# 遍历给定渠道 & 客群 默认等频画出liftchart # 遍历给定渠道 & 客群 默认等频画出liftchart
try: try:
lift_pred = datacal.cal_lift(df_test, score=prediction) lift_pred = datacal.cal_lift(df_test, score=prediction)
lift_online = datacal.cal_lift(df_test, score=score) lift_online = datacal.cal_lift(df_test, score=score)
# liftChart = matplot.plot_table(lift, title=i +' lift Chart',saved_path='./mvp/plots/cache/') # liftChart = matplot.plot_table(lift, title=i +' lift Chart',saved_path='./mvp/plots/cache/')
liftChart = matplot.plot_table_list([lift_pred['mean'], lift_online['mean']], liftChart = matplot.plot_table_list([lift_pred['mean'], lift_online['mean']],
[roc_auc_score(df_test[target], df_test[prediction]), [roc_auc_score(df_test[target], df_test[prediction]),
roc_auc_score(df_test[target], df_test[score])], datalist_description=None, roc_auc_score(df_test[target], df_test[score])], datalist_description=['新模型预测','线上模型'],
title='全渠道全量客群测试集上的 lift Chart', title='全渠道全量客群测试集上的 lift Chart',
X_label=None, y_label=None, X_label=None, y_label='逾期率',
tab_df_list=None, plot_tab=False, tab_df_list=[lift_pred['count'], lift_online['count']], plot_tab=False,
saved_path='./mvp/plots/cache/') saved_path='./mvp/plots/cache/')
document.add_picture(liftChart, width=Inches(8))
# 遍历渠道 document.add_picture(liftChart, width=Inches(7))
# 遍历渠道
for channel in applied_from.keys(): for channel in applied_from.keys():
# 遍历客群类型 print('lift ',channel)
for type in applied_type.keys(): # 数据切片
print('lift ',type,channel) df_sliced = df_test[
df_test.applied_type.map(lambda x: True if str(x) in type.split(',') else False) & df_test.applied_from.map(
lambda x: True if str(x) in channel.split(',') else False)]
#
lift_pred = datacal.cal_lift(df_sliced, score=prediction)
lift_online = datacal.cal_lift(df_sliced, score=score)
# liftChart = matplot.plot_table(lift, title=i +' lift Chart',saved_path='./mvp/plots/cache/')
liftChart = matplot.plot_table_list([lift_pred['mean'], lift_online['mean']],
[roc_auc_score(df_test[target], df_test[prediction]),
roc_auc_score(df_test[target], df_test[score])],
datalist_description=['新模型预测', '线上模型'],
title=applied_from[channel] + ' lift Chart',
X_label=None, y_label='逾期率',
tab_df_list=[lift_pred['count'], lift_online['count']], plot_tab=False,
saved_path='./mvp/plots/cache/')
document.add_picture(liftChart, width=Inches(5.5))
# 遍历客群类型
for type in applied_type.keys():
print('lift ',type)
# 数据切片 # 数据切片
df_sliced = df_test[df_test.applied_type.map(lambda x : True if str(x) in type.split(',') else False) & df_test.applied_from.map(lambda x : True if str(x) in channel.split(',') else False)] df_sliced = df_test[df_test.applied_type.map(lambda x : True if str(x) in type.split(',') else False) & df_test.applied_from.map(lambda x : True if str(x) in channel.split(',') else False)]
# #
lift_pred = datacal.cal_lift(df_sliced,score=prediction) lift_pred = datacal.cal_lift(df_sliced,score=prediction)
lift_online = datacal.cal_lift(df_sliced,score=score) lift_online = datacal.cal_lift(df_sliced,score=score)
#liftChart = matplot.plot_table(lift, title=i +' lift Chart',saved_path='./mvp/plots/cache/') #liftChart = matplot.plot_table(lift, title=i +' lift Chart',saved_path='./mvp/plots/cache/')
liftChart = matplot.plot_table_list([lift_pred['mean'], lift_online['mean']], [roc_auc_score(df_test[target],df_test[prediction]),roc_auc_score(df_test[target],df_test[score])], datalist_description=None, liftChart = matplot.plot_table_list([lift_pred['mean'], lift_online['mean']], [roc_auc_score(df_test[target],df_test[prediction]),roc_auc_score(df_test[target],df_test[score])], datalist_description=['新模型预测','线上模型'],
title= applied_from[channel]+applied_type[type]+ ' lift Chart', X_label=None, y_label=None, title= applied_type[type]+ ' lift Chart', X_label=None, y_label='逾期率',
tab_df_list=[lift_pred['count'], lift_online['count']], plot_tab=True, tab_df_list=[lift_pred['count'], lift_online['count']], plot_tab=False,
saved_path='./mvp/plots/cache/') saved_path='./mvp/plots/cache/')
document.add_picture(liftChart,width=Inches(8)) document.add_picture(liftChart,width=Inches(5.5))
# 存在某些渠道量很少的情况,加入try catch异常处理
except Exception as e: except Exception as e:
print('Exception: ',e) print(e)
pass pass
# 存在某些渠道量很少的情况,加入try catch异常处理
# docx 保存 # docx 保存
filetool.saveDocument(document, report_path, report_name) filetool.saveDocument(document, report_path, report_name)
return 1 return 1
......
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