Functional and Object-oriented APIs

All implemented CVIs take as mandatory input a dataset X and a clustering clusters. In addition, all implemented CVIs take as optional parameter a dictionary of keyword arguments dist_kwargs for the distance function used to compute pairwise distances between datapoints.

  • If the dataset X is time-series data and if ts_dist=True, the distance function is based on aeon.distances.pairwise_distance. In that case, the dist_kwargs keyword argument can include parameters such as method, window or itakura_max_slope.

  • Otherwise, the distance function used is based on scipy.spatial.distance.pdist. In that case, dist_kwargs can define the same parameters as this function.

In addition, some CVI functions take additional optional parameters, which can be specified when using the __call__ method of the corresponding CVI class via the cvi_kwargs keyword argument. Below is an example of the correspondance between the functional API (pycvi.cvi_func.silhouette()) and the object-oriented API (pycvi.cvi.Silhouette) for the silhouette CVI, but the same principle applies to all CVIs.

 1
 2from sklearn.cluster import KMeans
 3from sklearn.preprocessing import StandardScaler
 4from pycvi.cvi import Silhouette
 5from pycvi.cvi_func import silhouette
 6from pycvi.datasets.benchmark import load_data
 7from pycvi.cluster import get_clustering
 8
 9# -------------- Standard data handling operations ---------------------
10# Load data
11data, labels = load_data("xclara", "barton")
12
13# Data pre-processing
14scaler = StandardScaler()
15X = scaler.fit_transform(data)
16
17# ---------- Fit a clustering model and make predictions ---------------
18# Assumed number of clusters
19k = 3
20
21# Train and predict a KMeans model
22model = KMeans(n_clusters=k)
23labels_pred = model.fit_predict(X)
24
25# From predicted cluster-label for each datapoint to a list of
26# datapoints for each cluster.
27clusters_pred = get_clustering(labels_pred)
28
29# ---------------- Using Object-oriented API -----------------------
30# Instanciate a CVI instance, could be any class defined in pycvi.cvi
31cvi = Silhouette()
32cvi_kwargs = {"dist_kwargs": {"metric": "minkowski", "p": 3}}
33cvi_value = cvi(X, clusters_pred, cvi_kwargs=cvi_kwargs)
34print(f"OOP API           |  CVI value: {cvi_value:.4f}")
35
36# ---------------- Using Functional API -----------------------
37dist_kwargs = {"metric": "minkowski", "p": 3}
38cvi_value = silhouette(X, clusters_pred, dist_kwargs=dist_kwargs)
39print(f"Functional API    |  CVI value: {cvi_value:.4f}")
40
OOP API           |  CVI value: 0.6851
Functional API    |  CVI value: 0.6851