Documentation
Classes
KMeans

KMeans

K-Means clustering.

Read more in the User Guide.

Python Reference (opens in a new tab)

Constructors

constructor()

Signature

new KMeans(opts?: object): KMeans;

Parameters

NameTypeDescription
opts?object-
opts.algorithm?"auto" | "lloyd" | "elkan" | "full"K-means algorithm to use. 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). "auto" and "full" are deprecated and they will be removed in Scikit-Learn 1.3. They are both aliases for "lloyd". Default Value 'lloyd'
opts.copy_x?booleanWhen 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. Default Value true
opts.init?ArrayLike[] | "k-means++" | "random"Method for initialization: Default Value 'k-means++'
opts.max_iter?numberMaximum number of iterations of the k-means algorithm for a single run. Default Value 300
opts.n_clusters?numberThe number of clusters to form as well as the number of centroids to generate. Default Value 8
opts.n_init?number | "auto"Number of times the k-means algorithm is run with different centroid seeds. The final results is the best output of n\_init consecutive runs in terms of inertia. Several runs are recommended for sparse high-dimensional problems (see Clustering sparse data with k-means). When n\_init='auto', the number of runs depends on the value of init: 10 if using init='random' or init is a callable; 1 if using init='k-means++' or init is an array-like. Default Value 10
opts.random_state?numberDetermines random number generation for centroid initialization. Use an int to make the randomness deterministic. See Glossary.
opts.tol?numberRelative tolerance with regards to Frobenius norm of the difference in the cluster centers of two consecutive iterations to declare convergence. Default Value 0.0001
opts.verbose?numberVerbosity mode. Default Value 0

Returns

KMeans

Defined in: generated/cluster/KMeans.ts:23 (opens in a new tab)

Methods

dispose()

Disposes of the underlying Python resources.

Once dispose() is called, the instance is no longer usable.

Signature

dispose(): Promise<void>;

Returns

Promise<void>

Defined in: generated/cluster/KMeans.ts:153 (opens in a new tab)

fit()

Compute k-means clustering.

Signature

fit(opts: object): Promise<any>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLikeTraining instances to cluster. It must be noted that the data will be converted to C ordering, which will cause a memory copy if the given data is not C-contiguous. If a sparse matrix is passed, a copy will be made if it’s not in CSR format.
opts.sample_weight?ArrayLikeThe weights for each observation in X. If undefined, all observations are assigned equal weight. sample\_weight is not used during initialization if init is a callable or a user provided array.
opts.y?anyNot used, present here for API consistency by convention.

Returns

Promise<any>

Defined in: generated/cluster/KMeans.ts:170 (opens in a new tab)

fit_predict()

Compute cluster centers and predict cluster index for each sample.

Convenience method; equivalent to calling fit(X) followed by predict(X).

Signature

fit_predict(opts: object): Promise<ArrayLike>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLikeNew data to transform.
opts.sample_weight?ArrayLikeThe weights for each observation in X. If undefined, all observations are assigned equal weight.
opts.y?anyNot used, present here for API consistency by convention.

Returns

Promise<ArrayLike>

Defined in: generated/cluster/KMeans.ts:219 (opens in a new tab)

fit_transform()

Compute clustering and transform X to cluster-distance space.

Equivalent to fit(X).transform(X), but more efficiently implemented.

Signature

fit_transform(opts: object): Promise<ArrayLike[]>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLikeNew data to transform.
opts.sample_weight?ArrayLikeThe weights for each observation in X. If undefined, all observations are assigned equal weight.
opts.y?anyNot used, present here for API consistency by convention.

Returns

Promise<ArrayLike[]>

Defined in: generated/cluster/KMeans.ts:268 (opens in a new tab)

get_feature_names_out()

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"\].

Signature

get_feature_names_out(opts: object): Promise<any>;

Parameters

NameTypeDescription
optsobject-
opts.input_features?anyOnly used to validate feature names with the names seen in fit.

Returns

Promise<any>

Defined in: generated/cluster/KMeans.ts:317 (opens in a new tab)

get_metadata_routing()

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Signature

get_metadata_routing(opts: object): Promise<any>;

Parameters

NameTypeDescription
optsobject-
opts.routing?anyA MetadataRequest encapsulating routing information.

Returns

Promise<any>

Defined in: generated/cluster/KMeans.ts:352 (opens in a new tab)

init()

Initializes the underlying Python resources.

This instance is not usable until the Promise returned by init() resolves.

Signature

init(py: PythonBridge): Promise<void>;

Parameters

NameType
pyPythonBridge

Returns

Promise<void>

Defined in: generated/cluster/KMeans.ts:106 (opens in a new tab)

predict()

Predict the closest cluster each sample in X belongs to.

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.

Signature

predict(opts: object): Promise<ArrayLike>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLikeNew data to predict.
opts.sample_weight?ArrayLikeThe weights for each observation in X. If undefined, all observations are assigned equal weight.

