快速开始

查看RetailHero教程笔记本 (EN Open In Colab2, RU Open In Colab2) 以获取详细信息。

训练并预测你的提升模型

使用直观的python API通过sklift.models训练提升模型。

 1# import approaches
 2from sklift.models import SoloModel, ClassTransformation
 3# import any estimator adheres to scikit-learn conventions.
 4from lightgbm import LGBMClassifier
 5
 6# define models
 7estimator = LGBMClassifier(n_estimators=10)
 8
 9# define metamodel
10slearner = SoloModel(estimator=estimator)
11
12# fit model
13slearner.fit(
14    X=X_tr,
15    y=y_tr,
16    treatment=trmnt_tr,
17)
18
19# predict uplift
20uplift_slearner = slearner.predict(X_val)

评估你的提升模型

Uplift模型评估指标可在sklift.metrics中找到。

 1# import metrics to evaluate your model
 2from sklift.metrics import (
 3    uplift_at_k, uplift_auc_score, qini_auc_score, weighted_average_uplift
 4)
 5
 6
 7# Uplift@30%
 8uplift_at_k = uplift_at_k(y_true=y_val, uplift=uplift_slearner,
 9                          treatment=trmnt_val,
10                          strategy='overall', k=0.3)
11
12# Area Under Qini Curve
13qini_coef = qini_auc_score(y_true=y_val, uplift=uplift_slearner,
14                           treatment=trmnt_val)
15
16# Area Under Uplift Curve
17uplift_auc = uplift_auc_score(y_true=y_val, uplift=uplift_slearner,
18                              treatment=trmnt_val)
19
20# Weighted average uplift
21wau = weighted_average_uplift(y_true=y_val, uplift=uplift_slearner,
22                              treatment=trmnt_val)

可视化结果

使用sklift.viz可视化性能指标。

 1from sklift.viz import plot_qini_curve
 2import matplotlib.pyplot as plt
 3
 4fig, ax = plt.subplots(1, 1)
 5ax.set_title('Qini curves')
 6
 7plot_qini_curve(
 8    y_test, uplift_slearner, trmnt_test,
 9    perfect=True, name='Slearner', ax=ax
10);
11
12plot_qini_curve(
13    y_test, uplift_revert, trmnt_test,
14    perfect=False, name='Revert label', ax=ax
15);
Example of some models qini curves, perfect qini curve and random qini curve
 1from sklift.viz import plot_uplift_curve
 2import matplotlib.pyplot as plt
 3
 4fig, ax = plt.subplots(1, 1)
 5ax.set_title('Uplift curves')
 6
 7plot_uplift_curve(
 8    y_test, uplift_slearner, trmnt_test,
 9    perfect=True, name='Slearner', ax=ax
10);
11
12plot_uplift_curve(
13    y_test, uplift_revert, trmnt_test,
14    perfect=False, name='Revert label', ax=ax
15);
Example of some uplift curves, perfect uplift curve and random uplift curve
1from sklift.viz import plot_uplift_by_percentile
2
3plot_uplift_by_percentile(y_true=y_val, uplift=uplift_preds,
4                          treatment=treat_val, kind='bar')
Uplift by percentile visualization