Note

This page is a reference documentation. It only explains the class signature, and not how to use it. Please refer to the user guide for the big picture.

nilearn.regions.HierarchicalKMeans#

class nilearn.regions.HierarchicalKMeans(n_clusters, init='k-means++', batch_size=1000, n_init=10, max_no_improvement=10, verbose=0, random_state=0, scaling=False)[source]#

Hierarchical KMeans: First clusterize the samples into big clusters. Then clusterize the samples inside these big clusters into smaller ones.

Parameters
n_clusters: int

The number of clusters to find.

init{‘k-means++’, ‘random’ or an ndarray}

Method for initialization, defaults to ‘k-means++’:

  • ‘k-means++’ : selects initial cluster centers for k-means clustering in a smart way to speed up convergence. See section Notes in k_init for more details.

  • ‘random’: choose k observations (rows) at random from data for the initial centroids.

  • If an ndarray is passed, it should be of shape (n_clusters, n_features) and gives the initial centers.

batch_sizeint, optional, default: 1000

Size of the mini batches. (Kmeans performed through MiniBatchKMeans)

n_initint, default=10

Number of random initializations that are tried. In contrast to KMeans, the algorithm is only run once, using the best of the n_init initializations as measured by inertia.

max_no_improvementint, default: 10

Control early stopping based on the consecutive number of mini batches that does not yield an improvement on the smoothed inertia. To disable convergence detection based on inertia, set max_no_improvement to None.

random_stateint, RandomState instance or None (default)

Determines random number generation for centroid initialization and random reassignment. Use an int to make the randomness deterministic.

scaling: bool, optional (default False)

If scaling is True, each cluster is scaled by the square root of its size during transform(), preserving the l2-norm of the image. inverse_transform() will apply inversed scaling to yield an image with same l2-norm as input.

verbose: int, optional (default 0)

Verbosity level.

Attributes
`labels_ `: ndarray, shape = [n_features]

cluster labels for each feature.

`sizes_`: ndarray, shape = [n_features]

It contains the size of each cluster.

__init__(n_clusters, init='k-means++', batch_size=1000, n_init=10, max_no_improvement=10, verbose=0, random_state=0, scaling=False)[source]#
fit(X, y=None)[source]#

Compute clustering of the data.

Parameters
X: ndarray, shape = [n_features, n_samples]

Training data.

y: Ignored
Returns
self
transform(X, y=None)[source]#

Apply clustering, reduce the dimensionality of the data.

Parameters
X: ndarray, shape = [n_features, n_samples]

Data to transform with the fitted clustering.

Returns
X_red: ndarray, shape = [n_clusters, n_samples]

Data reduced with agglomerated signal for each cluster

inverse_transform(X_red)[source]#

Send the reduced 2D data matrix back to the original feature space (voxels).

Parameters
X_red: ndarray , shape = [n_clusters, n_samples]

Data reduced with agglomerated signal for each cluster

Returns
X_inv: ndarray, shape = [n_features, n_samples]

Data reduced expanded to the original feature space

fit_predict(X, y=None)#

Perform clustering on X and returns cluster labels.

Parameters
Xarray-like of shape (n_samples, n_features)

Input data.

yIgnored

Not used, present for API consistency by convention.

Returns
labelsndarray of shape (n_samples,), dtype=np.int64

Cluster labels.

fit_transform(X, y=None, **fit_params)#

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.

Returns
X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_params(deep=True)#

Get parameters for this estimator.

Parameters
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns
paramsdict

Parameter names mapped to their values.

set_params(**params)#

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters
**paramsdict

Estimator parameters.

Returns
selfestimator instance

Estimator instance.