Menu
CoddyTech

Star Clusters

Part of the Unsupervised Learning section of Coddy's Machine Learning journey. Lesson 50 of 51.

An astronomy club photographed one patch of sky. Some of its 225 stars belong together, born in the same cloud; the rest are strangers in front or behind:

225 stars as black dots on a 60 by 40 arcminute photo: a round crowd on the left, a long curved stream rising on the right, and lone stars scattered everywhere

Both positions are in arcminutes, one unit, so eps is a distance on the photo.

The club wants each cluster and its size, with the lone field stars left out. Would k-means do as well?

challenge icon

Challenge

Hard

The program reads eps; the starter sets up two maps, left and right.

  1. Run DBSCAN with that eps and min_samples=5 on the two positions. Print its clusters and field stars (label -1).
  2. Fit k-means with as many clusters (n_init=10, random_state=0). Print the biggest and smallest cluster of each method.
  3. On left, map the DBSCAN clusters, cluster 1 the biggest, then the field stars in gray. On right, the k-means clusters, numbered the same way. Legends, show.

For another photo and eps 2.5:

DBSCAN: 3 clusters and 29 field stars
Biggest cluster: 63 stars by DBSCAN, 77 by k-means
Smallest cluster: 41 stars by DBSCAN, 48 by k-means

and its maps:

Another photo, two maps side by side. Left, DBSCAN: cluster 1 a round crowd at the upper left, cluster 2 a round crowd at the upper right, cluster 3 a short bar of stars at the bottom, gray field stars scattered. Right, k-means with 3 clusters: the same three groups, every field star swallowed into the nearest one

Then Coddy describes yours. Which method would you trust with the field stars?

Try it yourself

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN, KMeans

# One row per star in the photo: position (arcminutes) and brightness (magnitude)
field = pd.read_csv("field.csv")
eps = float(input())

# Two maps side by side: DBSCAN on the left, k-means on the right
fig, axs = plt.subplots(1, 2, figsize=(10, 4))
left = axs[0]
right = axs[1]
left.set_title("DBSCAN")
right.set_title("k-means")
for ax in axs:
    ax.set_xlabel("x (arcmin)")
    ax.set_ylabel("y (arcmin)")

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Unsupervised Learning

Practice on your own: Online Python compiler