Seaborn: one kdeplot function error of seaborn

Created on 15 Jan 2017  ·  15Comments  ·  Source: mwaskom/seaborn

when I run the demo Example:

>>> import numpy as np; np.random.seed(10)
>>> import seaborn as sns; sns.set(color_codes=True)
>>> mean, cov = [0, 2], [(1, .5), (.5, 1)]
>>> x, y = np.random.multivariate_normal(mean, cov, size=50).T
>>> ax = sns.kdeplot(x)

it report an error as below:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python\Python35\lib\site-packages\seaborn\distributions.py", line 604, in kdeplot
    cumulative=cumulative, **kwargs)
  File "D:\Python\Python35\lib\site-packages\seaborn\distributions.py", line 270, in _univariate_kdeplot
    cumulative=cumulative)
  File "D:\Python\Python35\lib\site-packages\seaborn\distributions.py", line 328, in _statsmodels_univariate_kde
    kde.fit(kernel, bw, fft, gridsize=gridsize, cut=cut, clip=clip)
  File "D:\Python\Python35\lib\site-packages\statsmodels\nonparametric\kde.py", line 146, in fit
    clip=clip, cut=cut)
  File "D:\Python\Python35\lib\site-packages\statsmodels\nonparametric\kde.py", line 506, in kdensityfft
    f = revrt(zstar)
  File "D:\Python\Python35\lib\site-packages\statsmodels\nonparametric\kdetools.py", line 20, in revrt
    y = X[:m/2+1] + np.r_[0,X[m/2+1:],0]*1j
TypeError: slice indices must be integers or None or have an __index__ method

My platform is win10 and seaborn version is (0.7.1) and statsmodels version is (0.6.1).

Most helpful comment

@PyAntony update your statsmodels package to 0.8.0 pip install -U statsmodels should do the trick

All 15 comments

I can't replicate this on my setup (OS X), using those versions of the libraries. What happens if you try to use the statsmodels KDE code directly?

when I run the code

>>> import statsmodels.api as sm
>>> nobs = 300
>>> np.random.seed(1234)  # Seed random generator
>>> dens = sm.nonparametric.KDEUnivariate(np.random.normal(size=nobs))
>>> dens.fit()

it will give the same error.

That suggests that it's some problem with your statsmodels installation. I'd suggest maybe uninstalling and reinstalling, and if that doesn't fix it, asking over there.

I installed statsmodels “0.8.0rc1”. It works well now. Thank you.

Got the same error ..

TypeError                                 Traceback (most recent call last)
<ipython-input-11-2cd15aa48a3f> in <module>()
      1 # histogram of data
      2 for f in settings['features']:
----> 3     sns.distplot(df[f])
      4     sns.plt.show()

C:\Anaconda3\lib\site-packages\seaborn\distributions.py in distplot(a, bins, hist, kde, rug, fit, hist_kws, kde_kws, rug_kws, fit_kws, color, vertical, norm_hist, axlabel, label, ax)
    216     if kde:
    217         kde_color = kde_kws.pop("color", color)
--> 218         kdeplot(a, vertical=vertical, ax=ax, color=kde_color, **kde_kws)
    219         if kde_color != color:
    220             kde_kws["color"] = kde_color

C:\Anaconda3\lib\site-packages\seaborn\distributions.py in kdeplot(data, data2, shade, vertical, kernel, bw, gridsize, cut, clip, legend, cumulative, shade_lowest, ax, **kwargs)
    599         ax = _univariate_kdeplot(data, shade, vertical, kernel, bw,
    600                                  gridsize, cut, clip, legend, ax,
--> 601                                  cumulative=cumulative, **kwargs)
    602 
    603     return ax

C:\Anaconda3\lib\site-packages\seaborn\distributions.py in _univariate_kdeplot(data, shade, vertical, kernel, bw, gridsize, cut, clip, legend, ax, cumulative, **kwargs)
    265         x, y = _statsmodels_univariate_kde(data, kernel, bw,
    266                                            gridsize, cut, clip,
--> 267                                            cumulative=cumulative)
    268     else:
    269         # Fall back to scipy if missing statsmodels

