Class: KMeans
K-Means clustering.
Read more in the User Guide.
Constructors
new KMeans()
new KMeans(
opts?):KMeans
Parameters
| Parameter | Type | Description |
|---|---|---|
opts? | object | - |
opts.algorithm? | "lloyd" | "elkan" | 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). |
opts.copy_x? | boolean | 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. |
opts.init? | ArrayLike[] | "k-means++" | "random" | Method for initialization: |
opts.max_iter? | number | Maximum number of iterations of the k-means algorithm for a single run. |
opts.n_clusters? | number | The number of clusters to form as well as the number of centroids to generate. For an example of how to choose an optimal value for n_clusters refer to Selecting the number of clusters with silhouette analysis on KMeans clustering. |
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. |
opts.random_state? | number | Determines random number generation for centroid initialization. Use an int to make the randomness deterministic. See Glossary. |
opts.tol? | number | Relative tolerance with regards to Frobenius norm of the difference in the cluster centers of two consecutive iterations to declare convergence. |
opts.verbose? | number | Verbosity mode. |
Returns KMeans
Defined in generated/cluster/KMeans.ts:23
Properties
| Property | Type | Default value | Defined in |
|---|---|---|---|
_isDisposed | boolean | false | generated/cluster/KMeans.ts:21 |
_isInitialized | boolean | false | generated/cluster/KMeans.ts:20 |
_py | PythonBridge | undefined | generated/cluster/KMeans.ts:19 |
id | string | undefined | generated/cluster/KMeans.ts:16 |
opts | any | undefined | generated/cluster/KMeans.ts:17 |
Accessors
cluster_centers_
Get Signature
get cluster_centers_():
Promise<ArrayLike[]>
Coordinates of cluster centers. If the algorithm stops before fully converging (see tol and max_iter), these will not be consistent with labels_.
Returns Promise<ArrayLike[]>
Defined in generated/cluster/KMeans.ts:575
feature_names_in_
Get Signature
get feature_names_in_():
Promise<ArrayLike>
Names of features seen during fit. Defined only when X has feature names that are all strings.
Returns Promise<ArrayLike>
Defined in generated/cluster/KMeans.ts:690
inertia_
Get Signature
get inertia_():
Promise<number>
Sum of squared distances of samples to their closest cluster center, weighted by the sample weights if provided.
Returns Promise<number>
Defined in generated/cluster/KMeans.ts:622
labels_
Get Signature
get labels_():
Promise<ArrayLike>
Labels of each point
Returns Promise<ArrayLike>
Defined in generated/cluster/KMeans.ts:600
n_features_in_
Get Signature
get n_features_in_():
Promise<number>
Number of features seen during fit.
Returns Promise<number>
Defined in generated/cluster/KMeans.ts:667
n_iter_
Get Signature
get n_iter_():
Promise<number>
Number of iterations run.
Returns Promise<number>
Defined in generated/cluster/KMeans.ts:645
py
Get Signature
get py():
PythonBridge
Returns PythonBridge
Set Signature
set py(
pythonBridge):void
Parameters
| Parameter | Type |
|---|---|
pythonBridge | PythonBridge |
Returns void
Defined in generated/cluster/KMeans.ts:93
Methods
dispose()
dispose():
Promise<void>
Disposes of the underlying Python resources.
Once dispose() is called, the instance is no longer usable.
Returns Promise<void>
Defined in generated/cluster/KMeans.ts:144
fit()
fit(
opts):Promise<any>
Compute k-means clustering.
Parameters
| Parameter | Type | Description |
|---|---|---|
opts | object | - |
opts.sample_weight? | ArrayLike | The 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.X? | ArrayLike | Training 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.y? | any | Not used, present here for API consistency by convention. |
Returns Promise<any>
Defined in generated/cluster/KMeans.ts:161
fit_predict()
fit_predict(
opts):Promise<ArrayLike>
Compute cluster centers and predict cluster index for each sample.
Convenience method; equivalent to calling fit(X) followed by predict(X).
Parameters
| Parameter | Type | Description |
|---|---|---|
opts | object | - |
opts.sample_weight? | ArrayLike | The weights for each observation in X. If undefined, all observations are assigned equal weight. |
opts.X? | ArrayLike | New data to transform. |
opts.y? | any | Not used, present here for API consistency by convention. |
Returns Promise<ArrayLike>
Defined in generated/cluster/KMeans.ts:205
fit_transform()
fit_transform(
opts):Promise<ArrayLike[]>
Compute clustering and transform X to cluster-distance space.
Equivalent to fit(X).transform(X), but more efficiently implemented.
Parameters
| Parameter | Type | Description |
|---|---|---|
opts | object | - |
opts.sample_weight? | ArrayLike | The weights for each observation in X. If undefined, all observations are assigned equal weight. |
opts.X? | ArrayLike | New data to transform. |
opts.y? | any | Not used, present here for API consistency by convention. |
Returns Promise<ArrayLike[]>
Defined in generated/cluster/KMeans.ts:249
get_feature_names_out()
get_feature_names_out(
opts):Promise<any>
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"\].
Parameters
| Parameter | Type | Description |
|---|---|---|
opts | object | - |
opts.input_features? | any | Only used to validate feature names with the names seen in fit. |
Returns Promise<any>
Defined in generated/cluster/KMeans.ts:293
get_metadata_routing()
get_metadata_routing(
opts):Promise<any>
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
Parameters
| Parameter | Type | Description |
|---|---|---|
opts | object | - |
opts.routing? | any | A MetadataRequest encapsulating routing information. |
Returns Promise<any>
Defined in generated/cluster/KMeans.ts:327
init()
init(
py):Promise<void>
Initializes the underlying Python resources.
This instance is not usable until the Promise returned by init() resolves.
Parameters
| Parameter | Type |
|---|---|
py | PythonBridge |
Returns Promise<void>
Defined in generated/cluster/KMeans.ts:106
predict()
predict(
opts):Promise<ArrayLike>
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.
Parameters
| Parameter | Type | Description |
|---|---|---|
opts | object | - |
opts.X? | ArrayLike | New data to predict. |
Returns Promise<ArrayLike>
Defined in generated/cluster/KMeans.ts:361
score()
score(
opts):Promise<number>
Opposite of the value of X on the K-means objective.
Parameters
| Parameter | Type | Description |
|---|---|---|
opts | object | - |
opts.sample_weight? | ArrayLike | The weights for each observation in X. If undefined, all observations are assigned equal weight. |
opts.X? | ArrayLike | New data. |
opts.y? | any | Not used, present here for API consistency by convention. |
Returns Promise<number>
Defined in generated/cluster/KMeans.ts:393
set_fit_request()
set_fit_request(
opts):Promise<any>
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:
Parameters
| Parameter | Type | Description |
|---|---|---|
opts | object | - |
opts.sample_weight? | string | boolean | Metadata routing for sample_weight parameter in fit. |
Returns Promise<any>
Defined in generated/cluster/KMeans.ts:439
set_output()
set_output(
opts):Promise<any>
Set output container.
See Introducing the set_output API for an example on how to use the API.
Parameters
| Parameter | Type | Description |
|---|---|---|
opts | object | - |
opts.transform? | "default" | "pandas" | "polars" | Configure output of transform and fit_transform. |
Returns Promise<any>
Defined in generated/cluster/KMeans.ts:473
set_score_request()
set_score_request(
opts):Promise<any>
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:
Parameters
| Parameter | Type | Description |
|---|---|---|
opts | object | - |
opts.sample_weight? | string | boolean | Metadata routing for sample_weight parameter in score. |
Returns Promise<any>
Defined in generated/cluster/KMeans.ts:509
transform()
transform(
opts):Promise<ArrayLike[]>
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.
Parameters
| Parameter | Type | Description |
|---|---|---|
opts | object | - |
opts.X? | ArrayLike | New data to transform. |
Returns Promise<ArrayLike[]>
Defined in generated/cluster/KMeans.ts:543