mojolearn.AffinityPropagation#
- class mojolearn.AffinityPropagation(*, damping=0.5, max_iter=200, convergence_iter=15, copy=True, preference=None, affinity='euclidean', verbose=False, random_state=None)[source]#
Bases:
_XClusterAffinity propagation clustering.
Follows scikit-learn’s
AffinityPropagation: responsibility and availability messages are exchanged until the set of exemplars is stable forconvergence_iteriterations. The similarity is minus the squared euclidean distance, or a precomputed matrix. The small noise that breaks ties is drawn from this library’s own seeded random stream rather than NumPy’s, so results agree with scikit-learn’s within a tolerance. Memory grows with the square of the number of rows. Input is converted to float32.- Parameters:
damping (float in [0.5, 1), default 0.5) – Damping of the message updates.
max_iter (int, default 200) – Maximum number of iterations; must be at least 1.
convergence_iter (int, default 15) – Number of iterations with no change in the exemplars that stops the fit; must be at least 1.
copy (bool, default True) – Accepted for scikit-learn compatibility;
Xis never modified.preference (float, array-like of shape (n_samples,) or None, default None) – Preference of each row to be an exemplar. None uses the median of the similarities, as in scikit-learn.
affinity ({‘euclidean’, ‘precomputed’}, default ‘euclidean’) – With ‘precomputed’,
Xis a square similarity matrix.verbose (bool, default False) – Accepted for scikit-learn compatibility; nothing is printed.
random_state (int or None, default None) – Non-negative seed of the tie-breaking noise. None means 0. A NumPy
RandomStateis not accepted.numeric_mode (str or None, default None) – Numeric mode for this estimator. None uses the process default (see
mojolearn.set_numeric_mode()).
- Variables:
cluster_centers_indices_ (Array of shape (n_clusters,), int32) – Row indices of the exemplars.
cluster_centers_ (Array of shape (n_clusters, n_features), float32) – The exemplar rows. Not set with
affinity='precomputed'.labels_ (Array of shape (n_samples,), int32) – Cluster of each training row.
affinity_matrix_ (Array of shape (n_samples, n_samples), float32) – The similarity matrix used.
n_iter_ (int) – Number of iterations run.
n_features_in_ (int) – Number of features seen in
fit.
Examples
>>> import numpy as np, mojolearn >>> X = np.random.default_rng(0).random((500, 4), dtype=np.float32) >>> model = mojolearn.AffinityPropagation(random_state=0).fit(X) >>> labels = model.predict(X)
- __init__(*, damping=0.5, max_iter=200, convergence_iter=15, copy=True, preference=None, affinity='euclidean', verbose=False, random_state=None)[source]#
Methods
fit(X[, y])Find the exemplars of
X.fit_predict(X[, y])Fit the model on
Xand return the training labels.get_params([deep])numeric_mode_used()The numeric mode this estimator runs in.
predict(X)Assign each row of
Xto its nearest exemplar.set_params(**params)Set constructor parameters.
vendor_used()The GPU backend this estimator runs on.
- fit(X, y=None)[source]#
Find the exemplars of
X.- Parameters:
X (array-like of shape (n_samples, n_features)) – Training rows, or a square similarity matrix with
affinity='precomputed'. Converted to float32; NaN or infinity raises ValueError.y (ignored) – Present for scikit-learn compatibility.
- Returns:
self (AffinityPropagation) – The fitted estimator.
- Raises:
ValueError – If
affinity,damping,max_iter,convergence_iterorpreferenceis invalid, or a precomputed matrix is not square.
- predict(X)[source]#
Assign each row of
Xto its nearest exemplar.- Parameters:
X (array-like of shape (n_samples, n_features)) – Rows to label, converted to float32.
- Returns:
labels (Array of shape (n_samples,), int32) – Cluster of each row; every row gets -1 when the fit found no exemplars.
- Raises:
RuntimeError – If the model is not fitted.
ValueError – If
affinity='precomputed'.
- fit_predict(X, y=None)[source]#
Fit the model on
Xand return the training labels.- Parameters:
X (array-like of shape (n_samples, n_features)) – Training rows, or a square similarity matrix with
affinity='precomputed'.y (ignored) – Present for scikit-learn compatibility.
- Returns:
labels (Array of shape (n_samples,), int32) – The fitted
labels_.