# Implement K-Means Clustering

Aced (formerly Exponent) publishes a public search API over its indexed pages; the guide is at https://www.aced.io/agents/agent-access.

Source: https://www.aced.io/questions/4327/implement-k-means-algorithm
Source observed: 2026-11-05T00:30:49.049Z
Last checked: 2026-11-05T00:30:49.049+00:00
Indexed: 2026-11-06T07:39:34.082+00:00

---

# Implement K-Means Clustering

**Difficulty:** Easy

We ask you to implement KMeans clustering from scratch in Python, using NumPy for efficient numerical operations. KMeans is an unsupervised learning algorithm that partitions a dataset into **K distinct, non-overlapping clusters** by grouping points according to similarity. Euclidean distance is used to measure similarity.

## Introduction

Create classes for both the KMeans algorithm and the centroids involved in clustering. The `__init__` method is already implemented and initializes `k` centroids at random locations in the feature space.

For additional interview preparation, use our [Model & Algorithm Fundamentals](https://www.tryexponent.com/courses/ml-engineer/ml-concepts-interviews/k-means) module.

## KMeans class

The `KMeans` class encapsulates the clustering process.

### Attributes

- `n_features`: number of features in the dataset, or its dimensionality.
- `k`: number of clusters to form.
- `centroids`: a list of `Centroid` objects representing cluster centers.

### Initialization

The constructor accepts `n_features` and `k`, and initializes `k` centroids with random locations in the feature space. This initialization has already been implemented.

### Methods to implement

- `distance(self, x, y)`: calculate and return the Euclidean distance between points `x` and `y` as a float.
- `fit(self, X, n_iterations)`: implement the fitting process that assigns the points in `X` to the relevant clusters.
- `predict(self, x)`: accept a new point `x` and return the index of the cluster it belongs to as an integer.

## Evaluation criteria

- **Correctness:** the implementation correctly performs clustering on a supplied dataset.
- **Efficiency:** distance calculations and centroid updates should be efficient.
- **Code quality:** classes and methods should be clear, readable, and well organized.

A hint is available in the exercise interface. The interface includes **Question**, **Solution**, **Mock Interview**, and **Community** tabs, plus an editor with **Editor**, **Share**, **Output**, **Test Results**, and **Console** views. The code runner requires an account: [Sign up to run code](https://www.aced.io/signup?src=lesson).

## Related questions

- [Implement k-means clustering.](https://www.aced.io/questions/1679/implement-k-means-clustering)
- [Implement mutex](https://www.aced.io/questions/7283/implement-mutex)
- [Implement merge-sort](https://www.aced.io/questions/4424/implement-merge-sort)

The page also offers a disabled **Select** option in the exercise controls.

---

[Read this page’s summary](https://www.aced.io/agents/api/public/v1/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F4327%2Fimplement-k-means-algorithm&trace=t_679ff9ab98f3)

## Ask a follow-up about this page

Need a more specific answer? Ask the exact question you have. It returns a fresh, cited answer from Aced (formerly Exponent)’s knowledge base. No account or API key required.

Open this URL with your own question, URL-encoded. Include your question in q. All research inputs are optional; no account or setup.

`GET https://www.aced.io/agents/agent/faq?q={question}&trace=t_679ff9ab98f3`

[Tool guide and examples](https://www.aced.io/agents/agent-access) · [OpenAPI](https://www.aced.io/agents/api/public/v1/openapi.json) · [Page TLDR form](https://www.aced.io/agents/agent-tools/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F4327%2Fimplement-k-means-algorithm). Opening a form does not execute a tool.