mojolearn.NearestCentroid#
- class mojolearn.NearestCentroid(metric='euclidean', *, shrink_threshold=None, priors='uniform')[source]#
Bases:
_XNeighborsNearest centroid classifier.
Represents each class by its centroid (the per-class mean for
metric='euclidean', the per-class median for'manhattan') and assigns each sample to the nearest one, following scikit-learn’sNearestCentroid, including shrunken centroids and class priors. With non-uniform priors,predictuses the discriminant of decision_function(). Sparse input is densified. Computation is float32 (scikit-learn uses float64). A feature whose shrink scale is zero gets deviation 0 (scikit-learn divides by zero there).- Parameters:
metric ({‘euclidean’, ‘manhattan’}, default ‘euclidean’) – Distance used for centroids and prediction; anything else raises
ValueError.shrink_threshold (float or None, default None) – Threshold for shrinking centroids toward the overall centroid; None disables shrinking.
priors ({‘uniform’, ‘empirical’} or array-like of shape (n_classes,), default ‘uniform’) – Class priors.
'empirical'uses the class frequencies. Given priors must be non-negative, one per class, and are rescaled to sum to one.numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode for this estimator (
'fast'is Apple only). None uses the process default.
- Variables:
centroids_ (Array of shape (n_classes, n_features)) – Class centroids, after shrinking.
deviations_ (Array of shape (n_classes, n_features)) – Shrunken deviations of each class centroid from the overall centroid.
within_class_std_dev_ (Array of shape (n_features,)) – Pooled within-class standard deviation of each feature.
class_prior_ (Array of shape (n_classes,), float64) – Class priors.
classes_ (list) – Class labels.
n_features_in_ (int) – Number of features seen in
fit.
Examples
>>> import numpy as np, mojolearn >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> y = (X[:, 0] > 0.5).astype(np.int64) >>> clf = mojolearn.NearestCentroid().fit(X, y) >>> pred = clf.predict(X)
Methods
Discriminant score of each sample for each class.
fit(X, y)Fit the class centroids.
get_params([deep])numeric_mode_used()The numeric mode this estimator runs in.
predict(X)Predict the class of each sample.
Log class probabilities: the log-softmax of decision_function().
Class probabilities: the softmax of decision_function().
score(X, y[, sample_weight])Mean accuracy of predict() on the given data.
set_params(**params)Set constructor parameters.
vendor_used()The GPU backend this estimator runs on.
- fit(X, y)[source]#
Fit the class centroids.
- Parameters:
X (array-like or scipy.sparse matrix of shape (n_samples, n_features)) – Training data.
y (array-like of shape (n_samples,)) – Class labels; at least two classes.
- Returns:
self (NearestCentroid) – The fitted estimator.
- Raises:
ValueError – If there are fewer than two classes, the priors are invalid, or every feature has zero variance.
- predict(X)[source]#
Predict the class of each sample.
With uniform priors this is the class of the nearest centroid; otherwise the class with the largest decision_function() value.
- Parameters:
X (array-like of shape (n_queries, n_features)) – Samples to classify.
- Returns:
labels (Array or list of shape (n_queries,)) – Predicted class labels.
- decision_function(X)[source]#
Discriminant score of each sample for each class.
Available for
metric='euclidean'only; otherwise raisesAttributeError.- Parameters:
X (array-like of shape (n_queries, n_features)) – Samples to score.
- Returns:
scores (Array of shape (n_queries, n_classes), float32)
- predict_proba(X)[source]#
Class probabilities: the softmax of decision_function().
Available for
metric='euclidean'only.- Parameters:
X (array-like of shape (n_queries, n_features)) – Samples to score.
- Returns:
proba (Array of shape (n_queries, n_classes)) – Probabilities, columns in the order of
classes_.
- predict_log_proba(X)[source]#
Log class probabilities: the log-softmax of decision_function().
Available for
metric='euclidean'only.- Parameters:
X (array-like of shape (n_queries, n_features)) – Samples to score.
- Returns:
log_proba (Array of shape (n_queries, n_classes), float32)
- score(X, y, sample_weight=None)[source]#
Mean accuracy of predict() on the given data.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Test samples.
y (array-like of shape (n_samples,)) – True labels.
sample_weight (array-like of shape (n_samples,) or None, default None) – Sample weights.
- Returns:
score (float) – (Weighted) fraction of correct predictions.