{"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":"code","source":"!pip install python_speech_features\nimport os\nfrom tqdm import tqdm\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy.io import wavfile\nfrom python_speech_features import mfcc, logfbank\nimport librosa\n\nimport numpy as np\nnp.random.seed(1001)\n\nimport os\nimport shutil\n\nimport IPython\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport seaborn as sns\nfrom tqdm import tqdm_notebook\n#from sklearn.cross_validation import StratifiedKFold\nimport wave\n\n%matplotlib inline\nmatplotlib.style.use('ggplot')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-30T18:44:16.355251Z","iopub.execute_input":"2023-01-30T18:44:16.356164Z","iopub.status.idle":"2023-01-30T18:44:27.192439Z","shell.execute_reply.started":"2023-01-30T18:44:16.356122Z","shell.execute_reply":"2023-01-30T18:44:27.191310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef plot_signals(signals):\n    fig, axes = plt.subplots(nrows=2, ncols=5, sharex=False, sharey=True, figsize=(20,5))\n    fig.suptitle(\"Time Series\", size = 16)\n\n    i = 0\n\n    for x in range(2):\n        for y in range(5):\n            axes[x,y].set_title(list(signals.keys())[i])\n            axes[x,y].plot(list(signals.values())[i])\n            axes[x,y].get_xaxis().set_visible(False)\n            axes[x,y].get_yaxis().set_visible(False)\n            i += 1\n\ndef plot_fft(fft):\n    fig, axes = plt.subplots(nrows=2, ncols=5, sharex=False, sharey=True, figsize=(20,5))\n    fig.suptitle(\"Fourier Transforms\", size = 16)\n    i = 0\n\n    for x in range(2):\n        for y in range(5):\n            data = list(fft.values())[i]\n            Y, frequency = data[0], data[1]\n            axes[x,y].set_title(list(fft.keys())[i])\n            axes[x,y].plot(frequency, Y)\n            axes[x,y].get_xaxis().set_visible(False)\n            axes[x,y].get_yaxis().set_visible(False)\n            i += 1\n\n\ndef plot_fbank(fbank):\n    fig, axes = plt.subplots(nrows=2, ncols=5, sharex=False, sharey=True, figsize=(20,5))\n    fig.suptitle(\"Filter Bank Coefficents\", size = 16)\n    i = 0\n\n    for x in range(2):\n        for y in range(5):   \n             axes[x,y].set_title(list(fbank.keys())[i])\n             axes[x,y].imshow(list(fbank.values())[i], cmap=\"hot\", interpolation=\"nearest\")\n             axes[x,y].get_xaxis().set_visible(False)\n             axes[x,y].get_yaxis().set_visible(False)\n             i += 1\n                \n\"\"\"  We can choose this filters in the interpolation  \n'antialiased', 'none', 'nearest', 'bilinear', 'bicubic', 'spline16', 'spline36',\n'hanning', 'hamming', 'hermite', 'kaiser', 'quadric', 'catrom', 'gaussian', 'bessel', \n'mitchell', 'sinc', 'lanczos', 'blackman\n\"\"\"\n\ndef plot_mfccs(mfccs):\n    fig, axes = plt.subplots(nrows=2, ncols=5, sharex=False, sharey=True, figsize=(20,5))\n    fig.suptitle(\"Mel Frequency Cepstrum Coefficients\", size = 16)\n    i = 0    \n\n    for x in range(2):\n        for y in range(5):   \n             axes[x,y].set_title(list(mfccs.keys())[i])\n             axes[x,y].imshow(list(mfccs.values())[i], cmap=\"hot\", interpolation=\"nearest\")\n             axes[x,y].get_xaxis().set_visible(False)\n             axes[x,y].get_yaxis().set_visible(False)\n             i += 1\n\n","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:38:30.705169Z","iopub.execute_input":"2023-01-30T18:38:30.705567Z","iopub.status.idle":"2023-01-30T18:38:30.725902Z","shell.execute_reply.started":"2023-01-30T18:38:30.705534Z","shell.execute_reply":"2023-01-30T18:38:30.724389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## EDA Anlaysis\n\nExploratory Data Analysis (EDA) is a process of analyzing and summarizing a dataset to gain insights and understanding about the data. It involves using various techniques such as visualizations, statistical tests, and data transformations to get a better understanding of the data and its underlying structure, patterns, and relationships. The goal of EDA is to uncover meaningful information, identify trends, and discover interesting insights, while also uncovering potential problems in the data.","metadata":{}},{"cell_type":"markdown","source":"#### Exploratory analysis is important for several reasons:\n- Detecting outliers and anomalies\n- Identifying patterns and relationships in the data\n- Understanding the structure and nature of the data\n- Selecting appropriate statistical methods and models\n- Generating hypotheses for further testing.","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/freesound-audio-tagging/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/freesound-audio-tagging/sample_submission.csv\")\ntrain ","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:56:56.464182Z","iopub.execute_input":"2023-01-30T18:56:56.464592Z","iopub.status.idle":"2023-01-30T18:56:56.528650Z","shell.execute_reply.started":"2023-01-30T18:56:56.464558Z","shell.execute_reply":"2023-01-30T18:56:56.527660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_post_competition = pd.read_csv(\"/kaggle/input/freesound-audio-tagging/train_post_competition.csv\")\ntrain_post_competition","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:26:42.523724Z","iopub.execute_input":"2023-01-30T18:26:42.524770Z","iopub.status.idle":"2023-01-30T18:26:42.551995Z","shell.execute_reply.started":"2023-01-30T18:26:42.524726Z","shell.execute_reply":"2023-01-30T18:26:42.550998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"train and train_post_competition are same data vertification to reach the datas.","metadata":{}},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:26:44.901402Z","iopub.execute_input":"2023-01-30T18:26:44.901969Z","iopub.status.idle":"2023-01-30T18:26:44.910903Z","shell.execute_reply.started":"2023-01-30T18:26:44.901919Z","shell.execute_reply":"2023-01-30T18:26:44.909568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.groupby(\"label\").count()","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:26:45.424834Z","iopub.execute_input":"2023-01-30T18:26:45.425545Z","iopub.status.idle":"2023-01-30T18:26:45.442170Z","shell.execute_reply.started":"2023-01-30T18:26:45.425503Z","shell.execute_reply":"2023-01-30T18:26:45.441005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train.label.unique()))\ntrain.label.unique()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:26:45.832151Z","iopub.execute_input":"2023-01-30T18:26:45.832784Z","iopub.status.idle":"2023-01-30T18:26:45.843961Z","shell.execute_reply.started":"2023-01-30T18:26:45.832746Z","shell.execute_reply":"2023-01-30T18:26:45.842801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Number of training examples=\", train.shape[0], \"  Number of classes=\", len(train.label.unique()))","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:26:46.178872Z","iopub.execute_input":"2023-01-30T18:26:46.179270Z","iopub.status.idle":"2023-01-30T18:26:46.185596Z","shell.execute_reply.started":"2023-01-30T18:26:46.179235Z","shell.execute_reply":"2023-01-30T18:26:46.184573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"category_group = train.groupby(['label', 'manually_verified']).count()\ncategory_group","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:26:46.497772Z","iopub.execute_input":"2023-01-30T18:26:46.498146Z","iopub.status.idle":"2023-01-30T18:26:46.517365Z","shell.execute_reply.started":"2023-01-30T18:26:46.498118Z","shell.execute_reply":"2023-01-30T18:26:46.516335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot = category_group.unstack().reindex(category_group.unstack().sum(axis=1).sort_values().index)\\\n          .plot(kind='bar', stacked=True, title=\"Number of Audio Samples per Category\", figsize=(16,10))\nplot.set_xlabel(\"Category\")\nplot.set_ylabel(\"Number of Samples\");","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:26:46.862165Z","iopub.execute_input":"2023-01-30T18:26:46.863233Z","iopub.status.idle":"2023-01-30T18:26:47.553426Z","shell.execute_reply.started":"2023-01-30T18:26:46.863195Z","shell.execute_reply":"2023-01-30T18:26:47.552325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Minimum samples per category = ', min(train.label.value_counts()))\nprint('Maximum samples per category = ', max(train.label.value_counts()))","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:26:47.555387Z","iopub.execute_input":"2023-01-30T18:26:47.555749Z","iopub.status.idle":"2023-01-30T18:26:47.564506Z","shell.execute_reply.started":"2023-01-30T18:26:47.555717Z","shell.execute_reply":"2023-01-30T18:26:47.563245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Listening of the data","metadata":{}},{"cell_type":"code","source":"import IPython.display as ipd  # To play sound in the notebook\nfname = '../input/freesound-audio-tagging/audio_train/' + '00044347.wav'   # Hi-hat\nipd.Audio(fname)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:26:47.828053Z","iopub.execute_input":"2023-01-30T18:26:47.829053Z","iopub.status.idle":"2023-01-30T18:26:47.862085Z","shell.execute_reply.started":"2023-01-30T18:26:47.829012Z","shell.execute_reply":"2023-01-30T18:26:47.861164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fname2 = '../input/freesound-audio-tagging/audio_train/' + str(train[\"fname\"][1])   # Hi-hat\nprint(\"label: \" + str(train[\"label\"][1]))\nipd.Audio(fname2)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:26:48.175509Z","iopub.execute_input":"2023-01-30T18:26:48.176465Z","iopub.status.idle":"2023-01-30T18:26:48.204109Z","shell.execute_reply.started":"2023-01-30T18:26:48.176430Z","shell.execute_reply":"2023-01-30T18:26:48.203175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Using wave library\nimport wave\nwav = wave.open(fname)\nprint(\"Sampling (frame) rate = \", wav.getframerate())\nprint(\"Total samples (frames) = \", wav.getnframes())\nprint(\"Duration = \", wav.getnframes()/wav.getframerate())\n\n\nwav2 = wave.open(fname2)\nprint(\"Sampling (frame) rate = \", wav2.getframerate())\nprint(\"Total samples (frames) = \", wav2.getnframes())\nprint(\"Duration = \", wav2.getnframes()/wav2.getframerate())","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:47:48.152804Z","iopub.execute_input":"2023-01-30T18:47:48.153562Z","iopub.status.idle":"2023-01-30T18:47:48.165689Z","shell.execute_reply.started":"2023-01-30T18:47:48.153521Z","shell.execute_reply":"2023-01-30T18:47:48.164330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Using scipy\nfrom scipy.io import wavfile\nrate, data = wavfile.read(fname)\nprint(\"Sampling (frame) rate = \", rate)\nprint(\"Total samples (frames) = \", data.shape)\nprint(data)\nprint(len(data))\n\nrate2, data2 = wavfile.read(fname2)\nprint(\"Sampling (frame) rate = \", rate2)\nprint(\"Total samples (frames) = \", data2.shape)\nprint(data2)","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:48:25.073455Z","iopub.execute_input":"2023-01-30T18:48:25.074655Z","iopub.status.idle":"2023-01-30T18:48:25.085402Z","shell.execute_reply.started":"2023-01-30T18:48:25.074614Z","shell.execute_reply":"2023-01-30T18:48:25.084263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(data, '-', );\nplt.show()\n\nplt.plot(data2, '-', );\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:49:02.688718Z","iopub.execute_input":"2023-01-30T18:49:02.689148Z","iopub.status.idle":"2023-01-30T18:49:03.306262Z","shell.execute_reply.started":"2023-01-30T18:49:02.689112Z","shell.execute_reply":"2023-01-30T18:49:03.303557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Zoom in on the 1000 frames","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(16, 4))\nplt.plot(data[:500], '.'); plt.plot(data[:500], '-')\nplt.show()\n\nplt.figure(figsize=(16, 4))\nplt.plot(data2[:500], '.'); plt.plot(data2[:500], '-')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:50:15.741165Z","iopub.execute_input":"2023-01-30T18:50:15.741591Z","iopub.status.idle":"2023-01-30T18:50:16.164508Z","shell.execute_reply.started":"2023-01-30T18:50:15.741553Z","shell.execute_reply":"2023-01-30T18:50:16.163424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Audio Length","metadata":{}},{"cell_type":"code","source":"train['nframes'] = train['fname'].apply(lambda f: wave.open('../input/freesound-audio-tagging/audio_train/' + f).getnframes())\ntest['nframes'] = test['fname'].apply(lambda f: wave.open('../input/freesound-audio-tagging/audio_test/' + f).getnframes())\n\n_, ax = plt.subplots(figsize=(16, 4))\nsns.violinplot(ax=ax, x=\"label\", y=\"nframes\", data=train)\nplt.xticks(rotation=90)\nplt.title('Distribution of audio frames, per label', fontsize=16)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:57:03.493730Z","iopub.execute_input":"2023-01-30T18:57:03.494169Z","iopub.status.idle":"2023-01-30T18:59:35.994584Z","shell.execute_reply.started":"2023-01-30T18:57:03.494136Z","shell.execute_reply":"2023-01-30T18:59:35.992698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(16,5))\ntrain.nframes.hist(bins=100, ax=axes[0])\ntest.nframes.hist(bins=100, ax=axes[1])\nplt.suptitle('Frame Length Distribution in Train and Test', ha='center', fontsize='large');","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:54:08.712626Z","iopub.status.idle":"2023-01-30T18:54:08.713444Z","shell.execute_reply.started":"2023-01-30T18:54:08.713144Z","shell.execute_reply":"2023-01-30T18:54:08.713171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"abnormal_length = [707364, 353682, 138474, 184338]\n\nfor length in abnormal_length:\n    abnormal_fnames = test.loc[test.nframes == length, 'fname'].values\n    print(\"Frame length = \", length, \" Number of files = \", abnormal_fnames.shape[0], end=\"   \")\n    fname = np.random.choice(abnormal_fnames)\n    print(\"Playing \", fname)\n    IPython.display.display(ipd.Audio( '../input/freesound-audio-tagging/audio_test/' + fname))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = list(np.unique(train.label))\nclass_dist = train.groupby([\"label\"])[\"manually_verified\"].mean()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:26:48.492524Z","iopub.execute_input":"2023-01-30T18:26:48.493440Z","iopub.status.idle":"2023-01-30T18:26:48.505751Z","shell.execute_reply.started":"2023-01-30T18:26:48.493401Z","shell.execute_reply":"2023-01-30T18:26:48.504549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:26:48.833681Z","iopub.execute_input":"2023-01-30T18:26:48.834862Z","iopub.status.idle":"2023-01-30T18:26:48.840888Z","shell.execute_reply.started":"2023-01-30T18:26:48.834806Z","shell.execute_reply":"2023-01-30T18:26:48.839918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_dist                   ","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:26:49.129462Z","iopub.execute_input":"2023-01-30T18:26:49.130379Z","iopub.status.idle":"2023-01-30T18:26:49.139704Z","shell.execute_reply.started":"2023-01-30T18:26:49.130309Z","shell.execute_reply":"2023-01-30T18:26:49.138520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:26:49.468641Z","iopub.execute_input":"2023-01-30T18:26:49.469685Z","iopub.status.idle":"2023-01-30T18:26:49.482767Z","shell.execute_reply.started":"2023-01-30T18:26:49.469637Z","shell.execute_reply":"2023-01-30T18:26:49.481546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def calc_fft(y, rate):\n    \"\"\"\n    y = signal\n    \"\"\"\n    n = len(y)\n    frequency = np.fft.rfftfreq(n, d=1/rate)\n    Y = abs(np.fft.rfft(y)/n)\n    return (Y, frequency)\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:26:49.806634Z","iopub.execute_input":"2023-01-30T18:26:49.807357Z","iopub.status.idle":"2023-01-30T18:26:49.813678Z","shell.execute_reply.started":"2023-01-30T18:26:49.807318Z","shell.execute_reply":"2023-01-30T18:26:49.812380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[train.label == \"Hi-hat\"].iloc[0,0]","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:26:50.113554Z","iopub.execute_input":"2023-01-30T18:26:50.114447Z","iopub.status.idle":"2023-01-30T18:26:50.123275Z","shell.execute_reply.started":"2023-01-30T18:26:50.114406Z","shell.execute_reply":"2023-01-30T18:26:50.122149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"signals = {}\nfft = {}\nfbank = {}\nmfccs = {}","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:26:50.461416Z","iopub.execute_input":"2023-01-30T18:26:50.462352Z","iopub.status.idle":"2023-01-30T18:26:50.466977Z","shell.execute_reply.started":"2023-01-30T18:26:50.462311Z","shell.execute_reply":"2023-01-30T18:26:50.466052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for c in classes:\n    wave_file = train[train.label == c].iloc[0,0]\n    signal, rate = librosa.load(\"/kaggle/input/freesound-audio-tagging/audio_train/\"+ wave_file )\n    signals[c] = signal\n    fft[c] = calc_fft(signal, rate)\n    \n    bank = logfbank(signal[:rate], rate, nfilt=26, nfft=1003).T # Filter bank\n    fbank[c] = bank\n    \n    mel = mfcc(signal[:rate], rate, numcep=13, nfilt=26, nfft=1103).T\n    mfccs[c] = mel\n\nplot_signals(signals)\nplt.show()\n\nplot_fft(fft)\nplt.show()\n\nplot_fbank(fbank)\nplt.show()\n\nplot_mfccs(mfccs)\nplt.show()\n    \n    \n    ","metadata":{"execution":{"iopub.status.busy":"2023-01-30T18:38:36.932102Z","iopub.execute_input":"2023-01-30T18:38:36.932507Z","iopub.status.idle":"2023-01-30T18:38:51.507134Z","shell.execute_reply.started":"2023-01-30T18:38:36.932460Z","shell.execute_reply":"2023-01-30T18:38:51.505857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}