Classifying Musical Genres with Simple Machine Learning

MViz
4 min readAug 28, 2020

If I play you a song by Drake, odds are you know it’s not a rock song. You probably recognize the beat, the flow, the rhythm of a rap song. Country has twang, and rock has grit — are these features enough to distinguish these genres? Well, we know that understanding and recognizing music is a uniquely human trait, and we’re good at it. Can a computer compete?

In this micro study I used the underlying musical attributes of songs to build a simple machine learning model to differentiate between some popular genres: rock, rap, and country. Quickly take a guess, which of the following pairs of genres will be the easiest to differentiate?

  • Rock vs Rap
  • Rap vs Country
  • Rock vs Country

I used Spotify’s developer API to define and provide those musical attributes. For each song, Spotify provides musical features like danceability, instrumentalness, key, liveness, and speechiness. You can read more about how the features are defined on their website. I used a tool called Spotipy to download these six Spotify playlists:

The above playlists are curated by Spotify, but it is worth noting that this project assumes that the songs in the playlist are indicative of each genre. The playlists are always changing, so mileage may vary in repeated trials.

The songs from the playlists were organized into the three groups: Rock vs Rap, Rap vs Country, Rock vs Country. Though the songs were shared between groups, each pair was analyzed individually.

Finally, the genre prediction was done using the musical characteristics as follows:

Visualization of the machine learning model, showing that musical attributes are used to predict genre.
Overview of the prediction model

The prediction is done using a simple machine learning technique called logistic regression. In short, it takes all of the musical characteristics (danceability, accousticness, etc) of both genres (say Country and Rap) and determines which of the characteristics are important for each genre. It may be that most country songs have low instrumentalism, or that low valence songs are often Rap songs.

When you combine all of these characteristics, you get the probability a song belongs to one of the genres. In the successful case, it correctly associates Country attributes with country songs.

Correctly associating Country attributes to a Country song
Example of a Country song being correctly labeled as Country

Of course it’s possible a song has characteristics of the competing genre, and it gets incorrectly labeled.

Incorrectly associating Country attributes with a Rap song
Example of a Rap song being incorrectly labeled as Country

Success can be determined by how often you make the correct guesses. An incorrect guess would be labelling a country song as a rap song, or vice versa, so 80% accuracy would mean 20% of the songs are incorrectly labeled.

If you want to read more about the analysis check out my code behind it.

So how did we do? The models were able to correctly label the genres of the songs at the following rates:

  • Rap vs Rock: 91.1% accuracy
  • Rap vs Country: 83.5% accuracy
  • Rock vs Country: 61.8% accuracy

Did you correctly guess which pair would be easiest to differentiate?

Remember the guesses are just probabilities. In the visualization below, you can see how many of the Rock and Country songs have similar probabilities of being labelled as Country — so you get lower overall accuracy. In the Rap vs Country case — most songs were definitely one genre, but there is a cluster of similar songs in the middle.

Histograms showing the genre predictions for each pair of genres.
Predicting the genres of our test data

I think these results are quite intuitive — to my ears country songs sound more like rock than they do rap. The logical next step would be to include more genres. The Rock vs Country case is a good hint that you would have to be wary of similar genres like Rap and Hip Hop, or Rock and Punk Rock.

This project left me curious about the definitions of musical genres and the degree to which they are defined prescriptively. I believe the above results warrant a deeper dive, mapping a wider variety of music to musical attributes. Until then, here are the tracks that fell into the extreme ends of the musical predictions.

The rock song that is…

  • The least rap: Another One Bites the Dust — Queen
  • The most rap: Paradise City — Guns N’ Roses
  • The least country: Old Time Rock & Roll — Bob Seger
  • The most country: Born to be Wild — Steppenwolf

The rap song that is…

  • The least rock: We Paid — Lil Baby
  • The most rock: After Party — Don Toliver
  • The least country: Pinky Rosey — Perra
  • The most country: Pure Cocaine — Lil Baby

The country song that is…

  • The least rap: Done — Chris Janson
  • The most rap: Miss Me More — Kelsea Ballerini
  • The least rock: One Thing Right — Marshmello
  • The most rock: We Back — Jason Aldean

--

--

MViz

Dipping my toes into data science and data viz.