invalid kernel: '(param0)'
Package:
scikit-learn
47032

Exception Class:
ValueError
Raise code
# we're using clone() in the GenerativeBayes classifier,
# so we can't do this kind of logic in __init__
self._choose_algorithm(self.algorithm, self.metric)
if bandwidth <= 0:
raise ValueError("bandwidth must be positive")
if kernel not in VALID_KERNELS:
raise ValueError("invalid kernel: '{0}'".format(kernel))
def _choose_algorithm(self, algorithm, metric):
# given the algorithm string + metric string, choose the optimal
# algorithm to compute the result.
if algorithm == 'auto':
# use KD Tree if possible
if metric in KDTree.valid_metrics:
🙏 Scream for help to Ukraine
Today, 25th May 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too!
We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.
Links to the raise (1)
https://github.com/scikit-learn/scikit-learn/blob/c67518350f91072f9d37ed09c5ef7edf555b6cf6/sklearn/neighbors/_kde.py#L117Ways to fix
Invalid value of kernel was given. This happens if unsupported value of kernel is given or if there is any kind typo in the value.
Code to reproduce the exception:
from sklearn.neighbors import KernelDensity
import numpy as np
rng = np.random.RandomState(42)
X = rng.random_sample((100, 3))
kde = KernelDensity(kernel='gaussin', bandwidth=0.5).fit(X)
log_density = kde.score_samples(X[:3])
log_density
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-20-5b276da75b3a> in <module>() 3 rng = np.random.RandomState(42) 4 X = rng.random_sample((100, 3)) ----> 5 kde = KernelDensity(kernel='gaussin', bandwidth=0.5).fit(X) 6 log_density = kde.score_samples(X[:3]) 7 log_density
/usr/local/lib/python3.7/dist-packages/sklearn/neighbors/_kde.py in __init__(self, bandwidth, algorithm, kernel, metric, atol, rtol, breadth_first, leaf_size, metric_params) 111 raise ValueError("bandwidth must be positive") 112 if kernel not in VALID_KERNELS: --> 113 raise ValueError("invalid kernel: '{0}'".format(kernel)) 114 115 def _choose_algorithm(self, algorithm, metric):
ValueError: invalid kernel: 'gaussin'
Fixed:
from sklearn.neighbors import KernelDensity
import numpy as np
rng = np.random.RandomState(42)
X = rng.random_sample((100, 3))
kde = KernelDensity(kernel='gaussian', bandwidth=0.5).fit(X)
log_density = kde.score_samples(X[:3])
log_density
array([-1.52955942, -1.51462041, -1.60244657])
Add a possible fix
Please authorize to post fix