{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"![Music](https://i.imgur.com/LQH85f2.png)","metadata":{}},{"cell_type":"markdown","source":"# Music Categorization Competition","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nfrom matplotlib import pyplot as plt\n\nimport librosa\nimport librosa.display\nimport IPython.display as ipd\n\npd.options.display.max_rows = 50\npd.options.display.float_format = \"{:.4f}\".format\npd.set_option('max_columns', 200)\nplt.style.use('ggplot')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-11T17:04:02.150776Z","iopub.execute_input":"2022-03-11T17:04:02.151444Z","iopub.status.idle":"2022-03-11T17:04:02.157119Z","shell.execute_reply.started":"2022-03-11T17:04:02.151407Z","shell.execute_reply":"2022-03-11T17:04:02.156487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = pd.read_csv('../input/kaggle-pog-series-s01e02/train.csv')\ntest_csv = pd.read_csv('../input/kaggle-pog-series-s01e02/test.csv')\nss = pd.read_csv('../input/kaggle-pog-series-s01e02/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-11T15:57:38.720219Z","iopub.execute_input":"2022-03-11T15:57:38.720758Z","iopub.status.idle":"2022-03-11T15:57:38.816454Z","shell.execute_reply.started":"2022-03-11T15:57:38.720698Z","shell.execute_reply":"2022-03-11T15:57:38.815369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploratory Data Analysis","metadata":{}},{"cell_type":"code","source":"train_csv.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-11T16:33:03.894719Z","iopub.execute_input":"2022-03-11T16:33:03.895077Z","iopub.status.idle":"2022-03-11T16:33:03.909716Z","shell.execute_reply.started":"2022-03-11T16:33:03.895038Z","shell.execute_reply":"2022-03-11T16:33:03.908697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_csv.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-11T15:59:25.337976Z","iopub.execute_input":"2022-03-11T15:59:25.338369Z","iopub.status.idle":"2022-03-11T15:59:25.350712Z","shell.execute_reply.started":"2022-03-11T15:59:25.338328Z","shell.execute_reply":"2022-03-11T15:59:25.349674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Genre Distribution:\n- Genre distribution is not balanced. \n- Reminder that the metric is Macro-F1 score. \n- Meaning cannot overfit to top 4 categories and ignore the rest. ","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(10,6))\ngenres = train_csv['genre'].value_counts().sort_values(ascending=True)\nbars = ax.barh(genres.index, genres, color='#0044cc')\nax.bar_label(bars)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-11T16:19:02.352488Z","iopub.execute_input":"2022-03-11T16:19:02.352767Z","iopub.status.idle":"2022-03-11T16:19:02.674778Z","shell.execute_reply.started":"2022-03-11T16:19:02.352738Z","shell.execute_reply":"2022-03-11T16:19:02.673762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Sample Song","metadata":{}},{"cell_type":"code","source":"N_FFT = 2048\nHOP_LEN = 512\nFMIN = 20\nFMAX = 16000","metadata":{"execution":{"iopub.status.busy":"2022-03-11T16:56:14.138646Z","iopub.execute_input":"2022-03-11T16:56:14.138962Z","iopub.status.idle":"2022-03-11T16:56:14.143337Z","shell.execute_reply.started":"2022-03-11T16:56:14.138925Z","shell.execute_reply":"2022-03-11T16:56:14.142229Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_full_path(path):\n    return f\"../input/kaggle-pog-series-s01e02/train/{path}\"\n\n\ndef play_song(filename, genre):\n    display(genre, ipd.Audio(get_full_path(filename)))\n\n\ndef load_audio(filename):\n    full_path = get_full_path(filename)\n    assert os.path.isfile(full_path), (full_path, filename)\n    audio, sr = librosa.load(full_path)\n    return audio, sr\n\ndef create_spectrogram(filename, genre):\n    audio, SR = load_audio(filename)\n    stft = librosa.stft(audio, n_fft=N_FFT, hop_length=HOP_LEN, win_length=N_FFT)\n    S_db = librosa.amplitude_to_db(np.abs(stft), ref=np.max)\n    fig, ax = plt.subplots(figsize=(15, 6))\n    ax.set_title(\n        f\"Spectrogram of Audio:{filename} \\n Genre: {genre}\",\n        fontsize=15,\n    )\n    img = librosa.display.specshow(\n        S_db,\n        hop_length=HOP_LEN,\n        sr=SR,\n        fmin=FMIN,\n        fmax=FMAX,\n        x_axis=\"time\",\n        y_axis=\"hz\",\n    )  \n    fig.colorbar(img, ax=ax)\n\n\ndef view_spectrogram(filename):\n    df = train_csv.query(\"filename == @filename\")\n    file = df.filename.array[0]\n    genre = df.genre.array[0]\n    create_spectrogram(file, genre)\n    play_song(file, genre)","metadata":{"execution":{"iopub.status.busy":"2022-03-11T16:57:24.129306Z","iopub.execute_input":"2022-03-11T16:57:24.129873Z","iopub.status.idle":"2022-03-11T16:57:24.140345Z","shell.execute_reply.started":"2022-03-11T16:57:24.129834Z","shell.execute_reply":"2022-03-11T16:57:24.139627Z"},"_kg_hide-output":true,"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# I don't know why this song was categorized as Blues, but it is. \nview_spectrogram('007540.ogg')","metadata":{"execution":{"iopub.status.busy":"2022-03-11T17:09:57.925086Z","iopub.execute_input":"2022-03-11T17:09:57.925424Z","iopub.status.idle":"2022-03-11T17:10:01.004527Z","shell.execute_reply.started":"2022-03-11T17:09:57.925390Z","shell.execute_reply":"2022-03-11T17:10:01.003429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"view_spectrogram('009414.ogg')","metadata":{"execution":{"iopub.status.busy":"2022-03-11T17:04:26.278496Z","iopub.execute_input":"2022-03-11T17:04:26.278800Z","iopub.status.idle":"2022-03-11T17:04:29.350054Z","shell.execute_reply.started":"2022-03-11T17:04:26.278756Z","shell.execute_reply":"2022-03-11T17:04:29.349126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"view_spectrogram('023041.ogg')","metadata":{"execution":{"iopub.status.busy":"2022-03-11T17:05:53.430262Z","iopub.execute_input":"2022-03-11T17:05:53.430522Z","iopub.status.idle":"2022-03-11T17:05:56.432055Z","shell.execute_reply.started":"2022-03-11T17:05:53.430492Z","shell.execute_reply":"2022-03-11T17:05:56.431167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Good luck!\n- Good luck everyone who is going to compete in this. \n- I'll try to participate, if I can find some time to work on it. ","metadata":{}}]}