Hierarchical clustering with Dlib

The Dlib library implements the agglomerative hierarchical (bottom-up) clustering algorithm. The bottom_up_cluster() function implements this algorithm. This function takes the matrix of distances between dataset objects, the cluster indices container (as the output parameter), and the number of clusters as input parameters. Notice that it returns the container with cluster indices in the order of distances provided in the matrix.

In the following code sample, we fill the distance matrix with pairwise Euclidean distances between each pair of elements in the input dataset:

 matrix<double> dists(inputs.nr(), inputs.nr());
for (long r = 0; r < dists.nr(); ++r) {
for (long c = 0; c < dists.nc(); ++c) {
dists(r, c) = length(subm(inputs, r, 0, 1, 2) - subm(inputs, c, 0,
1, 2));
}
}
std::vector<unsigned long> clusters;
bottom_up_cluster(dists, clusters, num_clusters);

The bottom_up_cluster() function call filled the clusters object with cluster index values, which we can use to visualize the clustering result, as illustrated in the following screenshot:

In the preceding screenshot, we can see how the hierarchical clustering algorithm implemented in the Dlib library works on different artificial datasets.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.40.189