python之KS曲线

Python08

python之KS曲线,第1张

# 自定义绘制ks曲线的函数

def plot_ks(y_test, y_score, positive_flag):

    # 对y_test,y_score重新设置索引

    y_test.index = np.arange(len(y_test))

    #y_score.index = np.arange(len(y_score))

    # 构建目标数据集

    target_data = pd.DataFrame({'y_test':y_test, 'y_score':y_score})

    # 按y_score降序排列

    target_data.sort_values(by = 'y_score', ascending = False, inplace = True)

    # 自定义分位点

    cuts = np.arange(0.1,1,0.1)

    # 计算各分位点对应的Score值

    index = len(target_data.y_score)*cuts

    scores = target_data.y_score.iloc[index.astype('int')]

    # 根据不同的Score值,计算Sensitivity和Specificity

    Sensitivity = []

    Specificity = []

    for score in scores:

        # 正例覆盖样本数量与实际正例样本量

        positive_recall = target_data.loc[(target_data.y_test == positive_flag) &(target_data.y_score>score),:].shape[0]

        positive = sum(target_data.y_test == positive_flag)

        # 负例覆盖样本数量与实际负例样本量

        negative_recall = target_data.loc[(target_data.y_test != positive_flag) &(target_data.y_score<=score),:].shape[0]

        negative = sum(target_data.y_test != positive_flag)

        Sensitivity.append(positive_recall/positive)

        Specificity.append(negative_recall/negative)

    # 构建绘图数据

    plot_data = pd.DataFrame({'cuts':cuts,'y1':1-np.array(Specificity),'y2':np.array(Sensitivity),

                              'ks':np.array(Sensitivity)-(1-np.array(Specificity))})

    # 寻找Sensitivity和1-Specificity之差的最大值索引

    max_ks_index = np.argmax(plot_data.ks)

    plt.plot([0]+cuts.tolist()+[1], [0]+plot_data.y1.tolist()+[1], label = '1-Specificity')

    plt.plot([0]+cuts.tolist()+[1], [0]+plot_data.y2.tolist()+[1], label = 'Sensitivity')

    # 添加参考线

    plt.vlines(plot_data.cuts[max_ks_index], ymin = plot_data.y1[max_ks_index],

              ymax = plot_data.y2[max_ks_index], linestyles = '--')

    # 添加文本信息

    plt.text(x = plot_data.cuts[max_ks_index]+0.01,

            y = plot_data.y1[max_ks_index]+plot_data.ks[max_ks_index]/2,

            s = 'KS= %.2f' %plot_data.ks[max_ks_index])

    # 显示图例

    plt.legend()

    # 显示图形

    plt.show()

# 调用自定义函数,绘制K-S曲线

plot_ks(y_test = y_test, y_score = y_score, positive_flag = 1)

首先生成1000个服从N(0,1)标准正态分布的随机数,在使用k-s检验该数据是否服从正态分布,提出假设:x从正态分布。最终返回的结果,p-value=0.76584491300591395,比指定的显著水平(假设为5%)大,则我们不能拒绝假设:x服从正态分布。这并不是说x服从正态分布一定是正确的,而是说没有充分的证据证明x不服从正态分布。因此我们的假设被接受,认为x服从正态分布。如果p-value小于我们指定的显著性水平,则我们可以肯定的拒绝提出的假设,认为x肯定不服从正态分布,这个拒绝是绝对正确的。