

{"id":78020,"date":"2020-05-22T11:43:53","date_gmt":"2020-05-22T06:13:53","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=78020"},"modified":"2025-07-29T20:50:16","modified_gmt":"2025-07-29T15:20:16","slug":"python-project-music-genre-classification","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-project-music-genre-classification\/","title":{"rendered":"Python Project &#8211; Music Genre Classification"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:1117,&quot;href&quot;:&quot;http:\\\/\\\/marsyas.info\\\/downloads\\\/datasets.html&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20220120112420\\\/http:\\\/\\\/marsyas.info\\\/downloads\\\/datasets.html&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-09 01:24:06&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2025-12-12 05:04:01&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2025-12-15 08:24:37&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2025-12-18 08:36:28&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2025-12-21 09:25:25&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2025-12-24 09:52:36&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2025-12-27 11:35:43&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2025-12-30 13:56:00&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-01-02 14:12:46&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-01-05 14:33:36&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-01-08 14:52:53&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-01-11 15:45:57&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-01-14 16:09:07&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-01-17 17:19:30&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-01-20 17:32:04&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-01-23 18:17:13&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-01-26 18:50:04&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-01-29 20:27:52&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-02-01 22:28:57&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-02-04 22:48:53&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-02-07 22:58:45&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-02-11 04:36:37&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-02-14 10:00:03&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-02-17 10:11:23&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-02-20 10:17:37&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-02-23 13:26:58&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-02-26 13:56:56&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-01 14:58:19&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-04 15:17:10&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-07 15:18:01&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-10 15:21:17&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-14 09:29:45&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-17 10:25:50&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-20 11:39:00&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-23 11:59:37&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-26 14:22:17&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-29 15:29:05&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-04-01 17:55:50&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-04-05 07:56:17&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-04-08 09:13:11&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-04-11 09:39:59&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-04-14 09:54:45&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-04-17 10:14:41&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-04-20 10:28:06&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-04-23 11:17:51&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-04-26 20:05:25&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-04-30 10:10:22&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-05-03 10:22:36&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-05-06 13:57:38&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-05-09 15:01:11&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-05-12 15:06:08&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-05-15 17:18:44&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-05-18 18:01:40&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-05-22 13:08:07&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-05-25 13:22:51&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-05-28 13:23:11&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-05-31 18:25:18&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-06-03 19:02:41&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-06-06 19:09:58&quot;,&quot;http_code&quot;:404}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-06 19:09:58&quot;,&quot;http_code&quot;:404},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:1118,&quot;href&quot;:&quot;http:\\\/\\\/cs.carleton.edu\\\/cs_comps\\\/0910\\\/netflixprize\\\/final_results\\\/knn\\\/index.html&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20231223053348\\\/https:\\\/\\\/cs.carleton.edu\\\/cs_comps\\\/0910\\\/netflixprize\\\/final_results\\\/knn\\\/index.html&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-09 01:24:09&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-12 05:04:05&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-15 08:24:37&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-18 08:36:26&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-21 09:25:40&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-24 09:52:49&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-27 11:35:47&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-30 13:56:01&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-02 14:12:52&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-05 14:33:43&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-08 14:52:55&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-11 15:47:29&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-14 17:20:20&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-17 19:13:04&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-20 20:10:53&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-23 20:12:03&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-27 01:33:46&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-30 06:41:04&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-02 08:11:34&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-05 09:23:52&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-08 10:24:02&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-11 10:31:24&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-14 11:05:31&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-17 13:38:37&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-20 14:05:52&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-23 16:44:34&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-26 17:04:26&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-01 22:09:26&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-05 04:59:37&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-08 05:57:55&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-11 06:30:32&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-03-14 09:29:47&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-17 13:16:07&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-03-20 17:33:29&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-24 02:25:10&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-27 04:44:20&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-30 08:03:29&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-02 09:29:07&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-05 13:17:45&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-08 15:08:02&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-11 15:38:10&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-14 16:28:33&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-17 19:33:45&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-20 19:37:10&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-23 22:00:12&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-27 04:34:28&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-30 10:10:34&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-05-03 10:23:13&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-06 13:58:19&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-09 15:01:18&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-12 15:20:49&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-15 16:46:05&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-18 21:11:53&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-22 13:08:35&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-25 13:22:51&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-28 13:23:15&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-31 18:25:30&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-03 19:02:44&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-06 19:10:53&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-06 19:10:53&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p><strong>Music Genre Classification<\/strong> &#8211;\u00a0Automatically classify different musical genres<\/p>\n<p>In this tutorial we are going to develop a deep learning project to automatically classify different musical genres from audio files. We will classify these audio files using their low-level features of frequency and time domain.<\/p>\n<p>For this project we need a dataset of audio tracks having similar size and similar frequency range. GTZAN genre classification dataset is the most recommended dataset for the music genre classification project and it was collected for this task only.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-classifier-model.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78026\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-classifier-model.jpg\" alt=\"music genre classifier model\" width=\"936\" height=\"510\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-classifier-model.jpg 936w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-classifier-model-150x82.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-classifier-model-300x163.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-classifier-model-768x418.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-classifier-model-520x283.jpg 520w\" sizes=\"auto, (max-width: 936px) 100vw, 936px\" \/><\/a><\/p>\n<h3>Music Genre Classification<\/h3>\n<h3>About the dataset:<\/h3>\n<p>The GTZAN genre collection dataset was collected in 2000-2001. It consists of 1000 audio files each having 30 seconds duration. There are 10 classes ( 10 music genres) each containing 100 audio tracks. Each track is in .wav format. It contains audio files of the following 10 genres:<\/p>\n<ul>\n<li>Blues<\/li>\n<li>Classical<\/li>\n<li>Country<\/li>\n<li>Disco<\/li>\n<li>Hiphop<\/li>\n<li>Jazz<\/li>\n<li>Metal<\/li>\n<li>Pop<\/li>\n<li>Reggae<\/li>\n<li>Rock<\/li>\n<\/ul>\n<h3>Music Genre Classification approach:<\/h3>\n<p>There are various methods to perform classification on this dataset. Some of these approaches are:<\/p>\n<ul>\n<li>Multiclass support vector machines<\/li>\n<li>K-means clustering<\/li>\n<li>K-nearest neighbors<\/li>\n<li>Convolutional neural networks<\/li>\n<\/ul>\n<p>We will use K-nearest neighbors algorithm because in various researches it has shown the best results for this problem.<\/p>\n<p>K-Nearest Neighbors is a popular\u00a0<a href=\"https:\/\/data-flair.training\/blogs\/machine-learning-algorithms\/\">machine learning algorithm<\/a> for regression and classification. It makes predictions on data points based on their similarity measures i.e distance between them.<\/p>\n<h3>Feature Extraction:<\/h3>\n<p>The first step for music genre classification project would be to extract features and components from the audio files.\u00a0It includes identifying the linguistic content and discarding noise.<\/p>\n<h4>Mel Frequency Cepstral Coefficients:<\/h4>\n<p>These are state-of-the-art features used in automatic speech and speech recognition studies.\u00a0There are a set of steps for generation of these features:<\/p>\n<ul>\n<li>Since the audio signals are constantly changing, first we divide these signals into smaller frames. Each frame is around 20-40 ms long<\/li>\n<li>Then we try to identify different frequencies present in each frame<\/li>\n<li>Now, separate linguistic frequencies from the noise<\/li>\n<li>To discard the noise, it then takes discrete cosine transform (DCT) of these frequencies.\u00a0Using DCT we keep only a specific sequence of frequencies that have a high probability of information.<\/li>\n<\/ul>\n<h3>Steps to build Music Genre Classification:<\/h3>\n<p>Download the GTZAN dataset from the following link:<\/p>\n<p><a href=\"http:\/\/marsyas.info\/downloads\/datasets.html\">GTZAN dataset<\/a><\/p>\n<h4>Create a new python file &#8220;music_genre.py&#8221; and paste the code described in the steps below:<\/h4>\n<p>1. Imports:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from python_speech_features import mfcc\r\nimport scipy.io.wavfile as wav\r\nimport numpy as np\r\n\r\nfrom tempfile import TemporaryFile\r\nimport os\r\nimport pickle\r\nimport random \r\nimport operator\r\n\r\nimport math\r\nimport numpy as np<\/pre>\n<p>2.\u00a0Define a function to get the distance between feature vectors and find neighbors:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">def getNeighbors(trainingSet, instance, k):\r\n    distances = []\r\n    for x in range (len(trainingSet)):\r\n        dist = distance(trainingSet[x], instance, k )+ distance(instance, trainingSet[x], k)\r\n        distances.append((trainingSet[x][2], dist))\r\n    distances.sort(key=operator.itemgetter(1))\r\n    neighbors = []\r\n    for x in range(k):\r\n        neighbors.append(distances[x][0])\r\n    return neighbors\r\n<\/pre>\n<p>3.\u00a0Identify the nearest neighbors:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">def nearestClass(neighbors):\r\n    classVote = {}\r\n\r\n    for x in range(len(neighbors)):\r\n        response = neighbors[x]\r\n        if response in classVote:\r\n            classVote[response]+=1 \r\n        else:\r\n            classVote[response]=1\r\n\r\n    sorter = sorted(classVote.items(), key = operator.itemgetter(1), reverse=True)\r\n    return sorter[0][0]\r\n<\/pre>\n<p>4.\u00a0Define a function for model evaluation:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">def getAccuracy(testSet, predictions):\r\n    correct = 0 \r\n    for x in range (len(testSet)):\r\n        if testSet[x][-1]==predictions[x]:\r\n            correct+=1\r\n    return 1.0*correct\/len(testSet)\r\n<\/pre>\n<p>5.\u00a0Extract features from the dataset and dump these features into a binary .dat file &#8220;my.dat&#8221;:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">directory = \"__path_to_dataset__\"\r\nf= open(\"my.dat\" ,'wb')\r\ni=0\r\n\r\nfor folder in os.listdir(directory):\r\n    i+=1\r\n    if i==11 :\r\n        break \t\r\n    for file in os.listdir(directory+folder):\t\r\n        (rate,sig) = wav.read(directory+folder+\"\/\"+file)\r\n        mfcc_feat = mfcc(sig,rate ,winlen=0.020, appendEnergy = False)\r\n        covariance = np.cov(np.matrix.transpose(mfcc_feat))\r\n        mean_matrix = mfcc_feat.mean(0)\r\n        feature = (mean_matrix , covariance , i)\r\n        pickle.dump(feature , f)\r\n\r\nf.close()\r\n<\/pre>\n<p>6.\u00a0Train and test split on the dataset:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">dataset = []\r\ndef loadDataset(filename , split , trSet , teSet):\r\n    with open(\"my.dat\" , 'rb') as f:\r\n        while True:\r\n            try:\r\n                dataset.append(pickle.load(f))\r\n            except EOFError:\r\n                f.close()\r\n                break\t\r\n\r\n    for x in range(len(dataset)):\r\n        if random.random() &lt;split :\t\t\t\r\n            trSet.append(dataset[x])\r\n        else:\r\n            teSet.append(dataset[x])\t\r\n\r\ntrainingSet = []\r\ntestSet = []\r\nloadDataset(\"my.dat\" , 0.66, trainingSet, testSet)\r\n<\/pre>\n<p>7.\u00a0Make prediction using <a href=\"http:\/\/cs.carleton.edu\/cs_comps\/0910\/netflixprize\/final_results\/knn\/index.html\">KNN<\/a> and get the accuracy on test data:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">leng = len(testSet)\r\npredictions = []\r\nfor x in range (leng):\r\n    predictions.append(nearestClass(getNeighbors(trainingSet ,testSet[x] , 5))) \r\n\r\naccuracy1 = getAccuracy(testSet , predictions)\r\nprint(accuracy1)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-code-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78023\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-code-1.png\" alt=\"music genre code\" width=\"1160\" height=\"844\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-code-1.png 1160w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-code-1-150x109.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-code-1-300x218.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-code-1-768x559.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-code-1-1024x745.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-code-1-520x378.png 520w\" sizes=\"auto, (max-width: 1160px) 100vw, 1160px\" \/><\/a><\/p>\n<h3>Test the classifier with new audio file<\/h3>\n<p>Save the new audio file in the present directory. Make a new file test.py and paste the below script:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from python_speech_features import mfcc\r\nimport scipy.io.wavfile as wav\r\nimport numpy as np\r\nfrom tempfile import TemporaryFile\r\nimport os\r\nimport pickle\r\nimport random \r\nimport operator\r\n\r\nimport math\r\nimport numpy as np\r\nfrom collections import defaultdict\r\n\r\ndataset = []\r\ndef loadDataset(filename):\r\n    with open(\"my.dat\" , 'rb') as f:\r\n        while True:\r\n            try:\r\n                dataset.append(pickle.load(f))\r\n            except EOFError:\r\n                f.close()\r\n                break\r\n\r\nloadDataset(\"my.dat\")\r\n\r\ndef distance(instance1 , instance2 , k ):\r\n    distance =0 \r\n    mm1 = instance1[0] \r\n    cm1 = instance1[1]\r\n    mm2 = instance2[0]\r\n    cm2 = instance2[1]\r\n    distance = np.trace(np.dot(np.linalg.inv(cm2), cm1)) \r\n    distance+=(np.dot(np.dot((mm2-mm1).transpose() , np.linalg.inv(cm2)) , mm2-mm1 )) \r\n    distance+= np.log(np.linalg.det(cm2)) - np.log(np.linalg.det(cm1))\r\n    distance-= k\r\n    return distance\r\n\r\ndef getNeighbors(trainingSet , instance , k):\r\n    distances =[]\r\n    for x in range (len(trainingSet)):\r\n        dist = distance(trainingSet[x], instance, k )+ distance(instance, trainingSet[x], k)\r\n        distances.append((trainingSet[x][2], dist))\r\n    distances.sort(key=operator.itemgetter(1))\r\n    neighbors = []\r\n    for x in range(k):\r\n        neighbors.append(distances[x][0])\r\n    return neighbors\t\r\n\r\ndef nearestClass(neighbors):\r\n    classVote ={}\r\n    for x in range(len(neighbors)):\r\n        response = neighbors[x]\r\n        if response in classVote:\r\n            classVote[response]+=1 \r\n        else:\r\n            classVote[response]=1 \r\n    sorter = sorted(classVote.items(), key = operator.itemgetter(1), reverse=True)\r\n    return sorter[0][0]\r\n\r\n\r\nresults=defaultdict(int)\r\n\r\ni=1\r\nfor folder in os.listdir(\".\/musics\/wav_genres\/\"):\r\n    results[i]=folder\r\n    i+=1\r\n\r\n(rate,sig)=wav.read(\"__path_to_new_audio_file_\")\r\nmfcc_feat=mfcc(sig,rate,winlen=0.020,appendEnergy=False)\r\ncovariance = np.cov(np.matrix.transpose(mfcc_feat))\r\nmean_matrix = mfcc_feat.mean(0)\r\nfeature=(mean_matrix,covariance,0)\r\n\r\npred=nearestClass(getNeighbors(dataset ,feature , 5))\r\n\r\nprint(results[pred])\r\n<\/pre>\n<p><strong>Now, run this script to get the prediction:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">python3 test.py<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-test-model.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78233\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-test-model.png\" alt=\"music genre test model\" width=\"1151\" height=\"743\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-test-model.png 1151w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-test-model-150x97.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-test-model-300x194.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-test-model-768x496.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-test-model-1024x661.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/music-genre-test-model-520x336.png 520w\" sizes=\"auto, (max-width: 1151px) 100vw, 1151px\" \/><\/a><\/p>\n<h3>Summary:<\/h3>\n<p>Music has different types like rock, jazz, pop, or classical. This project helps a machine recognize the genre of a song using sound features. Using Python and libraries like librosa and Keras, we can build a model that listens to a song clip and tells its category.<\/p>\n<p>In this music genre classification project, we have developed a classifier on audio files to predict its genre. We work through this project on GTZAN music genre classification dataset. This tutorial explains how to extract important features from audio files. In this deep learning project we have implemented a K nearest neighbor using a count of K as 5.<\/p>\n<p>This project is perfect for music lovers and ML beginners. It teaches audio data handling, feature extraction, classification, and music signal processing. It also helps explore how AI interacts with the world of sound.<\/p>\n<p><strong>What Next? <\/strong><\/p>\n<p>Let&#8217;s proceed ahead to next-level, work on a capstone project:\u00a0<strong><a href=\"https:\/\/data-flair.training\/blogs\/python-project-driver-drowsiness-detection-system\/\">Driver Drowsiness Detection project<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Music Genre Classification &#8211;\u00a0Automatically classify different musical genres In this tutorial we are going to develop a deep learning project to automatically classify different musical genres from audio files. We will classify these audio&#46;&#46;&#46;<\/p>\n","protected":false},"author":10,"featured_media":78027,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[22272,8041,22316,21082],"class_list":["post-78020","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-machine-learning","tag-deep-learning-project-for-beginners","tag-knn-k-nearest-neighbors","tag-music-genre-classification","tag-python-project"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Project - Music Genre Classification - DataFlair<\/title>\n<meta name=\"description\" content=\"In this music genre classification python project, we will developed a classifier on audio files to predict its genre. In this deep learning project for beginners, we will classify audio files using KNN algorithm\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-project-music-genre-classification\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Project - Music Genre Classification - DataFlair\" \/>\n<meta property=\"og:description\" content=\"In this music genre classification python project, we will developed a classifier on audio files to predict its genre. In this deep learning project for beginners, we will classify audio files using KNN algorithm\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-project-music-genre-classification\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-22T06:13:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-07-29T15:20:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/python-project-music-genre-classification.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Project - Music Genre Classification - DataFlair","description":"In this music genre classification python project, we will developed a classifier on audio files to predict its genre. In this deep learning project for beginners, we will classify audio files using KNN algorithm","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/python-project-music-genre-classification\/","og_locale":"en_US","og_type":"article","og_title":"Python Project - Music Genre Classification - DataFlair","og_description":"In this music genre classification python project, we will developed a classifier on audio files to predict its genre. In this deep learning project for beginners, we will classify audio files using KNN algorithm","og_url":"https:\/\/data-flair.training\/blogs\/python-project-music-genre-classification\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2020-05-22T06:13:53+00:00","article_modified_time":"2025-07-29T15:20:16+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/python-project-music-genre-classification.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-project-music-genre-classification\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-project-music-genre-classification\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/a90b082e16aa38d207212d22b0581f33"},"headline":"Python Project &#8211; Music Genre Classification","datePublished":"2020-05-22T06:13:53+00:00","dateModified":"2025-07-29T15:20:16+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-project-music-genre-classification\/"},"wordCount":637,"commentCount":45,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-project-music-genre-classification\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/python-project-music-genre-classification.jpg","keywords":["deep learning project for beginners","kNN (k-Nearest Neighbors)","music genre classification","Python project"],"articleSection":["Machine Learning Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-project-music-genre-classification\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-project-music-genre-classification\/","url":"https:\/\/data-flair.training\/blogs\/python-project-music-genre-classification\/","name":"Python Project - Music Genre Classification - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-project-music-genre-classification\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-project-music-genre-classification\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/python-project-music-genre-classification.jpg","datePublished":"2020-05-22T06:13:53+00:00","dateModified":"2025-07-29T15:20:16+00:00","description":"In this music genre classification python project, we will developed a classifier on audio files to predict its genre. In this deep learning project for beginners, we will classify audio files using KNN algorithm","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-project-music-genre-classification\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-project-music-genre-classification\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-project-music-genre-classification\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/python-project-music-genre-classification.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/python-project-music-genre-classification.jpg","width":802,"height":420,"caption":"python project music genre classification"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-project-music-genre-classification\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Machine Learning Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/machine-learning\/"},{"@type":"ListItem","position":3,"name":"Python Project &#8211; Music Genre Classification"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/a90b082e16aa38d207212d22b0581f33","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team is passionate about delivering top-notch tutorials and resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With expertise in the tech industry, we simplify complex topics to help learners excel. Stay updated with our latest insights.","url":"https:\/\/data-flair.training\/blogs\/author\/dfadteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/78020","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=78020"}],"version-history":[{"count":8,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/78020\/revisions"}],"predecessor-version":[{"id":146353,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/78020\/revisions\/146353"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/78027"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=78020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=78020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=78020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}