Comparing scikit learn clusterings using a decision tree
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Clustering is an essential technique in unsupervised learning, commonly used for pattern recognition and data segmentation. In scientific and industrial applications, it’s often necessary to determine not only optimal groupings but also to compare different clustering outputs for further analysis and insights. This is where decision trees can be a versatile tool for evaluation. In this article, we'll discuss how to use a decision tree to compare clusterings generated by Scikit-learn
, a popular machine learning library in Python.
Clustering in Scikit-learn
Scikit-learn offers a variety of clustering algorithms, each suitable for different types of data:
- K-Means: Partitions data points into a predefined number of non-overlapping clusters.
- Hierarchical Clustering: Builds a multilevel hierarchy of clusters by creating a tree of clusters.
- DBSCAN: Groups together points that are close to each other based on a distance measurement and a minimum number of points.
- Gaussian Mixture Models (GMM): Represents the data as a mixture of several Gaussian distributions.
Why Use Decision Trees for Comparison?
Decision Trees are interpretative models that can highlight differences between the clustering results based on feature space. By training a decision tree using cluster labels as targets, you can gain insights into which features best differentiate the solutions provided by different clustering algorithms.
Technical Implementation
Let’s consider a practical example using Scikit-learn
to illustrate how a decision tree can be employed to compare different clustering solutions.
Dataset
We’ll use the popular Iris
dataset from Scikit-learn, which contains morphological data of iris flowers:
- Advantages:
- Clarity: Decision trees offer a clear and interpretable evaluation of feature importance.
- Flexibility: They can handle various types of approaches to clustering, including hierarchical and density-based methods.
- Limitations:
- Oversimplification: Decision trees might not capture all intricacies in feature contributions, especially when features interact in complex ways.
- Sensitivity: The model is sensitive to small changes in data, which might alter tree structure and interpretations.
Related reading
- Comparison-based ranking algorithm
- Complex dataset split - StratifiedGroupShuffleSplit
- Compress a TensorFlow model
- Compute gradient norm of each part of a composite loss function
- Comparing two NumPy arrays for equality, element-wise
- Compute rolling maximum drawdown of pandas Series
- Comparing two byte arrays in .NET
- Comparing two collections for equality irrespective of the order of items in them

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.