Machine Learning

Machine learning for unlabeled data: a clustering example and how to check it

Explore unlabeled data with a runnable clustering example. Understand cluster IDs, scaling, useful checks and when you still need human labels.

Machine learning for unlabeled data looks for structure in examples without a supplied target answer for each row. Clustering can group similar rows; dimensionality reduction can summarise variation; anomaly methods can flag unusual observations. The absence of labels changes what you can claim about the result.

A cluster is a grouping produced by a method under particular choices. It does not arrive with a verified business meaning. This guide uses six invented project sessions to show the distinction with complete, inline Python code.

Choose a question before choosing a method

Your questionPossible approachWhat the output does not establish
Which sessions look similar?ClusteringThat the groups are meaningful learner types
Can I view many measurements in fewer dimensions?Dimensionality reductionThat the picture preserves every useful difference
Which rows deserve inspection?Outlier detectionThat a flagged row is wrong or harmful
Which outcome will happen next?Usually needs labelled outcomes for supervised predictionClustering alone cannot supply the missing answers

If you need a numerical forecast or a class prediction, first ask whether outcomes can be collected. Read supervised vs unsupervised learning for the task distinction and outlier detection for inspection rather than grouping.

A small clustering example

Each row below represents minutes spent reading and coding in a made-up project session. Sessions A, B and C are short on both measures; D, E and F are longer. There is no label such as beginner or expert. More minutes do not establish either skill or learning quality.

We ask K-means for two clusters and standardise the two columns before comparing distances. Standardisation is a choice about how to measure similarity, not a requirement that makes any grouping correct. Record that choice whenever you report results.

Use Python 3 in a virtual environment. In a terminal, install scikit-learn, which also installs NumPy:

python -m venv .venv
source .venv/bin/activate
python -m pip install scikit-learn

On Windows, activate with .venv\Scripts\activate instead. Copy the Python block into a .py file and run it with python filename.py, or use a notebook cell. The example was run with Python 3.12.3, NumPy 2.5.4 and scikit-learn 1.9.1. All data is invented and included inline.

import numpy as np
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler

# Invented project sessions: [minutes reading, minutes coding].
names = np.array(["A", "B", "C", "D", "E", "F"])
X = np.array([[10, 12], [11, 13], [9, 11], [50, 48], [51, 49], [49, 47]])
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
model = KMeans(n_clusters=2, random_state=42, n_init=10)
labels = model.fit_predict(X_scaled)

groups = sorted(sorted(names[labels == label].tolist()) for label in np.unique(labels))
centres = scaler.inverse_transform(model.cluster_centers_)
centres = sorted(centres.round(1).tolist())
print("Groups:", groups)
print("Centres in minutes:", centres)
assert len(set(labels)) == 2

Expected output:

Groups: [['A', 'B', 'C'], ['D', 'E', 'F']]
Centres in minutes: [[10.0, 12.0], [50.0, 48.0]]

What the output actually says

The first group contains A, B and C. Its centre in the original units is 10 minutes reading and 12 minutes coding. The second contains D, E and F, centred at 50 and 48 minutes. These averages are easy to check by hand because the example has only three points in each group.

Cluster numbers from the estimator are identifiers. A result called cluster 0 is not a rank, a class of better learners or a label that is guaranteed to match another run. The reporting code sorts group membership so the displayed result does not depend on whether a particular group is numbered 0 or 1.

There are clear gaps in this invented dataset. Real data may have overlapping groups, several kinds of similarity or no useful cluster structure. K-means will still return the number of clusters you requested when it can fit them. The returned IDs do not prove that there are two natural categories.

Try it yourself: add session G with [30, 30]. Predict which existing centre is closer after the same preprocessing, then rerun the fit. Inspect how membership and centres change. Explain why refitting on seven rows can change earlier assignments too.

How to check a result without target labels

Start with the raw rows. Can you explain the measurements, their units and the rows that ended up together? Then change one modelling choice at a time and compare the groupings.

  • Try another plausible number of clusters and describe what changes.
  • Inspect whether standardisation lets a noisy column dominate similarity.
  • Refit on samples or different time periods to see whether membership is stable enough for the intended use.
  • Ask people who understand the data whether the grouping helps answer the original question.

An internal clustering score can help compare geometries, but a good score does not establish that the clusters improve a decision. For a prospective application, reserve new examples for checking how the fitted transformation and grouping behave later. Fit preprocessing only on the data intended for training.

When labels become necessary

Suppose you now want to predict whether a learner will finish a project. Reading and coding minutes alone do not tell you who finished. You need a definition of completion, an observation window and outcomes for examples you can use to evaluate a prediction.

Labelling a small, carefully chosen sample may be more useful than trying increasingly elaborate unsupervised methods. Semi-supervised learning combines labelled and unlabelled examples under additional assumptions; it does not make evaluation labels unnecessary.

Common questions

Can clustering create the correct labels automatically?

It can create group assignments. Calling those assignments correct target labels requires a separate definition and evidence. A cluster can be an input to human review, rather than a replacement for that review.

Must I use K-means for unlabeled data?

No. It is a starting example with a specific distance-based objective. Other methods can fit different shapes, densities or goals. Choose the method after defining the question and examining the measurements.

Can I compare clustering accuracy with classification accuracy?

Not directly without defining a comparable evaluation and obtaining reference labels where required. In this example there is no target class against which to compute classification accuracy.

Build the foundations for these experiments

SchoolWhool's NumPy module teaches arrays, shapes and operations. Try the inline NumPy projects, then study the Data Tools course for the preprocessing and estimator workflow.

The Machine Learning syllabus shows where clustering and dimensionality reduction fit in the wider course; check it for current lesson availability. A free account adds quizzes and progress. Pro adds practice for every lesson, projects, certificates and weekly live sessions with Gaurav.

References

See the official KMeans API for the fitted attributes and explicit n_init and random_state settings used here, and the clustering guide for method assumptions and evaluation options. All session data is invented.

Related course

The four libraries every machine learning notebook uses: NumPy arrays and broadcasting, pandas tables, readable Matplotlib charts, and the scikit-learn API.

Open lessons' parts and key terms are free to read, with no account.

What you will be able to do

  • Reason about an array's shape and dtype before an operation runs.
  • Select, filter, group and join tables with pandas.
  • Make charts that answer one question clearly.
  • Use scikit-learn's fit, transform and predict with confidence.