.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/evaluation/Qini_curves_for_uplift_regression.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_evaluation_Qini_curves_for_uplift_regression.py: ================================== Qini curves for uplift regression ================================== This example demonstrates the use of Qini curves for uplift regression problems, where the target variable is continuous (e.g., spend amount instead of binary conversion). The same Qini curve and uplift curve functions work for both classification and regression problems. The idea of using Qini curves for continuous outcomes was discussed in: Nicholas J. Radcliffe, "Using Control Groups to Target on Predicted Lift: Building and Assessing Uplift Models" .. GENERATED FROM PYTHON SOURCE LINES 19-21 The necessary imports ###################### .. GENERATED FROM PYTHON SOURCE LINES 21-39 .. code-block:: Python import numpy as np np.random.seed(123) import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn.preprocessing import OneHotEncoder from sklearn.preprocessing import StandardScaler from sklearn.compose import ColumnTransformer from sklearn.linear_model import LinearRegression from libuplift.meta import TLearnerUpliftRegressor from libuplift.metrics import uplift_curve, Qini_curve from libuplift.metrics import optimal_uplift_curve, optimal_Qini_curve from libuplift.metrics import Qini_coefficient .. GENERATED FROM PYTHON SOURCE LINES 40-42 Fetch and prepare data ####################### .. GENERATED FROM PYTHON SOURCE LINES 42-58 .. code-block:: Python from libuplift.datasets import fetch_Hillstrom D = fetch_Hillstrom(as_frame=True) trt = D.treatment # encode categorical features, standardize numerical features ct = ColumnTransformer([("ohe", OneHotEncoder(), list(D.categ_values.keys()))], remainder=StandardScaler()) X = ct.fit_transform(D.data) # keep only women's campaign mask = ~(trt == 1) X = X[mask] y = D.target_spend[mask] trt = (trt[mask] == 2)*1 .. GENERATED FROM PYTHON SOURCE LINES 59-61 Fit a model ############ .. GENERATED FROM PYTHON SOURCE LINES 61-66 .. code-block:: Python X_train, X_test, y_train, y_test, trt_train, trt_test = train_test_split(X, y, trt, train_size=0.7) m = TLearnerUpliftRegressor(base_estimator=LinearRegression()) m.fit(X_train, y_train, trt_train, n_trt=1) .. raw:: html
TLearnerUpliftRegressor()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


.. GENERATED FROM PYTHON SOURCE LINES 67-69 Draw Qini curves ################# .. GENERATED FROM PYTHON SOURCE LINES 69-82 .. code-block:: Python score = m.predict(X_test) cx, cy = Qini_curve(y_test, score, trt_test, n_trt=1) cx_opt, cy_opt = optimal_Qini_curve(y_test, trt_test, n_trt=1) plt.plot(cx, cy, label="curve") plt.plot(cx_opt, cy_opt, "r-", label="optimal") plt.plot([0,1], [0,cy[-1]], "k-", label="random") plt.title("Qini curve (spend)") plt.figlegend(ncols=3, loc="lower center") plt.show() .. image-sg:: /auto_examples/evaluation/images/sphx_glr_Qini_curves_for_uplift_regression_001.png :alt: Qini curve (spend) :srcset: /auto_examples/evaluation/images/sphx_glr_Qini_curves_for_uplift_regression_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 83-88 While Radcliffe et al. discourage using the optimal Qini curves for regression is discourages and another q_0 coefficient is suggested, libuplit allows for calculating optimal curves also for regression as shown above. The optimal curves are flat in the center due to many target values being exactly zero. .. GENERATED FROM PYTHON SOURCE LINES 90-91 The Qini coefficient is also defined: .. GENERATED FROM PYTHON SOURCE LINES 91-94 .. code-block:: Python print("Qini coefficient = ", Qini_coefficient(y_test, score, trt_test, n_trt=1)) .. rst-class:: sphx-glr-script-out .. code-block:: none Qini coefficient = 0.10710744697449383 .. GENERATED FROM PYTHON SOURCE LINES 95-97 Uplift curves for regression problems ###################################### .. GENERATED FROM PYTHON SOURCE LINES 99-101 Uplift curves can also be used for regression problems. The y-axis now corresponds to average net gain in spend per customer. .. GENERATED FROM PYTHON SOURCE LINES 101-112 .. code-block:: Python cx, cy = uplift_curve(y_test, score, trt_test, n_trt=1) cx_opt, cy_opt = optimal_uplift_curve(y_test, trt_test, n_trt=1) plt.plot(cx, cy) plt.plot(cx_opt, cy_opt, "r-") plt.plot([0,1], [0,cy[-1]], "k-") plt.title("Uplift curve (spend)") plt.figlegend(ncols=3, loc="lower center") plt.show() .. image-sg:: /auto_examples/evaluation/images/sphx_glr_Qini_curves_for_uplift_regression_002.png :alt: Uplift curve (spend) :srcset: /auto_examples/evaluation/images/sphx_glr_Qini_curves_for_uplift_regression_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/uplift-sklearn/checkouts/latest/examples/evaluation/Qini_curves_for_uplift_regression.py:109: UserWarning: No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument. plt.figlegend(ncols=3, loc="lower center") .. GENERATED FROM PYTHON SOURCE LINES 113-115 Evaluating model significance using a permutation test ####################################################### .. GENERATED FROM PYTHON SOURCE LINES 115-128 .. code-block:: Python from libuplift.model_selection import permutation_test_score score, permutation_scores, pv =\ permutation_test_score(m, X, y, trt, n_trt=1, cv=3, n_permutations=100, scoring="Qini_coeff", verbose=10, n_jobs=-1) plt.hist(permutation_scores, density=False, label=f"p-value={pv:.4f}") plt.axvline(score, color="r") plt.title("Permutation test") plt.legend() plt.show() .. image-sg:: /auto_examples/evaluation/images/sphx_glr_Qini_curves_for_uplift_regression_003.png :alt: Permutation test :srcset: /auto_examples/evaluation/images/sphx_glr_Qini_curves_for_uplift_regression_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [Parallel(n_jobs=-1)]: Using backend LokyBackend with 2 concurrent workers. [Parallel(n_jobs=-1)]: Done 1 tasks | elapsed: 0.1s [Parallel(n_jobs=-1)]: Batch computation too fast (0.09212493896484375s.) Setting batch_size=2. [Parallel(n_jobs=-1)]: Done 4 tasks | elapsed: 0.3s [Parallel(n_jobs=-1)]: Done 14 tasks | elapsed: 0.8s [Parallel(n_jobs=-1)]: Done 24 tasks | elapsed: 1.2s [Parallel(n_jobs=-1)]: Done 38 tasks | elapsed: 1.9s [Parallel(n_jobs=-1)]: Done 52 tasks | elapsed: 2.5s [Parallel(n_jobs=-1)]: Done 70 tasks | elapsed: 3.3s [Parallel(n_jobs=-1)]: Done 88 tasks | elapsed: 4.2s [Parallel(n_jobs=-1)]: Done 100 out of 100 | elapsed: 4.7s finished .. GENERATED FROM PYTHON SOURCE LINES 129-130 We see that the model significantly outperforms random predictions. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.044 seconds) .. _sphx_glr_download_auto_examples_evaluation_Qini_curves_for_uplift_regression.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: Qini_curves_for_uplift_regression.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: Qini_curves_for_uplift_regression.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: Qini_curves_for_uplift_regression.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_