Menu
CoddyTech

Recap: City Lights

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

challenge icon

Challenge

Medium

Remember the valley map from DBSCAN? Now you make one. A satellite photographed a valley at night; group its lights into towns and find the lone ones: farms, a lighthouse, a camper. The program reads eps in km.

  1. Run DBSCAN with that eps and min_samples=5.
  2. Print the number of towns, the number of lone lights, and the biggest town's size.
  3. Map them: each town its own scatter labelled town of N lights, lone lights as gray x marks labelled lone lights, a legend. Show it.

For another valley and eps 1.3:

Towns: 3
Lone lights: 10
Biggest town: 77 lights

A chart titled Valley at Night: a long curved town of 77 lights, a round town of 47 and a village of 20, each in its own color, and 10 lone lights as gray x marks, with a legend

Coddy describes your map. Then try eps 5: which towns merge?

Try it yourself

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN

# A satellite's night photo of a valley: one row per light, position in km
lights = pd.read_csv("lights.csv")
X = np.column_stack([lights["east_km"], lights["north_km"]])
eps = float(input())

fig, ax = plt.subplots()
ax.set_title("Valley at Night")
ax.set_xlabel("East (km)")
ax.set_ylabel("North (km)")

# Write your code below

All lessons in Unsupervised Learning

Practice on your own: Online Python compiler