Finding groups without labels.
Understand k-means, density-based groups, and why a cluster is not automatically a real category.
Start with the meaning.
Unsupervised learning begins without a target answer for each row. Clustering looks for groups of examples that are similar under a chosen representation and similarity rule. If points on a two-dimensional chart form separated clouds, the groups may be visually clear. In a real table with many columns, “similar” depends on scale, encoding, distance, and which features were included. A cluster label is an output of an algorithm, not a discovered natural kind unless independent evidence supports that interpretation.
K-means asks for a number k of groups. It places k centers, assigns each training point to its nearest center, updates centers to the means of assigned points, and repeats until the assignments settle under its stopping rule. The procedure minimises within-group squared distances. It works best when compact roughly round groups under the chosen distance are a useful description. It can force k groups even when the data has no meaningful grouping.
How the idea works.
Initialization matters because k-means can settle into different local solutions. Implementations often use a smart starting method and may run several starts. Scaling matters because large-unit features dominate distance. The cluster number must be chosen using purpose and evidence, not simply because a plot with that k looks attractive. Silhouette score compares cohesion with separation but can favor geometric shapes that are not meaningful for the domain.
Agglomerative clustering starts with separate points and merges nearby groups step by step, creating a hierarchy. It can show structure at several resolutions, but a linkage rule determines what nearby groups mean. DBSCAN groups points in dense regions and can label sparse points as noise. It can discover irregular shapes and avoids choosing k directly, yet it depends on a neighborhood radius and minimum density and can struggle when densities vary strongly.
Without target labels, evaluation is harder. External labels, when available for analysis, can compare clusters with a known categorization, but the goal may not be to reproduce those categories. A useful cluster analysis should describe its feature choices, stability across reasonable settings, and what actionable or explanatory question the groups answer. A cluster that correlates mostly with data-source artifact is not a useful insight.
Follow the code.
from sklearn.cluster import KMeans
X = [[0.0, 0.0], [0.1, 0.2], [5.0, 5.0], [5.2, 5.1]]
model = KMeans(n_clusters=2, n_init=10, random_state=0)
labels = model.fit_predict(X)
print(labels)
print(model.cluster_centers_)The four rows are points with two coordinates each. Two sit near the origin, and two sit near [5, 5]. KMeans is asked to form two groups. fit_predict performs fitting and returns an integer cluster ID for each row. The two nearby origin points should share an ID, and the two distant points should share the other. The integers 0 and 1 are arbitrary names; swapping them changes no grouping meaning. cluster_centers_ contains the learned center coordinates.
This picture is unusually clear. With overlapping or high-dimensional data, a two-cluster output may still be produced even if no stable separation exists. The algorithm does not know whether the points represent customers, images, or measurements. Interpretation belongs to the analyst and the task.
What the result does not say.
Clustering a dataset and then using the resulting cluster ID as a feature in a supervised model creates a learned transformation. It must be fitted on training data inside the validation procedure, or test information can leak into the model.
A cluster may contain sensitive patterns even when sensitive fields were omitted. Related features can still separate people by demographic or institutional factors. Use clusters as hypotheses to inspect, not labels to impose without scrutiny.
One clear idea.
Clustering groups according to chosen geometry; the meaning of those groups requires independent reasoning.
The ML cookbook
Complete, explained Python workflows sit alongside this reading course.