KnnClassifier
K-Nearest Neighbors classifier — majority vote among k nearest neighbors. / K plus proches voisins classifieur — vote majoritaire parmi les k voisins les plus proches.
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))
sp.KnnClassifier has the same API as sklearn.FR — Remplacement direct : même API que sklearn, changez l'import.
API Reference
ml_knn_classifier — aliases: knn_classifier, knn_cls
sp.KnnClassifier(n_neighbors=5, weights=uniform)
| Parameter | Type | Default | Description |
|---|---|---|---|
n_neighbors | int | 5 | Number of nearest neighbors. |
weights | str | uniform | Weighting: `uniform` or `distance`. |
JSON with predictions.
$$\hat{y} = \arg\max_c \sum_{i \in \mathcal{N}_k(x)} w_i \mathbf{1}[y_i = c]$$
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
ml_knn_classifier — alias : knn_classifier, knn_cls
sp.KnnClassifier(n_neighbors=5, weights=uniform)
| Paramètre | Type | Défaut | Description |
|---|---|---|---|
n_neighbors | int | 5 | Nombre de voisins les plus proches. |
weights | str | uniform | Pondération : `uniform` ou `distance`. |
JSON avec predictions.
$$\hat{y} = \arg\max_c \sum_{i \in \mathcal{N}_k(x)} w_i \mathbf{1}[y_i = c]$$
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
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.
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))
sp.KnnRegressor has the same API as sklearn.FR — Remplacement direct : même API que sklearn, changez l'import.
API Reference
ml_knn_regressor — aliases: knn_regressor, knn_reg
sp.KnnRegressor(n_neighbors=5, weights=uniform)
| Parameter | Type | Default | Description |
|---|---|---|---|
n_neighbors | int | 5 | Number of nearest neighbors. |
weights | str | uniform | Weighting: `uniform` or `distance`. |
JSON with predictions.
$$\hat{y} = \frac{\sum_{i \in \mathcal{N}k(x)} w_i y_i}{\sum{i \in \mathcal{N}_k(x)} w_i}$$
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
ml_knn_regressor — alias : knn_regressor, knn_reg
sp.KnnRegressor(n_neighbors=5, weights=uniform)
| Paramètre | Type | Défaut | Description |
|---|---|---|---|
n_neighbors | int | 5 | Nombre de voisins les plus proches. |
weights | str | uniform | Pondération : `uniform` ou `distance`. |
JSON avec predictions.
$$\hat{y} = \frac{\sum_{i \in \mathcal{N}k(x)} w_i y_i}{\sum{i \in \mathcal{N}_k(x)} w_i}$$
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
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.
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))
sp.NearestCentroid has the same API as sklearn.FR — Remplacement direct : même API que sklearn, changez l'import.
API Reference
ml_nearest_centroid — aliases: nearest_centroid
sp.NearestCentroid()
No constructor parameters.
JSON with predictions.
$$\hat{y} = \arg\min_c |x - \mu_c|_2^2$$
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
ml_nearest_centroid — alias : nearest_centroid
sp.NearestCentroid()
Aucun paramètre de constructeur.
JSON avec predictions.
$$\hat{y} = \arg\min_c |x - \mu_c|_2^2$$
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))