BisectingKMeans
- class sklearn.cluster.BisectingKMeans(n_clusters=8, *, init='random', n_init=1, random_state=None, max_iter=300, verbose=, , , ,
Bisecting K-Means clustering.
Read more in the User Guide.
The number of clusters to form as well as the number of centroids to generate.
Method for initialization for each bisection.
‘k-means++’ : selects initial cluster centers for k-mean clustering in a smart way to speed up convergence. See section Notes in k_init for more details.
‘random’: choose n_clusters observations (rows) at random from data
for the initial centroids.
If a callable is passed, it should take arguments X, n_clusters and a
random state and return an initialization. Note that the bisecting algorithm
always performs a 2-way split, so the callable will always be called with
n_clusters=2 and should return 2 centroids.
Number of time the inner k-means algorithm will be run with different centroid seeds in each bisection. That will result producing for each bisection best output of n_init consecutive runs in terms of inertia.
Determines random number generation for centroid initialization in inner K-Means. Use an int to make the randomness deterministic. See Glossary.
Maximum number of iterations of the inner k-means algorithm at each bisection.
Verbosity mode.
Relative tolerance with regards to Frobenius norm of the difference in the cluster centers of two consecutive iterations to declare convergence. Used in inner k-means algorithm at each bisection to pick best possible clusters.
When pre-computing distances it is more numerically accurate to center the data first. If copy_x is True (default), then the original data is not modified. If False, the original data is modified, and put back before the function returns, but small numerical differences may be introduced by subtracting and then adding the data mean. Note that if the original data is not C-contiguous, a copy will be made even if copy_x is False. If the original data is sparse, but not in CSR format, a copy will be made even if copy_x is False.
Inner K-means algorithm used in bisection.
The classical EM-style algorithm is "lloyd".
The "elkan" variation can be more efficient on some datasets with
well-defined clusters, by using the triangle inequality. However it’s
more memory intensive due to the allocation of an extra array of shape
(n_samples, n_clusters).
Defines how bisection should be performed:
“biggest_inertia” means that BisectingKMeans will always check all calculated cluster for cluster with biggest SSE (Sum of squared errors) and bisect it. This approach concentrates on precision, but may be costly in terms of execution time (especially for larger amount of data points).
“largest_cluster” - BisectingKMeans will always split cluster with largest amount of points assigned to it from all clusters previously calculated. That should work faster than picking by SSE (‘biggest_inertia’) and may produce similar results in most cases.
Coordinates of cluster centers. If the algorithm stops before fully
converging (see tol and max_iter), these will not be
consistent with labels_.
Labels of each point.
Sum of squared distances of samples to their closest cluster center, weighted by the sample weights if provided.
Number of features seen during fit.
It might be inefficient when n_cluster is less than 3, due to unnecessary calculations for that case.
>>> from sklearn.cluster import BisectingKMeans
>>> import numpy as np
>>> X = np.array([[1, 1], [10, 1], [3, 1],
... [
For a comparison between BisectingKMeans and K-Means refer to example Bisecting K-Means and Regular K-Means Performance Comparison.
Compute bisecting k-means clustering.
Training instances to cluster.
Compute cluster centers and predict cluster index for each sample.
Convenience method; equivalent to calling fit(X) followed by predict(X).
New data to transform.
Not used, present here for API consistency by convention.
Compute clustering and transform X to cluster-distance space.
Equivalent to fit(X).transform(X), but more efficiently implemented.
New data to transform.
Not used, present here for API consistency by convention.
Get output feature names for transformation.
The feature names out will prefixed by the lowercased class name. For
example, if the transformer outputs 3 features, then the feature names
out are: ["class_name0", "class_name1", "class_name2"].
Only used to validate feature names with the names seen in fit.
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
A MetadataRequest encapsulating
routing information.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Predict which cluster each sample in X belongs to.
Prediction is made by going down the hierarchical tree in searching of closest leaf cluster.
In the vector quantization literature, cluster_centers_ is called
the code book and each value returned by predict is the index of
the closest code in the code book.
New data to predict.
Opposite of the value of X on the K-means objective.
New data.
Not used, present here for API consistency by convention.
Configure whether metadata should be requested to be passed to the method.
Set output container.
Refer to the user guide for more details and Introducing the set_output API for an example on how to use the API.
Configure output of transform and .
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.
Estimator parameters.
Configure whether metadata should be requested to be passed to the method.
Transform X to a cluster-distance space.
In the new space, each dimension is the distance to the cluster
centers. Note that even if X is sparse, the array returned by
transform will typically be dense.
New data to transform.
X transformed in the new space.
Bisecting K-Means and Regular K-Means Performance Comparison
n_features_in_,)Names of features seen during fit. Defined only when X
has feature names that are all strings.
Not used, present here for API consistency by convention.
The weights for each observation in X. If None, all observations
are assigned equal weight. sample_weight is not used during
initialization if init is a callable.
Fitted estimator.
The weights for each observation in X. If None, all observations are assigned equal weight.
Index of the cluster each sample belongs to.
The weights for each observation in X. If None, all observations are assigned equal weight.
X transformed in the new space.
Transformed feature names.
Index of the cluster each sample belongs to.
The weights for each observation in X. If None, all observations are assigned equal weight.
Opposite of the value of X on the K-means objective.
Note that this method is only relevant when this estimator is used as a
sub-estimator within a meta-estimator and metadata routing is enabled
with enable_metadata_routing=True (see sklearn.set_config).
Please check the User Guide on how the routing
mechanism works.
The options for each parameter are:
True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.
False: metadata is not requested and the meta-estimator will not pass it to fit.
None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (sklearn.utils.metadata_routing.UNCHANGED) retains the
existing request. This allows you to change the request for some
parameters and not others.
Metadata routing for sample_weight parameter in fit.
The updated object.
"default": Default output format of a transformer
"pandas": DataFrame output
"polars": Polars output
None: Transform configuration is unchanged
Estimator instance.
Estimator instance.
Note that this method is only relevant when this estimator is used as a
sub-estimator within a meta-estimator and metadata routing is enabled
with enable_metadata_routing=True (see sklearn.set_config).
Please check the User Guide on how the routing
mechanism works.
The options for each parameter are:
True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.
False: metadata is not requested and the meta-estimator will not pass it to score.
None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (sklearn.utils.metadata_routing.UNCHANGED) retains the
existing request. This allows you to change the request for some
parameters and not others.
Metadata routing for sample_weight parameter in score.
The updated object.