Spaces:
Runtime error
Runtime error
File size: 58,030 Bytes
3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c 3714bf4 9dd7d9c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "DDADPl-phDUC"
},
"source": [
"# **Music recommender**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "E7Cu5Fmqct7J"
},
"source": [
"# **Load Data**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 540
},
"id": "bI8bNavbajsv",
"outputId": "7cba8b5d-4a63-433f-be3c-87ce794833ba"
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <input type=\"file\" id=\"files-793c32c8-99a6-4873-9585-738e1d4b2ab1\" name=\"files[]\" multiple disabled\n",
" style=\"border:none\" />\n",
" <output id=\"result-793c32c8-99a6-4873-9585-738e1d4b2ab1\">\n",
" Upload widget is only available when the cell has been executed in the\n",
" current browser session. Please rerun this cell to enable.\n",
" </output>\n",
" <script>// Copyright 2017 Google LLC\n",
"//\n",
"// Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"// you may not use this file except in compliance with the License.\n",
"// You may obtain a copy of the License at\n",
"//\n",
"// http://www.apache.org/licenses/LICENSE-2.0\n",
"//\n",
"// Unless required by applicable law or agreed to in writing, software\n",
"// distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"// See the License for the specific language governing permissions and\n",
"// limitations under the License.\n",
"\n",
"/**\n",
" * @fileoverview Helpers for google.colab Python module.\n",
" */\n",
"(function(scope) {\n",
"function span(text, styleAttributes = {}) {\n",
" const element = document.createElement('span');\n",
" element.textContent = text;\n",
" for (const key of Object.keys(styleAttributes)) {\n",
" element.style[key] = styleAttributes[key];\n",
" }\n",
" return element;\n",
"}\n",
"\n",
"// Max number of bytes which will be uploaded at a time.\n",
"const MAX_PAYLOAD_SIZE = 100 * 1024;\n",
"\n",
"function _uploadFiles(inputId, outputId) {\n",
" const steps = uploadFilesStep(inputId, outputId);\n",
" const outputElement = document.getElementById(outputId);\n",
" // Cache steps on the outputElement to make it available for the next call\n",
" // to uploadFilesContinue from Python.\n",
" outputElement.steps = steps;\n",
"\n",
" return _uploadFilesContinue(outputId);\n",
"}\n",
"\n",
"// This is roughly an async generator (not supported in the browser yet),\n",
"// where there are multiple asynchronous steps and the Python side is going\n",
"// to poll for completion of each step.\n",
"// This uses a Promise to block the python side on completion of each step,\n",
"// then passes the result of the previous step as the input to the next step.\n",
"function _uploadFilesContinue(outputId) {\n",
" const outputElement = document.getElementById(outputId);\n",
" const steps = outputElement.steps;\n",
"\n",
" const next = steps.next(outputElement.lastPromiseValue);\n",
" return Promise.resolve(next.value.promise).then((value) => {\n",
" // Cache the last promise value to make it available to the next\n",
" // step of the generator.\n",
" outputElement.lastPromiseValue = value;\n",
" return next.value.response;\n",
" });\n",
"}\n",
"\n",
"/**\n",
" * Generator function which is called between each async step of the upload\n",
" * process.\n",
" * @param {string} inputId Element ID of the input file picker element.\n",
" * @param {string} outputId Element ID of the output display.\n",
" * @return {!Iterable<!Object>} Iterable of next steps.\n",
" */\n",
"function* uploadFilesStep(inputId, outputId) {\n",
" const inputElement = document.getElementById(inputId);\n",
" inputElement.disabled = false;\n",
"\n",
" const outputElement = document.getElementById(outputId);\n",
" outputElement.innerHTML = '';\n",
"\n",
" const pickedPromise = new Promise((resolve) => {\n",
" inputElement.addEventListener('change', (e) => {\n",
" resolve(e.target.files);\n",
" });\n",
" });\n",
"\n",
" const cancel = document.createElement('button');\n",
" inputElement.parentElement.appendChild(cancel);\n",
" cancel.textContent = 'Cancel upload';\n",
" const cancelPromise = new Promise((resolve) => {\n",
" cancel.onclick = () => {\n",
" resolve(null);\n",
" };\n",
" });\n",
"\n",
" // Wait for the user to pick the files.\n",
" const files = yield {\n",
" promise: Promise.race([pickedPromise, cancelPromise]),\n",
" response: {\n",
" action: 'starting',\n",
" }\n",
" };\n",
"\n",
" cancel.remove();\n",
"\n",
" // Disable the input element since further picks are not allowed.\n",
" inputElement.disabled = true;\n",
"\n",
" if (!files) {\n",
" return {\n",
" response: {\n",
" action: 'complete',\n",
" }\n",
" };\n",
" }\n",
"\n",
" for (const file of files) {\n",
" const li = document.createElement('li');\n",
" li.append(span(file.name, {fontWeight: 'bold'}));\n",
" li.append(span(\n",
" `(${file.type || 'n/a'}) - ${file.size} bytes, ` +\n",
" `last modified: ${\n",
" file.lastModifiedDate ? file.lastModifiedDate.toLocaleDateString() :\n",
" 'n/a'} - `));\n",
" const percent = span('0% done');\n",
" li.appendChild(percent);\n",
"\n",
" outputElement.appendChild(li);\n",
"\n",
" const fileDataPromise = new Promise((resolve) => {\n",
" const reader = new FileReader();\n",
" reader.onload = (e) => {\n",
" resolve(e.target.result);\n",
" };\n",
" reader.readAsArrayBuffer(file);\n",
" });\n",
" // Wait for the data to be ready.\n",
" let fileData = yield {\n",
" promise: fileDataPromise,\n",
" response: {\n",
" action: 'continue',\n",
" }\n",
" };\n",
"\n",
" // Use a chunked sending to avoid message size limits. See b/62115660.\n",
" let position = 0;\n",
" do {\n",
" const length = Math.min(fileData.byteLength - position, MAX_PAYLOAD_SIZE);\n",
" const chunk = new Uint8Array(fileData, position, length);\n",
" position += length;\n",
"\n",
" const base64 = btoa(String.fromCharCode.apply(null, chunk));\n",
" yield {\n",
" response: {\n",
" action: 'append',\n",
" file: file.name,\n",
" data: base64,\n",
" },\n",
" };\n",
"\n",
" let percentDone = fileData.byteLength === 0 ?\n",
" 100 :\n",
" Math.round((position / fileData.byteLength) * 100);\n",
" percent.textContent = `${percentDone}% done`;\n",
"\n",
" } while (position < fileData.byteLength);\n",
" }\n",
"\n",
" // All done.\n",
" yield {\n",
" response: {\n",
" action: 'complete',\n",
" }\n",
" };\n",
"}\n",
"\n",
"scope.google = scope.google || {};\n",
"scope.google.colab = scope.google.colab || {};\n",
"scope.google.colab._files = {\n",
" _uploadFiles,\n",
" _uploadFilesContinue,\n",
"};\n",
"})(self);\n",
"</script> "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saving music_data.csv to music_data.csv\n",
" title \\\n",
"0 100 Club 1996 ''We Love You Beatles'' - Live \n",
"1 Yo Quiero Contigo \n",
"4 Emerald \n",
"6 Karma \n",
"7 Money Blues \n",
"\n",
" release artist_name duration \\\n",
"0 Sex Pistols - The Interviews Sex Pistols 88.73751 \n",
"1 Sentenciados - Platinum Edition Baby Rasta & Gringo 167.36608 \n",
"4 Emerald Bedrock 501.86404 \n",
"6 The Diary Of Alicia Keys Alicia Keys 255.99955 \n",
"7 Slidetime Joanna Connor 243.66975 \n",
"\n",
" artist_familiarity artist_hotttnesss year listeners playcount \\\n",
"0 0.731184 0.549204 0 172 210 \n",
"1 0.610186 0.355320 0 9753 16911 \n",
"4 0.654039 0.390625 2004 973 2247 \n",
"6 0.933916 0.778674 2003 250304 1028356 \n",
"7 0.479218 0.332857 0 429 1008 \n",
"\n",
" tags \n",
"0 The Beatles, title is a full sentence \n",
"1 Reggaeton, alexis y fido, Eliana, mis videos, ... \n",
"4 dance \n",
"6 rnb, soul, Alicia Keys, female vocalists, Karma \n",
"7 guitar girl, blues \n"
]
}
],
"source": [
"import pandas as pd\n",
"from google.colab import files\n",
"\n",
"# Upload the file\n",
"uploaded = files.upload()\n",
"\n",
"# Assuming the file is named \"music_data.csv\"\n",
"data_path = \"music_data.csv\"\n",
"\n",
"# Load the data\n",
"df = pd.read_csv(data_path)\n",
"df.dropna(inplace=True)\n",
"\n",
"# Display the first few rows of the dataset\n",
"print(df.head())\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 206
},
"id": "9E3in0U3dK5I",
"outputId": "c1d5362a-6a33-4543-ff4d-4e11cf8220ec"
},
"outputs": [
{
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"summary": "{\n \"name\": \"df\",\n \"rows\": 5063,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 4854,\n \"samples\": [\n \"I Wish I Had A Girl\",\n \"Jump [Jacques Lu Cont Edit]\",\n \"Mulin' Around\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"release\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 4187,\n \"samples\": [\n \"Le Bordel Magnifique\",\n \"Charlotte's Web (OST)\",\n \"X.O. Experience\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"artist_name\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 2461,\n \"samples\": [\n \"Lee Ritenour\",\n \"Pennywise\",\n \"Anneli Drecker\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"duration\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 107.73289375974717,\n \"min\": 1.04444,\n \"max\": 1815.2224,\n \"num_unique_values\": 3939,\n \"samples\": [\n 294.24281,\n 240.79628,\n 115.53914\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"artist_familiarity\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.14886096792686204,\n \"min\": 0.0,\n \"max\": 1.0,\n \"num_unique_values\": 2474,\n \"samples\": [\n 0.787098355481,\n 0.481771820142,\n 0.374024633035\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"artist_hotttnesss\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.1347303774485448,\n \"min\": 0.0,\n \"max\": 1.08250255673,\n \"num_unique_values\": 2398,\n \"samples\": [\n 0.376018761952,\n 0.355667956383,\n 0.289970666912\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"year\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 917,\n \"min\": 0,\n \"max\": 2010,\n \"num_unique_values\": 69,\n \"samples\": [\n 1979,\n 0,\n 1965\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"listeners\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 150513,\n \"min\": 0,\n \"max\": 2451482,\n \"num_unique_values\": 3914,\n \"samples\": [\n 781546,\n 6216,\n 396579\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"playcount\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1115103,\n \"min\": 0,\n \"max\": 23182516,\n \"num_unique_values\": 4422,\n \"samples\": [\n 62736,\n 1305,\n 17033\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"tags\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 4583,\n \"samples\": [\n \"dance, 90s, trance, House, jungle\",\n \"country, favorite songs, classic country, linedance, Martina McBride\",\n \"90s, heavy metal, thrash metal, metal, punk\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}",
"type": "dataframe",
"variable_name": "df"
},
"text/html": [
"\n",
" <div id=\"df-b9e5c35d-1534-4ad7-8661-887b39a472e9\" class=\"colab-df-container\">\n",
" <div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>title</th>\n",
" <th>release</th>\n",
" <th>artist_name</th>\n",
" <th>duration</th>\n",
" <th>artist_familiarity</th>\n",
" <th>artist_hotttnesss</th>\n",
" <th>year</th>\n",
" <th>listeners</th>\n",
" <th>playcount</th>\n",
" <th>tags</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>100 Club 1996 ''We Love You Beatles'' - Live</td>\n",
" <td>Sex Pistols - The Interviews</td>\n",
" <td>Sex Pistols</td>\n",
" <td>88.73751</td>\n",
" <td>0.731184</td>\n",
" <td>0.549204</td>\n",
" <td>0</td>\n",
" <td>172</td>\n",
" <td>210</td>\n",
" <td>The Beatles, title is a full sentence</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Yo Quiero Contigo</td>\n",
" <td>Sentenciados - Platinum Edition</td>\n",
" <td>Baby Rasta & Gringo</td>\n",
" <td>167.36608</td>\n",
" <td>0.610186</td>\n",
" <td>0.355320</td>\n",
" <td>0</td>\n",
" <td>9753</td>\n",
" <td>16911</td>\n",
" <td>Reggaeton, alexis y fido, Eliana, mis videos, ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Emerald</td>\n",
" <td>Emerald</td>\n",
" <td>Bedrock</td>\n",
" <td>501.86404</td>\n",
" <td>0.654039</td>\n",
" <td>0.390625</td>\n",
" <td>2004</td>\n",
" <td>973</td>\n",
" <td>2247</td>\n",
" <td>dance</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>Karma</td>\n",
" <td>The Diary Of Alicia Keys</td>\n",
" <td>Alicia Keys</td>\n",
" <td>255.99955</td>\n",
" <td>0.933916</td>\n",
" <td>0.778674</td>\n",
" <td>2003</td>\n",
" <td>250304</td>\n",
" <td>1028356</td>\n",
" <td>rnb, soul, Alicia Keys, female vocalists, Karma</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>Money Blues</td>\n",
" <td>Slidetime</td>\n",
" <td>Joanna Connor</td>\n",
" <td>243.66975</td>\n",
" <td>0.479218</td>\n",
" <td>0.332857</td>\n",
" <td>0</td>\n",
" <td>429</td>\n",
" <td>1008</td>\n",
" <td>guitar girl, blues</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>\n",
" <div class=\"colab-df-buttons\">\n",
"\n",
" <div class=\"colab-df-container\">\n",
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-b9e5c35d-1534-4ad7-8661-887b39a472e9')\"\n",
" title=\"Convert this dataframe to an interactive table.\"\n",
" style=\"display:none;\">\n",
"\n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
" <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
" </svg>\n",
" </button>\n",
"\n",
" <style>\n",
" .colab-df-container {\n",
" display:flex;\n",
" gap: 12px;\n",
" }\n",
"\n",
" .colab-df-convert {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-convert:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" .colab-df-buttons div {\n",
" margin-bottom: 4px;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
"\n",
" <script>\n",
" const buttonEl =\n",
" document.querySelector('#df-b9e5c35d-1534-4ad7-8661-887b39a472e9 button.colab-df-convert');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" async function convertToInteractive(key) {\n",
" const element = document.querySelector('#df-b9e5c35d-1534-4ad7-8661-887b39a472e9');\n",
" const dataTable =\n",
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
" [key], {});\n",
" if (!dataTable) return;\n",
"\n",
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
" + ' to learn more about interactive tables.';\n",
" element.innerHTML = '';\n",
" dataTable['output_type'] = 'display_data';\n",
" await google.colab.output.renderOutput(dataTable, element);\n",
" const docLink = document.createElement('div');\n",
" docLink.innerHTML = docLinkHtml;\n",
" element.appendChild(docLink);\n",
" }\n",
" </script>\n",
" </div>\n",
"\n",
"\n",
"<div id=\"df-3ffda883-e826-470a-8413-bc736b2d9130\">\n",
" <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-3ffda883-e826-470a-8413-bc736b2d9130')\"\n",
" title=\"Suggest charts\"\n",
" style=\"display:none;\">\n",
"\n",
"<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
" width=\"24px\">\n",
" <g>\n",
" <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
" </g>\n",
"</svg>\n",
" </button>\n",
"\n",
"<style>\n",
" .colab-df-quickchart {\n",
" --bg-color: #E8F0FE;\n",
" --fill-color: #1967D2;\n",
" --hover-bg-color: #E2EBFA;\n",
" --hover-fill-color: #174EA6;\n",
" --disabled-fill-color: #AAA;\n",
" --disabled-bg-color: #DDD;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-quickchart {\n",
" --bg-color: #3B4455;\n",
" --fill-color: #D2E3FC;\n",
" --hover-bg-color: #434B5C;\n",
" --hover-fill-color: #FFFFFF;\n",
" --disabled-bg-color: #3B4455;\n",
" --disabled-fill-color: #666;\n",
" }\n",
"\n",
" .colab-df-quickchart {\n",
" background-color: var(--bg-color);\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: var(--fill-color);\n",
" height: 32px;\n",
" padding: 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-quickchart:hover {\n",
" background-color: var(--hover-bg-color);\n",
" box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: var(--button-hover-fill-color);\n",
" }\n",
"\n",
" .colab-df-quickchart-complete:disabled,\n",
" .colab-df-quickchart-complete:disabled:hover {\n",
" background-color: var(--disabled-bg-color);\n",
" fill: var(--disabled-fill-color);\n",
" box-shadow: none;\n",
" }\n",
"\n",
" .colab-df-spinner {\n",
" border: 2px solid var(--fill-color);\n",
" border-color: transparent;\n",
" border-bottom-color: var(--fill-color);\n",
" animation:\n",
" spin 1s steps(1) infinite;\n",
" }\n",
"\n",
" @keyframes spin {\n",
" 0% {\n",
" border-color: transparent;\n",
" border-bottom-color: var(--fill-color);\n",
" border-left-color: var(--fill-color);\n",
" }\n",
" 20% {\n",
" border-color: transparent;\n",
" border-left-color: var(--fill-color);\n",
" border-top-color: var(--fill-color);\n",
" }\n",
" 30% {\n",
" border-color: transparent;\n",
" border-left-color: var(--fill-color);\n",
" border-top-color: var(--fill-color);\n",
" border-right-color: var(--fill-color);\n",
" }\n",
" 40% {\n",
" border-color: transparent;\n",
" border-right-color: var(--fill-color);\n",
" border-top-color: var(--fill-color);\n",
" }\n",
" 60% {\n",
" border-color: transparent;\n",
" border-right-color: var(--fill-color);\n",
" }\n",
" 80% {\n",
" border-color: transparent;\n",
" border-right-color: var(--fill-color);\n",
" border-bottom-color: var(--fill-color);\n",
" }\n",
" 90% {\n",
" border-color: transparent;\n",
" border-bottom-color: var(--fill-color);\n",
" }\n",
" }\n",
"</style>\n",
"\n",
" <script>\n",
" async function quickchart(key) {\n",
" const quickchartButtonEl =\n",
" document.querySelector('#' + key + ' button');\n",
" quickchartButtonEl.disabled = true; // To prevent multiple clicks.\n",
" quickchartButtonEl.classList.add('colab-df-spinner');\n",
" try {\n",
" const charts = await google.colab.kernel.invokeFunction(\n",
" 'suggestCharts', [key], {});\n",
" } catch (error) {\n",
" console.error('Error during call to suggestCharts:', error);\n",
" }\n",
" quickchartButtonEl.classList.remove('colab-df-spinner');\n",
" quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
" }\n",
" (() => {\n",
" let quickchartButtonEl =\n",
" document.querySelector('#df-3ffda883-e826-470a-8413-bc736b2d9130 button');\n",
" quickchartButtonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
" })();\n",
" </script>\n",
"</div>\n",
"\n",
" </div>\n",
" </div>\n"
],
"text/plain": [
" title \\\n",
"0 100 Club 1996 ''We Love You Beatles'' - Live \n",
"1 Yo Quiero Contigo \n",
"4 Emerald \n",
"6 Karma \n",
"7 Money Blues \n",
"\n",
" release artist_name duration \\\n",
"0 Sex Pistols - The Interviews Sex Pistols 88.73751 \n",
"1 Sentenciados - Platinum Edition Baby Rasta & Gringo 167.36608 \n",
"4 Emerald Bedrock 501.86404 \n",
"6 The Diary Of Alicia Keys Alicia Keys 255.99955 \n",
"7 Slidetime Joanna Connor 243.66975 \n",
"\n",
" artist_familiarity artist_hotttnesss year listeners playcount \\\n",
"0 0.731184 0.549204 0 172 210 \n",
"1 0.610186 0.355320 0 9753 16911 \n",
"4 0.654039 0.390625 2004 973 2247 \n",
"6 0.933916 0.778674 2003 250304 1028356 \n",
"7 0.479218 0.332857 0 429 1008 \n",
"\n",
" tags \n",
"0 The Beatles, title is a full sentence \n",
"1 Reggaeton, alexis y fido, Eliana, mis videos, ... \n",
"4 dance \n",
"6 rnb, soul, Alicia Keys, female vocalists, Karma \n",
"7 guitar girl, blues "
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "b_sSacbdHcn6",
"outputId": "f745b028-fd97-4b19-b9f0-9e041621e5d3"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'pandas.core.frame.DataFrame'>\n",
"Index: 5063 entries, 0 to 9530\n",
"Data columns (total 10 columns):\n",
" # Column Non-Null Count Dtype \n",
"--- ------ -------------- ----- \n",
" 0 title 5063 non-null object \n",
" 1 release 5063 non-null object \n",
" 2 artist_name 5063 non-null object \n",
" 3 duration 5063 non-null float64\n",
" 4 artist_familiarity 5063 non-null float64\n",
" 5 artist_hotttnesss 5063 non-null float64\n",
" 6 year 5063 non-null int64 \n",
" 7 listeners 5063 non-null int64 \n",
" 8 playcount 5063 non-null int64 \n",
" 9 tags 5063 non-null object \n",
"dtypes: float64(3), int64(3), object(4)\n",
"memory usage: 435.1+ KB\n",
"None\n",
" duration artist_familiarity artist_hotttnesss year \\\n",
"count 5063.000000 5063.000000 5063.000000 5063.000000 \n",
"mean 243.156073 0.626861 0.439664 1392.483705 \n",
"std 107.732894 0.148861 0.134730 917.360336 \n",
"min 1.044440 0.000000 0.000000 0.000000 \n",
"25% 183.535870 0.527033 0.363132 0.000000 \n",
"50% 229.145670 0.619531 0.417819 1993.000000 \n",
"75% 280.920365 0.731184 0.510325 2004.000000 \n",
"max 1815.222400 1.000000 1.082503 2010.000000 \n",
"\n",
" listeners playcount \n",
"count 5.063000e+03 5.063000e+03 \n",
"mean 4.526352e+04 2.622274e+05 \n",
"std 1.505135e+05 1.115104e+06 \n",
"min 0.000000e+00 0.000000e+00 \n",
"25% 7.545000e+02 1.894500e+03 \n",
"50% 3.387000e+03 9.439000e+03 \n",
"75% 1.787350e+04 6.269500e+04 \n",
"max 2.451482e+06 2.318252e+07 \n",
"Unique values in 'title': 4854\n",
"Unique values in 'artist_name': 2461\n",
"Unique values in 'tags': 4583\n"
]
}
],
"source": [
"# Display basic information about the dataset\n",
"print(df.info())\n",
"\n",
"# Display summary statistics for numerical columns\n",
"print(df.describe())\n",
"\n",
"# Display unique values for categorical columns\n",
"print(\"Unique values in 'title':\", df['title'].nunique())\n",
"print(\"Unique values in 'artist_name':\", df['artist_name'].nunique())\n",
"print(\"Unique values in 'tags':\", df['tags'].nunique())"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "wPVFDtk9g9ox"
},
"source": [
"# **Preprocessing**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "3fsU1IvylyZg",
"outputId": "c2ba3adc-c077-454a-94de-ca9bb0ba4807"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Label encoders and scaler saved successfully.\n"
]
}
],
"source": [
"import pandas as pd\n",
"from sklearn.preprocessing import LabelEncoder, MinMaxScaler\n",
"import joblib\n",
"import re\n",
"\n",
"# Function to clean tags and artist names\n",
"def clean_text(text):\n",
" # Convert to lowercase\n",
" text = text.lower()\n",
" # Remove special characters and digits\n",
" text = re.sub(r'[^a-zA-Z\\s]', '', text)\n",
" # Remove extra white spaces\n",
" text = re.sub(r'\\s+', ' ', text).strip()\n",
" return text\n",
"\n",
"# Clean 'tags' and 'artist_name' columns\n",
"df['tags'] = df['tags'].apply(clean_text)\n",
"df['artist_name'] = df['artist_name'].apply(clean_text)\n",
"\n",
"def label_encode_data(df):\n",
" df = df.copy(deep=True)\n",
" label_encoders = {}\n",
" unknown_label = 'unknown' # Define an unknown label\n",
"\n",
" for column in ['tags', 'title', 'artist_name']:\n",
" le = LabelEncoder()\n",
" unique_categories = df[column].unique().tolist()\n",
" unique_categories.append(unknown_label)\n",
" le.fit(unique_categories)\n",
" df[column] = le.transform(df[column].astype(str))\n",
" label_encoders[column] = le\n",
"\n",
" return df, label_encoders\n",
"\n",
"# Normalize numerical features\n",
"scaler = MinMaxScaler()\n",
"df[['listeners', 'playcount']] = scaler.fit_transform(df[['listeners', 'playcount']])\n",
"\n",
"# Label encode categorical features\n",
"df_scaled, label_encoders = label_encode_data(df)\n",
"\n",
"# Save the encoders and scaler\n",
"joblib.dump(label_encoders, \"/content/new_label_encoders.joblib\")\n",
"joblib.dump(scaler, \"/content/new_scaler.joblib\")\n",
"\n",
"print(\"Label encoders and scaler saved successfully.\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "JBWZWp_8Jr82",
"outputId": "73a312c1-3615-4a87-965b-c2fc41fc50e7"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Data split into training and testing sets.\n",
"Maximum value in y_train: 4854\n",
"Maximum value in y_test: 4850\n",
"Number of unique titles: 4855\n",
"Maximum value in y_train after clipping: 4854\n",
"Maximum value in y_test after clipping: 4850\n"
]
}
],
"source": [
"from sklearn.model_selection import train_test_split\n",
"\n",
"# Split data into features and target\n",
"X = df_scaled[['tags', 'artist_name']]\n",
"y = df_scaled['title']\n",
"\n",
"# Split the dataset into training and testing sets\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
"print(\"Data split into training and testing sets.\")\n",
"\n",
"# Number of unique titles\n",
"num_unique_titles = len(label_encoders['title'].classes_)\n",
"\n",
"# Check for out-of-bounds indices in y_train and y_test\n",
"print(\"Maximum value in y_train:\", y_train.max())\n",
"print(\"Maximum value in y_test:\", y_test.max())\n",
"print(\"Number of unique titles:\", num_unique_titles)\n",
"\n",
"# If any out-of-bounds values are found, print them\n",
"out_of_bounds_train = y_train[y_train >= num_unique_titles]\n",
"out_of_bounds_test = y_test[y_test >= num_unique_titles]\n",
"\n",
"if not out_of_bounds_train.empty:\n",
" print(\"Out-of-bounds values in y_train:\", out_of_bounds_train)\n",
"if not out_of_bounds_test.empty:\n",
" print(\"Out-of-bounds values in y_test:\", out_of_bounds_test)\n",
"\n",
"# Fix out-of-bounds values by setting them to a valid index\n",
"y_train = y_train.clip(upper=num_unique_titles - 1)\n",
"y_test = y_test.clip(upper=num_unique_titles - 1)\n",
"\n",
"# Print the maximum values after clipping\n",
"print(\"Maximum value in y_train after clipping:\", y_train.max())\n",
"print(\"Maximum value in y_test after clipping:\", y_test.max())\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "syYhdUbxgA-K"
},
"source": [
"# **Training**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "aaR1IGymKQq2",
"outputId": "9e5115a5-1a75-4672-a0b3-4fdd314e1a79"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1, Training Loss: 8.921830113728841, Validation Loss: 8.836441385979747\n",
"Epoch 2, Training Loss: 8.331391870239635, Validation Loss: 9.148561271966672\n",
"Epoch 3, Training Loss: 7.494005516429007, Validation Loss: 10.484928570541681\n",
"Epoch 4, Training Loss: 6.704833826606657, Validation Loss: 11.745069999320835\n",
"Early stopping triggered\n",
"Improved model trained and saved successfully.\n"
]
}
],
"source": [
"import torch\n",
"import torch.nn as nn\n",
"import torch.optim as optim\n",
"from torch.utils.data import DataLoader\n",
"import numpy as np\n",
"\n",
"# Define the neural network model with Dropout and Batch Normalization\n",
"class ImprovedSongRecommender(nn.Module):\n",
" def __init__(self, input_size, num_titles):\n",
" super(ImprovedSongRecommender, self).__init__()\n",
" self.fc1 = nn.Linear(input_size, 128)\n",
" self.bn1 = nn.BatchNorm1d(128)\n",
" self.fc2 = nn.Linear(128, 256)\n",
" self.bn2 = nn.BatchNorm1d(256)\n",
" self.fc3 = nn.Linear(256, 128)\n",
" self.bn3 = nn.BatchNorm1d(128)\n",
" self.output = nn.Linear(128, num_titles)\n",
" self.dropout = nn.Dropout(0.5)\n",
"\n",
" def forward(self, x):\n",
" x = torch.relu(self.bn1(self.fc1(x)))\n",
" x = self.dropout(x)\n",
" x = torch.relu(self.bn2(self.fc2(x)))\n",
" x = self.dropout(x)\n",
" x = torch.relu(self.bn3(self.fc3(x)))\n",
" x = self.dropout(x)\n",
" x = self.output(x)\n",
" return x\n",
"\n",
"# Adjusting input size for the model\n",
"input_size = X_train.shape[1] # Number of features in the input\n",
"num_unique_titles = len(label_encoders['title'].classes_) # Number of unique titles including 'unknown'\n",
"\n",
"# Initialize the model with the correct input size and output size\n",
"model = ImprovedSongRecommender(input_size, num_unique_titles)\n",
"\n",
"# Initialize the optimizer and loss function\n",
"optimizer = optim.AdamW(model.parameters(), lr=0.001, weight_decay=0.01)\n",
"criterion = nn.CrossEntropyLoss()\n",
"\n",
"# Use a learning rate scheduler\n",
"scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.1)\n",
"\n",
"# Early stopping parameters\n",
"patience = 3\n",
"min_delta = 0.01\n",
"best_val_loss = np.inf\n",
"patience_counter = 0\n",
"\n",
"# Function to train the model\n",
"def train_model(model, X_train, y_train, X_test, y_test):\n",
" global best_val_loss, patience_counter\n",
" train_loader = DataLoader(list(zip(X_train.values.astype(float), y_train)), batch_size=10, shuffle=True)\n",
" test_loader = DataLoader(list(zip(X_test.values.astype(float), y_test)), batch_size=10, shuffle=False)\n",
"\n",
" model.train()\n",
" for epoch in range(20): # Increase the number of epochs\n",
" train_loss = 0\n",
" for features, labels in train_loader:\n",
" optimizer.zero_grad()\n",
" outputs = model(features.float())\n",
" loss = criterion(outputs, labels.long())\n",
" loss.backward()\n",
" optimizer.step()\n",
" train_loss += loss.item()\n",
"\n",
" # Step the scheduler\n",
" scheduler.step()\n",
"\n",
" # Validation phase\n",
" model.eval()\n",
" validation_loss = 0\n",
" with torch.no_grad():\n",
" for features, labels in test_loader:\n",
" outputs = model(features.float())\n",
" loss = criterion(outputs, labels.long())\n",
" validation_loss += loss.item()\n",
"\n",
" avg_val_loss = validation_loss / len(test_loader)\n",
" print(f'Epoch {epoch+1}, Training Loss: {train_loss / len(train_loader)}, Validation Loss: {avg_val_loss}')\n",
"\n",
" # Early stopping\n",
" if avg_val_loss < best_val_loss - min_delta:\n",
" best_val_loss = avg_val_loss\n",
" patience_counter = 0\n",
" else:\n",
" patience_counter += 1\n",
" if patience_counter >= patience:\n",
" print(\"Early stopping triggered\")\n",
" break\n",
"\n",
"# Train the model\n",
"train_model(model, X_train, y_train, X_test, y_test)\n",
"\n",
"# Save the trained model\n",
"model_path = '/content/improved_model.pth'\n",
"torch.save(model.state_dict(), model_path)\n",
"\n",
"print(\"Improved model trained and saved successfully.\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "g4hJVlNXf5Vu"
},
"source": [
"# **Testing**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "KwqV-HnCOvtz",
"outputId": "d412ce92-3ab8-4f3d-df83-22ef9e857203"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Recommendations: ['Betrayal Is A Symptom', 'The Earth Will Shake', 'Saturday', 'Firehouse Rock', 'Breathe Easy']\n"
]
}
],
"source": [
"import torch\n",
"from joblib import load\n",
"\n",
"# Define the same neural network model\n",
"class ImprovedSongRecommender(nn.Module):\n",
" def __init__(self, input_size, num_titles):\n",
" super(ImprovedSongRecommender, self).__init__()\n",
" self.fc1 = nn.Linear(input_size, 128)\n",
" self.bn1 = nn.BatchNorm1d(128)\n",
" self.fc2 = nn.Linear(128, 256)\n",
" self.bn2 = nn.BatchNorm1d(256)\n",
" self.fc3 = nn.Linear(256, 128)\n",
" self.bn3 = nn.BatchNorm1d(128)\n",
" self.output = nn.Linear(128, num_titles)\n",
" self.dropout = nn.Dropout(0.5)\n",
"\n",
" def forward(self, x):\n",
" x = torch.relu(self.bn1(self.fc1(x)))\n",
" x = self.dropout(x)\n",
" x = torch.relu(self.bn2(self.fc2(x)))\n",
" x = self.dropout(x)\n",
" x = torch.relu(self.bn3(self.fc3(x)))\n",
" x = self.dropout(x)\n",
" x = self.output(x)\n",
" return x\n",
"\n",
"# Load the trained model\n",
"model_path = '/content/improved_model.pth'\n",
"num_unique_titles = 4855 # Update this to match your dataset\n",
"\n",
"model = ImprovedSongRecommender(input_size=2, num_titles=num_unique_titles) # Adjust input size accordingly\n",
"model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))\n",
"model.eval()\n",
"\n",
"# Load the label encoders and scaler\n",
"label_encoders_path = '/content/new_label_encoders.joblib'\n",
"scaler_path = '/content/new_scaler.joblib'\n",
"\n",
"label_encoders = load(label_encoders_path)\n",
"scaler = load(scaler_path)\n",
"\n",
"# Create a mapping from encoded indices to actual song titles\n",
"index_to_song_title = {index: title for index, title in enumerate(label_encoders['title'].classes_)}\n",
"\n",
"def encode_input(tags, artist_name):\n",
" tags = tags.strip().replace('\\n', '')\n",
" artist_name = artist_name.strip().replace('\\n', '')\n",
"\n",
" try:\n",
" encoded_tags = label_encoders['tags'].transform([tags])[0]\n",
" except ValueError:\n",
" encoded_tags = label_encoders['tags'].transform(['unknown'])[0]\n",
"\n",
" try:\n",
" encoded_artist = label_encoders['artist_name'].transform([artist_name])[0]\n",
" except ValueError:\n",
" encoded_artist = label_encoders['artist_name'].transform(['unknown'])[0]\n",
"\n",
" return [encoded_tags, encoded_artist]\n",
"\n",
"def recommend_songs(tags, artist_name):\n",
" encoded_input = encode_input(tags, artist_name)\n",
" input_tensor = torch.tensor([encoded_input]).float()\n",
"\n",
" with torch.no_grad():\n",
" output = model(input_tensor)\n",
"\n",
" recommendations_indices = torch.topk(output, 5).indices.squeeze().tolist()\n",
" recommendations = [index_to_song_title.get(idx, \"Unknown song\") for idx in recommendations_indices]\n",
"\n",
" return recommendations\n",
"\n",
"# Test the recommendation function\n",
"tags = \"rock\"\n",
"artist_name = \"The Beatles\"\n",
"\n",
"recommendations = recommend_songs(tags, artist_name)\n",
"print(\"Recommendations:\", recommendations)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "3HzLKv5mPxOv",
"outputId": "62b37d04-4857-44fb-b5c4-8ead55db9b1a"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Recommendations: ['Betrayal Is A Symptom', 'Carnival (from \"Black Orpheus\")', 'Saturday', 'The Earth Will Shake', 'Start!']\n",
"Recommendations: ['Old Friends', 'Betrayal Is A Symptom', 'Between Love & Hate', 'Carnival (from \"Black Orpheus\")', 'Satin Doll']\n"
]
}
],
"source": [
"import torch\n",
"from joblib import load\n",
"\n",
"# Define the same neural network model\n",
"class ImprovedSongRecommender(nn.Module):\n",
" def __init__(self, input_size, num_titles):\n",
" super(ImprovedSongRecommender, self).__init__()\n",
" self.fc1 = nn.Linear(input_size, 128)\n",
" self.bn1 = nn.BatchNorm1d(128)\n",
" self.fc2 = nn.Linear(128, 256)\n",
" self.bn2 = nn.BatchNorm1d(256)\n",
" self.fc3 = nn.Linear(256, 128)\n",
" self.bn3 = nn.BatchNorm1d(128)\n",
" self.output = nn.Linear(128, num_titles)\n",
" self.dropout = nn.Dropout(0.5)\n",
"\n",
" def forward(self, x):\n",
" x = torch.relu(self.bn1(self.fc1(x)))\n",
" x = self.dropout(x)\n",
" x = torch.relu(self.bn2(self.fc2(x)))\n",
" x = self.dropout(x)\n",
" x = torch.relu(self.bn3(self.fc3(x)))\n",
" x = self.dropout(x)\n",
" x = self.output(x)\n",
" return x\n",
"\n",
"# Load the trained model\n",
"model_path = '/content/improved_model.pth'\n",
"num_unique_titles = 4855 # Update this to match your dataset\n",
"\n",
"model = ImprovedSongRecommender(input_size=2, num_titles=num_unique_titles) # Adjust input size accordingly\n",
"model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))\n",
"model.eval()\n",
"\n",
"# Load the label encoders and scaler\n",
"label_encoders_path = '/content/new_label_encoders.joblib'\n",
"scaler_path = '/content/new_scaler.joblib'\n",
"\n",
"label_encoders = load(label_encoders_path)\n",
"scaler = load(scaler_path)\n",
"\n",
"# Create a mapping from encoded indices to actual song titles\n",
"index_to_song_title = {index: title for index, title in enumerate(label_encoders['title'].classes_)}\n",
"\n",
"def encode_input(tags, artist_name):\n",
" tags = tags.strip().replace('\\n', '')\n",
" artist_name = artist_name.strip().replace('\\n', '')\n",
"\n",
" try:\n",
" encoded_tags = label_encoders['tags'].transform([tags])[0]\n",
" except ValueError:\n",
" encoded_tags = label_encoders['tags'].transform(['unknown'])[0]\n",
"\n",
" try:\n",
" encoded_artist = label_encoders['artist_name'].transform([artist_name])[0]\n",
" except ValueError:\n",
" encoded_artist = label_encoders['artist_name'].transform(['unknown'])[0]\n",
"\n",
" return [encoded_tags, encoded_artist]\n",
"\n",
"def recommend_songs(tags, artist_name):\n",
" encoded_input = encode_input(tags, artist_name)\n",
" input_tensor = torch.tensor([encoded_input]).float()\n",
"\n",
" with torch.no_grad():\n",
" output = model(input_tensor)\n",
"\n",
" recommendations_indices = torch.topk(output, 5).indices.squeeze().tolist()\n",
" recommendations = [index_to_song_title.get(idx, \"Unknown song\") for idx in recommendations_indices]\n",
"\n",
" return recommendations\n",
"\n",
"# Test the recommendation function with new inputs\n",
"tags = \"pop\"\n",
"artist_name = \"Adele\"\n",
"\n",
"recommendations = recommend_songs(tags, artist_name)\n",
"print(\"Recommendations:\", recommendations)\n",
"\n",
"# Test with another set of inputs\n",
"tags = \"jazz\"\n",
"artist_name = \"Miles Davis\"\n",
"\n",
"recommendations = recommend_songs(tags, artist_name)\n",
"print(\"Recommendations:\", recommendations)\n"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.8.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
|