ML Step-by-Step: Using KNN Algorithm to Classify Spotify Songs into Playlists
By: Sameeha Afrulbasha
Tags: Machine Learning, Knn, Spotify Playlist, Data Science, Knn Algorithm

Photo by Travis Ye well on Unsplash

As a song enthusiast, I thought the best way to get my hands into Machine Learning and Data Science was to meddle in my Spotify music data. The broad concept behind this project is to create a model that can take any song and, depending on its musical attributes, classify it into a playlist with the nearest, shared qualities. Instead of sorting it myself, the algorithm will do it for me!

The 2 playlists I used for this model were my “happy playlist” filled with upbeat, high-energy songs and my “euphonious playlist” which contains songs that are mostly slow, beautiful, and peaceful music.

For this project, I used Jupyter Notebook with ML Library scikit learn. I also used my personal Spotify playlists but you could also use Spotify curated playlists (they probably have more diversified songs in their playlists so I tried choosing playlists from mine that are diversified in artists as well).

You can find my code here on GitHub.

An overview of what I am covering:

Brief Overview of KNN Accessing Spotify Credentials/Scraping Data Libraries/Reading in Datasets/Graphing Refining Datasets Creating the model

standardizing data

splitting data into training and testing sets

training the model

making predictions and evaluating the predictions

adjusting the K-value for a stronger model

6. Conclusion

1. Brief Overview of KNN

KNN (aka. K Nearest Neighbors) is a type of Machine Learning algorithm that classifies a new data point based on how its neighboring points are classified. In the diagram below, A is the new data point. K = the number of nearest points (neighbors) around A. In this case, K=7. The model identifies the 7 points closest to A (smallest distance). Based on the classification of the 7 surrounding points, data point A classifies as Class Z because its closer to more points in Class Z than the other 2 classes. A is more similar to Class Z data points. KNN, in simple terms, is a similarity measure between a new point and its surrounding points.

Image by Author

Time to Start the Project

2. Accessing Spotify Credentials

A more detailed overview of how I accessed my Spotify Credentials can be found in my last article post where I go through the code step by step. You can also find the code to it here.

Essentially, I go to Spotify for Developers and use my Spotify log in to sign in to My Dashboard. Hit “Create an App” and once you name it you should see your Client ID and Client Secret on the left of the screen. From there you can paste the Client information in the appropriate places in the code.

To access the Spotify URI for a playlist, go to the wanted playlist, click the 3 dots at the top and while hovering over the “Share” option, press the alt key to see the the option “Copy Spotify URI” and click it. You can also do this without using the alt key by choosing “Copy link to playlist” but you will need to cut out everything but the code at the end of the URL.

Once you inserted your information into the right places in the code, you should be able to download a csv file containing the music attributes of each song in your chosen playlist.

There are multiple methods of accessing Spotify credentials and data but this is how I did it with help from this article.

3. Libraries /Reading in Datasets/Graphing

The goal for this step is to import the needed libraries, visualize the differences in musical attributes between each playlist to see which attributes we should keep or eliminate.

from sklearn.neighbors import NearestNeighbors

import pandas as pd

import plotly.express as px

import matplotlib.pyplot as plt

%matplotlib inline

import numpy as np

import seaborn as sns

Libraries: Panda and numpys are for data exploration and matplotlib and seaborn are for data visualization. Sklearn is for carrying out the KNN algorithm.

Reading in Datasets: I first uploaded the 2 csv files to Jupyter Notebook (1 file for each playlist I was using) and used Pandas to read in the datasets. “Happy” and “Euphon” are the names for my playlist objects.

happy = pd.read_csv("name of file.csv")

euphon = pd.read_csv("name of file.csv")

Graphing: I then made very simple graphs for each Spotify music attribute with matplotlib so I can distinguish the differences between each playlist. I kept the variables that showed distinct, considerable gaps between the 2 lines for each graph as well as a consistent pattern for each line. For my playlists, I noticed consistent pattern and gaps in energy, acousticness, and valence.

Red = euphonious playlist

Blue = happy playlist