{ "metadata": { "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.4-final" }, "orig_nbformat": 2, "kernelspec": { "name": "python3", "display_name": "Python 3", "language": "python" } }, "nbformat": 4, "nbformat_minor": 2, "cells": [ { "source": [ "# Playlist Classifier\n", "\n", "Given a list of playlists, can unknown tracks be correctly classified?" ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [], "source": [ "playlist_names = [\"RAP\", \"EDM\", \"ROCK\", \"METAL\", \"JAZZ\", \"POP\"] # super-genres\n", "# playlist_names = [\"RAP\", \"EDM\", \"ROCK\", \"METAL\", \"JAZZ\"] # super-genres without POP\n", "# playlist_names = [\"DNB\", \"HOUSE\", \"TECHNO\", \"GARAGE\", \"DUBSTEP\", \"BASS\"] # EDM playlists\n", "# playlist_names = [\"20s rap\", \"10s rap\", \"00s rap\", \"90s rap\", \"80s rap\"] # rap decades\n", "# playlist_names = [\"UK RAP\", \"US RAP\"] # UK/US split\n", "# playlist_names = [\"uk rap\", \"grime\", \"drill\", \"afro bash\"] # british rap playlists\n", "# playlist_names = [\"20s rap\", \"10s rap\", \"00s rap\", \"90s rap\", \"80s rap\", \"trap\", \"gangsta rap\", \"industrial rap\", \"weird rap\", \"jazz rap\", \"boom bap\", \"trap metal\"] # american rap playlists\n", "# playlist_names = [\"rock\", \"indie\", \"punk\", \"pop rock\", \"bluesy rock\", \"hard rock\", \"chilled rock\", \"emo\", \"pop punk\", \"stoner rock/metal\", \"post-hardcore\", \"melodic hardcore\", \"art rock\", \"post-rock\", \"classic pop punk\", \"90s rock & grunge\", \"90s indie & britpop\", \"psych\"] # rock playlists\n", "# playlist_names = [\"metal\", \"metalcore\", \"mathcore\", \"hardcore\", \"black metal\", \"death metal\", \"doom metal\", \"sludge metal\", \"classic metal\", \"industrial\", \"nu metal\", \"calm metal\", \"thrash metal\"] # metal playlists\n", "\n", "# headers = float_headers + [\"duration_ms\", \"mode\", \"loudness\", \"tempo\"]\n", "headers = float_headers" ] }, { "source": [ "Pull and process playlist information.\n", "\n", "1. Get live playlist track information from spotify\n", "2. Filter listening history for these tracks\n", "\n", "Filter out tracks without features and drop duplicates before taking only the descriptor parameters" ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [], "source": [ "playlists = [get_playlist(i, spotnet) for i in playlist_names] # 1)\n", "\n", "# filter playlists by join with playlist track/artist names\n", "filtered_playlists = [pd.merge(track_frame(i.tracks), scrobbles, on=['track', 'artist']) for i in playlists] # 2)\n", "\n", "filtered_playlists = [i[pd.notnull(i[\"uri\"])] for i in filtered_playlists]\n", "# distinct on uri\n", "filtered_playlists = [i.drop_duplicates(['uri']) for i in filtered_playlists]\n", "# select only descriptor float columns\n", "filtered_playlists = [i.loc[:, headers] for i in filtered_playlists]" ] }, { "source": [ "Construct the dataset with associated labels before splitting into a train and test set." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [], "source": [ "dataset = pd.concat(filtered_playlists)\n", "labels = [np.full(len(plst), idx) for idx, plst in enumerate(filtered_playlists)]\n", "labels = np.concatenate(labels)\n", "\n", "data_train, data_test, labels_train, labels_test = train_test_split(dataset, labels, test_size=0.33, random_state=70)" ] }, { "source": [ "# SVM\n", "Support Vector Machine" ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " uw-rbf w-rbf linear poly sigmoid\n", "accuracy % 72.95 69.95 67.05 69.95 30.98" ], "text/html": "
\n | uw-rbf | \nw-rbf | \nlinear | \npoly | \nsigmoid | \n
---|---|---|---|---|---|
accuracy % | \n72.95 | \n69.95 | \n67.05 | \n69.95 | \n30.98 | \n