Add Clusters

Cluster paths using ML and add a new segment column with integer cluster labels.

Per-path metrics are computed from features, optionally scaled, then passed to the chosen clustering algorithm. The resulting cluster label is broadcast to every row of the corresponding path.

Usage

stream.add_clusters(
    name="cluster",
    features=[
        {"metric": "length"},
        {"metric": "event_count", "metric_args": {"events": "purchase"}},
    ],
    method="kmeans",
    n_clusters=4,
    scaler="minmax",
)

Parameters

ParameterTypeDescription
namestrName of the new segment column to add.
featureslist of dictMetric configurations used as clustering features. Each dict has a "metric" key (str) and an optional "metric_args" key (dict). Available metrics: "length", "duration", "event_count", "has_event", "time_between", "first_event_time", "active_days", "matches_pattern", "in_segment". See the Path Metrics documentation page for the full metric reference.
methodstr, default "kmeans"``Clustering algorithm. One of "kmeans" or "hdbscan".
scalerstr or None, default None``Feature scaler applied before clustering. One of "minmax", "standard", or None.
n_clustersint, optionalNumber of clusters (required for "kmeans").
min_cluster_sizeint, optionalMinimum cluster size (used by "hdbscan").
cluster_selection_epsilonfloat, optionalHDBSCAN cluster-selection epsilon.
nmf_componentsint, optionalWhen set, reduces features to this many NMF components before clustering.
path_colstr, optionalPath ID column override; defaults to schema.path_col.
event_colstr, optionalEvent column override; defaults to schema.event_col.