我正在研究一个广告活动的倾向性建模问题。我的数据集由历史上点击过广告的用户和没有点击过广告的用户组成。
为了测量我的模型的性能,我使用sklearn绘制了累积增益和提升图表。下面是相同的代码:
import matplotlib.pyplot as plt
import scikitplot as skplt
Y_test_pred_ = model.predict_proba(X_test_df)[:]
skplt.metrics.plot_cumulative_gain(Y_test, Y_test_pred_)
plt.show()
skplt.metrics.plot_lift_curve(Y_test, Y_test_pred_)
plt.show()
我得到的图显示了0级用户和1级用户的图形
我只需要根据基线曲线绘制1类曲线。有什么办法可以做到这一点吗?
发布于 2021-02-25 18:07:51
您可以使用kds包也是一样的。
对于累积增益图:
# pip install kds
import kds
kds.metrics.plot_cumulative_gain(y_test, y_prob)
对于升降图:
import kds
kds.metrics.plot_lift(y_test, y_prob)
示例
# REPRODUCABLE EXAMPLE
# Load Dataset and train-test split
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn import tree
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.33,random_state=3)
clf = tree.DecisionTreeClassifier(max_depth=1,random_state=3)
clf = clf.fit(X_train, y_train)
y_prob = clf.predict_proba(X_test)
# CUMMULATIVE GAIN PLOT
import kds
kds.metrics.plot_cumulative_gain(y_test, y_prob[:,1])
# LIFT PLOT
kds.metrics.plot_lift(y_test, y_prob[:,1])
发布于 2019-12-16 18:06:38
如果需要,我可以解释代码:
参数:
df :包含一个得分列和一个目标列的数据帧
score :包含score列名称的字符串
target :包含目标列名称的字符串
title :包含将生成的图形的名称的字符串
def get_cum_gains(df, score, target, title):
df1 = df[[score,target]].dropna()
fpr, tpr, thresholds = roc_curve(df1[target], df1[score])
ppr=(tpr*df[target].sum()+fpr*(df[target].count()-
df[target].sum()))/df[target].count()
plt.figure(figsize=(12,4))
plt.subplot(1,2,1)
plt.plot(ppr, tpr, label='')
plt.plot([0, 1], [0, 1], 'k--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.grid(b=True, which='both', color='0.65',linestyle='-')
plt.xlabel('%Population')
plt.ylabel('%Target')
plt.title(title+'Cumulative Gains Chart')
plt.legend(loc="lower right")
plt.subplot(1,2,2)
plt.plot(ppr, tpr/ppr, label='')
plt.plot([0, 1], [1, 1], 'k--')
plt.grid(b=True, which='both', color='0.65',linestyle='-')
plt.xlabel('%Population')
plt.ylabel('Lift')
plt.title(title+'Lift Curve')
https://stackoverflow.com/questions/58722650
复制相似问题