0

I have two real/float variables stored in native python lists:

x = [2.232, 2.331, 2.112...]
y = [-3.431, 2.213, -1.232...]

Notice that there are negative values. This seems to be giving the kde call some problems. I have gotten this error:

ZeroDivisionError: 0.0 cannot be raised to a negative power.

For my purposes, the plot doesn't need to be insanely accurate, so I attempted to add in a small amount ad hoc to both x and y:

x = []
for i in range(len(raw_data)):
    x.append(float(raw_data[i][1])+.000000001)

To my surprise, the error persisted. I even added a much larger amount (.1) but no progress was made with that either.

Question

Is there a more fool-proof way to ensure the kde call is implemented robust to non-zero entries? The fact that altering my data ad hoc didn't work suggests that something else may be wrong here.

Call

seaborn.kdeplot(x,y)

Error text

~\Anaconda3\lib\site-packages\statsmodels\nonparametric\bandwidths.py in bw_scott(x, kernel)
     53     A = _select_sigma(x)
     54     n = len(x)
---> 55     return 1.059 * A * n ** (-0.2)
     56 
     57 def bw_silverman(x, kernel=None):
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Arash Howaida
  • 2,575
  • 2
  • 19
  • 50
  • Could you add some code of how exactly you call which kde? Is this just `sns.kdeplot(x=x, y=y)`? Why should such give a problem with zeros or negative values? It would only be a problem to use them together with a log scale axis. Or are you using the same values for the x and the y vector? Could you add a full error trace (as text) to your post? Preferably together with some reproducible test data. – JohanC Mar 21 '23 at 06:37
  • @JohanC That must have slipped my mind, updated, just a standard call as far as I know. – Arash Howaida Mar 21 '23 at 06:47
  • You need `seaborn.kdeplot(x=x,y=y)`. `seaborn.kdeplot(x,y)` would result in a type error in the latest seaborn version, and to something strange in older versions. Note that simply the error message isn't enough to know what's going on. A full trace would be recommended. – JohanC Mar 21 '23 at 07:18

0 Answers0