Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python seaborn:所选KDE带宽为0。无法估计密度_Python_Pandas_Data Visualization_Seaborn_Kernel Density - Fatal编程技术网

Python seaborn:所选KDE带宽为0。无法估计密度

Python seaborn:所选KDE带宽为0。无法估计密度,python,pandas,data-visualization,seaborn,kernel-density,Python,Pandas,Data Visualization,Seaborn,Kernel Density,上述代码生成以下CDF图: 但当系列元素修改为: 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) 我得到以下错误: ValueError:无法将字符串转换为浮点:“scott” 运行时错误:所选KDE带宽为0。无法估计密度 这个错误意味着什么?我如何解决它以生成CDF(即使它非常

上述代码生成以下CDF图:

但当系列元素修改为:

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)
我得到以下错误:

ValueError:无法将字符串转换为浮点:“scott”

运行时错误:所选KDE带宽为0。无法估计密度

这个错误意味着什么?我如何解决它以生成CDF(即使它非常倾斜)

编辑:我正在使用seaborn版本0.9.0

完整的跟踪如下所示:

ser_test = pd.Series([1,0,1,1,6,0,6,1,1,0,2,1,1])
sns.kdeplot(ser_test, cumulative=True)
ValueError:无法将字符串转换为浮点:“scott”
在处理上述异常期间,发生了另一个异常:
运行时错误回溯(上次最近调用)
在里面
1序列试验=pd.系列([1,0,1,1,6,0,6,1,1,0,2,1,1])
---->2 sns.kdeplot(序列检验,累积=真)
kdeplot中的~/.local/lib/python3.5/site-packages/seaborn/distributions.py(数据、数据2、阴影、垂直、内核、bw、网格大小、剪切、剪辑、图例、累积、阴影最低、cbar、cbar、cbar、ax、cbar、kws、ax、**kwargs)
689 ax=_单变量_kdeplot(数据、阴影、垂直、内核、bw、,
690网格大小、切割、剪辑、图例、ax、,
-->691累计=累计,**千克)
692
693返回斧头
~/.local/lib/python3.5/site-packages/seaborn/distributions.py in_univariate_kdeplot(数据、阴影、垂直、内核、bw、网格大小、剪切、剪辑、图例、ax、累积、**kwargs)
281 x,y=_statsmodels_单变量_kde(数据,内核,bw,
282网格大小、切割、剪辑、,
-->283累计=累计)
284其他:
285#如果缺少statsmodels,请返回scipy
~/.local/lib/python3.5/site-packages/seaborn/distributions.py in_statsmodels_univariate_kde(数据、内核、bw、网格大小、剪切、剪辑、累积)
353 fft=内核==“gau”
354 kDae=smnp.KDEUnivariate(数据)
-->355 kde.fit(内核,bw,fft,gridsize=gridsize,cut=cut,clip=clip)
356如果累计:
357网格,y=kde.support,kde.cdf
~/.local/lib/python3.5/site-packages/statsmodels/nonparametric/kde.py in fit(self、kernel、bw、fft、weights、gridsize、adjust、cut、clip)
138密度,网格,bw=kdensityfft(endog,kernel=kernel,bw=bw,
139调整=调整,权重=权重,网格大小=网格大小,
-->140夹子=夹子,切割=切割)
141.其他:
142密度,网格,bw=K密度(endog,kernel=kernel,bw=bw,
kdensityfft中的~/.local/lib/python3.5/site-packages/statsmodels/nonparametric/kde.py(X,kernel,bw,weights,gridsize,adjust,clip,cut,retgrid)
451 bw=浮动(bw)
452除:
-->453 bw=带宽。选择“带宽(X,bw,kern)#交叉值是否适合此模式?
454 bw*=调整
455
选择带宽(x,bw,kernel)中的~/.local/lib/python3.5/site-packages/statsmodels/nonparametric/bandwidths.py
172#最终,这可能取决于另一个选择标准。
173 err=“所选KDE带宽为0。无法估计密度。”
-->174提升运行时错误(err)
175.其他:
176返回带宽
运行时错误:所选KDE带宽为0。无法估计密度。

这里发生的事情是Seaborn(或者更确切地说,它计算KDE-scipy或statsmodels所依赖的库)无法计算出“带宽”,a。您可以手动传递。我使用了一些值,发现1.5给出了一个与您以前的相同比例的图:

ValueError: could not convert string to float: 'scott'

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
<ipython-input-93-7cee594b4526> in <module>
      1 ser_test = pd.Series([1,0,1,1,6,0,6,1,1,0,2,1,1])
----> 2 sns.kdeplot(ser_test, cumulative=True)

~/.local/lib/python3.5/site-packages/seaborn/distributions.py in kdeplot(data, data2, shade, vertical, kernel, bw, gridsize, cut, clip, legend, cumulative, shade_lowest, cbar, cbar_ax, cbar_kws, ax, **kwargs)
    689         ax = _univariate_kdeplot(data, shade, vertical, kernel, bw,
    690                                  gridsize, cut, clip, legend, ax,
--> 691                                  cumulative=cumulative, **kwargs)
    692 
    693     return ax

~/.local/lib/python3.5/site-packages/seaborn/distributions.py in _univariate_kdeplot(data, shade, vertical, kernel, bw, gridsize, cut, clip, legend, ax, cumulative, **kwargs)
    281         x, y = _statsmodels_univariate_kde(data, kernel, bw,
    282                                            gridsize, cut, clip,
--> 283                                            cumulative=cumulative)
    284     else:
    285         # Fall back to scipy if missing statsmodels

~/.local/lib/python3.5/site-packages/seaborn/distributions.py in _statsmodels_univariate_kde(data, kernel, bw, gridsize, cut, clip, cumulative)
    353     fft = kernel == "gau"
    354     kde = smnp.KDEUnivariate(data)
--> 355     kde.fit(kernel, bw, fft, gridsize=gridsize, cut=cut, clip=clip)
    356     if cumulative:
    357         grid, y = kde.support, kde.cdf

~/.local/lib/python3.5/site-packages/statsmodels/nonparametric/kde.py in fit(self, kernel, bw, fft, weights, gridsize, adjust, cut, clip)
    138             density, grid, bw = kdensityfft(endog, kernel=kernel, bw=bw,
    139                     adjust=adjust, weights=weights, gridsize=gridsize,
--> 140                     clip=clip, cut=cut)
    141         else:
    142             density, grid, bw = kdensity(endog, kernel=kernel, bw=bw,

~/.local/lib/python3.5/site-packages/statsmodels/nonparametric/kde.py in kdensityfft(X, kernel, bw, weights, gridsize, adjust, clip, cut, retgrid)
    451         bw = float(bw)
    452     except:
--> 453         bw = bandwidths.select_bandwidth(X, bw, kern) # will cross-val fit this pattern?
    454     bw *= adjust
    455 

~/.local/lib/python3.5/site-packages/statsmodels/nonparametric/bandwidths.py in select_bandwidth(x, bw, kernel)
    172         # eventually this can fall back on another selection criterion.
    173         err = "Selected KDE bandwidth is 0. Cannot estimate density."
--> 174         raise RuntimeError(err)
    175     else:
    176         return bandwidth

RuntimeError: Selected KDE bandwidth is 0. Cannot estimate density.

另请参阅。如果您没有安装statsmodels,请参阅。值得安装。

pip卸载statsmodels解决了一个类似的问题,但出现了相同的错误。

如果您不想等待seaborn git更新以稳定的版本发布,您可以在中尝试其中一个解决方案。特别是henrymartin1的建议在try/catch块(由ahartikainen建议)内手动传入一个小带宽,该块捕获此特定错误的文本(因此仍会引发其他错误):

这对我很有效。

您有三种选择可以尝试 第一:使用默认设置显示KDE集块
sns.distplot(seru-test,hist=False,rug=True,rug\u-kws={'color':'r'})

第二:带宽窄的KDE,用于显示单个概率块
sns.distplot(ser_test,hist=False,rug=True,rug_-kws={'color':'r'},kde_-kws={'bw':1})

第三:选择不同的三角形核函数(块状)
sns.distplot(seru-test,hist=False,rug=True,rug\u-kws={'color':'r'},kde\u-kws={'bw':1.5,'kernel':'tri'})

问题的发生是因为statsmodels

无论如何,要解决从0.10.0开始的seaborn版本的问题,只需将diag_kws={'bw':1}作为arg


尝试找出带宽的最佳值。

请包括完整的回溯。我现在已经包括了它。如果你想在包中使用statsmodels,这没有帮助。seaborn的git repo上有一个更新,预计可以解决它。我有完全相同的问题,只是
sns.jointplot
和玩
bw
没有帮助。如果我禁用
statsmodels
会有帮助,但是其他的看起来都很糟糕。对此有什么评论吗?最好是提出一个新问题。我必须使用此参数才能使绘图生效。谢谢!
sns.kdeplot(ser_test, cumulative=True, bw=1.5)
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