Music recommender
Million Song Dataset (MSD) is a freely available dataset that contains detailed information about a million songs, including metadata (title, artist, release year), audio features (e.g., tempo, key, loudness), and user listening history (such as play counts and user IDs). It's a valuable resource for music recommendation and analysis.
Creating a full-fledged music recommendation system based on the Million Song Dataset (MSD) requires several steps, including data preprocessing, feature engineering, model training, and serving recommendations. Below is a simplified example of how you can download a portion of the MSD and build a basic content-based recommendation system using Python.
Please note that this code provides a starting point and is not a comprehensive recommendation system. Developing a production-ready recommendation system typically involves more complex techniques and scalability considerations.
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import linear_kernel
# Download a subset of the Million Song Dataset (MSD)
# You can download the data from the MSD website: http://millionsongdataset.com/pages/getting-dataset/#subset
# This code assumes you have a CSV file with song metadata, e.g., 'subset_msd_summary.csv'
msd_data = pd.read_csv('subset_msd_summary.csv')
# Select relevant features for content-based recommendation
selected_features = ['artist_name', 'title', 'release', 'year']
# Fill missing values with empty strings
msd_data[selected_features] = msd_data[selected_features].fillna('')
# Create a new feature 'content' by combining selected features
msd_data['content'] = msd_data['artist_name'] + ' ' + msd_data['title'] + ' ' + msd_data['release'] + ' ' + msd_data['year'].astype(str)
# Create a TF-IDF vectorizer to convert 'content' into numerical vectors
tfidf_vectorizer = TfidfVectorizer(stop_words='english')
tfidf_matrix = tfidf_vectorizer.fit_transform(msd_data['content'])
# Compute cosine similarities between songs based on TF-IDF vectors
cosine_similarities = linear_kernel(tfidf_matrix, tfidf_matrix)
# Create a mapping of song titles to their corresponding indices
indices = pd.Series(msd_data.index, index=msd_data['title']).drop_duplicates()
# Function to get song recommendations based on input song title
def get_recommendations(title, cosine_sim=cosine_similarities):
idx = indices[title]
sim_scores = list(enumerate(cosine_sim[idx]))
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
sim_scores = sim_scores[1:11] # Get the top 10 most similar songs (excluding itself)
song_indices = [i[0] for i in sim_scores]
return msd_data['title'].iloc[song_indices]
# Example usage:
input_song = "Bohemian Rhapsody" # Replace with the song title of your choice
recommendations = get_recommendations(input_song)
print("Recommended songs for:", input_song)
print(recommendations)
What does this code do?
- We load a subset of the Million Song Dataset (MSD) from a CSV file containing song metadata.
- We select relevant features for content-based recommendation (artist name, title, release, and year) and create a new feature 'content' by combining them.
- We use TF-IDF vectorization to convert the 'content' text data into numerical vectors.
- We compute cosine similarities between songs based on their TF-IDF vectors.
- The "get_recommendations" function takes a song title as input and returns a list of recommended songs based on cosine similarities.