Menu
CoddyTech

Recap: Playlist Moods

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

challenge icon

Challenge

Medium

Your playlist's 98 songs are in songs.csv. Sort them into moods; the chart is set up.

  1. Scale the four features and cluster the songs into 4.
  2. Name each cluster from its mean energy and valence: both 0.5 or more is happy, only energy tense, only valence calm, neither sad.
  3. Biggest first, print each mood's size and anthem: the song nearest its center.
  4. Scatter valence across and energy up, one group per mood labelled with its name; legend, show.

For the chapter's first playlist:

happy: 26 songs, anthem Lucky Avenue
tense: 22 songs, anthem Midnight Highway
sad: 17 songs, anthem Grey Bridges
calm: 15 songs, anthem Soft Window

and its chart:

Another playlist's 80 songs titled Playlist Moods, valence across and energy up, in four labelled groups: tense at the top left, happy at the top right, sad at the bottom left, calm at the bottom right

Then Coddy describes yours.

Try it yourself

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans

# One row per song: energy and valence (0 sad to 1 happy) from 0 to 1, tempo, acousticness
songs = pd.read_csv("songs.csv")
features = ["energy", "valence", "tempo_bpm", "acousticness"]

fig, ax = plt.subplots()
ax.set_title("Playlist Moods")
ax.set_xlabel("Valence (sad to happy)")
ax.set_ylabel("Energy")

# Write your code below

All lessons in Unsupervised Learning

Practice on your own: Online Python compiler