tommyod / KDEpy

Kernel Density Estimation in Python
https://kdepy.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
584 stars 90 forks source link

FutureWarning #162

Open SergejKr opened 9 months ago

SergejKr commented 9 months ago

Hello everyone,

I am getting the following FutureWarning:

...lib\site-packages\scipy\optimize\_zeros_py.py:809: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)
r = _zeros._brentq(f, a, b, xtol, rtol, maxiter, args, full_output, disp)

A minimal example to reproduce the behaviour:

from KDEpy import FFTKDE
import warnings
warnings.filterwarnings("always")

customer_ages = [40, 56, 20, 35, 27, 24, 29, 37, 39, 46]
FFTKDE(kernel="gaussian", bw=1.0).fit(customer_ages).evaluate([0,10,20,30,40,50,60])

I am using the version 1.1.8 of KDEpy and 1.26.4 of numpy.

tommyod commented 9 months ago

Thanks for letting me know. If someone wants to look into this, I will happily merge a PR

yanbomb commented 9 months ago

Hi all, I also had this problem. However, I am reluctant to submit a PR as I do not fully understand the ins-and-outs of this (excellent!!) library and its capabilities, . The warning appears when calculating the practical support for kernels with infinite supports. Here is where is happens for my problem:

kernel_funcs.py @ line 294 (v1.1.8):

def f(x):
    return self.evaluate(x, bw=bw) - atol # <--- HERE!!!

try:
    xtol = 1e-3
    ans = brentq(f, a=0, b=8 * bw, xtol=xtol, full_output=False)
    return ans + xtol

The warning happens as f(x) returns an array. I think that this can be fixed (for my problem at least) by simply returning the first element, e.g.:

def f(x):
    return self.evaluate(x, bw=bw)[0] - atol

However, this will not work if bw is not a float (a brief look at the code seems to imply that bw could be an array... is that correct?). As I did not study the code sufficiently, I am reluctant to formally propose this fix. What do you think?

tommyod commented 9 months ago

I looks like you're onto something! Makes sense that the f that goes into brentq should return a number and not a 1-element array. If you add something like self.evaluate(x, bw=bw)[0] and the test suite passes, then I would consider the problem solved.

Please do propose a fix! If tests pass with no FutureWarning then we're good!

yanbomb commented 9 months ago

Thank you for your support. While the mod fixed my problem, warnings were raised while running test_api.py, likely coming from the usage of brentq in bw_selection.py. I will investigate and try to propose a fix when I have free time.