{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Importing the libraries"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import pandas as pd\n",
"import numpy as np\n",
"import json\n",
"import spotipy\n",
"import spotipy.oauth2 as oauth2\n",
"from spotipy.oauth2 import SpotifyOAuth,SpotifyClientCredentials\n",
"import yaml\n",
"import re\n",
"from tqdm import tqdm\n",
"import multiprocessing as mp\n",
"import time\n",
"import random\n",
"import datetime\n",
"import pickle\n",
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
"from sklearn.metrics.pairwise import cosine_similarity\n",
"from sklearn.preprocessing import MinMaxScaler\n",
"import matplotlib.pyplot as plt\n",
"from skimage import io\n",
"from sklearn.preprocessing import OneHotEncoder"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"stream= open(\"spotify/spotify.yaml\")\n",
"spotify_details = yaml.safe_load(stream)\n",
"auth_manager = SpotifyClientCredentials(client_id=spotify_details['Client_id'],\n",
" client_secret=spotify_details['client_secret'])\n",
"sp = spotipy.client.Spotify(auth_manager=auth_manager)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Importing the dataset"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"dtypes = {'track_uri': 'object', 'artist_uri': 'object', 'album_uri': 'object', 'danceability': 'float16', 'energy': 'float16', 'key': 'float16',\n",
" 'loudness': 'float16', 'mode': 'float16', 'speechiness': 'float16', 'acousticness': 'float16', 'instrumentalness': 'float16',\n",
" 'liveness': 'float16', 'valence': 'float16', 'tempo': 'float16', 'duration_ms': 'float32', 'time_signature': 'float16',\n",
" 'Track_release_date': 'int8', 'Track_pop': 'int8', 'Artist_pop': 'int8', 'Artist_genres': 'object'}\n",
"try:\n",
" df=pd.read_csv('Data/1M_unique_processed_data_grow.csv',dtype=dtypes)\n",
"except:\n",
" print('Failed to load grow')\n",
" df=pd.read_csv('Data/1M_unique_processed_data.csv',dtype=dtypes)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Extract playlist tracks and artist uri"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"80\n",
"80\n"
]
}
],
"source": [
"def get_IDs (user, playlist_id):\n",
" track_ids = []\n",
" artist_id = []\n",
" playlist=sp.user_playlist (user, playlist_id)\n",
" for item in playlist['tracks']['items']:\n",
" track=item['track']\n",
" track_ids.append(track['id'])\n",
" artist=item['track']['artists']\n",
" artist_id.append(artist[0]['id'])\n",
" return track_ids,artist_id\n",
"\n",
"\n",
"track_ids,artist_id = get_IDs ('Ruby', 'spotify:playlist:37i9dQZF1DX8FwnYE6PRvL') \n",
"print (len(track_ids))\n",
"print (len(artist_id))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"getting the unique URI and repeating the extraction features and preprocessing steps for the user's playlist (input)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"artist_id_uni=list(set(artist_id))\n",
"track_ids_uni=list(set(track_ids))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 4/4 [00:00<00:00, 12.20it/s]\n"
]
}
],
"source": [
"audio_features=pd.DataFrame()\n",
"for i in tqdm(range(0,len(track_ids_uni),25)):\n",
" try:\n",
" track_feature = sp.audio_features(track_ids_uni[i:i+25])\n",
" track_df = pd.DataFrame(track_feature)\n",
" audio_features=pd.concat([audio_features,track_df],axis=0)\n",
" except Exception as e:\n",
" print(e)\n",
" continue"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 4/4 [00:00<00:00, 4.37it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"list index out of range\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"track_=pd.DataFrame()\n",
"for i in tqdm(range(0,len(track_ids_uni),25)):\n",
" try:\n",
" track_features = sp.tracks(track_ids_uni[i:i+25])\n",
" for x in range(25):\n",
" track_pop=pd.DataFrame([track_ids_uni[i+x]],columns=['Track_uri'])\n",
" track_pop['Track_release_date']=track_features['tracks'][x]['album']['release_date']\n",
" track_pop['Track_pop'] = track_features['tracks'][x][\"popularity\"]\n",
" track_pop['Artist_uri']=track_features['tracks'][x]['artists'][0]['id']\n",
" track_pop['Album_uri']=track_features['tracks'][x]['album']['id']\n",
" track_=pd.concat([track_,track_pop],axis=0)\n",
" except Exception as e:\n",
" print(e)\n",
" continue"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 3/3 [00:00<00:00, 9.76it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"list index out of range\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"artist_=pd.DataFrame()\n",
"for i in tqdm(range(0,len(artist_id_uni),25)):\n",
" try:\n",
" artist_features = sp.artists(artist_id_uni[i:i+25])\n",
" for x in range(25):\n",
" artist_df=pd.DataFrame([artist_id_uni[i+x]],columns=['Artist_uri'])\n",
" artist_pop = artist_features['artists'][x][\"popularity\"]\n",
" artist_genres = artist_features['artists'][x][\"genres\"]\n",
" artist_df[\"Artist_pop\"] = artist_pop\n",
" if artist_genres: \n",
" artist_df[\"genres\"] = \" \".join([re.sub(' ','_',i) for i in artist_genres])\n",
" else:\n",
" artist_df[\"genres\"] = \"unknown\"\n",
" artist_=pd.concat([artist_,artist_df],axis=0)\n",
" except Exception as e:\n",
" print(e)\n",
" continue"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"test=pd.DataFrame(track_,columns=['Track_uri','Artist_uri','Album_uri'])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"test.rename(columns = {'Track_uri':'track_uri','Artist_uri':'artist_uri','Album_uri':'album_uri'}, inplace = True)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"audio_features.drop(columns=['type','uri','track_href','analysis_url'],axis=1,inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"test = pd.merge(test,audio_features, left_on = \"track_uri\", right_on= \"id\",how = 'outer')\n",
"test = pd.merge(test,track_, left_on = \"track_uri\", right_on= \"Track_uri\",how = 'outer')\n",
"test = pd.merge(test,artist_, left_on = \"artist_uri\", right_on= \"Artist_uri\",how = 'outer')"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"del audio_features,track_,artist_"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"test.rename(columns = {'genres':'Artist_genres'}, inplace = True)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"test.drop(columns=['Track_uri','Artist_uri_x','Artist_uri_y','Album_uri','id'],axis=1,inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"test.dropna(axis=0,inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"test['Track_pop'] = test['Track_pop'].apply(lambda x: int(x/5))\n",
"test['Artist_pop'] = test['Artist_pop'].apply(lambda x: int(x/5))\n",
"test['Track_release_date'] = test['Track_release_date'].apply(lambda x: x.split('-')[0])\n",
"test['Track_release_date']=test['Track_release_date'].astype('int16')\n",
"test['Track_release_date'] = test['Track_release_date'].apply(lambda x: int(x/50))"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"test[['danceability', 'energy', 'key','loudness', 'mode', 'speechiness', 'acousticness', 'instrumentalness','liveness', 'valence', 'tempo', 'time_signature']]=test[['danceability', 'energy', 'key','loudness', 'mode', 'speechiness', 'acousticness', 'instrumentalness','liveness', 'valence', 'tempo','time_signature']].astype('float16')\n",
"test[['duration_ms']]=test[['duration_ms']].astype('float32')\n",
"test[['Track_release_date', 'Track_pop', 'Artist_pop']]=test[['Track_release_date', 'Track_pop', 'Artist_pop']].astype('int8')"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"currentdf=len(df)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"df=pd.concat([df,test],axis=0)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"df.drop_duplicates(subset=['track_uri'],inplace=True,keep='last') ## keep last to keep the dataset updated "
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"df.dropna(axis=0,inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"36 New Tracks Found\n"
]
}
],
"source": [
"print('{} New Tracks Found'.format(len(df)-currentdf))"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"36 New Found\n"
]
}
],
"source": [
"#saving the tracks if they weren't found in the dataset\n",
"if len(df)>currentdf: \n",
" df.to_csv('data/1M_unique_processed_data_grow.csv',index=False)\n",
" print('{} New Found'.format(len(df)-currentdf))\n",
" streamlit=df[df.Track_pop >0] # dropped track with 0 popularity score to save space and ram for the final model\n",
" streamlit.to_csv('data/streamlit.csv',index=False)\n",
" del streamlit"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"df = df[~df['track_uri'].isin(test['track_uri'].values)]"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"test['Artist_genres'] = test['Artist_genres'].apply(lambda x: x.split(\" \"))\n",
"tfidf = TfidfVectorizer(max_features=3) #max_features=5 \n",
"tfidf_matrix = tfidf.fit_transform(test['Artist_genres'].apply(lambda x: \" \".join(x)))\n",
"genre_df = pd.DataFrame(tfidf_matrix.toarray())\n",
"genre_df.columns = ['genre' + \"|\" + i for i in tfidf.get_feature_names_out()]"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"genre_df=genre_df.astype('float16')\n",
"test.drop(columns=['Artist_genres'],axis=1,inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"test = pd.concat([test.reset_index(drop=True), genre_df.reset_index(drop=True)],axis = 1)\n"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test.isna().sum().sum()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# df"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"df['Artist_genres'] = df['Artist_genres'].apply(lambda x: x.split(\" \"))\n",
"tfidf_matrix = tfidf.transform(df['Artist_genres'].apply(lambda x: \" \".join(x)))\n",
"genre_df = pd.DataFrame(tfidf_matrix.toarray())\n",
"genre_df.columns = ['genre' + \"|\" + i for i in tfidf.get_feature_names_out()]"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"genre_df=genre_df.astype('float16')\n",
"df.drop(columns=['Artist_genres'],axis=1,inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"df = pd.concat([df.reset_index(drop=True), genre_df.reset_index(drop=True)],axis = 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# pred"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"genre|unknown not found\n"
]
}
],
"source": [
"try:\n",
" df.drop(columns=['genre|unknown'],axis=1,inplace=True)\n",
" test.drop(columns=['genre|unknown'],axis=1,inplace=True)\n",
"except:\n",
" print('genre|unknown not found')"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Index(['track_uri', 'artist_uri', 'album_uri', 'danceability', 'energy', 'key',\n",
" 'loudness', 'mode', 'speechiness', 'acousticness', 'instrumentalness',\n",
" 'liveness', 'valence', 'tempo', 'duration_ms', 'time_signature',\n",
" 'Track_release_date', 'Track_pop', 'Artist_pop', 'genre|modern_rock',\n",
" 'genre|permanent_wave', 'genre|rock'],\n",
" dtype='object')"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test.columns"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Index(['track_uri', 'artist_uri', 'album_uri', 'danceability', 'energy', 'key',\n",
" 'loudness', 'mode', 'speechiness', 'acousticness', 'instrumentalness',\n",
" 'liveness', 'valence', 'tempo', 'duration_ms', 'time_signature',\n",
" 'Track_release_date', 'Track_pop', 'Artist_pop', 'genre|modern_rock',\n",
" 'genre|permanent_wave', 'genre|rock'],\n",
" dtype='object')"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.columns"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I was first using OneHotEncoder for \"Track_release_date\", \"Track_pop\", and \"Artist_pop,\" but I found no difference in the final result other than high memory usage."
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\" ohe = OneHotEncoder(handle_unknown='ignore')\\ndummies = pd.DataFrame(ohe.fit_transform(test[['Track_release_date', 'Track_pop', 'Artist_pop']]).toarray(), index=test.index,dtype=int)\\ncolumn_name = ohe.get_feature_names_out(['Track_release_date', 'Track_pop', 'Artist_pop'])\\ndummies.columns=column_name\\ntest = pd.concat([test.drop(['Track_release_date', 'Track_pop', 'Artist_pop'], axis=1), dummies], axis=1) \""
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"\"\" ohe = OneHotEncoder(handle_unknown='ignore')\n",
"dummies = pd.DataFrame(ohe.fit_transform(test[['Track_release_date', 'Track_pop', 'Artist_pop']]).toarray(), index=test.index,dtype=int)\n",
"column_name = ohe.get_feature_names_out(['Track_release_date', 'Track_pop', 'Artist_pop'])\n",
"dummies.columns=column_name\n",
"test = pd.concat([test.drop(['Track_release_date', 'Track_pop', 'Artist_pop'], axis=1), dummies], axis=1) \"\"\""
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\" ohe2 = OneHotEncoder(categories=ohe.categories_,handle_unknown='ignore')\\ndummies = pd.DataFrame(ohe2.fit_transform(df[['Track_release_date', 'Track_pop', 'Artist_pop']]).toarray(), index=df.index, dtype=int)\\ncolumn_name = ohe2.get_feature_names_out(['Track_release_date', 'Track_pop', 'Artist_pop'])\\ndummies.columns=column_name\\ndf=pd.concat([df.drop(['Track_release_date', 'Track_pop', 'Artist_pop'], axis=1), dummies], axis=1)\\n \""
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"\"\" ohe2 = OneHotEncoder(categories=ohe.categories_,handle_unknown='ignore')\n",
"dummies = pd.DataFrame(ohe2.fit_transform(df[['Track_release_date', 'Track_pop', 'Artist_pop']]).toarray(), index=df.index, dtype=int)\n",
"column_name = ohe2.get_feature_names_out(['Track_release_date', 'Track_pop', 'Artist_pop'])\n",
"dummies.columns=column_name\n",
"df=pd.concat([df.drop(['Track_release_date', 'Track_pop', 'Artist_pop'], axis=1), dummies], axis=1)\n",
" \"\"\""
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"#df.info(memory_usage = \"deep\")"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"#test.loc[:,test.columns.str.startswith('genre')]=test.loc[:,test.columns.str.startswith('genre')].astype('bool')\n",
"#df.loc[:,df.columns.str.startswith('genre')]=df.loc[:,df.columns.str.startswith('genre')].astype('bool')\n"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"sc=MinMaxScaler()\n",
"df.iloc[:,3:19]=sc.fit_transform(df.iloc[:,3:19])\n",
"pickle.dump(sc, open('data/sc.sav', 'wb'))"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"test.iloc[:,3:19]=sc.transform(test.iloc[:,3:19])"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" track_uri | \n",
" artist_uri | \n",
" album_uri | \n",
" danceability | \n",
" energy | \n",
" key | \n",
" loudness | \n",
" mode | \n",
" speechiness | \n",
" acousticness | \n",
" ... | \n",
" valence | \n",
" tempo | \n",
" duration_ms | \n",
" time_signature | \n",
" Track_release_date | \n",
" Track_pop | \n",
" Artist_pop | \n",
" genre|modern_rock | \n",
" genre|permanent_wave | \n",
" genre|rock | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 2AT8iROs4FQueDv2c8q2KE69uxyAqqPIsUyTO8txoP2M37... | \n",
" 7Ln80lUS6He07XvHI8qqHH4gzpq5DPGxSnKTe4SA8HAU58... | \n",
" 78bpIziExqiI9qztvNFlQu3cfAM8b8KqJRoIzt3zLKqw0k... | \n",
" 44.138238 | \n",
" 66.62793 | \n",
" 36.363637 | \n",
" 66.575184 | \n",
" 57.0 | \n",
" 5.761925 | \n",
" 4.38462 | \n",
" ... | \n",
" 51.553955 | \n",
" 38.882252 | \n",
" 2.999022 | \n",
" 64.000001 | \n",
" 78.950001 | \n",
" 67.777777 | \n",
" 56.650001 | \n",
" 16.453125 | \n",
" 17.1875 | \n",
" 43.09375 | \n",
"
\n",
" \n",
"
\n",
"
1 rows × 22 columns
\n",
"
"
],
"text/plain": [
" track_uri \\\n",
"0 2AT8iROs4FQueDv2c8q2KE69uxyAqqPIsUyTO8txoP2M37... \n",
"\n",
" artist_uri \\\n",
"0 7Ln80lUS6He07XvHI8qqHH4gzpq5DPGxSnKTe4SA8HAU58... \n",
"\n",
" album_uri danceability energy \\\n",
"0 78bpIziExqiI9qztvNFlQu3cfAM8b8KqJRoIzt3zLKqw0k... 44.138238 66.62793 \n",
"\n",
" key loudness mode speechiness acousticness ... valence \\\n",
"0 36.363637 66.575184 57.0 5.761925 4.38462 ... 51.553955 \n",
"\n",
" tempo duration_ms time_signature Track_release_date Track_pop \\\n",
"0 38.882252 2.999022 64.000001 78.950001 67.777777 \n",
"\n",
" Artist_pop genre|modern_rock genre|permanent_wave genre|rock \n",
"0 56.650001 16.453125 17.1875 43.09375 \n",
"\n",
"[1 rows x 22 columns]"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"playvec=pd.DataFrame(test.sum(axis=0)).T\n",
"playvec"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 0 | \n",
" track_name | \n",
" artist_name | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 0 | \n",
" Don't Stop Me Now - Remastered 2011 | \n",
" Queen | \n",
"
\n",
" \n",
" | 0 | \n",
" 1 | \n",
" Another One Bites The Dust - Remastered 2011 | \n",
" Queen | \n",
"
\n",
" \n",
" | 0 | \n",
" 2 | \n",
" It's My Life | \n",
" Bon Jovi | \n",
"
\n",
" \n",
" | 0 | \n",
" 3 | \n",
" Can You Feel My Heart | \n",
" Bring Me The Horizon | \n",
"
\n",
" \n",
" | 0 | \n",
" 4 | \n",
" Sweet Child O' Mine | \n",
" Guns N' Roses | \n",
"
\n",
" \n",
" | 0 | \n",
" 5 | \n",
" Welcome To The Jungle | \n",
" Guns N' Roses | \n",
"
\n",
" \n",
" | 0 | \n",
" 6 | \n",
" Highway to Hell | \n",
" AC/DC | \n",
"
\n",
" \n",
" | 0 | \n",
" 7 | \n",
" Can't Help Falling in Love | \n",
" Elvis Presley | \n",
"
\n",
" \n",
" | 0 | \n",
" 8 | \n",
" Have You Ever Seen The Rain | \n",
" Creedence Clearwater Revival | \n",
"
\n",
" \n",
" | 0 | \n",
" 9 | \n",
" Fortunate Son | \n",
" Creedence Clearwater Revival | \n",
"
\n",
" \n",
" | 0 | \n",
" 10 | \n",
" Dreams - 2004 Remaster | \n",
" Fleetwood Mac | \n",
"
\n",
" \n",
" | 0 | \n",
" 11 | \n",
" Dream On | \n",
" Aerosmith | \n",
"
\n",
" \n",
" | 0 | \n",
" 12 | \n",
" Change (In the House of Flies) | \n",
" Deftones | \n",
"
\n",
" \n",
" | 0 | \n",
" 13 | \n",
" Under Pressure - Remastered 2011 | \n",
" Queen | \n",
"
\n",
" \n",
" | 0 | \n",
" 14 | \n",
" Crazy Little Thing Called Love - Remastered 2011 | \n",
" Queen | \n",
"
\n",
" \n",
" | 0 | \n",
" 15 | \n",
" We Will Rock You - Remastered 2011 | \n",
" Queen | \n",
"
\n",
" \n",
" | 0 | \n",
" 16 | \n",
" T.N.T. | \n",
" AC/DC | \n",
"
\n",
" \n",
" | 0 | \n",
" 17 | \n",
" Suspicious Minds | \n",
" Elvis Presley | \n",
"
\n",
" \n",
" | 0 | \n",
" 18 | \n",
" Paradise City | \n",
" Guns N' Roses | \n",
"
\n",
" \n",
" | 0 | \n",
" 19 | \n",
" Hotel California - 2013 Remaster | \n",
" Eagles | \n",
"
\n",
" \n",
" | 0 | \n",
" 20 | \n",
" Africa | \n",
" TOTO | \n",
"
\n",
" \n",
" | 0 | \n",
" 21 | \n",
" Go Your Own Way - 2004 Remaster | \n",
" Fleetwood Mac | \n",
"
\n",
" \n",
" | 0 | \n",
" 22 | \n",
" We Didn't Start the Fire | \n",
" Billy Joel | \n",
"
\n",
" \n",
" | 0 | \n",
" 23 | \n",
" Uptown Girl | \n",
" Billy Joel | \n",
"
\n",
" \n",
" | 0 | \n",
" 24 | \n",
" Take It Easy - 2013 Remaster | \n",
" Eagles | \n",
"
\n",
" \n",
" | 0 | \n",
" 25 | \n",
" Freak On a Leash | \n",
" Korn | \n",
"
\n",
" \n",
" | 0 | \n",
" 26 | \n",
" Everywhere - 2017 Remaster | \n",
" Fleetwood Mac | \n",
"
\n",
" \n",
" | 0 | \n",
" 27 | \n",
" My Own Summer (Shove It) | \n",
" Deftones | \n",
"
\n",
" \n",
" | 0 | \n",
" 28 | \n",
" Another Brick in the Wall, Pt. 2 | \n",
" Pink Floyd | \n",
"
\n",
" \n",
" | 0 | \n",
" 29 | \n",
" Crazy | \n",
" Aerosmith | \n",
"
\n",
" \n",
" | 0 | \n",
" 30 | \n",
" Vienna | \n",
" Billy Joel | \n",
"
\n",
" \n",
" | 0 | \n",
" 31 | \n",
" Be Quiet and Drive (Far Away) | \n",
" Deftones | \n",
"
\n",
" \n",
" | 0 | \n",
" 32 | \n",
" Wish You Were Here | \n",
" Pink Floyd | \n",
"
\n",
" \n",
" | 0 | \n",
" 33 | \n",
" Stairway to Heaven - Remaster | \n",
" Led Zeppelin | \n",
"
\n",
" \n",
" | 0 | \n",
" 34 | \n",
" Landslide | \n",
" Fleetwood Mac | \n",
"
\n",
" \n",
" | 0 | \n",
" 35 | \n",
" Rock N Roll Train | \n",
" AC/DC | \n",
"
\n",
" \n",
" | 0 | \n",
" 36 | \n",
" The Rock Show | \n",
" blink-182 | \n",
"
\n",
" \n",
" | 0 | \n",
" 37 | \n",
" I Miss You | \n",
" blink-182 | \n",
"
\n",
" \n",
" | 0 | \n",
" 38 | \n",
" Bulls On Parade | \n",
" Rage Against The Machine | \n",
"
\n",
" \n",
" | 0 | \n",
" 39 | \n",
" Man in the Box | \n",
" Alice In Chains | \n",
"
\n",
" \n",
" | 0 | \n",
" 40 | \n",
" Brown Eyed Girl | \n",
" Van Morrison | \n",
"
\n",
" \n",
" | 0 | \n",
" 41 | \n",
" You Make My Dreams (Come True) | \n",
" Daryl Hall & John Oates | \n",
"
\n",
" \n",
" | 0 | \n",
" 42 | \n",
" Free Bird | \n",
" Lynyrd Skynyrd | \n",
"
\n",
" \n",
" | 0 | \n",
" 43 | \n",
" Hold the Line | \n",
" TOTO | \n",
"
\n",
" \n",
" | 0 | \n",
" 44 | \n",
" Mrs. Robinson - From \"The Graduate\" Soundtrack | \n",
" Simon & Garfunkel | \n",
"
\n",
" \n",
" | 0 | \n",
" 45 | \n",
" Stand by Me | \n",
" Ben E. King | \n",
"
\n",
" \n",
" | 0 | \n",
" 46 | \n",
" Sweet Dreams (Are Made of This) - Remastered | \n",
" Eurythmics | \n",
"
\n",
" \n",
" | 0 | \n",
" 47 | \n",
" Cherry Waves | \n",
" Deftones | \n",
"
\n",
" \n",
" | 0 | \n",
" 48 | \n",
" Rosemary | \n",
" Deftones | \n",
"
\n",
" \n",
" | 0 | \n",
" 49 | \n",
" It's a Long Way to the Top (If You Wanna Rock ... | \n",
" AC/DC | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 0 track_name \\\n",
"0 0 Don't Stop Me Now - Remastered 2011 \n",
"0 1 Another One Bites The Dust - Remastered 2011 \n",
"0 2 It's My Life \n",
"0 3 Can You Feel My Heart \n",
"0 4 Sweet Child O' Mine \n",
"0 5 Welcome To The Jungle \n",
"0 6 Highway to Hell \n",
"0 7 Can't Help Falling in Love \n",
"0 8 Have You Ever Seen The Rain \n",
"0 9 Fortunate Son \n",
"0 10 Dreams - 2004 Remaster \n",
"0 11 Dream On \n",
"0 12 Change (In the House of Flies) \n",
"0 13 Under Pressure - Remastered 2011 \n",
"0 14 Crazy Little Thing Called Love - Remastered 2011 \n",
"0 15 We Will Rock You - Remastered 2011 \n",
"0 16 T.N.T. \n",
"0 17 Suspicious Minds \n",
"0 18 Paradise City \n",
"0 19 Hotel California - 2013 Remaster \n",
"0 20 Africa \n",
"0 21 Go Your Own Way - 2004 Remaster \n",
"0 22 We Didn't Start the Fire \n",
"0 23 Uptown Girl \n",
"0 24 Take It Easy - 2013 Remaster \n",
"0 25 Freak On a Leash \n",
"0 26 Everywhere - 2017 Remaster \n",
"0 27 My Own Summer (Shove It) \n",
"0 28 Another Brick in the Wall, Pt. 2 \n",
"0 29 Crazy \n",
"0 30 Vienna \n",
"0 31 Be Quiet and Drive (Far Away) \n",
"0 32 Wish You Were Here \n",
"0 33 Stairway to Heaven - Remaster \n",
"0 34 Landslide \n",
"0 35 Rock N Roll Train \n",
"0 36 The Rock Show \n",
"0 37 I Miss You \n",
"0 38 Bulls On Parade \n",
"0 39 Man in the Box \n",
"0 40 Brown Eyed Girl \n",
"0 41 You Make My Dreams (Come True) \n",
"0 42 Free Bird \n",
"0 43 Hold the Line \n",
"0 44 Mrs. Robinson - From \"The Graduate\" Soundtrack \n",
"0 45 Stand by Me \n",
"0 46 Sweet Dreams (Are Made of This) - Remastered \n",
"0 47 Cherry Waves \n",
"0 48 Rosemary \n",
"0 49 It's a Long Way to the Top (If You Wanna Rock ... \n",
"\n",
" artist_name \n",
"0 Queen \n",
"0 Queen \n",
"0 Bon Jovi \n",
"0 Bring Me The Horizon \n",
"0 Guns N' Roses \n",
"0 Guns N' Roses \n",
"0 AC/DC \n",
"0 Elvis Presley \n",
"0 Creedence Clearwater Revival \n",
"0 Creedence Clearwater Revival \n",
"0 Fleetwood Mac \n",
"0 Aerosmith \n",
"0 Deftones \n",
"0 Queen \n",
"0 Queen \n",
"0 Queen \n",
"0 AC/DC \n",
"0 Elvis Presley \n",
"0 Guns N' Roses \n",
"0 Eagles \n",
"0 TOTO \n",
"0 Fleetwood Mac \n",
"0 Billy Joel \n",
"0 Billy Joel \n",
"0 Eagles \n",
"0 Korn \n",
"0 Fleetwood Mac \n",
"0 Deftones \n",
"0 Pink Floyd \n",
"0 Aerosmith \n",
"0 Billy Joel \n",
"0 Deftones \n",
"0 Pink Floyd \n",
"0 Led Zeppelin \n",
"0 Fleetwood Mac \n",
"0 AC/DC \n",
"0 blink-182 \n",
"0 blink-182 \n",
"0 Rage Against The Machine \n",
"0 Alice In Chains \n",
"0 Van Morrison \n",
"0 Daryl Hall & John Oates \n",
"0 Lynyrd Skynyrd \n",
"0 TOTO \n",
"0 Simon & Garfunkel \n",
"0 Ben E. King \n",
"0 Eurythmics \n",
"0 Deftones \n",
"0 Deftones \n",
"0 AC/DC "
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['sim']=cosine_similarity(df.drop(['track_uri', 'artist_uri', 'album_uri'], axis = 1),playvec.drop(['track_uri', 'artist_uri', 'album_uri'], axis = 1))\n",
"df['sim2']=cosine_similarity(df.iloc[:,16:-1],playvec.iloc[:,16:])\n",
"df['sim3']=cosine_similarity(df.iloc[:,19:-2],playvec.iloc[:,19:])\n",
"df = df.sort_values(['sim3','sim2','sim'],ascending = False,kind='stable')\n",
"qq=df.groupby('artist_uri').head(5).track_uri.head(50) #to limit recmmendation by same artist\n",
"aa=sp.tracks(qq[0:50])\n",
"Fresult=pd.DataFrame()\n",
"for i in range(50):\n",
" result=pd.DataFrame([i])\n",
" result['track_name']=aa['tracks'][i]['name']\n",
" result['artist_name']=aa['tracks'][i]['artists'][0]['name']\n",
" #result['url']=aa['tracks'][i]['external_urls']['spotify']\n",
" #result['image']=aa['tracks'][i]['album']['images'][1]['url']\n",
" Fresult=pd.concat([Fresult,result],axis=0)\n",
"Fresult"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 0 | \n",
" track_name | \n",
" artist_name | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 0 | \n",
" Go Your Own Way - 2004 Remaster | \n",
" Fleetwood Mac | \n",
"
\n",
" \n",
" | 0 | \n",
" 1 | \n",
" We Didn't Start the Fire | \n",
" Billy Joel | \n",
"
\n",
" \n",
" | 0 | \n",
" 2 | \n",
" Gimme All Your Lovin' | \n",
" ZZ Top | \n",
"
\n",
" \n",
" | 0 | \n",
" 3 | \n",
" Thorn in My Side - Remastered | \n",
" Eurythmics | \n",
"
\n",
" \n",
" | 0 | \n",
" 4 | \n",
" Any Way You Want It | \n",
" Journey | \n",
"
\n",
" \n",
" | 0 | \n",
" 5 | \n",
" I Don't Wanna Stop | \n",
" Ozzy Osbourne | \n",
"
\n",
" \n",
" | 0 | \n",
" 6 | \n",
" Semi-Charmed Life | \n",
" Third Eye Blind | \n",
"
\n",
" \n",
" | 0 | \n",
" 7 | \n",
" Sweet Child O' Mine | \n",
" Guns N' Roses | \n",
"
\n",
" \n",
" | 0 | \n",
" 8 | \n",
" Old Time Rock & Roll | \n",
" Bob Seger | \n",
"
\n",
" \n",
" | 0 | \n",
" 9 | \n",
" Lump | \n",
" The Presidents Of The United States Of America | \n",
"
\n",
" \n",
" | 0 | \n",
" 10 | \n",
" Run-Around | \n",
" Blues Traveler | \n",
"
\n",
" \n",
" | 0 | \n",
" 11 | \n",
" Kyouran Hey Kids!! | \n",
" THE ORAL CIGARETTES | \n",
"
\n",
" \n",
" | 0 | \n",
" 12 | \n",
" We're Not Gonna Take It | \n",
" Twisted Sister | \n",
"
\n",
" \n",
" | 0 | \n",
" 13 | \n",
" Legs - 2008 Remaster | \n",
" ZZ Top | \n",
"
\n",
" \n",
" | 0 | \n",
" 14 | \n",
" Highway Tune | \n",
" Greta Van Fleet | \n",
"
\n",
" \n",
" | 0 | \n",
" 15 | \n",
" Sharp Dressed Man - 2008 Remaster | \n",
" ZZ Top | \n",
"
\n",
" \n",
" | 0 | \n",
" 16 | \n",
" Owner of a Lonely Heart | \n",
" Yes | \n",
"
\n",
" \n",
" | 0 | \n",
" 17 | \n",
" Mighty Wings - From \"Top Gun\" Original Soundtrack | \n",
" Cheap Trick | \n",
"
\n",
" \n",
" | 0 | \n",
" 18 | \n",
" Living After Midnight | \n",
" Judas Priest | \n",
"
\n",
" \n",
" | 0 | \n",
" 19 | \n",
" I Believe in a Thing Called Love | \n",
" The Darkness | \n",
"
\n",
" \n",
" | 0 | \n",
" 20 | \n",
" Dance the Night Away - 2015 Remaster | \n",
" Van Halen | \n",
"
\n",
" \n",
" | 0 | \n",
" 21 | \n",
" Pink Houses | \n",
" John Mellencamp | \n",
"
\n",
" \n",
" | 0 | \n",
" 22 | \n",
" How I Could Just Kill a Man | \n",
" Rage Against The Machine | \n",
"
\n",
" \n",
" | 0 | \n",
" 23 | \n",
" Stiff Upper Lip | \n",
" AC/DC | \n",
"
\n",
" \n",
" | 0 | \n",
" 24 | \n",
" More Than This | \n",
" Roxy Music | \n",
"
\n",
" \n",
" | 0 | \n",
" 25 | \n",
" Black Smoke Rising | \n",
" Greta Van Fleet | \n",
"
\n",
" \n",
" | 0 | \n",
" 26 | \n",
" We're An American Band - Remastered 2002 | \n",
" Grand Funk Railroad | \n",
"
\n",
" \n",
" | 0 | \n",
" 27 | \n",
" Never Let You Go - 2008 Remaster | \n",
" Third Eye Blind | \n",
"
\n",
" \n",
" | 0 | \n",
" 28 | \n",
" White Wedding - Pt. 1 | \n",
" Billy Idol | \n",
"
\n",
" \n",
" | 0 | \n",
" 29 | \n",
" Hey Tonight | \n",
" Creedence Clearwater Revival | \n",
"
\n",
" \n",
" | 0 | \n",
" 30 | \n",
" Shot in the Dark | \n",
" Ozzy Osbourne | \n",
"
\n",
" \n",
" | 0 | \n",
" 31 | \n",
" Rock And Roll Never Forgets | \n",
" Bob Seger | \n",
"
\n",
" \n",
" | 0 | \n",
" 32 | \n",
" Peace of Mind | \n",
" Boston | \n",
"
\n",
" \n",
" | 0 | \n",
" 33 | \n",
" Carry on Wayward Son | \n",
" Kansas | \n",
"
\n",
" \n",
" | 0 | \n",
" 34 | \n",
" Hollywood Nights | \n",
" Bob Seger | \n",
"
\n",
" \n",
" | 0 | \n",
" 35 | \n",
" Against The Wind | \n",
" Bob Seger | \n",
"
\n",
" \n",
" | 0 | \n",
" 36 | \n",
" Bad Medicine | \n",
" Bon Jovi | \n",
"
\n",
" \n",
" | 0 | \n",
" 37 | \n",
" Have A Nice Day | \n",
" Bon Jovi | \n",
"
\n",
" \n",
" | 0 | \n",
" 38 | \n",
" Touch Too Much | \n",
" AC/DC | \n",
"
\n",
" \n",
" | 0 | \n",
" 39 | \n",
" Come Undone | \n",
" Duran Duran | \n",
"
\n",
" \n",
" | 0 | \n",
" 40 | \n",
" All I Wanna Do Is Make Love To You | \n",
" Heart | \n",
"
\n",
" \n",
" | 0 | \n",
" 41 | \n",
" No Excuses | \n",
" Alice In Chains | \n",
"
\n",
" \n",
" | 0 | \n",
" 42 | \n",
" Walk This Way | \n",
" Aerosmith | \n",
"
\n",
" \n",
" | 0 | \n",
" 43 | \n",
" Two Princes | \n",
" Spin Doctors | \n",
"
\n",
" \n",
" | 0 | \n",
" 44 | \n",
" Girls on Film - 2010 Remaster | \n",
" Duran Duran | \n",
"
\n",
" \n",
" | 0 | \n",
" 45 | \n",
" Jamie's Cryin' - 2015 Remaster | \n",
" Van Halen | \n",
"
\n",
" \n",
" | 0 | \n",
" 46 | \n",
" Who Says You Can't Go Home | \n",
" Bon Jovi | \n",
"
\n",
" \n",
" | 0 | \n",
" 47 | \n",
" Anna Molly | \n",
" Incubus | \n",
"
\n",
" \n",
" | 0 | \n",
" 48 | \n",
" Ace of Spades | \n",
" Motörhead | \n",
"
\n",
" \n",
" | 0 | \n",
" 49 | \n",
" Fallen Angel - Remastered | \n",
" Poison | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 0 track_name \\\n",
"0 0 Go Your Own Way - 2004 Remaster \n",
"0 1 We Didn't Start the Fire \n",
"0 2 Gimme All Your Lovin' \n",
"0 3 Thorn in My Side - Remastered \n",
"0 4 Any Way You Want It \n",
"0 5 I Don't Wanna Stop \n",
"0 6 Semi-Charmed Life \n",
"0 7 Sweet Child O' Mine \n",
"0 8 Old Time Rock & Roll \n",
"0 9 Lump \n",
"0 10 Run-Around \n",
"0 11 Kyouran Hey Kids!! \n",
"0 12 We're Not Gonna Take It \n",
"0 13 Legs - 2008 Remaster \n",
"0 14 Highway Tune \n",
"0 15 Sharp Dressed Man - 2008 Remaster \n",
"0 16 Owner of a Lonely Heart \n",
"0 17 Mighty Wings - From \"Top Gun\" Original Soundtrack \n",
"0 18 Living After Midnight \n",
"0 19 I Believe in a Thing Called Love \n",
"0 20 Dance the Night Away - 2015 Remaster \n",
"0 21 Pink Houses \n",
"0 22 How I Could Just Kill a Man \n",
"0 23 Stiff Upper Lip \n",
"0 24 More Than This \n",
"0 25 Black Smoke Rising \n",
"0 26 We're An American Band - Remastered 2002 \n",
"0 27 Never Let You Go - 2008 Remaster \n",
"0 28 White Wedding - Pt. 1 \n",
"0 29 Hey Tonight \n",
"0 30 Shot in the Dark \n",
"0 31 Rock And Roll Never Forgets \n",
"0 32 Peace of Mind \n",
"0 33 Carry on Wayward Son \n",
"0 34 Hollywood Nights \n",
"0 35 Against The Wind \n",
"0 36 Bad Medicine \n",
"0 37 Have A Nice Day \n",
"0 38 Touch Too Much \n",
"0 39 Come Undone \n",
"0 40 All I Wanna Do Is Make Love To You \n",
"0 41 No Excuses \n",
"0 42 Walk This Way \n",
"0 43 Two Princes \n",
"0 44 Girls on Film - 2010 Remaster \n",
"0 45 Jamie's Cryin' - 2015 Remaster \n",
"0 46 Who Says You Can't Go Home \n",
"0 47 Anna Molly \n",
"0 48 Ace of Spades \n",
"0 49 Fallen Angel - Remastered \n",
"\n",
" artist_name \n",
"0 Fleetwood Mac \n",
"0 Billy Joel \n",
"0 ZZ Top \n",
"0 Eurythmics \n",
"0 Journey \n",
"0 Ozzy Osbourne \n",
"0 Third Eye Blind \n",
"0 Guns N' Roses \n",
"0 Bob Seger \n",
"0 The Presidents Of The United States Of America \n",
"0 Blues Traveler \n",
"0 THE ORAL CIGARETTES \n",
"0 Twisted Sister \n",
"0 ZZ Top \n",
"0 Greta Van Fleet \n",
"0 ZZ Top \n",
"0 Yes \n",
"0 Cheap Trick \n",
"0 Judas Priest \n",
"0 The Darkness \n",
"0 Van Halen \n",
"0 John Mellencamp \n",
"0 Rage Against The Machine \n",
"0 AC/DC \n",
"0 Roxy Music \n",
"0 Greta Van Fleet \n",
"0 Grand Funk Railroad \n",
"0 Third Eye Blind \n",
"0 Billy Idol \n",
"0 Creedence Clearwater Revival \n",
"0 Ozzy Osbourne \n",
"0 Bob Seger \n",
"0 Boston \n",
"0 Kansas \n",
"0 Bob Seger \n",
"0 Bob Seger \n",
"0 Bon Jovi \n",
"0 Bon Jovi \n",
"0 AC/DC \n",
"0 Duran Duran \n",
"0 Heart \n",
"0 Alice In Chains \n",
"0 Aerosmith \n",
"0 Spin Doctors \n",
"0 Duran Duran \n",
"0 Van Halen \n",
"0 Bon Jovi \n",
"0 Incubus \n",
"0 Motörhead \n",
"0 Poison "
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['sim']=cosine_similarity(df.iloc[:,3:16],playvec.iloc[:,3:16])\n",
"df['sim2']=cosine_similarity(df.loc[:, df.columns.str.startswith('T')|df.columns.str.startswith('A')],playvec.loc[:, playvec.columns.str.startswith('T')|playvec.columns.str.startswith('A')])\n",
"df['sim3']=cosine_similarity(df.loc[:, df.columns.str.startswith('genre')],playvec.loc[:, playvec.columns.str.startswith('genre')])\n",
"df['sim4']=(df['sim']+df['sim2']+df['sim3'])/3\n",
"df = df.sort_values(['sim4'],ascending = False,kind='stable')\n",
"# genra>audio>pop\n",
"qq=df.groupby('artist_uri').head(5).track_uri.head(50)\n",
"aa=sp.tracks(qq[0:50])\n",
"Fresult=pd.DataFrame()\n",
"for i in range(50):\n",
" result=pd.DataFrame([i])\n",
" result['track_name']=aa['tracks'][i]['name']\n",
" result['artist_name']=aa['tracks'][i]['artists'][0]['name']\n",
" #result['url']=aa['tracks'][i]['external_urls']['spotify']\n",
" #result['image']=aa['tracks'][i]['album']['images'][1]['url']\n",
" Fresult=pd.concat([Fresult,result],axis=0)\n",
"Fresult"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 0 | \n",
" track_name | \n",
" artist_name | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 1 | \n",
" R.O.C.K. In The U.S.A. (A Salute To 60's Rock) | \n",
" John Mellencamp | \n",
"
\n",
" \n",
" | 0 | \n",
" 2 | \n",
" Blood On Blood | \n",
" Bon Jovi | \n",
"
\n",
" \n",
" | 0 | \n",
" 3 | \n",
" T.N.T. | \n",
" AC/DC | \n",
"
\n",
" \n",
" | 0 | \n",
" 4 | \n",
" Warning | \n",
" Green Day | \n",
"
\n",
" \n",
" | 0 | \n",
" 5 | \n",
" Turn The Page | \n",
" Metallica | \n",
"
\n",
" \n",
" | 0 | \n",
" 6 | \n",
" Forever Now | \n",
" Green Day | \n",
"
\n",
" \n",
" | 0 | \n",
" 7 | \n",
" Duality | \n",
" Slipknot | \n",
"
\n",
" \n",
" | 0 | \n",
" 8 | \n",
" Lit Up | \n",
" Buckcherry | \n",
"
\n",
" \n",
" | 0 | \n",
" 9 | \n",
" Plug in Baby | \n",
" Muse | \n",
"
\n",
" \n",
" | 0 | \n",
" 10 | \n",
" Wheel in the Sky | \n",
" Journey | \n",
"
\n",
" \n",
" | 0 | \n",
" 11 | \n",
" Start Me Up - Remastered 2009 | \n",
" The Rolling Stones | \n",
"
\n",
" \n",
" | 0 | \n",
" 12 | \n",
" I Was Made For Lovin' You | \n",
" KISS | \n",
"
\n",
" \n",
" | 0 | \n",
" 13 | \n",
" Since You've Been Gone | \n",
" The Outfield | \n",
"
\n",
" \n",
" | 0 | \n",
" 14 | \n",
" ERA | \n",
" The Faim | \n",
"
\n",
" \n",
" | 0 | \n",
" 15 | \n",
" King Of Wishful Thinking | \n",
" New Found Glory | \n",
"
\n",
" \n",
" | 0 | \n",
" 16 | \n",
" PMA (feat. Pale Waves) | \n",
" All Time Low | \n",
"
\n",
" \n",
" | 0 | \n",
" 17 | \n",
" Re-Education (Through Labor) | \n",
" Rise Against | \n",
"
\n",
" \n",
" | 0 | \n",
" 18 | \n",
" Situations | \n",
" Escape the Fate | \n",
"
\n",
" \n",
" | 0 | \n",
" 19 | \n",
" Lost | \n",
" Charlotte Sands | \n",
"
\n",
" \n",
" | 0 | \n",
" 20 | \n",
" Way Away | \n",
" Yellowcard | \n",
"
\n",
" \n",
" | 0 | \n",
" 21 | \n",
" Way Away | \n",
" Yellowcard | \n",
"
\n",
" \n",
" | 0 | \n",
" 22 | \n",
" Can't Fight This Feeling | \n",
" REO Speedwagon | \n",
"
\n",
" \n",
" | 0 | \n",
" 23 | \n",
" Rock America | \n",
" Danger Danger | \n",
"
\n",
" \n",
" | 0 | \n",
" 24 | \n",
" Until the Day I Die | \n",
" Story Of The Year | \n",
"
\n",
" \n",
" | 0 | \n",
" 25 | \n",
" Where Is My Mind? - Remastered | \n",
" Pixies | \n",
"
\n",
" \n",
" | 0 | \n",
" 26 | \n",
" You Don't Mess Around with Jim | \n",
" Jim Croce | \n",
"
\n",
" \n",
" | 0 | \n",
" 27 | \n",
" Maneater | \n",
" Daryl Hall & John Oates | \n",
"
\n",
" \n",
" | 0 | \n",
" 28 | \n",
" The Price | \n",
" Twisted Sister | \n",
"
\n",
" \n",
" | 0 | \n",
" 29 | \n",
" Heartbreaker - 1990 Remaster | \n",
" Led Zeppelin | \n",
"
\n",
" \n",
" | 0 | \n",
" 30 | \n",
" The Hardest Button to Button | \n",
" The White Stripes | \n",
"
\n",
" \n",
" | 0 | \n",
" 31 | \n",
" Ain't Talkin' 'Bout Love - 2015 Remaster | \n",
" Van Halen | \n",
"
\n",
" \n",
" | 0 | \n",
" 32 | \n",
" Where Is My Mind? - Remastered | \n",
" Pixies | \n",
"
\n",
" \n",
" | 0 | \n",
" 33 | \n",
" All Systems Go | \n",
" Box Car Racer | \n",
"
\n",
" \n",
" | 0 | \n",
" 34 | \n",
" May 16 | \n",
" Lagwagon | \n",
"
\n",
" \n",
" | 0 | \n",
" 35 | \n",
" Creepin Up The Backstairs - Composite Edit (do... | \n",
" The Fratellis | \n",
"
\n",
" \n",
" | 0 | \n",
" 36 | \n",
" Animal | \n",
" Neon Trees | \n",
"
\n",
" \n",
" | 0 | \n",
" 37 | \n",
" Watch The World | \n",
" Box Car Racer | \n",
"
\n",
" \n",
" | 0 | \n",
" 38 | \n",
" All My Fault | \n",
" Fenix TX | \n",
"
\n",
" \n",
" | 0 | \n",
" 39 | \n",
" Pork And Beans | \n",
" Weezer | \n",
"
\n",
" \n",
" | 0 | \n",
" 40 | \n",
" Mixtape 2003 | \n",
" The Academic | \n",
"
\n",
" \n",
" | 0 | \n",
" 41 | \n",
" 1+1 | \n",
" Scouting For Girls | \n",
"
\n",
" \n",
" | 0 | \n",
" 42 | \n",
" Be Still | \n",
" The Killers | \n",
"
\n",
" \n",
" | 0 | \n",
" 43 | \n",
" Only The Horses | \n",
" Scissor Sisters | \n",
"
\n",
" \n",
" | 0 | \n",
" 44 | \n",
" Mardy Bum | \n",
" Arctic Monkeys | \n",
"
\n",
" \n",
" | 0 | \n",
" 45 | \n",
" Blue Orchid | \n",
" The White Stripes | \n",
"
\n",
" \n",
" | 0 | \n",
" 46 | \n",
" Holy Wars...The Punishment Due - Remastered | \n",
" Megadeth | \n",
"
\n",
" \n",
" | 0 | \n",
" 47 | \n",
" Lucifer Sam | \n",
" Pink Floyd | \n",
"
\n",
" \n",
" | 0 | \n",
" 48 | \n",
" What Is and What Should Never Be - 29/6/69 Top... | \n",
" Led Zeppelin | \n",
"
\n",
" \n",
" | 0 | \n",
" 49 | \n",
" Peace of Mind | \n",
" Boston | \n",
"
\n",
" \n",
" | 0 | \n",
" 50 | \n",
" Alive | \n",
" Pearl Jam | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 0 track_name \\\n",
"0 1 R.O.C.K. In The U.S.A. (A Salute To 60's Rock) \n",
"0 2 Blood On Blood \n",
"0 3 T.N.T. \n",
"0 4 Warning \n",
"0 5 Turn The Page \n",
"0 6 Forever Now \n",
"0 7 Duality \n",
"0 8 Lit Up \n",
"0 9 Plug in Baby \n",
"0 10 Wheel in the Sky \n",
"0 11 Start Me Up - Remastered 2009 \n",
"0 12 I Was Made For Lovin' You \n",
"0 13 Since You've Been Gone \n",
"0 14 ERA \n",
"0 15 King Of Wishful Thinking \n",
"0 16 PMA (feat. Pale Waves) \n",
"0 17 Re-Education (Through Labor) \n",
"0 18 Situations \n",
"0 19 Lost \n",
"0 20 Way Away \n",
"0 21 Way Away \n",
"0 22 Can't Fight This Feeling \n",
"0 23 Rock America \n",
"0 24 Until the Day I Die \n",
"0 25 Where Is My Mind? - Remastered \n",
"0 26 You Don't Mess Around with Jim \n",
"0 27 Maneater \n",
"0 28 The Price \n",
"0 29 Heartbreaker - 1990 Remaster \n",
"0 30 The Hardest Button to Button \n",
"0 31 Ain't Talkin' 'Bout Love - 2015 Remaster \n",
"0 32 Where Is My Mind? - Remastered \n",
"0 33 All Systems Go \n",
"0 34 May 16 \n",
"0 35 Creepin Up The Backstairs - Composite Edit (do... \n",
"0 36 Animal \n",
"0 37 Watch The World \n",
"0 38 All My Fault \n",
"0 39 Pork And Beans \n",
"0 40 Mixtape 2003 \n",
"0 41 1+1 \n",
"0 42 Be Still \n",
"0 43 Only The Horses \n",
"0 44 Mardy Bum \n",
"0 45 Blue Orchid \n",
"0 46 Holy Wars...The Punishment Due - Remastered \n",
"0 47 Lucifer Sam \n",
"0 48 What Is and What Should Never Be - 29/6/69 Top... \n",
"0 49 Peace of Mind \n",
"0 50 Alive \n",
"\n",
" artist_name \n",
"0 John Mellencamp \n",
"0 Bon Jovi \n",
"0 AC/DC \n",
"0 Green Day \n",
"0 Metallica \n",
"0 Green Day \n",
"0 Slipknot \n",
"0 Buckcherry \n",
"0 Muse \n",
"0 Journey \n",
"0 The Rolling Stones \n",
"0 KISS \n",
"0 The Outfield \n",
"0 The Faim \n",
"0 New Found Glory \n",
"0 All Time Low \n",
"0 Rise Against \n",
"0 Escape the Fate \n",
"0 Charlotte Sands \n",
"0 Yellowcard \n",
"0 Yellowcard \n",
"0 REO Speedwagon \n",
"0 Danger Danger \n",
"0 Story Of The Year \n",
"0 Pixies \n",
"0 Jim Croce \n",
"0 Daryl Hall & John Oates \n",
"0 Twisted Sister \n",
"0 Led Zeppelin \n",
"0 The White Stripes \n",
"0 Van Halen \n",
"0 Pixies \n",
"0 Box Car Racer \n",
"0 Lagwagon \n",
"0 The Fratellis \n",
"0 Neon Trees \n",
"0 Box Car Racer \n",
"0 Fenix TX \n",
"0 Weezer \n",
"0 The Academic \n",
"0 Scouting For Girls \n",
"0 The Killers \n",
"0 Scissor Sisters \n",
"0 Arctic Monkeys \n",
"0 The White Stripes \n",
"0 Megadeth \n",
"0 Pink Floyd \n",
"0 Led Zeppelin \n",
"0 Boston \n",
"0 Pearl Jam "
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Spotifyresult=pd.DataFrame()\n",
"for i in range(len(test)-1):\n",
" if len(Spotifyresult)>=50:\n",
" break\n",
" ff=sp.recommendations(seed_tracks=list(test.track_uri[1+i:5+i]),limit=2)\n",
" for z in range(2):\n",
" result=pd.DataFrame([z+(2*i)+1])\n",
" result['track_name']=ff['tracks'][z]['name']\n",
" result['artist_name']=ff['tracks'][z]['artists'][0]['name']\n",
" #result['uri']=ff['tracks'][z]['id']\n",
" #result['url']=ff['tracks'][z]['external_urls']['spotify']\n",
" #result['image']=ff['tracks'][z]['album']['images'][1]['url']\n",
" Spotifyresult=pd.concat([Spotifyresult,result],axis=0)\n",
"Spotifyresult"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"sorry ur playlist must have atleast 5 tracks for this method to work"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.13 ('base')",
"language": "python",
"name": "python3"
},
"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.9.13"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "e246d2215c418239c9316a1ebf2d8abb44dc50b2e5b0e29defd87143398aa387"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}