:mod:`heat.classification.kneighborsclassifier` =============================================== .. py:module:: heat.classification.kneighborsclassifier .. autoapi-nested-parse:: Implements the k-nearest neighbors (kNN) classifier Module Contents --------------- .. py:class:: KNeighborsClassifier(n_neighbors: int = 5, effective_metric_: Callable = None) Bases: :class:`heat.BaseEstimator`, :class:`heat.ClassificationMixin` Implementation of the k-nearest-neighbors Algorithm [1]. This algorithm predicts labels to data vectors by using an labeled training dataset as reference. The input vector to be predicted is compared to the training vectors by calculating the Euclidean distance between each of them. A majority vote of the k-nearest, i.e. closest or smallest distanced, training vectors labels is selected as predicted class. :param n_neighbors: Number of neighbours to consider when choosing label. :type n_neighbors: int, optional, default: 5 :param effective_metric_: The distance function used to identify the nearest neighbors, defaults to the Euclidean distance. :type effective_metric_: Callable, optional .. rubric:: References [1] T. Cover and P. Hart, "Nearest Neighbor Pattern Classification," in IEEE Transactions on Information Theory, vol. 13, no. 1, pp. 21-27, January 1967, doi: 10.1109/TIT.1967.1053964. .. attribute:: n_neighbors :annotation: = 5 .. attribute:: effective_metric_ .. attribute:: x :annotation: = None .. attribute:: y :annotation: = None .. attribute:: n_samples_fit_ :annotation: = -1 .. attribute:: outputs_2d_ :annotation: = True .. attribute:: classes_ :annotation: = None .. role:: raw-html(raw) :format: html .. method:: one_hot_encoding(x: heat.core.dndarray.DNDarray) -> heat.core.dndarray.DNDarray One-hot-encodes the passed vector or single-column matrix. :param x: The data to be encoded. :type x: DNDarray .. method:: fit(x: heat.core.dndarray.DNDarray, y: heat.core.dndarray.DNDarray) Fit the k-nearest neighbors classifier from the training dataset. :param x: Labeled training vectors used for comparison in predictions, Shape=(n_samples, n_features). :type x: DNDarray :param y: Corresponding labels for the training feature vectors. Must have the same number of samples as ``x``. Shape=(n_samples) if integral labels or Shape=(n_samples, n_classes) if one-hot-encoded. :type y: DNDarray :raises TypeError: If ``x`` or ``y`` are not DNDarrays. :raises ValueError: If ``x`` and ``y`` shapes mismatch or are not two-dimensional matrices. .. rubric:: Examples >>> samples = ht.rand(10, 3) >>> knn = KNeighborsClassifier(n_neighbors=1) >>> knn.fit(samples) .. method:: predict(x: heat.core.dndarray.DNDarray) -> heat.core.dndarray.DNDarray Predict the class labels for the provided data. :param x: The test samples. :type x: DNDarray