Returns

Promise<ArrayLike>

Defined in: generated/cluster/KMeans.ts:387 (opens in a new tab)

score()

Opposite of the value of X on the K-means objective.

Signature

score(opts: object): Promise<number>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLikeNew data.
opts.sample_weight?ArrayLikeThe weights for each observation in X. If undefined, all observations are assigned equal weight.
opts.y?anyNot used, present here for API consistency by convention.

Returns

Promise<number>

Defined in: generated/cluster/KMeans.ts:427 (opens in a new tab)

set_fit_request()

Request metadata passed to the fit method.

Note that this method is only relevant if enable\_metadata\_routing=True (see sklearn.set\_config). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

Signature

set_fit_request(opts: object): Promise<any>;

Parameters

NameTypeDescription
optsobject-
opts.sample_weight?string | booleanMetadata routing for sample\_weight parameter in fit.

Returns

Promise<any>

Defined in: generated/cluster/KMeans.ts:478 (opens in a new tab)

set_output()

Set output container.

See Introducing the set_output API for an example on how to use the API.

Signature

set_output(opts: object): Promise<any>;

Parameters

NameTypeDescription
optsobject-
opts.transform?"default" | "pandas"Configure output of transform and fit\_transform.

Returns

Promise<any>

Defined in: generated/cluster/KMeans.ts:513 (opens in a new tab)

set_predict_request()

Request metadata passed to the predict method.

Note that this method is only relevant if enable\_metadata\_routing=True (see sklearn.set\_config). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

Signature

set_predict_request(opts: object): Promise<any>;

Parameters

NameTypeDescription
optsobject-
opts.sample_weight?string | booleanMetadata routing for sample\_weight parameter in predict.

Returns

Promise<any>

Defined in: generated/cluster/KMeans.ts:550 (opens in a new tab)

set_score_request()

Request metadata passed to the score method.

Note that this method is only relevant if enable\_metadata\_routing=True (see sklearn.set\_config). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

Signature

set_score_request(opts: object): Promise<any>;

Parameters

NameTypeDescription
optsobject-
opts.sample_weight?string | booleanMetadata routing for sample\_weight parameter in score.

Returns

Promise<any>

Defined in: generated/cluster/KMeans.ts:587 (opens in a new tab)

transform()

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.

Signature

transform(opts: object): Promise<ArrayLike[]>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLikeNew data to transform.

Returns

Promise<ArrayLike[]>

Defined in: generated/cluster/KMeans.ts:622 (opens in a new tab)

Properties

_isDisposed

boolean = false

Defined in: generated/cluster/KMeans.ts:21 (opens in a new tab)

_isInitialized

boolean = false

Defined in: generated/cluster/KMeans.ts:20 (opens in a new tab)

_py

PythonBridge

Defined in: generated/cluster/KMeans.ts:19 (opens in a new tab)

id

string

Defined in: generated/cluster/KMeans.ts:16 (opens in a new tab)

opts

any

Defined in: generated/cluster/KMeans.ts:17 (opens in a new tab)

Accessors

cluster_centers_

Coordinates of cluster centers. If the algorithm stops before fully converging (see tol and max\_iter), these will not be consistent with labels\_.

Signature

cluster_centers_(): Promise<ArrayLike[]>;

Returns

Promise<ArrayLike[]>

Defined in: generated/cluster/KMeans.ts:655 (opens in a new tab)

feature_names_in_

Names of features seen during fit. Defined only when X has feature names that are all strings.

Signature

feature_names_in_(): Promise<ArrayLike>;

Returns

Promise<ArrayLike>

Defined in: generated/cluster/KMeans.ts:770 (opens in a new tab)

inertia_

Sum of squared distances of samples to their closest cluster center, weighted by the sample weights if provided.

Signature

inertia_(): Promise<number>;

Returns

Promise<number>

Defined in: generated/cluster/KMeans.ts:702 (opens in a new tab)

labels_

Labels of each point

Signature

labels_(): Promise<ArrayLike>;

Returns

Promise<ArrayLike>

Defined in: generated/cluster/KMeans.ts:680 (opens in a new tab)

n_features_in_

Number of features seen during fit.

Signature

n_features_in_(): Promise<number>;

Returns

Promise<number>

Defined in: generated/cluster/KMeans.ts:747 (opens in a new tab)

n_iter_

Number of iterations run.

Signature

n_iter_(): Promise<number>;

Returns

Promise<number>

Defined in: generated/cluster/KMeans.ts:725 (opens in a new tab)

py

Signature

py(): PythonBridge;

Returns

PythonBridge

Defined in: generated/cluster/KMeans.ts:93 (opens in a new tab)

Signature

py(pythonBridge: PythonBridge): void;

Parameters

NameType
pythonBridgePythonBridge

Returns

void

Defined in: generated/cluster/KMeans.ts:97 (opens in a new tab)