【问题标题】:Diffrence KDE rendering when using scipy gaussian_kde and seaborn kdeplot使用 scipy gaussian_kde 和 seaborn kdeplot 时的差异 KDE 渲染
【发布时间】:2021-06-10 07:28:43
【问题描述】:

documentation 而言,seaborn kdeplot 使用scipy.stats.gaussian_kde 工作。

但是,尽管使用相同的 bandwidth 大小,但在使用 seaborngaussian_kde 进行绘图时,我得到了两种不同的分布。

在上图中,左边是数据直接输入gaussian_kde时的分布。其中,正确的绘图是数据输入seaborn kdeplot 时的分布。

此外,给定边界的曲线下面积在这两种绘制分布的方式之间并不相似。

auc 使用 gaussian_kde : 47.7 和 auc 使用 via seaborn : 49.5

我想知道是什么导致了这种差异吗?有没有一种方法可以标准化输出而不管使用哪种方法(例如,seaborngaussian_kde

重现上述plotauc的代码如下。

import seaborn as sns
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde


time_window_order = ['272', '268', '264', '260', '256', '252', '248', '244', '240']
order_dict = {k: i for i, k in enumerate ( time_window_order )}
df = pd.DataFrame ( {'time_window': ['268', '268', '268', '264', '252', '252', '252', '240',
                                     '256', '256', '256', '256', '252', '252', '252', '240'],
                     'seq_no': ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
                                'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b']} )
df ['centre_point'] = df ['time_window'].map ( order_dict )
filter_band = df ["seq_no"].isin ( ['a'] )
df = df [filter_band].reset_index ( drop=True )
auc_x_min, auc_x_max = 0, 4
bandwith=0.5
########################

plt.subplots(1, 2)
# make the first plot
plt.subplot(1, 2, 1)
kde0 = gaussian_kde ( df ['centre_point'], bw_method=bandwith )
xmin, xmax = -3, 12
x_1 = np.linspace ( xmin, xmax, 500 )
kde0_x = kde0 ( x_1 )
sel_region_x = x_1 [(x_1 > auc_x_min) * (x_1 < auc_x_max)]
sel_region_y = kde0_x [(x_1 > auc_x_min) * (x_1 < auc_x_max)]
auc_bond_1 = np.trapz ( sel_region_y, sel_region_x )
area_whole = np.trapz ( kde0_x, x_1 )
plt.plot ( x_1, kde0_x, color='b', label='KDE' )
plt.ylim(bottom=0)
plt.title(f'Direct gaussian_kde with bw {bandwith}')
plt.fill_between ( sel_region_x, sel_region_y, 0, facecolor='none', edgecolor='r', hatch='xx',
                   label='intersection' )

# make second plot
plt.subplot(1, 2, 2)

g = sns.kdeplot ( data=df, x="centre_point", bw_adjust=bandwith )
c = g.get_lines () [0].get_data ()
x_val = c [0]
kde0_x = c [1]
idx = (x_val> auc_x_min) * (x_val < auc_x_max)
sel_region_x = x_val [idx]
sel_region_y = kde0_x [idx]
auc_bond_2 = np.trapz ( sel_region_y, sel_region_x )
g.fill_between ( sel_region_x, sel_region_y, 0, facecolor='none', edgecolor='r', hatch='xx' )
plt.title(f'Via Seaborn with bw {bandwith}')
plt.tight_layout()
plt.show()

# show much the area differ between these two plotting
print ( f'auc using gaussian_kde : {auc_bond_1 * 100:.1f} and auc using via seaborn : {auc_bond_2 * 100:.1f}' )
x=1

编辑

基于mwaskon,这两行的变化

kde0 = gaussian_kde ( df ['centre_point'], bw_method='scott' )

g = sns.kdeplot ( data=df, x="centre_point", bw_adjust=1 ) # Seaborn by default use the scott method to determine the bw size

返回

在视觉上,这两个情节看起来相同。

但是,图表之间的auc 仍然返回两个不同的值

auc 使用 gaussian_kde : 45.1 和 auc 使用 via seaborn : 44.6

【问题讨论】:

    标签: python scipy seaborn auc


    【解决方案1】:

    你是这样调用 scipy 的:

    kde0 = gaussian_kde ( df ['centre_point'], bw_method=bandwith )
    

    和这样的海生

    g = sns.kdeplot ( data=df, x="centre_point", bw_adjust=bandwith )
    

    但是kdeplot docs 告诉我们bw_adjust 是一个

    乘法缩放使用 bw_method 选择的值的因子。增加将使曲线更平滑。见注释。

    而 kdeplot 也有一个 bw_method 参数,它是一个

    确定要使用的平滑带宽的方法;传递给 scipy.stats.gaussian_kde。

    因此,如果您想将两个库的结果等同起来,您需要确保使用正确的参数。

    【讨论】:

    • 感谢@mwaskom 的洞察力。我更新了代码,两个图现在看起来相同。但是,这两个情节中的auc 仍然不同。如果您能分享任何关于此的意见,不胜感激。
    • 只有当两条曲线在同一个网格上采样时,您计算 AUC 的方法才有效,这里不是这种情况。
    • 感谢您提出如何正确获取此案例的 AUC 的建议。至少我知道从哪里开始。
    猜你喜欢
    • 2021-08-26
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 2021-07-15
    • 2014-07-01
    • 2015-08-02
    相关资源
    最近更新 更多