Hi everyone,
I have a problem with the following function that implement a Gaussian KDE:
@njit
def kde_function(points, dataset, weights=None, bw_method=None, in_log=False):
dataset = np.atleast_2d(dataset)
d_dataset, n_dataset = dataset.shape
points = np.atleast_2d(points)
d_points, n_points = points.shape
if d_points != d_dataset:
if d_points == 1 and n_points == d_dataset:
points = points.T
n_points = points.shape[1]
else:
msg = "points have dimension " + str(d_points) + ", dataset has dimension " + str(d_dataset)
raise ValueError(msg)
if weights is not None:
if weights.ndim != 1:
raise ValueError("`weights` input should be one-dimensional.")
if len(weights) != n_dataset:
raise ValueError("`weights` input should be of length n_dataset")
weights = weights / np.sum(weights)
else:
weights = np.full(n_dataset, 1.0 / n_dataset, dtype=dataset.dtype)
# other stuff here
where the “weights>” variable is optional and I set iis default value to None.
However, when I call the function without passing such variable, the compilation of the function executes this piece of code
if weights is not None:
if weights.ndim != 1:
raise ValueError("`weights` input should be one-dimensional.")
if len(weights) != n_dataset:
raise ValueError("`weights` input should be of length n_dataset")
weights = weights / np.sum(weights)
and of course fails with the error Unknown attribute 'ndim' of type none
.
Do you know a workaround for this problem?
Thanks in advanced!