snowflake.ml.modeling.cluster.BisectingKMeans

class snowflake.ml.modeling.cluster.BisectingKMeans(*, n_clusters=8, init='random', n_init=1, random_state=None, max_iter=300, verbose=0, tol=0.0001, copy_x=True, algorithm='lloyd', bisecting_strategy='biggest_inertia', input_cols: Optional[Union[str, Iterable[str]]] = None, output_cols: Optional[Union[str, Iterable[str]]] = None, label_cols: Optional[Union[str, Iterable[str]]] = None, passthrough_cols: Optional[Union[str, Iterable[str]]] = None, drop_input_cols: Optional[bool] = False, sample_weight_col: Optional[str] = None)

Bases: BaseTransformer

Bisecting K-Means clustering For more details on this class, see sklearn.cluster.BisectingKMeans

input_cols: Optional[Union[str, List[str]]]

A string or list of strings representing column names that contain features. If this parameter is not specified, all columns in the input DataFrame except the columns specified by label_cols, sample_weight_col, and passthrough_cols parameters are considered input columns. Input columns can also be set after initialization with the set_input_cols method.

label_cols: Optional[Union[str, List[str]]]

This parameter is optional and will be ignored during fit. It is present here for API consistency by convention.

output_cols: Optional[Union[str, List[str]]]

A string or list of strings representing column names that will store the output of predict and transform operations. The length of output_cols must match the expected number of output columns from the specific predictor or transformer class used. If you omit this parameter, output column names are derived by adding an OUTPUT_ prefix to the label column names for supervised estimators, or OUTPUT_<IDX>for unsupervised estimators. These inferred output column names work for predictors, but output_cols must be set explicitly for transformers. In general, explicitly specifying output column names is clearer, especially if you don’t specify the input column names. To transform in place, pass the same names for input_cols and output_cols. be set explicitly for transformers. Output columns can also be set after initialization with the set_output_cols method.

sample_weight_col: Optional[str]

A string representing the column name containing the sample weights. This argument is only required when working with weighted datasets. Sample weight column can also be set after initialization with the set_sample_weight_col method.

passthrough_cols: Optional[Union[str, List[str]]]

A string or a list of strings indicating column names to be excluded from any operations (such as train, transform, or inference). These specified column(s) will remain untouched throughout the process. This option is helpful in scenarios requiring automatic input_cols inference, but need to avoid using specific columns, like index columns, during training or inference. Passthrough columns can also be set after initialization with the set_passthrough_cols method.

drop_input_cols: Optional[bool], default=False

If set, the response of predict(), transform() methods will not contain input columns.

n_clusters: int, default=8

The number of clusters to form as well as the number of centroids to generate.

init: {‘k-means++’, ‘random’} or callable, default=’random’

Method for initialization:

‘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.

n_init: int, default=1

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.

random_state: int, RandomState instance or None, default=None

Determines random number generation for centroid initialization in inner K-Means. Use an int to make the randomness deterministic. See Glossary.

max_iter: int, default=300

Maximum number of iterations of the inner k-means algorithm at each bisection.

verbose: int, default=0

Verbosity mode.

tol: float, default=1e-4

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.

copy_x: bool, default=True

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.

algorithm: {“lloyd”, “elkan”}, default=”lloyd”

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).

bisecting_strategy: {“biggest_inertia”, “largest_cluster”}, default=”biggest_inertia”

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.

Methods

fit(dataset)

Compute bisecting k-means clustering For more details on this function, see sklearn.cluster.BisectingKMeans.fit

fit_predict(dataset)

Compute cluster centers and predict cluster index for each sample For more details on this function, see sklearn.cluster.BisectingKMeans.fit_predict

get_input_cols()

Input columns getter.

get_label_cols()

Label column getter.

get_output_cols()

Output columns getter.

get_params([deep])

Get parameters for this transformer.

get_passthrough_cols()

Passthrough columns getter.

get_sample_weight_col()

Sample weight column getter.

get_sklearn_args([default_sklearn_obj, ...])

Get sklearn keyword arguments.

predict(dataset)

Predict which cluster each sample in X belongs to For more details on this function, see sklearn.cluster.BisectingKMeans.predict

score(dataset)

Opposite of the value of X on the K-means objective For more details on this function, see sklearn.cluster.BisectingKMeans.score

set_drop_input_cols([drop_input_cols])

set_input_cols(input_cols)

Input columns setter.

set_label_cols(label_cols)

Label column setter.

set_output_cols(output_cols)

Output columns setter.

set_params(**params)

Set the parameters of this transformer.

set_passthrough_cols(passthrough_cols)

Passthrough columns setter.

set_sample_weight_col(sample_weight_col)

Sample weight column setter.

to_sklearn()

Get sklearn.cluster.BisectingKMeans object.

transform(dataset)

Transform X to a cluster-distance space For more details on this function, see sklearn.cluster.BisectingKMeans.transform

Attributes

model_signatures

Returns model signature of current class.