fuzzyclust.base.BaseFuzzyClusterer#

class fuzzyclust.base.BaseFuzzyClusterer(n_clusters=3, m=2.0, tol=0.0001, max_iter=300, init='random', n_init=1, random_state=None, verbose=0)[source]#

Bases: ClusterMixin, TransformerMixin, BaseEstimator, ABC

Abstract base of the prototype-based clustering algorithms.

Parameters:
n_clustersint, default=3

Number of clusters \(c\).

mfloat, default=2.0

Fuzziness (weighting) exponent \(m>1\). Larger values give fuzzier partitions; \(m\to 1\) approaches a hard partition.

tolfloat, default=1e-4

Termination tolerance \(\epsilon\) on \(\max_{ik} |\mu_{ik}^{(l)} - \mu_{ik}^{(l-1)}|\).

max_iterint, default=300

Maximum number of alternating-optimization iterations.

init{“random”, “kmeans++”} or ndarray, default=”random”

Initialization. "random" draws random prototypes inside the bounding box of the data and derives the initial partition from them (MATLAB toolbox behavior). "kmeans++" uses the k-means++ seeding. An array of shape (n_clusters, n_features) is used as initial prototypes, an array of shape (n_samples, n_clusters) as initial partition.

n_initint, default=1

Number of random restarts. The alternating optimization converges to a local optimum of \(J\); with n_init > 1 the run with the lowest final objective is kept (ignored when init is an array).

random_stateint, RandomState instance or None, default=None

Controls the random initialization.

verboseint, default=0

Print the objective at every iteration when > 0.

Attributes:
cluster_centers_ndarray of shape (n_clusters, n_features)

Prototypes \(\mathbf{v}_i\).

membership_ndarray of shape (n_samples, n_clusters)

Fuzzy partition \(\mathbf{U}^T\) (rows sum to one).

labels_ndarray of shape (n_samples,)

Hard labels, membership_.argmax(axis=1).

distances_ndarray of shape (n_samples, n_clusters)

Distances \(D_{ik}\) of the training samples to the prototypes at convergence (algorithm specific norm).

objective_float

Final value of the objective \(J\).

objective_history_ndarray of shape (n_iter_,)

\(J\) after every iteration.

n_iter_int

Number of iterations run.

n_features_in_int
feature_names_in_ndarray of shape (n_features_in_,)

Defined when X has feature names (e.g. a pandas.DataFrame).

fit(X, y=None)[source]#

Compute the clustering.

Parameters:
Xarray_like of shape (n_samples, n_features)

Training data. pandas.DataFrame input is accepted and its column names are stored in feature_names_in_.

yIgnored

Present for API consistency.

Returns:
selfobject

Fitted estimator.

fit_transform(X, y=None, **fit_params)[source]#

Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.

Parameters:
Xarray_like of shape (n_samples, n_features)

Input samples.

yarray_like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).

**fit_paramsdict

Additional fit parameters. Pass only if the estimator accepts additional params in its fit method.

Returns:
X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

predict(X)[source]#

Hard cluster labels of (new) samples (argmax of predict_proba()).

predict_proba(X)[source]#

Membership degrees of (new) samples in the fitted clusters.

Equivalent to clusteval of the MATLAB toolbox: the distances to the fitted prototypes (with their fitted norms) are converted into memberships with the algorithm’s own membership formula.

Returns:
Undarray of shape (n_samples, n_clusters)
score(X, y=None)[source]#

Negative objective \(-J\) on X (higher is better).

transform(X)[source]#

Distances \(D_{ik}\) of samples to the fitted prototypes.

Returns:
Dndarray of shape (n_samples, n_clusters)