seaborn: Selected KDE bandwidth is 0. Cannot estimate density

后端 未结 5 667
北恋
北恋 2021-01-04 18:32
import pandas as pd
import seaborn as sns

ser_test = pd.Series([1,0,1,4,6,0,6,5,1,3,2,5,1])
sns.kdeplot(ser_test, cumulative=True)

The above code g

相关标签:
5条回答
  • 2021-01-04 18:53

    if you don't want to wait for the seaborn git update to get released in a stable version, you can try one of the solutions in the issue page. specifically henrymartin1's suggestion to try manually passing in a small bandwidth inside a try/catch block (suggested by ahartikainen) which grabs the text of this specific error (so other errors still get raised):

    try:
        sns.distplot(df)
    except RuntimeError as re:
        if str(re).startswith("Selected KDE bandwidth is 0. Cannot estimate density."):
            sns.distplot(df, kde_kws={'bw': 0.1})
        else:
            raise re
    

    This worked for me.

    0 讨论(0)
  • 2021-01-04 18:53

    you have three options to try

    first: showing KDE lumps with the default settings

    sns.distplot(ser_test, hist = False, rug = True, rug_kws = {'color' : 'r'})

    second: KDE with narrow bandwidth to show individual probability lumps

    sns.distplot(ser_test, hist = False, rug = True, rug_kws = {'color' : 'r'}, kde_kws = {'bw' : 1})

    third: choosing a different, triangular kernel function (lump shape)

    sns.distplot(ser_test, hist = False, rug = True, rug_kws = {'color' : 'r'}, kde_kws = {'bw' : 1.5, 'kernel' : 'tri'})

    0 讨论(0)
  • 2021-01-04 18:54

    pip uninstall statsmodels solved a similar problem with the same error.

    0 讨论(0)
  • 2021-01-04 19:06

    What's going on here is that Seaborn (or rather, the library it relies on to calculate the KDE - scipy or statsmodels) isn't managing to figure out the "bandwidth", a scaling parameter used in the calculation. You can pass it manually. I played with a few values and found 1.5 gave a graph at the same scale as your previous:

    sns.kdeplot(ser_test, cumulative=True, bw=1.5)
    

    See also here. Worth installing statsmodels if you don't have it.

    0 讨论(0)
  • 2021-01-04 19:12

    The problem occurs because of statsmodels.

    Anyway, to solve the issue for seaborn version starting from 0.10.0, just place diag_kws={'bw': 1} as arg.

    Try to figure out the optimal value for bandwidth.

    0 讨论(0)
提交回复
热议问题