C:\Anaconda3\lib\site-packages\seaborn\distributions.py in _statsmodels_univariate_kde(data, kernel, bw, gridsize, cut, clip, cumulative)
    323     fft = kernel == "gau"
    324     kde = smnp.KDEUnivariate(data)
--> 325     kde.fit(kernel, bw, fft, gridsize=gridsize, cut=cut, clip=clip)
    326     if cumulative:
    327         grid, y = kde.support, kde.cdf

C:\Anaconda3\lib\site-packages\statsmodels\nonparametric\kde.py in fit(self, kernel, bw, fft, weights, gridsize, adjust, cut, clip)
    144             density, grid, bw = kdensityfft(endog, kernel=kernel, bw=bw,
    145                     adjust=adjust, weights=weights, gridsize=gridsize,
--> 146                     clip=clip, cut=cut)
    147         else:
    148             density, grid, bw = kdensity(endog, kernel=kernel, bw=bw,

C:\Anaconda3\lib\site-packages\statsmodels\nonparametric\kde.py in kdensityfft(X, kernel, bw, weights, gridsize, adjust, clip, cut, retgrid)
    504     zstar = silverman_transform(bw, gridsize, RANGE)*y # 3.49 in Silverman
    505                                                    # 3.50 w Gaussian kernel
--> 506     f = revrt(zstar)
    507     if retgrid:
    508         return f, grid, bw

C:\Anaconda3\lib\site-packages\statsmodels\nonparametric\kdetools.py in revrt(X, m)
     18     if m is None:
     19         m = len(X)
---> 20     y = X[:m/2+1] + np.r_[0,X[m/2+1:],0]*1j
     21     return np.fft.irfft(y)*m
     22 

TypeError: slice indices must be integers or None or have an __index__ method

Tried updated statsmodels through conda to no avail. Searched in statsmodels forums but couldn't find a related issue.

Even something as simple as this comes up with an error ..

sns.distplot(np.random.random(100))

Searched in statsmodels forums but couldn't find a related issue.

Then perhaps you should open one? As was determined upthread a) I cannot reproduce this issue on my system and b) it appears to be independent of seaborn and is a problem with some statsmodels/numpy version combinations. Unless you have new information that contradicts that understanding, I'm not sure what you are hoping to achieve here.

Also it took me about 2 minutes to track down this commit in statsmodels, which probably is what fixes the issue: https://github.com/statsmodels/statsmodels/commit/570604b3a8f044a89627c71494c8e324ed37cf85

Thanks @mwaskom -- apologies I missed that.

I am having the same problem. What am I supposed to do with this??

"""
if m is None:
m = len(X)
- y = X[:m // 2+1] + np.r_[0,X[m // 2 + 1:],0]1j
+ i = int(m // 2+1)
+ y = X[:i] + np.r_[0,X[i:],0]
1j
return np.fft.irfft(y)*m

def silverman_transform(bw, M, RANGE):

@PyAntony update your statsmodels package to 0.8.0 pip install -U statsmodels should do the trick

This issue is still not resolved. I upgraded to statsmodels 0.8.0 using conda, pip etc. Has anyone resolved this?

I had this issue but upgrading to statsmodels 0.8.0 worked for me. I used:

conda install -c statsmodels statsmodels=0.8.0

Addition to JoshuaC3, you need to update Scipy package I think :
conda install scipy

I thinks this one "conda install -c statsmodels statsmodels=0.8.0" works

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stonebig picture stonebig  ·  4Comments

JanHomann picture JanHomann  ·  3Comments

btyukodi picture btyukodi  ·  3Comments

amelio-vazquez-reina picture amelio-vazquez-reina  ·  4Comments

tritemio picture tritemio  ·  3Comments