# Clustering: K-Means

Clustering is the most well-known unsupervised learning technique. It finds structure in unlabeled data by identifying similar groups.

Start## Key Concepts

Review core concepts you need to learn to master this subject

K-Means: Inertia

Unsupervised Learning Basics

K-Means Algorithm: Intro

K-Means Algorithm: 2nd Step

Scikit-Learn Datasets

K-Means Using Scikit-Learn

Cross Tabulation Overview

K-Means: Reaching Convergence

K-Means: Inertia

K-Means: Inertia

*Inertia* measures how well a dataset was clustered by K-Means. It is calculated by measuring the distance between each data point and its centroid, squaring this distance, and summing these squares across one cluster.

A good model is one with low inertia AND a low number of clusters (`K`

). However, this is a tradeoff because as `K`

increases, inertia decreases.

To find the optimal `K`

for a dataset, use the *Elbow method*; find the point where the decrease in inertia begins to slow. `K=3`

is the “elbow” of this graph.

- 1Often, the data you encounter in the real world won’t have flags attached and won’t provide labeled answers to your question. Finding patterns in this type of data, unlabeled data, is a common them…
- 2The goal of clustering is to separate data so that data similar to one another are in the same group, while data different from one another are in different groups. So two questions arise: - How m…
- 3Before we implement the K-means algorithm, let’s find a dataset. The sklearn package embeds some datasets and sample images. One of them is the Iris dataset . The Iris dataset consists of measure…
- 4To get a better sense of the data in the iris.data matrix, let’s visualize it! With Matplotlib, we can create a 2D scatter plot of the Iris dataset using two of its features (sepal length vs. peta…
- 5The K-Means algorithm: 1.
**Place k random centroids for the initial clusters.**2. Assign data samples to the nearest centroid. 3. Update centroids based on the above-assigned data samples. Repe… - 6The K-Means algorithm: 1. Place k random centroids for the initial clusters. 2.
**Assign data samples to the nearest centroid.**3. Update centroids based on the above-assigned data samples. Repe… - 7The K-Means algorithm: 1. Place k random centroids for the initial clusters. 2. Assign data samples to the nearest centroid. 3.
**Update centroids based on the above-assigned data samples.**Repe… - 8The K-Means algorithm: 1. Place k random centroids for the initial clusters. 2. Assign data samples to the nearest centroid. 3. Update centroids based on the above-assigned data samples. **Repeat…
- 9Awesome, you have implemented K-Means clustering from scratch! Writing an algorithm whenever you need it can be very time-consuming and you might make mistakes and typos along the way. We will no…
- 11We have done the following using sklearn library: - Load the embedded dataset - Compute K-Means on the dataset (where k is 3) - Predict the labels of the data samples And the labels resulted in e…
- 12At this point, we have clustered the Iris data into 3 different groups (implemented using Python and using scikit-learn). But do the clusters correspond to the actual species? Let’s find out! Firs…
- 13At this point, we have grouped the Iris plants into 3 clusters. But suppose we didn’t know there are three species of Iris in the dataset, what is the best number of clusters? And how do we determi…
- 14Now it is your turn! In this review section, find another dataset from one of the following: - The scikit-learn library - UCI Machine Learning Repo - Codecademy GitHub Repo (coming soon!) …

## What you'll create

Portfolio projects that showcase your new skills

## How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory