Modeling demo

更新时间:
复制 MD 格式

This topic provides a modeling demonstration in a secure environment.

### Import packages
import pandas as pd
import numpy as np
import random
import pickle
import toad
import scorecardpy as sc

from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score, roc_curve, auc
from sklearn2pmml import sklearn2pmml
from sklearn2pmml import PMMLPipeline 
from sklearn_pandas import DataFrameMapper

from xgboost import XGBClassifier

import warnings
warnings.filterwarnings('ignore')
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

### Function to calculate AUC and KS
def calc_auc_ks(tag, label, score):
  auc = roc_auc_score(list(label), list(score))
  fpr, tpr, _ = roc_curve(list(label), list(score))
  ks = abs(fpr - tpr).max()
  return pd.DataFrame({'tag': [tag], 'auc': [auc], 'ks': [ks]})

### Set the seed value for reproducibility
seed = 202412
random.seed(seed) # Set the seed for the Python random module
np.random.seed(seed) # Set the seed for NumPy

### Read the sample file
df = pd.read_csv(data_path)

### Specify the label column and feature columns
y = 'y'
feat_list = [i for i in df.columns if i not in ['idx', y]]

"""
Feature engineering: Implement the processing logic in the DataFrameMapper of the pipeline.
Feature selection: psi, iv, corr, coverage, nunique
"""

### Split the training and test datasets
test_ratio = 0.2
train_df, test_df = train_test_split(df,
                                     test_size=test_ratio,
                                     stratify=df[y],
                                     random_state=seed)

### Initialize XGBoost parameters
clf_pmml = XGBClassifier(objective='binary:logistic',
                         n_estimators=100,
                         learning_rate=0.2,
                         max_depth=3,
                         reg_alpha=0.6,
                         reg_lambda=0.3,
                         gamma=10,
                         min_child_weight=500,
                         subsample=0.6,
                         colsample_bylevel=0.6,
                         scale_pos_weight=0.7,
                         seed=seed)

### Build the pipeline. Perform feature engineering in the DataFrameMapper.
default_mapper = DataFrameMapper([(i, None) for i in feat_list])
# pipeline = PMMLPipeline([("mapper", default_mapper), ("classifier", clf_pmml)])
pipeline = PMMLPipeline([("classifier", clf_pmml)])
params = {"classifier__eval_set": [(train_df[feat_list], train_df[y]), (test_df[feat_list], test_df[y])],
          "classifier__early_stopping_rounds": 50, 
          "classifier__eval_metric": 'auc'}

### Train the model
pipeline.fit(train_df[feat_list], train_df[y], **params)

### Save the model as a PMML file (use an absolute path)
sklearn2pmml(pipeline, pmml_save_path, debug=True, with_repr=True)

### Save a copy of the model as a PKL file for offline scoring.
model_pmml = pipeline.steps[0][1]
pickle.dump(model_pmml, open(pkl_save_path, 'wb'))

### Feature importance
df_importance = pd.DataFrame({'feat_name': list(model_pmml.get_booster().get_fscore().keys()), 
                              'importance': list(model_pmml.get_booster().get_fscore().values())})
df_importance = df_importance.sort_values('importance', ascending=False).reset_index(drop=True)

### Model performance
train_df['preds'] = model_pmml.predict_proba(train_df[feat_list])[:, 1]
test_df['preds'] = model_pmml.predict_proba(test_df[feat_list])[:, 1]
total_df = pd.concat([train_df, test_df], axis=0)
x1 = calc_auc_ks('train', train_df[y], train_df['preds'])
x2 = calc_auc_ks('test', test_df[y], test_df['preds'])
x3 = calc_auc_ks('total', total_df[y], total_df['preds'])
pd.concat([x1, x2, x3], axis=0)

### Plot the AUC and KS curves
train_perf = sc.perf_eva(train_df[y], train_df['preds'], title = "train")
test_perf = sc.perf_eva(test_df[y], test_df['preds'], title = "test")

### Binning monotonicity
toad.metrics.KS_bucket(train_df['preds'], train_df[y], bucket=10, method='quantile')
toad.metrics.KS_bucket(test_df['preds'], test_df[y], bucket=10, method='quantile')
toad.metrics.KS_bucket(total_df['preds'], total_df[y], bucket=10, method='quantile')