Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

KnnClassifier

Classifier sklearn-compatible 🔍 Neighbors

K-Nearest Neighbors classifier — majority vote among k nearest neighbors. / K plus proches voisins classifieur — vote majoritaire parmi les k voisins les plus proches.

⚡ Rust-native ✓ sklearn parity
Quick start — Python
import seraplot as sp
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
knn = sp.KNeighborsClassifier(n_neighbors=5)
knn.fit(X, y)
print(knn.score(X, y))
💡
EN — Drop-in replacement: sp.KnnClassifier has the same API as sklearn.
FR — Remplacement direct : même API que sklearn, changez l'import.

API Reference

JSON function name

ml_knn_classifier — aliases: knn_classifier, knn_cls

Python class
sp.KnnClassifier(n_neighbors=5, weights=uniform)
Constructor Parameters
ParameterTypeDefaultDescription
n_neighborsint5Number of nearest neighbors.
weightsstruniformWeighting: `uniform` or `distance`.
Returns

JSON with predictions.

Algorithm

$$\hat{y} = \arg\max_c \sum_{i \in \mathcal{N}_k(x)} w_i \mathbf{1}[y_i = c]$$

Example
import seraplot as sp
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
knn = sp.KNeighborsClassifier(n_neighbors=5)
knn.fit(X, y)
print(knn.score(X, y))

Référence API

Nom de fonction JSON

ml_knn_classifier — alias : knn_classifier, knn_cls

Classe Python
sp.KnnClassifier(n_neighbors=5, weights=uniform)
Paramètres du constructeur
ParamètreTypeDéfautDescription
n_neighborsint5Nombre de voisins les plus proches.
weightsstruniformPondération : `uniform` ou `distance`.
Retourne

JSON avec predictions.

Algorithme

$$\hat{y} = \arg\max_c \sum_{i \in \mathcal{N}_k(x)} w_i \mathbf{1}[y_i = c]$$

Exemple
import seraplot as sp
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
knn = sp.KNeighborsClassifier(n_neighbors=5)
knn.fit(X, y)
print(knn.score(X, y))

KnnRegressor

Regressor sklearn-compatible 🔍 Neighbors

K-Nearest Neighbors regressor — weighted average of k nearest neighbors. / K plus proches voisins régresseur — moyenne pondérée des k voisins les plus proches.

⚡ Rust-native ✓ sklearn parity
Quick start — Python
import seraplot as sp, numpy as np
X = np.random.randn(300, 3)
y = X[:, 0] ** 2 + X[:, 1] + np.random.randn(300) * 0.5
knn = sp.KNeighborsRegressor(n_neighbors=7, weights="distance")
knn.fit(X, y)
print(knn.score(X, y))
💡
EN — Drop-in replacement: sp.KnnRegressor has the same API as sklearn.
FR — Remplacement direct : même API que sklearn, changez l'import.

API Reference

JSON function name

ml_knn_regressor — aliases: knn_regressor, knn_reg

Python class
sp.KnnRegressor(n_neighbors=5, weights=uniform)
Constructor Parameters
ParameterTypeDefaultDescription
n_neighborsint5Number of nearest neighbors.
weightsstruniformWeighting: `uniform` or `distance`.
Returns

JSON with predictions.

Algorithm

$$\hat{y} = \frac{\sum_{i \in \mathcal{N}k(x)} w_i y_i}{\sum{i \in \mathcal{N}_k(x)} w_i}$$

Example
import seraplot as sp, numpy as np
X = np.random.randn(300, 3)
y = X[:, 0] ** 2 + X[:, 1] + np.random.randn(300) * 0.5
knn = sp.KNeighborsRegressor(n_neighbors=7, weights="distance")
knn.fit(X, y)
print(knn.score(X, y))

Référence API

Nom de fonction JSON

ml_knn_regressor — alias : knn_regressor, knn_reg

Classe Python
sp.KnnRegressor(n_neighbors=5, weights=uniform)
Paramètres du constructeur
ParamètreTypeDéfautDescription
n_neighborsint5Nombre de voisins les plus proches.
weightsstruniformPondération : `uniform` ou `distance`.
Retourne

JSON avec predictions.

Algorithme

$$\hat{y} = \frac{\sum_{i \in \mathcal{N}k(x)} w_i y_i}{\sum{i \in \mathcal{N}_k(x)} w_i}$$

Exemple
import seraplot as sp, numpy as np
X = np.random.randn(300, 3)
y = X[:, 0] ** 2 + X[:, 1] + np.random.randn(300) * 0.5
knn = sp.KNeighborsRegressor(n_neighbors=7, weights="distance")
knn.fit(X, y)
print(knn.score(X, y))

NearestCentroid

Classifier sklearn-compatible 🔍 Neighbors

Nearest Centroid classifier — assigns class by closest class mean. / Classificateur par centroïde le plus proche — assigne la classe par la moyenne de classe la plus proche.

⚡ Rust-native ✓ sklearn parity
Quick start — Python
import seraplot as sp
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
nc = sp.NearestCentroid()
nc.fit(X, y)
print(nc.score(X, y))
💡
EN — Drop-in replacement: sp.NearestCentroid has the same API as sklearn.
FR — Remplacement direct : même API que sklearn, changez l'import.

API Reference

JSON function name

ml_nearest_centroid — aliases: nearest_centroid

Python class
sp.NearestCentroid()
Constructor Parameters

No constructor parameters.

Returns

JSON with predictions.

Algorithm

$$\hat{y} = \arg\min_c |x - \mu_c|_2^2$$

Example
import seraplot as sp
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
nc = sp.NearestCentroid()
nc.fit(X, y)
print(nc.score(X, y))

Référence API

Nom de fonction JSON

ml_nearest_centroid — alias : nearest_centroid

Classe Python
sp.NearestCentroid()
Paramètres du constructeur

Aucun paramètre de constructeur.

Retourne

JSON avec predictions.

Algorithme

$$\hat{y} = \arg\min_c |x - \mu_c|_2^2$$

Exemple
import seraplot as sp
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
nc = sp.NearestCentroid()
nc.fit(X, y)
print(nc.score(X, y))