CVI - Basic usage with time-series
In this example, we integrate PyCVI into the usual clustering pipeline with time series data in order to select the best clustering while using DTW as the distance measure and DBA as the cluster center.
If you wish to run the example scripts on your own computer, please first follow the instructions detailed in Running example scripts on your computer.
from aeon.clustering import TimeSeriesKMeans
from sklearn.preprocessing import MinMaxScaler
from pycvi.cvi import Dunn
from pycvi.datasets.benchmark import load_data
from pycvi.cluster import get_clustering
from pycvi_examples_utils import plot_true_selected
# -------------- Standard data handling operations ---------------------
# Load data
data, labels = load_data("Trace", "ucr")
(N, T, d) = data.shape
# Data pre-processing
scaler = MinMaxScaler()
# Scaling for each variable and not time step wise
X = scaler.fit_transform(data.reshape(N*T, d)).reshape(N, T, d)
# CVI to use, could be any class defined in pycvi.cvi
cvi = Dunn()
# ---------- Integrating PyCVI in the clustering pipeline --------------
# ------ 1. Compute CVI values of the generated clusterings ------------
# ------ 2. Select the best clustering according to the CVI ------------
clusterings = {}
cvi_values = {}
k_range = range(2, 10)
for k in k_range:
# Generate the clusters assuming that there are k clusters
# Clustering model to use, could be any sklearn-like clustering class
model = TimeSeriesKMeans(n_clusters=k)
labels_pred = model.fit_predict(X)
# From predicted cluster-label for each datapoint to a list of
# datapoints for each cluster.
clusters_pred = get_clustering(labels_pred)
# Compute the CVI value of this clustering
cvi_value = cvi(X, clusters_pred)
# Store clustering and CVI value
clusterings[k] = clusters_pred
cvi_values[k] = cvi_value
print(f"k={k} | CVI value:{cvi_value}")
k_selected = cvi.select(cvi_values)
print(f"k selected: {k_selected}")
# ---------------------- Summmary fig ----------------------------------
clustering_true = get_clustering(labels)
fig = plot_true_selected(data, clustering_true, clusterings[k_selected])
fig_title = "KMeans clustering with Dunn score"
fig_name = "basic_usage_TS_KMeans_Dunn.png"
fig.suptitle(fig_title)
fig.savefig(fig_name)
k=2 | CVI value:0.8305380457449938
k=3 | CVI value:0.004036096418553552
k=4 | CVI value:0.004344772249537684
k=5 | CVI value:0.004344772249537684
k=6 | CVI value:0.0044550605885709344
k=7 | CVI value:0.0044550605885709344
k=8 | CVI value:0.004061202849919142
k=9 | CVI value:0.004616167556829456
k selected: 2