Spotify Playlist Classifier


I wanted to experiment with machine learning for a long time and after I saw this medium article about Making your own Spotify Discover Weekly I decided that Spotify would be a good first starting point a I heavily use the service.
But I didn’t want to create one discover weekly playlist like the author did. I often follow playlists I find or see my friends listen to and later listen through them to find new songs that fit into my existing playlists.
“This process would be an ideal problem to solve for a machine learning classifier” I thought to myself so I copy and pasted the code snippets from the article and started to adapt them to my use case.

How it works

  1. Get Spotify Web API tokens and enter client id and secret into script
  2. Give the Spotify IDs of a fitting playlist and one with unfitting music
  3. Give the Spotify IDs of a playlist to filter out songs from and one playlist where to add the found songs to
  4. It gets the IDs of all songs in fitting and unfitting playlists
  5. Gets the audio featutes for all the tracks (things like valence, bpm, energy, “danceability”, etc)
  6. Generated a dataframe with all songs and all features with a tag value wether the song is positive and fitting or not
  7. Split trainingData Dataframe into training and test data
  8. Train all algorithms with the data given and show their accuracy score
  9. Predict which songs should fit
  10. Uses the IDs of predicted song to add them to new playlist given in step 3
  11. Listen to the filled playlist and decide how much you like the decisions of the classifiers and how they compare against each other


Unfortunately I just hacked together most of the code and trial and errored some code snippets into a 500 line script so there is no nice code to show but if you interested contact me and I can give you the code I have.


I did some benchmarks on some of my playlists and noted the results but did not find a clear result I’m happy with and did not develop it further into a usable programm.
Maybe I will do that some time in the future but for now here are my benchmarking results:

Algorithms used

  • DecisionTreeClassifier
  • KNeighborsClassifier
  • MLPClassifier
  • RandomForestClassifier
  • AdaBoostClassifier
  • GaussianNB
  • KMeans
  • GradientBoostingClassifier

How i tested

I selected a few of my smaller playlists for specific moods and used them as learning input for the classifier as “good”/fitting songs and some other music like a techno or chart pop playlist as “bad”/unfitting music.
Then I used the trained models with different classifiers but same training data on my long like playlist with over 1000 songs to filter some music that fitted into these smaller playlists.
I saved the resulting song predictions into playlists representing their classifer names and noted the results.

Most of the songs contained in the smaller playlists I used as training data were from my long like playlist so I could also easily test the results. The results are probably flaw not representative because the songs were present in destination and source playlist and because the input data has to be split into training and test data which has happened randomly with a ration of 0.8 training to 0.2 test data. Resulting from this split not every algorithm received exactly the same training and test data in each benchmark.
I could redo this some time again with a more controlled environment but for now I just wanted to experiment.

Resulting playlists in Spotify

Subjective results from benchmark with quite music to listen to before going to sleep

  1. gaussian nb
    • Found 6 songs and none was really off
  2. dec tree
    • Found some I even considered before like location from khalid and one good idea I did not have before: In between from linkin park
  3. random forest
    • Found 3 song 1 was really off but 2 were very fitting
  4. knn
    • Only 2 songs Haze from tessa was fitting but if i dont go to hell rap little off
  5. k means
    • Some good songs but mostly bad music
  6. ada boost
    • Basically no fitting decision just some random songs
  7. mlp
    • Returned no song at all

Subjective results from benchmark with driving music (mostly rap or bass centric)

  1. Ada boost
    • Overall very fitting, chilly rap; but some that were already in playlist?
  2. dec tree
    • Fitting rap and Chuck Berry but all were already in playlist
  3. knn
    • Worked: got some good tracks but also some strange unfitting like very ballad like quiet songs
  4. random forest
    • Only 1 rap and 1 beat centric but fit (or even were in?)
  5. mlp / gaussian
    • Returned none

Subjective results from sum41 benchmark

  1. Ada boost
    • Overall very fitting
  2. dec tree
    • Mostly fitting but some very strange like some chill beat only music or edm
  3. random forest
    • Only 2 songs fitting from falling in reverse but also some chill beat only music
  4. knn
    • Almost only unfitting songs
  5. gaussian
    • Only unfitting. electro and life version of 50s rock


  • maybe use dec tree + random forest
  • for rap ada boost?
  • dont know about knn
  • dont use k mean or mlp dont work