From 1a2a34c8ec552e5923178de508144ddb46884ead Mon Sep 17 00:00:00 2001
From: MridulGupta75 <66024896+MridulGupta75@users.noreply.github.com>
Date: Thu, 6 Oct 2022 11:46:34 +0530
Subject: [PATCH] Added Music Recommandation System
---
.../Music Recommandation system.ipynb | 570 ++++++++++++++++++
1 file changed, 570 insertions(+)
create mode 100644 Python Codes/Music Recommandation system.ipynb
diff --git a/Python Codes/Music Recommandation system.ipynb b/Python Codes/Music Recommandation system.ipynb
new file mode 100644
index 0000000..0ee187f
--- /dev/null
+++ b/Python Codes/Music Recommandation system.ipynb
@@ -0,0 +1,570 @@
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": 163,
+ "id": "bbec132a",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import numpy as np\n",
+ "import pandas as pd"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 164,
+ "id": "2e799e1a",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from sklearn.feature_extraction.text import TfidfVectorizer\n",
+ "from sklearn.metrics.pairwise import cosine_similarity"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 165,
+ "id": "0a59f28b",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "songs=pd.read_csv(\"songdata.csv\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 166,
+ "id": "d07d684a",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " artist | \n",
+ " song | \n",
+ " link | \n",
+ " text | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 0 | \n",
+ " ABBA | \n",
+ " Ahe's My Kind Of Girl | \n",
+ " /a/abba/ahes+my+kind+of+girl_20598417.html | \n",
+ " Look at her face, it's a wonderful face \\r\\nA... | \n",
+ "
\n",
+ " \n",
+ " | 1 | \n",
+ " ABBA | \n",
+ " Andante, Andante | \n",
+ " /a/abba/andante+andante_20002708.html | \n",
+ " Take it easy with me, please \\r\\nTouch me gen... | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " ABBA | \n",
+ " As Good As New | \n",
+ " /a/abba/as+good+as+new_20003033.html | \n",
+ " I'll never know why I had to go \\r\\nWhy I had... | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " ABBA | \n",
+ " Bang | \n",
+ " /a/abba/bang_20598415.html | \n",
+ " Making somebody happy is a question of give an... | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " ABBA | \n",
+ " Bang-A-Boomerang | \n",
+ " /a/abba/bang+a+boomerang_20002668.html | \n",
+ " Making somebody happy is a question of give an... | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ " artist song link \\\n",
+ "0 ABBA Ahe's My Kind Of Girl /a/abba/ahes+my+kind+of+girl_20598417.html \n",
+ "1 ABBA Andante, Andante /a/abba/andante+andante_20002708.html \n",
+ "2 ABBA As Good As New /a/abba/as+good+as+new_20003033.html \n",
+ "3 ABBA Bang /a/abba/bang_20598415.html \n",
+ "4 ABBA Bang-A-Boomerang /a/abba/bang+a+boomerang_20002668.html \n",
+ "\n",
+ " text \n",
+ "0 Look at her face, it's a wonderful face \\r\\nA... \n",
+ "1 Take it easy with me, please \\r\\nTouch me gen... \n",
+ "2 I'll never know why I had to go \\r\\nWhy I had... \n",
+ "3 Making somebody happy is a question of give an... \n",
+ "4 Making somebody happy is a question of give an... "
+ ]
+ },
+ "execution_count": 166,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "songs.head()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 167,
+ "id": "f5585fc6",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "(57650, 4)"
+ ]
+ },
+ "execution_count": 167,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "songs.shape"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 168,
+ "id": "aff4b924",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "songs = songs.sample(n=5000).drop('link', axis=1).reset_index(drop=True)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 169,
+ "id": "277ee09b",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " artist | \n",
+ " song | \n",
+ " text | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 0 | \n",
+ " R. Kelly | \n",
+ " Break Up (Thats All We Do) | \n",
+ " Make up, and break up \\r\\nThat's all we do \\... | \n",
+ "
\n",
+ " \n",
+ " | 1 | \n",
+ " Zeromancer | \n",
+ " Mosquito Coil | \n",
+ " It's looking out \\r\\nFrom inside \\r\\nA Chris... | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " Great Big Sea | \n",
+ " Wave Over Wave | \n",
+ " Only name Table Rogers a share man am I \\r\\nO... | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " Dan Fogelberg | \n",
+ " Faces Of America | \n",
+ " There was a time, a simpler time \\r\\nWhen a m... | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " Elvis Costello | \n",
+ " Joe Porterhouse | \n",
+ " The children sit upon the stairs \\r\\nHigh abo... | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ " artist song \\\n",
+ "0 R. Kelly Break Up (Thats All We Do) \n",
+ "1 Zeromancer Mosquito Coil \n",
+ "2 Great Big Sea Wave Over Wave \n",
+ "3 Dan Fogelberg Faces Of America \n",
+ "4 Elvis Costello Joe Porterhouse \n",
+ "\n",
+ " text \n",
+ "0 Make up, and break up \\r\\nThat's all we do \\... \n",
+ "1 It's looking out \\r\\nFrom inside \\r\\nA Chris... \n",
+ "2 Only name Table Rogers a share man am I \\r\\nO... \n",
+ "3 There was a time, a simpler time \\r\\nWhen a m... \n",
+ "4 The children sit upon the stairs \\r\\nHigh abo... "
+ ]
+ },
+ "execution_count": 169,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "songs.head()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 170,
+ "id": "e11f1dbf",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "(5000, 3)"
+ ]
+ },
+ "execution_count": 170,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "songs.shape"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 171,
+ "id": "f2058edb",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "songs['text'] = songs['text'].str.replace('\\r', '')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 172,
+ "id": "187736e3",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "songs['text'] = songs['text'].str.replace('\\n', '')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 173,
+ "id": "6244e6a4",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " artist | \n",
+ " song | \n",
+ " text | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 0 | \n",
+ " R. Kelly | \n",
+ " Break Up (Thats All We Do) | \n",
+ " Make up, and break up That's all we do Then ... | \n",
+ "
\n",
+ " \n",
+ " | 1 | \n",
+ " Zeromancer | \n",
+ " Mosquito Coil | \n",
+ " It's looking out From inside A Christmas bon... | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " Great Big Sea | \n",
+ " Wave Over Wave | \n",
+ " Only name Table Rogers a share man am I On a ... | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " Dan Fogelberg | \n",
+ " Faces Of America | \n",
+ " There was a time, a simpler time When a man c... | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " Elvis Costello | \n",
+ " Joe Porterhouse | \n",
+ " The children sit upon the stairs High above a... | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ " artist song \\\n",
+ "0 R. Kelly Break Up (Thats All We Do) \n",
+ "1 Zeromancer Mosquito Coil \n",
+ "2 Great Big Sea Wave Over Wave \n",
+ "3 Dan Fogelberg Faces Of America \n",
+ "4 Elvis Costello Joe Porterhouse \n",
+ "\n",
+ " text \n",
+ "0 Make up, and break up That's all we do Then ... \n",
+ "1 It's looking out From inside A Christmas bon... \n",
+ "2 Only name Table Rogers a share man am I On a ... \n",
+ "3 There was a time, a simpler time When a man c... \n",
+ "4 The children sit upon the stairs High above a... "
+ ]
+ },
+ "execution_count": 173,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "songs.head()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 174,
+ "id": "84d02c33",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "tfidf = TfidfVectorizer(analyzer='word', stop_words='english')\n",
+ "lyrics_matrix = tfidf.fit_transform(songs['text'])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 175,
+ "id": "c326c8ab",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "cosine_similarities = cosine_similarity(lyrics_matrix) "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 176,
+ "id": "dbada089",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "similarities = {}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 177,
+ "id": "1efe66f0",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "for i in range(len(cosine_similarities)):\n",
+ " similar_indices = cosine_similarities[i].argsort()[:-50:-1] \n",
+ " similarities[songs['song'].iloc[i]] = [(cosine_similarities[i][x], songs['song'][x], songs['artist'][x]) for x in similar_indices][1:]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 178,
+ "id": "01209ca8",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "class ContentBasedRecommender:\n",
+ " def __init__(self, matrix):\n",
+ " self.matrix_similar = matrix\n",
+ "\n",
+ " def _print_message(self, song, recom_song):\n",
+ " rec_items = len(recom_song)\n",
+ " print(f'The recommended songs for {song} are:')\n",
+ " for i in range(rec_items):\n",
+ " print(f\"{recom_song[i][1]} by {recom_song[i][2]}\") \n",
+ " \n",
+ " def recommend(self, recommendation):\n",
+ " song = recommendation['song']\n",
+ " number_songs = recommendation['number_songs']\n",
+ " recom_song = self.matrix_similar[song][:number_songs]\n",
+ " self._print_message(song=song, recom_song=recom_song)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 179,
+ "id": "56c182c8",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "recommedations = ContentBasedRecommender(similarities)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 180,
+ "id": "8c90bd31",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "recommendation = {\n",
+ " \"song\": songs['song'].iloc[10],\n",
+ " \"number_songs\": 4 \n",
+ "}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 181,
+ "id": "53ea6163",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The recommended songs for Running For Cover are:\n",
+ "Keep On Running by Stevie Wonder\n",
+ "Running Man by Hanson\n",
+ "Save Me A Place by Fleetwood Mac\n",
+ "Watercolours In The Rain by Roxette\n"
+ ]
+ }
+ ],
+ "source": [
+ "recommedations.recommend(recommendation)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 182,
+ "id": "38ecc2b6",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "recommendation2 = {\n",
+ " \"song\": songs['song'].iloc[120],\n",
+ " \"number_songs\": 4 \n",
+ "}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 161,
+ "id": "122b988f",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The recommended songs for No Way are:\n",
+ "Replacement Girl by Drake\n",
+ "Guadalajara by Elvis Presley\n",
+ "Soul Deep by Roxette\n",
+ "I Put A Spell On You by Bette Midler\n"
+ ]
+ }
+ ],
+ "source": [
+ "recommedations.recommend(recommendation2)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 162,
+ "id": "cc0867a4",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import pickle\n",
+ "pickle.dump(similarities,open(\"sim.pkl\",\"wb\"))\n",
+ "pickle.dump(songs.to_dict(),open('songs.pkl','wb'))"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3 (ipykernel)",
+ "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.7"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}