{"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":"# Rudimentary Analysis\n\nThis is my first Kaggle competition attempt. With limited time I do not have high expectations on the result but the journey is just as important. Please feel free to send any critiques or advice (once the competition is over as we wouldn't want to break the competition rules).","metadata":{}},{"cell_type":"markdown","source":"## Load in packages","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pylab as plt\nimport seaborn as sns\nimport librosa\nimport librosa.display\nimport IPython.display as ipd\nfrom tqdm import tqdm\n\npd.set_option('display.max_columns', 100)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-22T01:46:40.703460Z","iopub.execute_input":"2022-03-22T01:46:40.703709Z","iopub.status.idle":"2022-03-22T01:46:40.708814Z","shell.execute_reply.started":"2022-03-22T01:46:40.703680Z","shell.execute_reply":"2022-03-22T01:46:40.707798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load in Data","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/kaggle-pog-series-s01e02/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:10:53.695588Z","iopub.execute_input":"2022-03-22T00:10:53.695886Z","iopub.status.idle":"2022-03-22T00:10:53.759987Z","shell.execute_reply.started":"2022-03-22T00:10:53.695858Z","shell.execute_reply":"2022-03-22T00:10:53.759194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Exploratory Data Analysis","metadata":{}},{"cell_type":"markdown","source":"## Dataset Size","metadata":{}},{"cell_type":"markdown","source":"### Training","metadata":{}},{"cell_type":"code","source":"print(f\"There are {train.shape[0]} songs to analyze in the training dataset\")","metadata":{"execution":{"iopub.status.busy":"2022-03-22T03:35:30.600105Z","iopub.execute_input":"2022-03-22T03:35:30.600370Z","iopub.status.idle":"2022-03-22T03:35:30.605409Z","shell.execute_reply.started":"2022-03-22T03:35:30.600344Z","shell.execute_reply":"2022-03-22T03:35:30.604124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:10:57.030894Z","iopub.execute_input":"2022-03-22T00:10:57.031224Z","iopub.status.idle":"2022-03-22T00:10:57.048233Z","shell.execute_reply.started":"2022-03-22T00:10:57.031199Z","shell.execute_reply":"2022-03-22T00:10:57.047766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Investigating Genre Distribution\n\nBelow we see that there are 19 different genres in the training dataset. The distribution of these genres is not equal in the dataset.","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(18, 6))\nchart = sns.countplot(x=\"genre\", data=train, order=train.genre.value_counts().index, palette=\"flare\")\nchart.set_xticklabels(chart.get_xticklabels(), rotation=45, horizontalalignment=\"right\")\nvcs = train.genre.value_counts().values\npatches = chart.patches\nfor i in range(len(patches)):\n   x = patches[i].get_x() + patches[i].get_width()/2\n   y = patches[i].get_height()+1\n   chart.annotate(vcs[i], (x, y), ha=\"center\")\nchart.set_xlabel(\"\")\nchart.set_ylabel(\"Count\")\nplt.title(\"Distribution of Genres in Training Dataset\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:10:59.294992Z","iopub.execute_input":"2022-03-22T00:10:59.295402Z","iopub.status.idle":"2022-03-22T00:10:59.659937Z","shell.execute_reply.started":"2022-03-22T00:10:59.295233Z","shell.execute_reply":"2022-03-22T00:10:59.658705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Investigating Signal Plot of Each Genre","metadata":{}},{"cell_type":"code","source":"genre_df = train.groupby(\"genre\").first()\n\nfigure, axis = plt.subplots(nrows=7, ncols=3, figsize=(18,40))\n\nc_row = 0\nc_col = 0\n\nfor idx, row in tqdm(genre_df.iterrows()):\n    signal, sr = librosa.load(\"/kaggle/input/kaggle-pog-series-s01e02/\" + row[\"filepath\"])\n    librosa.display.waveshow(signal, sr=sr, alpha=0.5, ax=axis[c_row, c_col])\n    axis[c_row, c_col].set_title(f\"Signal Plot for {idx} genre.\")\n    axis[c_row, c_col].set_xlabel(\"Time (s)\")\n    axis[c_row, c_col].set_ylabel(\"Amplitude\")\n    \n    c_col = (c_col + 1) % 3\n    if c_col == 0:\n        c_row += 1\nfigure.delaxes(axis[6][1])\nfigure.delaxes(axis[6][2])\nplt.show();","metadata":{"execution":{"iopub.status.busy":"2022-03-22T03:15:14.280635Z","iopub.execute_input":"2022-03-22T03:15:14.281647Z","iopub.status.idle":"2022-03-22T03:16:18.991443Z","shell.execute_reply.started":"2022-03-22T03:15:14.281573Z","shell.execute_reply":"2022-03-22T03:16:18.990667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Investigating the first song in our training Dataset","metadata":{}},{"cell_type":"code","source":"first_row = train.iloc[0]\nfirst_song, sr = librosa.load(\"/kaggle/input/kaggle-pog-series-s01e02/\" + first_row.filepath, sr=None)","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:11:01.795192Z","iopub.execute_input":"2022-03-22T00:11:01.795447Z","iopub.status.idle":"2022-03-22T00:11:01.942323Z","shell.execute_reply.started":"2022-03-22T00:11:01.795423Z","shell.execute_reply":"2022-03-22T00:11:01.940972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(first_song, rate=sr)","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:11:03.734754Z","iopub.execute_input":"2022-03-22T00:11:03.734967Z","iopub.status.idle":"2022-03-22T00:11:03.809863Z","shell.execute_reply.started":"2022-03-22T00:11:03.734944Z","shell.execute_reply":"2022-03-22T00:11:03.809090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8,4))\nlibrosa.display.waveshow(first_song, sr=sr)\nplt.title(\"Vizualiation of first song in training set\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:13:53.684203Z","iopub.execute_input":"2022-03-22T00:13:53.684557Z","iopub.status.idle":"2022-03-22T00:13:53.990071Z","shell.execute_reply.started":"2022-03-22T00:13:53.684531Z","shell.execute_reply":"2022-03-22T00:13:53.988979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Root-Mean-Square (RMS)","metadata":{}},{"cell_type":"code","source":"rms = librosa.feature.rms(y=first_song)\nplt.title(\"RMS of first song\")\nplt.plot(rms[0]);","metadata":{"execution":{"iopub.status.busy":"2022-03-22T01:03:02.316114Z","iopub.execute_input":"2022-03-22T01:03:02.317206Z","iopub.status.idle":"2022-03-22T01:03:02.499505Z","shell.execute_reply.started":"2022-03-22T01:03:02.317141Z","shell.execute_reply":"2022-03-22T01:03:02.498402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Chromagram","metadata":{}},{"cell_type":"code","source":"S = np.abs(librosa.stft(first_song))\nchroma = librosa.feature.chroma_stft(S=S, sr=sr)\nfig, ax = plt.subplots(nrows=2, sharex=True)\nimg = librosa.display.specshow(librosa.amplitude_to_db(S, ref=np.max), y_axis='log', x_axis='time', ax=ax[0])\nfig.colorbar(img, ax=[ax[0]])\nax[0].label_outer()\nimg = librosa.display.specshow(chroma, y_axis='chroma', x_axis='time', ax=ax[1])\nfig.colorbar(img, ax=[ax[1]])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T01:05:44.394748Z","iopub.execute_input":"2022-03-22T01:05:44.394995Z","iopub.status.idle":"2022-03-22T01:05:46.374876Z","shell.execute_reply.started":"2022-03-22T01:05:44.394970Z","shell.execute_reply":"2022-03-22T01:05:46.373838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Spectral Centroid and Rolloff","metadata":{}},{"cell_type":"code","source":"cent = librosa.feature.spectral_centroid(y=first_song, sr=sr)\nrolloff = librosa.feature.spectral_rolloff(y=first_song, sr=sr, roll_percent=0.99)\nrolloff_min = librosa.feature.spectral_rolloff(y=first_song, sr=sr, roll_percent=0.01)\n\nS, phase = librosa.magphase(librosa.stft(y=first_song))\ntimes = librosa.times_like(cent)\n\n\nfig, ax = plt.subplots()\nlibrosa.display.specshow(librosa.amplitude_to_db(S, ref=np.max), y_axis='log', x_axis='time', ax=ax)\nax.plot(times, cent.T, label='Spectral centroid', color='w')\nax.plot(librosa.times_like(rolloff), rolloff[0], label='Roll-off frequency (0.99)')\nax.plot(librosa.times_like(rolloff), rolloff_min[0], color='w', label='Roll-off frequency (0.01)')\nax.legend(loc='upper right')\nax.set(title='log Power spectrogram')","metadata":{"execution":{"iopub.status.busy":"2022-03-22T01:11:10.661811Z","iopub.execute_input":"2022-03-22T01:11:10.662053Z","iopub.status.idle":"2022-03-22T01:11:13.620311Z","shell.execute_reply.started":"2022-03-22T01:11:10.662030Z","shell.execute_reply":"2022-03-22T01:11:13.619405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Mel-frequency cepstral coefficients (MFCCs)","metadata":{}},{"cell_type":"code","source":"mfccs = librosa.feature.mfcc(y=first_song, sr=sr, n_mfcc=20)\nS = librosa.feature.melspectrogram(y=first_song, sr=sr, fmax=8000)\n\nimg = librosa.display.specshow(librosa.power_to_db(S, ref=np.max), x_axis='time', y_axis='mel', fmax=8000)\nplt.colorbar(img)\nplt.title(\"MFCC\")\nplt.show();","metadata":{"execution":{"iopub.status.busy":"2022-03-22T01:19:21.352460Z","iopub.execute_input":"2022-03-22T01:19:21.353062Z","iopub.status.idle":"2022-03-22T01:19:21.960925Z","shell.execute_reply.started":"2022-03-22T01:19:21.353023Z","shell.execute_reply":"2022-03-22T01:19:21.959185Z"},"trusted":true},"execution_count":null,"outputs":[]}]}