Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Created Mar 3, 2021
Embed
What would you like to do?
class Spotify_Recommendation():
def __init__(self, dataset):
self.dataset = dataset
def recommend(self, songs, amount=1):
distance = []
song = self.dataset[(self.dataset.name.str.lower() == songs.lower())].head(1).values[0]
rec = self.dataset[self.dataset.name.str.lower() != songs.lower()]
for songs in tqdm(rec.values):
d = 0
for col in np.arange(len(rec.columns)):
if not col in [1, 6, 12, 14, 18]:
d = d + np.absolute(float(song[col]) - float(songs[col]))
distance.append(d)
rec['distance'] = distance
rec = rec.sort_values('distance')
columns = ['artists', 'name']
return rec[columns][:amount]
recommendations = Spotify_Recommendation(data)
recommendations.recommend("Lovers Rock", 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment