0% found this document useful (0 votes)
55 views8 pages

Music Recommendation System with KNN & K-Means

The document outlines a mini project for building a music recommendation system using Python with machine learning algorithms such as K-Nearest Neighbors and K-Means clustering. It details the objectives, software requirements, and algorithms for preprocessing music data, implementing models, and generating recommendations based on user input. The project includes code snippets for data cleaning, model training, and a recommendation interface.

Uploaded by

rkaviya008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views8 pages

Music Recommendation System with KNN & K-Means

The document outlines a mini project for building a music recommendation system using Python with machine learning algorithms such as K-Nearest Neighbors and K-Means clustering. It details the objectives, software requirements, and algorithms for preprocessing music data, implementing models, and generating recommendations based on user input. The project includes code snippets for data cleaning, model training, and a recommendation interface.

Uploaded by

rkaviya008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

MEENAKSHI SUNDARARAJAN ENGINEERING COLLEGE

(An autonomous Institution)


#363, Arcot Road, Kodambakkam, Chennai – 600024, Tamil Nadu, India

Department : IT Lab Name: CS3491 – Artificial Intelligence and Machine Learning Lab

EX NO: 13 MINI PROJECT - BUILD A MUSIC RECOMMENDATION


DATE: SYSTEM USING KNN AND K-MEANS CLUSTERING
AIM:
To write a Python program to build a music recommendation system using machine learning
algorithms like Nearest Neighbors and K-Means clustering based on features such as language,
genre, artist, and mood.

OBJECTIVES:
 To preprocess and scale music feature data for effective similarity measurement.
 To implement Nearest Neighbors for recommending similar songs based on input features.
 To apply K-Means clustering to group similar songs and improve recommendation accuracy.
 To evaluate clustering quality using silhouette score and optimize the number of clusters.

SOFTWARE REQUIRED:
 Jupyter Notebook / Python environment
 Libraries: scikit-learn, pandas, numpy

DESCRIPTION (MAPPING THE THEORY):


A music recommendation system identifies songs similar to a user’s preference by measuring
proximity in feature space. Nearest Neighbors finds the closest songs based on feature similarity,
while K-Means clustering groups songs into clusters with similar attributes. Scaling features ensures
all attributes contribute equally to the distance metric. Evaluating cluster quality using silhouette
scores helps in selecting the optimal number of clusters for better recommendations.

ALGORITHM:
Step 1: Import necessary libraries
Step 2: Load the dataset containing music features like language, genre, artist, mood.
Step 3: Preprocess the data
 Handle missing values if any
 Encode categorical features (e.g., one-hot encoding or label encoding)
 Scale features using StandardScaler for uniformity
Step 4: Apply K-Means clustering
 Use the elbow method or silhouette score to determine optimal number of clusters
 Fit K-Means model to the scaled data
MEENAKSHI SUNDARARAJAN ENGINEERING COLLEGE
(An autonomous Institution)
#363, Arcot Road, Kodambakkam, Chennai – 600024, Tamil Nadu, India

Department : IT Lab Name: CS3491 – Artificial Intelligence and Machine Learning Lab
Step 5: Evaluate clusters using silhouette score by adjust number of clusters if needed for better
cohesion and separation
Step 6: Implement Nearest Neighbors model by fit Nearest Neighbors to the scaled data to find
songs similar to input features
Step 7: Generate recommendations for a given song or user input, find nearest neighbors and
recommend those songs

PROGRAM:
data_cleaning.py
# data_cleaning.py
import pandas as pd

# Load the dataset


df = pd.read_csv("[Link]")

# Drop duplicates and nulls


df.drop_duplicates(inplace=True)
[Link](inplace=True)

# Clean artist names


df['artists'] = df['artists'].[Link]().[Link]()

# Normalize float columns


numeric_cols = ['valence', 'acousticness', 'danceability', 'duration_ms', 'energy',
'instrumentalness', 'liveness', 'loudness', 'speechiness', 'tempo']

df[numeric_cols] = df[numeric_cols].astype(float)

# Scale tempo and loudness for uniformity


from [Link] import MinMaxScaler
scaler = MinMaxScaler()
df[['tempo', 'loudness']] = scaler.fit_transform(df[['tempo', 'loudness']])

# Save cleaned version


df.to_csv("cleaned_music_dataset.csv", index=False)
print("✅ Data cleaned and saved as 'cleaned_music_dataset.csv'")

train_model.py
# train_model.py
MEENAKSHI SUNDARARAJAN ENGINEERING COLLEGE
(An autonomous Institution)
#363, Arcot Road, Kodambakkam, Chennai – 600024, Tamil Nadu, India

Department : IT Lab Name: CS3491 – Artificial Intelligence and Machine Learning Lab
import pandas as pd
import joblib
from [Link] import NearestNeighbors
from [Link] import StandardScaler
from [Link] import KMeans
from [Link] import silhouette_score

# Load cleaned dataset


df = pd.read_csv("cleaned_music_dataset.csv")

# Feature selection
features = ['valence', 'acousticness', 'danceability', 'energy',
'instrumentalness', 'liveness', 'loudness', 'speechiness', 'tempo']

# Drop features with zero variance


features = [f for f in features if df[f].nunique() > 1]

print("✅ Using features:", features)

X = df[features]

# Normalize features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Fit Nearest Neighbors model


model = NearestNeighbors(n_neighbors=6, metric='euclidean')
[Link](X_scaled)

# Evaluation using Silhouette Score (via KMeans clusters)


kmeans = KMeans(n_clusters=10, random_state=42, n_init=10)
[Link](X_scaled)
score = silhouette_score(X_scaled, kmeans.labels_)

print(f"\n✅ Model trained on {len(df)} songs.")


print(f"📈 Silhouette Score (clustering quality check): {score:.4f}")
print("\n📌 Example songs:\n", df[['name', 'artists']].head())

# Save model and assets


[Link](model, 'knn_model.pkl')
[Link](scaler, '[Link]')
[Link](df, 'music_df.pkl')
MEENAKSHI SUNDARARAJAN ENGINEERING COLLEGE
(An autonomous Institution)
#363, Arcot Road, Kodambakkam, Chennai – 600024, Tamil Nadu, India

Department : IT Lab Name: CS3491 – Artificial Intelligence and Machine Learning Lab
# Optional: Save scaled dataset for future use
scaled_df = [Link](X_scaled, columns=features)
scaled_df['name'] = df['name']
scaled_df['artists'] = df['artists']
scaled_df.to_csv("scaled_music_dataset.csv", index=False)

print("\n💾 All assets saved: knn_model.pkl, [Link], music_df.pkl, scaled_music_dataset.csv")

[Link]
# [Link]
import joblib
import pandas as pd
import numpy as np

# Load saved models and data


model = [Link]('knn_model.pkl')
scaler = [Link]('[Link]')
df = [Link]('music_df.pkl')

# Define moods and genres manually (basic mock)


mood_map = {
"happy": {"valence": 0.9, "energy": 0.8},
"sad": {"valence": 0.2, "energy": 0.3},
"chill": {"valence": 0.5, "energy": 0.4},
"party": {"valence": 0.8, "energy": 0.9},
}

genre_keywords = {
"rock": ["rock", "metal", "grunge"],
"pop": ["pop", "dance", "electropop"],
"hiphop": ["hip hop", "rap", "trap"],
"jazz": ["jazz", "swing"],
"classical": ["classical", "symphony", "orchestra"]
}

# Helper to recommend similar songs by vector features


def recommend_by_vector(input_vector):
features = ['valence', 'acousticness', 'danceability', 'energy',
'instrumentalness', 'liveness', 'loudness', 'speechiness', 'tempo']
input_df = [Link]([input_vector], columns=features)
input_scaled = [Link](input_df)

distances, indices = [Link](input_scaled)


MEENAKSHI SUNDARARAJAN ENGINEERING COLLEGE
(An autonomous Institution)
#363, Arcot Road, Kodambakkam, Chennai – 600024, Tamil Nadu, India

Department : IT Lab Name: CS3491 – Artificial Intelligence and Machine Learning Lab
print("\n🎧 Recommended Songs:")
for i in indices[0][1:4]: # Top 3 excluding the first (closest)
print(f"- {[Link][i]['name']} by {[Link][i]['artists']}")

# Menu-driven CLI for recommendations


def recommend_menu():
print("\n🎶 Music Recommendation System")
print("1. Recommend by Artist")
print("2. Recommend by Mood")
print("3. Recommend by Genre")
print("4. Exit")

choice = input("Choose an option (1-4): ")

if choice == '1':
artist = input("🎤 Enter artist name: ").lower()
matches = df[df['artists'].[Link]().[Link](artist)]
if [Link]:
print("❌ No songs found for that artist.")
else:
songs = matches[['name', 'artists']].drop_duplicates().head(3)
print(f"\n🎧 Songs by {[Link]()}:")
for _, row in [Link]():
print(f"- {row['name']} by {row['artists']}")

elif choice == '2':


print("😊 Available moods: happy, sad, chill, party")
mood = input("Enter mood: ").lower()
if mood not in mood_map:
print("❌ Invalid mood.")
else:
mood_profile = mood_map[mood]
filtered = df[
(df['valence'] > mood_profile['valence'] - 0.2) &
(df['valence'] < mood_profile['valence'] + 0.2) &
(df['energy'] > mood_profile['energy'] - 0.2) &
(df['energy'] < mood_profile['energy'] + 0.2)
]
if [Link]:
print("❌ No songs matched the mood.")
else:
song = [Link](1).iloc[0]
vector = song[['valence', 'acousticness', 'danceability', 'energy',
MEENAKSHI SUNDARARAJAN ENGINEERING COLLEGE
(An autonomous Institution)
#363, Arcot Road, Kodambakkam, Chennai – 600024, Tamil Nadu, India

Department : IT Lab Name: CS3491 – Artificial Intelligence and Machine Learning Lab
'instrumentalness', 'liveness', 'loudness', 'speechiness', 'tempo']].values
recommend_by_vector(vector)

elif choice == '3':


print("🎼 Available genres: rock, pop, hiphop, jazz, classical")
genre = input("Enter genre: ").lower()
keywords = genre_keywords.get(genre, [])
if not keywords:
print("❌ Unknown genre.")
else:
found = df[df['name'].[Link]().[Link]('|'.join(keywords)) |
df['artists'].[Link]().[Link]('|'.join(keywords))]
if [Link]:
print("❌ No songs matched that genre.")
else:
song = [Link](1).iloc[0]
vector = song[['valence', 'acousticness', 'danceability', 'energy',
'instrumentalness', 'liveness', 'loudness', 'speechiness', 'tempo']].values
recommend_by_vector(vector)

elif choice == '4':


print("👋 Exiting. Enjoy the music!")
break
return
else:
print("❌ Invalid choice.")

input("\nPress Enter to return to menu...")


recommend_menu()

# Run the program


if __name__ == "__main__":
recommend_menu()

OUTPUT:
MEENAKSHI SUNDARARAJAN ENGINEERING COLLEGE
(An autonomous Institution)
#363, Arcot Road, Kodambakkam, Chennai – 600024, Tamil Nadu, India

Department : IT Lab Name: CS3491 – Artificial Intelligence and Machine Learning Lab
MEENAKSHI SUNDARARAJAN ENGINEERING COLLEGE
(An autonomous Institution)
#363, Arcot Road, Kodambakkam, Chennai – 600024, Tamil Nadu, India

Department : IT Lab Name: CS3491 – Artificial Intelligence and Machine Learning Lab

RESULT:
Thus a mini project to implement Music Recommendation System is executed successfully.

You might also like