Menu
CoddyTech

Recap: Dog Breeds

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

challenge icon

Challenge

Medium

A dog shelter has five kennel rooms and wants similar breeds to share one. breeds.csv holds 21 breeds; the starter sets the cut height, cut.

  1. Scale the features and build a ward tree.
  2. Draw its dendrogram on ax: breed names turned 90 degrees, colors stopping at the cut, and a gray dashed cut line. Show it.
  3. Cut the tree, profile the groups in real units, and print them from lightest to heaviest, each named after its first breed.

For another shelter:

5 breeds like Maltese: 4.1 kg, 24 cm, energy 3.2, 14.4 years
3 breeds like Whippet: 12.7 kg, 46 cm, energy 4.7, 13.3 years

and its tree, cut into 2 groups:

A tree titled Dog Breeds over 8 breeds, names running upward, with a gray dashed cut line at height 4 crossing two colored branches: Maltese, Shih Tzu, Bichon Frise, Papillon and Toy Poodle; Whippet, Brittany and Basenji

Then Coddy describes your tree.

Try it yourself

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from scipy.cluster.hierarchy import linkage, dendrogram, fcluster

# 21 dog breeds: weight (kg), height (cm), energy from 1 to 5, lifespan (years)
breeds = pd.read_csv("breeds.csv")
features = ["weight_kg", "height_cm", "energy", "lifespan_yr"]
# The shelter has five kennel rooms, so it cuts its tree at this height
cut = 2.5

fig, ax = plt.subplots(figsize=(8, 5))
fig.subplots_adjust(bottom=0.35)  # room for the names below the tree
ax.set_title("Dog Breeds")
ax.set_ylabel("Merge height")

# Write your code below

All lessons in Unsupervised Learning

Practice on your own: Online Python compiler