# KNN RandomizedSearchCV typerror

While trying to study a binary classification problem with KNN and trying to tune the parameters of the model I'm getting a typerror that I quite don't understand. Is a parameter missing or something?

TypeError: init() takes exactly 1 positional argument (0 given)

Here is my code:

import pandas as pd
import numpy as np
from sklearn import model_selection
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RandomizedSearchCV

# generate example data
X = pd.DataFrame({
'a': np.linspace(0, 10, 500),
'b': np.random.randint(0, 10, size=500),
})
y = np.random.randint(0, 2, size=500)

# set search parameters
n_neighbors = [int(x) for x in np.linspace(start = 1, stop = 100, num = 50)]
weights = ['uniform','distance']
metric = ['euclidean','manhattan','chebyshev','seuclidean','minkowski']
random_grid = {
'n_neighbors': n_neighbors,
'weights': weights,
'metric': metric,
}

# run search
knn = KNeighborsClassifier()
knn_random = RandomizedSearchCV(estimator = knn, random_state = 42,n_jobs = -1,param_distributions = random_grid,n_iter = 100, cv=3,verbose = 2)
knn_random.fit(X,y)
knn_random.best_params_


Full error:

_RemoteTraceback                          Traceback (most recent call last)
_RemoteTraceback:
"""
Traceback (most recent call last):
File "C:\Users\dungeon\Anaconda3\lib\site-packages\sklearn\externals\joblib\externals\loky\process_executor.py", line 418, in _process_worker
r = call_item()
File "C:\Users\dungeon\Anaconda3\lib\site-packages\sklearn\externals\joblib\externals\loky\process_executor.py", line 272, in __call__
return self.fn(*self.args, **self.kwargs)
File "C:\Users\dungeon\Anaconda3\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 567, in __call__
return self.func(*args, **kwargs)
File "C:\Users\dungeon\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 225, in __call__
for func, args, kwargs in self.items]
File "C:\Users\dungeon\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 225, in <listcomp>
for func, args, kwargs in self.items]
File "C:\Users\dungeon\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py", line 528, in _fit_and_score
estimator.fit(X_train, y_train, **fit_params)
File "C:\Users\dungeon\Anaconda3\lib\site-packages\sklearn\neighbors\base.py", line 916, in fit
return self._fit(X)
File "C:\Users\dungeon\Anaconda3\lib\site-packages\sklearn\neighbors\base.py", line 254, in _fit
**self.effective_metric_params_)
File "sklearn\neighbors\binary_tree.pxi", line 1071, in sklearn.neighbors.ball_tree.BinaryTree.__init__
File "sklearn\neighbors\dist_metrics.pyx", line 286, in sklearn.neighbors.dist_metrics.DistanceMetric.get_metric
File "sklearn\neighbors\dist_metrics.pyx", line 443, in sklearn.neighbors.dist_metrics.SEuclideanDistance.__init__
TypeError: __init__() takes exactly 1 positional argument (0 given)
"""

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-2-b5a9f7ea82d0> in <module>
29 knn_random = RandomizedSearchCV(estimator = knn, random_state = 42,n_jobs = -1,param_distributions = random_grid,n_iter = 100, cv=3,verbose = 2)
30
---> 31 knn_random.fit(X,y)
32 knn_random.best_params_

~\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in fit(self, X, y, groups, **fit_params)
720                 return results_container[0]
721
--> 722             self._run_search(evaluate_candidates)
723
724         results = results_container[0]

~\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in _run_search(self, evaluate_candidates)
1513         evaluate_candidates(ParameterSampler(
1514             self.param_distributions, self.n_iter,
-> 1515             random_state=self.random_state))

~\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in evaluate_candidates(candidate_params)
709                                for parameters, (train, test)
710                                in product(candidate_params,
--> 711                                           cv.split(X, y, groups)))
712
713                 all_candidate_params.extend(candidate_params)

~\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py in __call__(self, iterable)
928
929             with self._backend.retrieval_context():
--> 930                 self.retrieve()
931             # Make sure that we get a last message telling us we are done
932             elapsed_time = time.time() - self._start_time

~\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py in retrieve(self)
831             try:
832                 if getattr(self._backend, 'supports_timeout', False):
--> 833                     self._output.extend(job.get(timeout=self.timeout))
834                 else:
835                     self._output.extend(job.get())

~\Anaconda3\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py in wrap_future_result(future, timeout)
519         AsyncResults.get from multiprocessing."""
520         try:
--> 521             return future.result(timeout=timeout)
522         except LokyTimeoutError:
523             raise TimeoutError()

~\Anaconda3\lib\concurrent\futures\_base.py in result(self, timeout)
430                 raise CancelledError()
431             elif self._state == FINISHED:
--> 432                 return self.__get_result()
433             else:
434                 raise TimeoutError()

~\Anaconda3\lib\concurrent\futures\_base.py in __get_result(self)
382     def __get_result(self):
383         if self._exception:
--> 384             raise self._exception
385         else:
386             return self._result

TypeError: __init__() takes exactly 1 positional argument (0 given)

• Thanks for your quick answer, what exactly do you mean a minimal example? This is a parameter tuning script it's gonna give me , best n_estimator, best weights, best metric, do you mean like the output ? Also I've already run RandomizedSearchCV in 2 different models, random forest, logistic regression and it did work why does it work differently in knn? – dungeon Jun 30 '19 at 11:16
• If we want to run tests with your code it is helpful if you can provide some data to test on. Ideally we want to copy-paste the code and see for ourselves what is wrong. By the way, I deleted my previous comment because it contained an error and I do not want to confuse others. – Louic Jun 30 '19 at 11:19
• Oh ok I get what you mean now I will try to fix this, can you please also comment on my edited question? – dungeon Jun 30 '19 at 11:22

The problem is with the metric seuclidean. The SEuclideanDistance constructor requires a parameter (see the DistanceMetric documentation). This parameter is not given, hence the error about the missing argument.

It should in principle be possible to give the parameter in the searchgrid, but there are several known issues with RandomizedSearchCV that make this impossible (or at least harder than necessary).

So until these issues are fixed I would suggest to remove seuclidean from the list of search parameters, or to use GridSearchCV. Whatever option is the best choice depends on the details of what you are trying to achieve.

• Thanks a lot for your time sir, indeed it was seuclidean that was causing the problem once I removed it, it immediately worked, on my next questions I will try to provide subsets of data as well after your recommendation. – dungeon Jun 30 '19 at 11:57