How to build a lift chart (a.k.a gains chart) in Python?

前端 未结 2 1642
长发绾君心
长发绾君心 2021-01-31 04:39

I just created a model using scikit-learn which estimates the probability of how likely a client will respond to some offer. Now I\'m trying to evaluate my model. For that I wan

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-31 05:38

    You can use the scikit-plot package to do the heavy lifting.

    skplt.metrics.plot_cumulative_gain(y_test, predicted_probas)
    

    Example

    # The usual train-test split mumbo-jumbo
    from sklearn.datasets import load_breast_cancer
    from sklearn.model_selection import train_test_split
    from sklearn.naive_bayes import GaussianNB
    
    X, y = load_breast_cancer(return_X_y=True)
    X_train, X_test, y_train, y_test = train_test_split(X, y, 
    test_size=0.33)
    nb = GaussianNB()
    nb.fit(X_train, y_train)
    predicted_probas = nb.predict_proba(X_test)
    
    # The magic happens here
    import matplotlib.pyplot as plt
    import scikitplot as skplt
    skplt.metrics.plot_cumulative_gain(y_test, predicted_probas)
    plt.show()
    

    This should result in a plot like this:

提交回复
热议问题