[docs]classKNN(SklearnMethod):""" This is a wrapper of scikit-learn’s KNeighborsClassifier class. This ML method creates a distance matrix using the given encoded data. If the encoded data is already a distance matrix (for example, when using the :ref:`Distance` or :ref:`CompAIRRDistance` encoders), please use :ref:`PrecomputedKNN` instead. Please see the `scikit-learn documentation <https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html>`_ of KNeighborsClassifier for the parameters. For usage instructions, check :py:obj:`~immuneML.ml_methods.classifiers.SklearnMethod.SklearnMethod`. **YAML specification:** .. indent with spaces .. code-block:: yaml definitions: ml_methods: my_knn_method: KNN: # sklearn parameters (same names as in original sklearn class) weights: uniform # always use this setting for weights n_neighbors: [5, 10, 15] # find the optimal number of neighbors # Additional parameter that determines whether to print convergence warnings show_warnings: True # if any of the parameters under KNN is a list and model_selection_cv is True, # a grid search will be done over the given parameters, using the number of folds specified in model_selection_n_folds, # and the optimal model will be selected model_selection_cv: True model_selection_n_folds: 5 # alternative way to define ML method with default values: my_default_knn: KNN """def__init__(self,parameter_grid:dict=None,parameters:dict=None):parameters=parametersifparametersisnotNoneelse{}parameter_grid=parameter_gridifparameter_gridisnotNoneelse{}super(KNN,self).__init__(parameter_grid=parameter_grid,parameters=parameters)def_get_ml_model(self,cores_for_training:int=2,X=None):params=self._parametersparams["n_jobs"]=cores_for_trainingreturnKNeighborsClassifier(**params)