{"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":"                                                                                # This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport json\nimport librosa\nimport librosa.display\nimport matplotlib.pyplot as plt\nfrom scipy import signal\nfrom scipy.io import wavfile\n#reading CSV with pandas\nTaxonomy = pd.read_csv('../input/birdclef-2022/eBird_Taxonomy_v2021.csv')\ntest = pd.read_csv('../input/birdclef-2022/test.csv')\ntrain_metadata = pd.read_csv('../input/birdclef-2022/train_metadata.csv')\nsubmission = pd.read_csv('../input/birdclef-2022/sample_submission.csv')\nfrom os import path\nfrom pydub import AudioSegment\nimport seaborn as sns\n\n# files\nsrc = \"../input/birdclef-2022/train_audio/afrsil1/XC395771.ogg\"#this audio have 4.5 rating\ndst = \"/kaggle/working/XC395771.wav\"\n\n# convert ogg to wav\nsound = AudioSegment.from_ogg(src)\nsound.export(dst, format=\"wav\")\n\n#transform into dataframe\ndf_taxonomy = pd.DataFrame(Taxonomy)\n\ndf_test = pd.DataFrame(test)\n\ndf_train = pd.DataFrame(train_metadata)\n\ndf_submission = pd.DataFrame(submission)\n\ndf_taxonomy.head()\n\n\n\n\n\n\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n#import os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n    #for filename in filenames:\n        #print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-13T12:58:53.926862Z","iopub.execute_input":"2022-05-13T12:58:53.927211Z","iopub.status.idle":"2022-05-13T12:58:54.368699Z","shell.execute_reply.started":"2022-05-13T12:58:53.927164Z","shell.execute_reply":"2022-05-13T12:58:54.367640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-13T12:58:54.370704Z","iopub.execute_input":"2022-05-13T12:58:54.370963Z","iopub.status.idle":"2022-05-13T12:58:54.384246Z","shell.execute_reply.started":"2022-05-13T12:58:54.370934Z","shell.execute_reply":"2022-05-13T12:58:54.383557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.size","metadata":{"execution":{"iopub.status.busy":"2022-05-13T12:58:54.385435Z","iopub.execute_input":"2022-05-13T12:58:54.385838Z","iopub.status.idle":"2022-05-13T12:58:54.401980Z","shell.execute_reply.started":"2022-05-13T12:58:54.385807Z","shell.execute_reply":"2022-05-13T12:58:54.400876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"df_train['primary_label']","metadata":{"execution":{"iopub.status.busy":"2022-05-13T12:58:54.403403Z","iopub.execute_input":"2022-05-13T12:58:54.403859Z","iopub.status.idle":"2022-05-13T12:58:54.421814Z","shell.execute_reply.started":"2022-05-13T12:58:54.403802Z","shell.execute_reply":"2022-05-13T12:58:54.420673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.columns\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-13T12:58:54.423410Z","iopub.execute_input":"2022-05-13T12:58:54.423664Z","iopub.status.idle":"2022-05-13T12:58:54.446876Z","shell.execute_reply.started":"2022-05-13T12:58:54.423635Z","shell.execute_reply":"2022-05-13T12:58:54.445787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['filename'][16]","metadata":{"execution":{"iopub.status.busy":"2022-05-13T12:58:54.448092Z","iopub.execute_input":"2022-05-13T12:58:54.448631Z","iopub.status.idle":"2022-05-13T12:58:54.463271Z","shell.execute_reply.started":"2022-05-13T12:58:54.448503Z","shell.execute_reply":"2022-05-13T12:58:54.462513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['rating']\ny_axis = df_train['rating']\nx_axis = df_train['primary_label']\nplt.bar(x_axis, y_axis)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-13T12:58:54.464804Z","iopub.execute_input":"2022-05-13T12:58:54.465306Z","iopub.status.idle":"2022-05-13T12:59:27.404802Z","shell.execute_reply.started":"2022-05-13T12:58:54.465268Z","shell.execute_reply":"2022-05-13T12:59:27.403840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-13T12:59:27.406168Z","iopub.execute_input":"2022-05-13T12:59:27.406439Z","iopub.status.idle":"2022-05-13T12:59:27.417746Z","shell.execute_reply.started":"2022-05-13T12:59:27.406407Z","shell.execute_reply":"2022-05-13T12:59:27.416632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#opening the scored bird file\nwith open('../input/birdclef-2022/scored_birds.json','r') as f:\n    data = json.load(f)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T12:59:27.419388Z","iopub.execute_input":"2022-05-13T12:59:27.419785Z","iopub.status.idle":"2022-05-13T12:59:27.432360Z","shell.execute_reply.started":"2022-05-13T12:59:27.419744Z","shell.execute_reply":"2022-05-13T12:59:27.431690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(data)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T12:59:27.433625Z","iopub.execute_input":"2022-05-13T12:59:27.434654Z","iopub.status.idle":"2022-05-13T12:59:27.446158Z","shell.execute_reply.started":"2022-05-13T12:59:27.434605Z","shell.execute_reply":"2022-05-13T12:59:27.445164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_rate, samples = wavfile.read('/kaggle/working/XC395771.wav')\n","metadata":{"execution":{"iopub.status.busy":"2022-05-13T12:59:27.447883Z","iopub.execute_input":"2022-05-13T12:59:27.448502Z","iopub.status.idle":"2022-05-13T12:59:27.460714Z","shell.execute_reply.started":"2022-05-13T12:59:27.448455Z","shell.execute_reply":"2022-05-13T12:59:27.459590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(samples)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T12:59:27.464407Z","iopub.execute_input":"2022-05-13T12:59:27.464837Z","iopub.status.idle":"2022-05-13T12:59:27.474047Z","shell.execute_reply.started":"2022-05-13T12:59:27.464787Z","shell.execute_reply":"2022-05-13T12:59:27.473111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frequencies, times, spectrogram = signal.spectrogram(samples, sample_rate)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T12:59:27.475835Z","iopub.execute_input":"2022-05-13T12:59:27.476448Z","iopub.status.idle":"2022-05-13T12:59:27.505648Z","shell.execute_reply.started":"2022-05-13T12:59:27.476399Z","shell.execute_reply":"2022-05-13T12:59:27.504557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Plotting Amplitude of a wav file against time\nfrom scipy.io.wavfile import read\nimport matplotlib.pyplot as plt\nplt.rcParams[\"figure.figsize\"] = [7.50, 3.50]\nplt.rcParams[\"figure.autolayout\"] = True\ninput_data = read(\"/kaggle/working/XC395771.wav\")\naudio = input_data[1]\nplt.plot(audio[0:200000])#6seconds of video\nplt.ylabel(\"Amplitude\")\nplt.xlabel(\"Time\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-13T12:59:27.506885Z","iopub.execute_input":"2022-05-13T12:59:27.507103Z","iopub.status.idle":"2022-05-13T12:59:27.840424Z","shell.execute_reply.started":"2022-05-13T12:59:27.507076Z","shell.execute_reply":"2022-05-13T12:59:27.839838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"hypothesis: we need to consider the distancies between peaks with aproximately the same amplitude but only the ones which are close to each other, for example, the first two peaks that can be seeing in the image above. I think one of the features can be the average speed at which the bird repeat the same frequency. It is easier to understand when you listen the first 6 second of the audio and see the image above at the same time.","metadata":{}},{"cell_type":"code","source":"#Lets try to look for periodicity in this audio\n\nfrom statsmodels.graphics.tsaplots import plot_acf\nplot_acf(audio)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-13T12:59:27.841405Z","iopub.execute_input":"2022-05-13T12:59:27.841857Z","iopub.status.idle":"2022-05-13T13:02:23.191683Z","shell.execute_reply.started":"2022-05-13T12:59:27.841802Z","shell.execute_reply":"2022-05-13T13:02:23.190347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Creating an spectrogram of the same file\nimport matplotlib.pyplot as plt\nfrom scipy import signal\nfrom scipy.io import wavfile\nplt.rcParams[\"figure.figsize\"] = [7.00, 3.50]\nplt.rcParams[\"figure.autolayout\"] = True\nsample_rate, samples = wavfile.read('/kaggle/working/XC395771.wav')\nfrequencies, times, spectrogram = signal.spectrogram(samples, sample_rate)\nplt.pcolormesh(times, frequencies, 10 * np.log10(spectrogram),shading = 'auto')\nplt.figure(figsize = (3,3))\n\nplt.show","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:23.193805Z","iopub.execute_input":"2022-05-13T13:02:23.194176Z","iopub.status.idle":"2022-05-13T13:02:24.040050Z","shell.execute_reply.started":"2022-05-13T13:02:23.194113Z","shell.execute_reply":"2022-05-13T13:02:24.038940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#We calculate the discrete Fourier Transformation for our signal\n\nfft_spectrum = np.fft.rfft(input_data[1])","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:24.041488Z","iopub.execute_input":"2022-05-13T13:02:24.041761Z","iopub.status.idle":"2022-05-13T13:02:24.111689Z","shell.execute_reply.started":"2022-05-13T13:02:24.041732Z","shell.execute_reply":"2022-05-13T13:02:24.110598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fft_spectrum","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:24.113060Z","iopub.execute_input":"2022-05-13T13:02:24.113302Z","iopub.status.idle":"2022-05-13T13:02:24.120352Z","shell.execute_reply.started":"2022-05-13T13:02:24.113274Z","shell.execute_reply":"2022-05-13T13:02:24.119661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#range of frequencies\nfreq = np.fft.rfftfreq(input_data[1].size, d = 1./sample_rate)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:24.121374Z","iopub.execute_input":"2022-05-13T13:02:24.121655Z","iopub.status.idle":"2022-05-13T13:02:24.134563Z","shell.execute_reply.started":"2022-05-13T13:02:24.121622Z","shell.execute_reply":"2022-05-13T13:02:24.133584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fft_spectrum_abs = np.abs(fft_spectrum)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:24.136128Z","iopub.execute_input":"2022-05-13T13:02:24.136658Z","iopub.status.idle":"2022-05-13T13:02:24.156913Z","shell.execute_reply.started":"2022-05-13T13:02:24.136619Z","shell.execute_reply":"2022-05-13T13:02:24.155702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# we plot FFT\nplt.plot(freq,fft_spectrum_abs)\nplt.xlabel(\"frequency, Hz\")\nplt.ylabel(\"Amplitude, units\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:24.158436Z","iopub.execute_input":"2022-05-13T13:02:24.158869Z","iopub.status.idle":"2022-05-13T13:02:24.483558Z","shell.execute_reply.started":"2022-05-13T13:02:24.158834Z","shell.execute_reply":"2022-05-13T13:02:24.482575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Hypothesis:  We may fit a gaussian here and get sigma values. Depending on the bird we may fit more than one gaussian and get more than one sigma value.","metadata":{}},{"cell_type":"code","source":"np.shape(spectrogram)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:24.485042Z","iopub.execute_input":"2022-05-13T13:02:24.485922Z","iopub.status.idle":"2022-05-13T13:02:24.492821Z","shell.execute_reply.started":"2022-05-13T13:02:24.485866Z","shell.execute_reply":"2022-05-13T13:02:24.491783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(frequencies)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:24.493967Z","iopub.execute_input":"2022-05-13T13:02:24.494201Z","iopub.status.idle":"2022-05-13T13:02:24.510992Z","shell.execute_reply.started":"2022-05-13T13:02:24.494173Z","shell.execute_reply":"2022-05-13T13:02:24.510246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(times)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:24.512328Z","iopub.execute_input":"2022-05-13T13:02:24.512624Z","iopub.status.idle":"2022-05-13T13:02:24.526989Z","shell.execute_reply.started":"2022-05-13T13:02:24.512579Z","shell.execute_reply":"2022-05-13T13:02:24.525807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 6))\n\nsns.countplot(df_train['rating'])\nplt.title(\"Distribution of Ratings\", fontsize=20)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:24.530565Z","iopub.execute_input":"2022-05-13T13:02:24.531877Z","iopub.status.idle":"2022-05-13T13:02:24.847309Z","shell.execute_reply.started":"2022-05-13T13:02:24.531812Z","shell.execute_reply":"2022-05-13T13:02:24.846520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 6))\n\nsns.countplot(df_train['primary_label'])\nplt.xticks(rotation=90)\nplt.title(\"Distribution of Primary Labels\", fontsize=20)\n\nplt.show","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:24.848896Z","iopub.execute_input":"2022-05-13T13:02:24.849443Z","iopub.status.idle":"2022-05-13T13:02:27.862708Z","shell.execute_reply.started":"2022-05-13T13:02:24.849392Z","shell.execute_reply":"2022-05-13T13:02:27.861759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 6))\n\nsns.countplot(df_train['scientific_name'])\nplt.xticks(rotation=90)\nplt.title(\"Distribution of scientific names\", fontsize=20)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:27.864395Z","iopub.execute_input":"2022-05-13T13:02:27.864937Z","iopub.status.idle":"2022-05-13T13:02:30.696778Z","shell.execute_reply.started":"2022-05-13T13:02:27.864888Z","shell.execute_reply":"2022-05-13T13:02:30.695721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 6))\n\nsns.countplot(df_train['type'][3000:4000])\nplt.xticks(rotation=90)\nplt.title(\"type\", fontsize=20)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:30.698496Z","iopub.execute_input":"2022-05-13T13:02:30.698915Z","iopub.status.idle":"2022-05-13T13:02:33.983430Z","shell.execute_reply.started":"2022-05-13T13:02:30.698865Z","shell.execute_reply":"2022-05-13T13:02:33.982640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 6))\n\nsns.countplot(df_train['type'])\nplt.title(\"Distribution of Primary types\", fontsize=30)\nplt.tick_params(axis = 'x', which = 'both', bottom = False,top = False, labelbottom = False )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:33.984742Z","iopub.execute_input":"2022-05-13T13:02:33.985494Z","iopub.status.idle":"2022-05-13T13:02:39.949998Z","shell.execute_reply.started":"2022-05-13T13:02:33.985457Z","shell.execute_reply":"2022-05-13T13:02:39.948848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Elimino los elementos con un rating menor que 2.5, \n#lo que elimina tb los elementos que no tienen rating\n#for i in df_train.index:\n    #if df_train['rating'][i]<2.5:\n       # df_train.drop([i],axis = 0, inplace = True)\n","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:39.951257Z","iopub.execute_input":"2022-05-13T13:02:39.951552Z","iopub.status.idle":"2022-05-13T13:02:39.956918Z","shell.execute_reply.started":"2022-05-13T13:02:39.951484Z","shell.execute_reply":"2022-05-13T13:02:39.955299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets check the rating of the scored birds\n\nindex_row_scored = []\nfor i in data:\n    a=i.strip('\"')\n    for j in range(len(df_train[\"primary_label\"])):\n        if df_train[\"primary_label\"][j] == a:\n            index_row_scored.append(j)\nlabel_row_scored = []\nfor i in index_row_scored:\n    label_row_scored.append(df_train[\"rating\"][i])\n    \n\nplt.figure(figsize=(20, 6))\nsns.countplot(label_row_scored)\nplt.title(\"rating of scored birds\", fontsize=20)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:39.958689Z","iopub.execute_input":"2022-05-13T13:02:39.959050Z","iopub.status.idle":"2022-05-13T13:02:42.730013Z","shell.execute_reply.started":"2022-05-13T13:02:39.959004Z","shell.execute_reply":"2022-05-13T13:02:42.728677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#let's check the primary_label of scored birds\n\nlabel_row_scored = []\nfor i in index_row_scored:\n    label_row_scored.append(df_train[\"primary_label\"][i])\n    \n\nplt.figure(figsize=(20, 6))\nsns.countplot(label_row_scored)\nplt.title(\"primary_label scored birds\", fontsize=20)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:42.735621Z","iopub.execute_input":"2022-05-13T13:02:42.736791Z","iopub.status.idle":"2022-05-13T13:02:44.275424Z","shell.execute_reply.started":"2022-05-13T13:02:42.736722Z","shell.execute_reply":"2022-05-13T13:02:44.274185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"List with the rated audios:","metadata":{}},{"cell_type":"code","source":"\nfrom os import listdir\nfrom os.path import isfile, join\nrated_list = []\nfor i in data:\n    onlyfiles = [f for f in listdir(\"../input/birdclef-2022/train_audio/\"+i) if isfile(join(\"../input/birdclef-2022/train_audio/\"+i,f))]\n    rated_list.append(i)\n    rated_list.append(onlyfiles)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:44.277031Z","iopub.execute_input":"2022-05-13T13:02:44.277312Z","iopub.status.idle":"2022-05-13T13:02:44.948749Z","shell.execute_reply.started":"2022-05-13T13:02:44.277281Z","shell.execute_reply":"2022-05-13T13:02:44.947516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Distribution of time of each scored audio\n#Times goes from 0 to max(time_row_scored)=1414 sec.\ncounter = 0\ntime_row_scored = []\nfor i in index_row_scored:\n    counter = counter +1\n    if df_train[\"time\"][i][0] not in [\"?\",\"x\"] and df_train[\"time\"][i][1] not in [\":\"]:\n        time = int(df_train[\"time\"][i][0])*10*60 + int(df_train[\"time\"][i][1])*60 + int(df_train[\"time\"][i][3])*10 +int(df_train[\"time\"][i][4])\n        time_row_scored.append(time)\n    elif df_train[\"time\"][i][0] not in [\"?\",\"x\"]:\n        time = int(df_train[\"time\"][i][0])*60 + int(df_train[\"time\"][i][2])*10 +int(df_train[\"time\"][i][3])\n        time_row_scored.append(time)\n        \n    \nplt.figure(figsize=(20, 6))\nsns.countplot(time_row_scored)\nplt.title(\"time scored birds, max = 1414 sec.\", fontsize=20)\nplt.tick_params(axis = 'x', which = 'both', bottom = False,top = False, labelbottom = False )\nplt.xticks(rotation=90)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:44.950632Z","iopub.execute_input":"2022-05-13T13:02:44.951674Z","iopub.status.idle":"2022-05-13T13:02:46.966225Z","shell.execute_reply.started":"2022-05-13T13:02:44.951615Z","shell.execute_reply":"2022-05-13T13:02:46.965241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Let's calculate how many seconds of audio we have for each scored bird if possible and \n#let save it in a dictionary\n\ncounter = 0\ntime_row_scored = []\nfor i in index_row_scored:\n    counter = counter +1\n    if df_train[\"time\"][i][0] not in [\"?\",\"x\"] and df_train[\"time\"][i][1] not in [\":\"]:\n        time = int(df_train[\"time\"][i][0])*10*60 + int(df_train[\"time\"][i][1])*60 + int(df_train[\"time\"][i][3])*10 +int(df_train[\"time\"][i][4])\n        time_row_scored.append(time)\n    elif df_train[\"time\"][i][0] not in [\"?\",\"x\"]:\n        time = int(df_train[\"time\"][i][0])*60 + int(df_train[\"time\"][i][2])*10 +int(df_train[\"time\"][i][3])\n        time_row_scored.append(time)\n    else:\n        time_row_scored.append(['Undefined'])\n\n\n    ","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:46.967613Z","iopub.execute_input":"2022-05-13T13:02:46.967881Z","iopub.status.idle":"2022-05-13T13:02:47.042253Z","shell.execute_reply.started":"2022-05-13T13:02:46.967848Z","shell.execute_reply":"2022-05-13T13:02:47.040856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from collections import defaultdict\nlabel_row_scored.sort()\noutputlist = defaultdict(list)\nfor A, B in zip(label_row_scored, time_row_scored):\n    outputlist[A].append(B)\ntime_scored_sum = []\nfor i in data:\n    try:\n        suma = sum(outputlist[i])\n        time_scored_sum.append(suma)\n    except:\n        time_scored_sum.append([\"?\"])","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:47.043701Z","iopub.execute_input":"2022-05-13T13:02:47.043994Z","iopub.status.idle":"2022-05-13T13:02:47.053902Z","shell.execute_reply.started":"2022-05-13T13:02:47.043958Z","shell.execute_reply":"2022-05-13T13:02:47.052651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dict_species_time = dict(zip(data,time_scored_sum))","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:47.055463Z","iopub.execute_input":"2022-05-13T13:02:47.056199Z","iopub.status.idle":"2022-05-13T13:02:47.071804Z","shell.execute_reply.started":"2022-05-13T13:02:47.056144Z","shell.execute_reply":"2022-05-13T13:02:47.070611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dict_species_time","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:47.073319Z","iopub.execute_input":"2022-05-13T13:02:47.074056Z","iopub.status.idle":"2022-05-13T13:02:47.089411Z","shell.execute_reply.started":"2022-05-13T13:02:47.074017Z","shell.execute_reply":"2022-05-13T13:02:47.088616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# As there are not so many scored birds, it can be interesting to get spectrograms of \n#high quality audios, and listen those audios while we check the frequencies, intensities, etc\n#df_high = df_train\n#for i in df_high.index:\n    #if df_high['rating'][i]<3.5:\n        #df_high.drop([i],axis = 0, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:47.091279Z","iopub.execute_input":"2022-05-13T13:02:47.092119Z","iopub.status.idle":"2022-05-13T13:02:47.101404Z","shell.execute_reply.started":"2022-05-13T13:02:47.092065Z","shell.execute_reply":"2022-05-13T13:02:47.100625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#for j in data:\n   # for i in df_high.index:\n        #if df_high['primary_label'][i]==j:\n            #print(df_high['filename'][i])\n#label_row_scored","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:47.102837Z","iopub.execute_input":"2022-05-13T13:02:47.103272Z","iopub.status.idle":"2022-05-13T13:02:47.115099Z","shell.execute_reply.started":"2022-05-13T13:02:47.103222Z","shell.execute_reply":"2022-05-13T13:02:47.114424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#we can try to find how many audios we have of each scored species and check the rating of\n# them, for crehon for example, there are only 2 audios and with low quality.\nscoredbird_rating = []\nfor i in index_row_scored:\n    try:\n        scoredbird_rating.append(str(df_train['primary_label'][i])+ \" \" +str(df_train['rating'][i]))\n    except:\n        print('error')\nscoredbird_rating\nplt.figure(figsize=(20, 6))\nsns.countplot(scoredbird_rating)\nplt.xticks(rotation=90)\nplt.title(\"rating of scored birds\", fontsize=20)\n\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:47.116203Z","iopub.execute_input":"2022-05-13T13:02:47.116892Z","iopub.status.idle":"2022-05-13T13:02:49.399833Z","shell.execute_reply.started":"2022-05-13T13:02:47.116837Z","shell.execute_reply":"2022-05-13T13:02:49.398876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"we can see that for crahon the best rating is 2.5, for puaioh 3.5. there are many species with not so many audios, for example omao, puaioh, crahon...","metadata":{}},{"cell_type":"code","source":"# with scorebird_rating.index('bird rating') we can localize the index for the best rating of each bird\nscoredbird_rating.index('akiapo 5.0')","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:49.401501Z","iopub.execute_input":"2022-05-13T13:02:49.402063Z","iopub.status.idle":"2022-05-13T13:02:49.409453Z","shell.execute_reply.started":"2022-05-13T13:02:49.402013Z","shell.execute_reply":"2022-05-13T13:02:49.408587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#to find them in the dataset we write the indexes on index_row_scored\nindex_row_scored[10]","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:49.410736Z","iopub.execute_input":"2022-05-13T13:02:49.411048Z","iopub.status.idle":"2022-05-13T13:02:49.424877Z","shell.execute_reply.started":"2022-05-13T13:02:49.411003Z","shell.execute_reply":"2022-05-13T13:02:49.423945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(str(df_train['primary_label'][42]) + ' ' + str(df_train['rating'][42]))","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:49.426371Z","iopub.execute_input":"2022-05-13T13:02:49.427085Z","iopub.status.idle":"2022-05-13T13:02:49.438883Z","shell.execute_reply.started":"2022-05-13T13:02:49.427027Z","shell.execute_reply":"2022-05-13T13:02:49.438064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(str(df_train['filename'][42]))","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:49.440247Z","iopub.execute_input":"2022-05-13T13:02:49.441113Z","iopub.status.idle":"2022-05-13T13:02:49.454714Z","shell.execute_reply.started":"2022-05-13T13:02:49.441071Z","shell.execute_reply":"2022-05-13T13:02:49.453418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#the list with the indexes like line [80] is\n\nindexes = [10,17,30,78,88,97,105,111,138,152,160,163,175,501,525,603,611,625,629,1129,1205]","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:49.456850Z","iopub.execute_input":"2022-05-13T13:02:49.457575Z","iopub.status.idle":"2022-05-13T13:02:49.468143Z","shell.execute_reply.started":"2022-05-13T13:02:49.457498Z","shell.execute_reply":"2022-05-13T13:02:49.467390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#let's get our data from the dataframe\nindexes_high_rate = []\nfor i in indexes:\n    indexes_high_rate.append(index_row_scored[i])\n\nprint(df_train['primary_label'][indexes_high_rate], df_train['rating'][indexes_high_rate])\n\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:49.470109Z","iopub.execute_input":"2022-05-13T13:02:49.470821Z","iopub.status.idle":"2022-05-13T13:02:49.488769Z","shell.execute_reply.started":"2022-05-13T13:02:49.470770Z","shell.execute_reply":"2022-05-13T13:02:49.487519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Let's loop our first code with the random audio but for our list of rated birds\nfor i in indexes_high_rate:\n    src = \"../input/birdclef-2022/train_audio/\" + str(df_train['filename'][i])\n    dst = \"/kaggle/working/\" + str(df_train['primary_label'][i]) + \".wav\"\n\n# convert ogg to wav\n    sound = AudioSegment.from_ogg(src)\n    sound.export(dst, format=\"wav\")\n    \n    plt.rcParams[\"figure.figsize\"] = [7.50, 3.50]\n    plt.rcParams[\"figure.autolayout\"] = True\n    input_data = read(dst)\n    audio = input_data[1]\n    print(audio)\n    plt.plot(audio)\n    plt.ylabel(\"Amplitude\"+' ' +str(df_train['primary_label'][i]) + ' ' + str(df_train['rating'][i]))\n    plt.xlabel(\"Time\")\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:02:49.490589Z","iopub.execute_input":"2022-05-13T13:02:49.490938Z","iopub.status.idle":"2022-05-13T13:03:17.657709Z","shell.execute_reply.started":"2022-05-13T13:02:49.490895Z","shell.execute_reply":"2022-05-13T13:03:17.656544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#in some cases we have multiple signal. thats because we have stereo and mono audios.","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:03:17.659829Z","iopub.execute_input":"2022-05-13T13:03:17.660084Z","iopub.status.idle":"2022-05-13T13:03:17.665327Z","shell.execute_reply.started":"2022-05-13T13:03:17.660056Z","shell.execute_reply":"2022-05-13T13:03:17.664342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Let's start MFCC for one of the audios and then apply it to everyone in order to get \n#features of all the audios\n\ndata_, sample_rate_ = librosa.load('/kaggle/working/XC395771.wav')\nmfccs = librosa.feature.mfcc(y=data_, sr=sample_rate, n_mfcc=40)\nprint(mfccs.shape)\nprint(mfccs)\n","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:03:17.666992Z","iopub.execute_input":"2022-05-13T13:03:17.667462Z","iopub.status.idle":"2022-05-13T13:03:18.563850Z","shell.execute_reply.started":"2022-05-13T13:03:17.667425Z","shell.execute_reply":"2022-05-13T13:03:18.562396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Let's do the same for all the audios\ndef features_extractor(file):\n    #load the file (audio)\n    audio, sample_rate = librosa.load(file_name, res_type='kaiser_fast') \n    #we extract mfcc\n    mfccs_features = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)\n    #in order to find out scaled feature we do mean of transpose of value\n    mfccs_scaled_features = np.mean(mfccs_features.T,axis=0)\n    return mfccs_scaled_features\n","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:03:18.566143Z","iopub.execute_input":"2022-05-13T13:03:18.566652Z","iopub.status.idle":"2022-05-13T13:03:18.576477Z","shell.execute_reply.started":"2022-05-13T13:03:18.566599Z","shell.execute_reply":"2022-05-13T13:03:18.575211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Next code works but I will leave it as comment because it takes like 10 hours to get \n# all the features for each audio.\n##from tqdm import tqdm\n# Now we iterate through every audio file and extract features \n# using Mel-Frequency Cepstral Coefficients\n##extracted_features=[]\n##for index_num,row in tqdm(df_train.iterrows()):\n    ##file_name = os.path.join(os.path.abspath(\"../input/birdclef-2022/train_audio/\"),str(row[\"filename\"]))\n    ##final_class_labels=row[\"primary_label\"]\n    ##data=features_extractor(file_name)\n    ##extracted_features.append([data,final_class_labels])","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:03:18.578947Z","iopub.execute_input":"2022-05-13T13:03:18.579794Z","iopub.status.idle":"2022-05-13T13:03:18.592926Z","shell.execute_reply.started":"2022-05-13T13:03:18.579731Z","shell.execute_reply":"2022-05-13T13:03:18.591793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#We will use a dataframe with the scored birds just to see that everything works fine \n#without spending so much time\ndf_scored = df_train.copy()\nfor i in df_train.index:\n    #if df_train['rating'][i]<2.5:\n        #df_train.drop([i],axis = 0, inplace = True)\n    if i not in index_row_scored:\n        df_scored.drop([i],axis = 0, inplace = True)\ndf_scored","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:03:18.598316Z","iopub.execute_input":"2022-05-13T13:03:18.603870Z","iopub.status.idle":"2022-05-13T13:03:38.966151Z","shell.execute_reply.started":"2022-05-13T13:03:18.603774Z","shell.execute_reply":"2022-05-13T13:03:38.965143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Let's see if we get more accuracy when we drop elements with less than 2.5 rating\n#for i in df_scored.index:\n   # if df_scored['rating'][i]<2.5:\n        #df_scored.drop([i],axis = 0, inplace = True)","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2022-05-13T13:03:38.967413Z","iopub.execute_input":"2022-05-13T13:03:38.967713Z","iopub.status.idle":"2022-05-13T13:03:38.973044Z","shell.execute_reply.started":"2022-05-13T13:03:38.967677Z","shell.execute_reply":"2022-05-13T13:03:38.971904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#We will use a method that stract features from the audios\nfrom tqdm import tqdm\n# Now we iterate through every audio file and extract features \n# using Mel-Frequency Cepstral Coefficients\nextracted_features=[]\nfor index_num,row in tqdm(df_scored.iterrows()):\n    file_name = os.path.join(os.path.abspath(\"../input/birdclef-2022/train_audio/\"),str(row[\"filename\"]))\n    final_class_labels=row[\"primary_label\"]\n    data=features_extractor(file_name)\n    extracted_features.append([data,final_class_labels])","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:03:38.974494Z","iopub.execute_input":"2022-05-13T13:03:38.974815Z","iopub.status.idle":"2022-05-13T13:17:09.231649Z","shell.execute_reply.started":"2022-05-13T13:03:38.974780Z","shell.execute_reply":"2022-05-13T13:17:09.230512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(extracted_features)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:09.237921Z","iopub.execute_input":"2022-05-13T13:17:09.239124Z","iopub.status.idle":"2022-05-13T13:17:09.252844Z","shell.execute_reply.started":"2022-05-13T13:17:09.239058Z","shell.execute_reply":"2022-05-13T13:17:09.251463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"extracted_features_df=pd.DataFrame(extracted_features,columns=['feature','primary_label'])\nextracted_features_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:09.254777Z","iopub.execute_input":"2022-05-13T13:17:09.255317Z","iopub.status.idle":"2022-05-13T13:17:09.285659Z","shell.execute_reply.started":"2022-05-13T13:17:09.255258Z","shell.execute_reply":"2022-05-13T13:17:09.284653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Now we split the data into train and test\nfrom imblearn.over_sampling import RandomOverSampler #we improved accuracy with this!\noversample = RandomOverSampler(sampling_strategy='minority')\n# Split the dataset into independent and dependent dataset\nX=np.array(extracted_features_df['feature'].tolist())\ny=np.array(extracted_features_df['primary_label'].tolist())\n# Label Encoding -> Label Encoder\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.preprocessing import LabelEncoder\nlabelencoder=LabelEncoder()\ny=to_categorical(labelencoder.fit_transform(y))\n### Train Test Split\nfrom sklearn.model_selection import train_test_split\n#X,y = oversample.fit_resample(X, y)\nX_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:09.287289Z","iopub.execute_input":"2022-05-13T13:17:09.287918Z","iopub.status.idle":"2022-05-13T13:17:09.306879Z","shell.execute_reply.started":"2022-05-13T13:17:09.287865Z","shell.execute_reply":"2022-05-13T13:17:09.305477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from imblearn.over_sampling import RandomOverSampler\noversample = RandomOverSampler(sampling_strategy='minority')","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:09.309407Z","iopub.execute_input":"2022-05-13T13:17:09.310232Z","iopub.status.idle":"2022-05-13T13:17:09.321035Z","shell.execute_reply.started":"2022-05-13T13:17:09.310177Z","shell.execute_reply":"2022-05-13T13:17:09.319901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense,Dropout,Activation,Flatten\nfrom tensorflow.keras.optimizers import Adam\nfrom sklearn import metrics\n# No of classes\nnum_labels=y.shape[1]","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:09.323226Z","iopub.execute_input":"2022-05-13T13:17:09.324036Z","iopub.status.idle":"2022-05-13T13:17:09.337036Z","shell.execute_reply.started":"2022-05-13T13:17:09.323973Z","shell.execute_reply":"2022-05-13T13:17:09.336046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#import numpy as np\n#import matplotlib.pyplot as plt\n\n#from sklearn import svm, datasets\n#from sklearn.model_selection import train_test_split\n#from sklearn.metrics import ConfusionMatrixDisplay\n\n#classifier = svm.SVC(kernel=\"linear\", C=0.01).fit(X_train, y_train)\n\n#np.set_printoptions(precision=2)\n\n# Plot non-normalized confusion matrix\n#titles_options = [\n    #(\"Confusion matrix, without normalization\", None),\n    #(\"Normalized confusion matrix\", \"true\"),\n#]\n\n#for title, normalize in titles_options:\n   # disp = ConfusionMatrixDisplay.from_estimator(\n        #classifier,\n       # X_test,\n        #y_test,\n        #cmap=plt.cm.Blues,\n        #normalize=normalize,\n    #)\n    #disp.ax_.set_title(title)\n\n    #print(title)\n    #print(disp.confusion_matrix)\n\n#plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:09.338773Z","iopub.execute_input":"2022-05-13T13:17:09.339285Z","iopub.status.idle":"2022-05-13T13:17:09.352827Z","shell.execute_reply.started":"2022-05-13T13:17:09.339236Z","shell.execute_reply":"2022-05-13T13:17:09.351706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel=Sequential()\n###first layer\nmodel.add(Dense(100,input_shape=(40,)))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\n###second layer\nmodel.add(Dense(200))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\n###third layer\nmodel.add(Dense(100))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\n###final layer\nmodel.add(Dense(num_labels))\nmodel.add(Activation('softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:09.354289Z","iopub.execute_input":"2022-05-13T13:17:09.354794Z","iopub.status.idle":"2022-05-13T13:17:09.440440Z","shell.execute_reply.started":"2022-05-13T13:17:09.354753Z","shell.execute_reply":"2022-05-13T13:17:09.439659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',metrics=['accuracy'],optimizer='adam')","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:09.441623Z","iopub.execute_input":"2022-05-13T13:17:09.441998Z","iopub.status.idle":"2022-05-13T13:17:09.452845Z","shell.execute_reply.started":"2022-05-13T13:17:09.441968Z","shell.execute_reply":"2022-05-13T13:17:09.452038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Trianing my model\nfrom tensorflow.keras.callbacks import ModelCheckpoint\nfrom datetime import datetime \nnum_epochs = 200\nnum_batch_size = 32\ncheckpointer = ModelCheckpoint(filepath='./audio_classification.hdf5', \n                               verbose=1, save_best_only=True)\nstart = datetime.now()\nmodel.fit(X_train, y_train, batch_size=num_batch_size, epochs=num_epochs, validation_data=(X_test, y_test), callbacks=[checkpointer], verbose=1)\nduration = datetime.now() - start\nprint(\"Training completed in time: \", duration)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:09.454269Z","iopub.execute_input":"2022-05-13T13:17:09.454568Z","iopub.status.idle":"2022-05-13T13:17:49.734165Z","shell.execute_reply.started":"2022-05-13T13:17:09.454512Z","shell.execute_reply":"2022-05-13T13:17:49.733414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.predict(X_test)[0]","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:49.735733Z","iopub.execute_input":"2022-05-13T13:17:49.736576Z","iopub.status.idle":"2022-05-13T13:17:49.907327Z","shell.execute_reply.started":"2022-05-13T13:17:49.736496Z","shell.execute_reply":"2022-05-13T13:17:49.906279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"To fully understand what we are doing and change our code propperly we will make a little explanation","metadata":{}},{"cell_type":"code","source":"y = np.array(extracted_features_df['primary_label'].tolist())","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:49.908703Z","iopub.execute_input":"2022-05-13T13:17:49.909006Z","iopub.status.idle":"2022-05-13T13:17:49.915450Z","shell.execute_reply.started":"2022-05-13T13:17:49.908972Z","shell.execute_reply":"2022-05-13T13:17:49.914302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:49.916731Z","iopub.execute_input":"2022-05-13T13:17:49.917036Z","iopub.status.idle":"2022-05-13T13:17:49.935140Z","shell.execute_reply.started":"2022-05-13T13:17:49.917001Z","shell.execute_reply":"2022-05-13T13:17:49.934045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"we our dataset getting x and y being x the feature column and y the prymary label column","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"labelencoder.fit_transform(y)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:49.936648Z","iopub.execute_input":"2022-05-13T13:17:49.936931Z","iopub.status.idle":"2022-05-13T13:17:49.951417Z","shell.execute_reply.started":"2022-05-13T13:17:49.936899Z","shell.execute_reply":"2022-05-13T13:17:49.950348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"we trnasform into an array with the  names as numbers","metadata":{}},{"cell_type":"code","source":"y = to_categorical(labelencoder.fit_transform(y))","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:49.953345Z","iopub.execute_input":"2022-05-13T13:17:49.953905Z","iopub.status.idle":"2022-05-13T13:17:49.967378Z","shell.execute_reply.started":"2022-05-13T13:17:49.953849Z","shell.execute_reply":"2022-05-13T13:17:49.966604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:49.968796Z","iopub.execute_input":"2022-05-13T13:17:49.969056Z","iopub.status.idle":"2022-05-13T13:17:49.985197Z","shell.execute_reply.started":"2022-05-13T13:17:49.969023Z","shell.execute_reply":"2022-05-13T13:17:49.984451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"now our y is an array of arrais. In first array we see, that the  first element is 1, that means the first kind (akiapo), the second array is the same because for the next audio we have the same bird(akiapo). so this is refered to the audios and the kind of bird we have","metadata":{}},{"cell_type":"code","source":"X = np.array(extracted_features_df['feature'].tolist())","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:49.986191Z","iopub.execute_input":"2022-05-13T13:17:49.986442Z","iopub.status.idle":"2022-05-13T13:17:49.999474Z","shell.execute_reply.started":"2022-05-13T13:17:49.986409Z","shell.execute_reply":"2022-05-13T13:17:49.998502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:50.000493Z","iopub.execute_input":"2022-05-13T13:17:50.000797Z","iopub.status.idle":"2022-05-13T13:17:50.017371Z","shell.execute_reply.started":"2022-05-13T13:17:50.000762Z","shell.execute_reply":"2022-05-13T13:17:50.016576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For X we have the list of audios with the features. first string we see is associated to the first audios and the number with commmas are the features of such audio","metadata":{}},{"cell_type":"code","source":"#X,y = oversample.fit_resample(X,y)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:50.018546Z","iopub.execute_input":"2022-05-13T13:17:50.018831Z","iopub.status.idle":"2022-05-13T13:17:50.030927Z","shell.execute_reply.started":"2022-05-13T13:17:50.018795Z","shell.execute_reply":"2022-05-13T13:17:50.029851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:50.032423Z","iopub.execute_input":"2022-05-13T13:17:50.032830Z","iopub.status.idle":"2022-05-13T13:17:50.049610Z","shell.execute_reply.started":"2022-05-13T13:17:50.032780Z","shell.execute_reply":"2022-05-13T13:17:50.048253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"we have increase the number of samples in order to change accuracy","metadata":{}},{"cell_type":"code","source":"X_test.shape\nX_train.shape\nX.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:50.051518Z","iopub.execute_input":"2022-05-13T13:17:50.051921Z","iopub.status.idle":"2022-05-13T13:17:50.065981Z","shell.execute_reply.started":"2022-05-13T13:17:50.051871Z","shell.execute_reply":"2022-05-13T13:17:50.064875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We got 20 per cent of the samples for X_test and 80 per cent for X_train . the same for y","metadata":{}},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:50.067495Z","iopub.execute_input":"2022-05-13T13:17:50.068356Z","iopub.status.idle":"2022-05-13T13:17:50.081409Z","shell.execute_reply.started":"2022-05-13T13:17:50.068302Z","shell.execute_reply":"2022-05-13T13:17:50.080483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:50.082944Z","iopub.execute_input":"2022-05-13T13:17:50.083350Z","iopub.status.idle":"2022-05-13T13:17:50.097968Z","shell.execute_reply.started":"2022-05-13T13:17:50.083299Z","shell.execute_reply":"2022-05-13T13:17:50.097054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Let's check the accuracy!\ntest_accuracy=model.evaluate(X_test,y_test,verbose=0)\nprint(test_accuracy[1])","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:50.099442Z","iopub.execute_input":"2022-05-13T13:17:50.100242Z","iopub.status.idle":"2022-05-13T13:17:50.195473Z","shell.execute_reply.started":"2022-05-13T13:17:50.100188Z","shell.execute_reply":"2022-05-13T13:17:50.194146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.conf = 0.25","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:50.197043Z","iopub.execute_input":"2022-05-13T13:17:50.197433Z","iopub.status.idle":"2022-05-13T13:17:50.203094Z","shell.execute_reply.started":"2022-05-13T13:17:50.197395Z","shell.execute_reply":"2022-05-13T13:17:50.202117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_x = model.predict(X_test)\nclasses_x = np.argmax(predict_x, axis = 1)\npredict_x[1]","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:50.204726Z","iopub.execute_input":"2022-05-13T13:17:50.205223Z","iopub.status.idle":"2022-05-13T13:17:50.295793Z","shell.execute_reply.started":"2022-05-13T13:17:50.205185Z","shell.execute_reply":"2022-05-13T13:17:50.294725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We have improve from the accuracy from 0.43 to 0.58 just using RandomOverSampler in categorical cross entropy loss :D. I tried others like SMOTE but I got errors because there is a class with just 2 samples and n_samples must be > n_labels with those algorithms. We get accuracy of 0.53 with poisson loss. 0.54 for KLDivergence loss. 0.56 for binary cross entropy loss. 0.65 for MeanSquaredError. 0.66 for MeanAbsoluteError. 0.65 for MeanAbsolutePercentageError. 0.62 for MeanSquaredLogarithmicError. 0.65 for huber. 0.65 for LogCosh. 0.66 for Hinge. 0.66 for SquaredHinge. 0.63 for CategoricalHinge.\nWe have improved accuracy again :D. ","metadata":{}},{"cell_type":"markdown","source":"It looks like the accuracy depends a lot with tne random oversample, so each time we train the neural network we will get different accuracy for the same loss. It is important to find the best oversample and save it.","metadata":{}},{"cell_type":"code","source":"#Let's see how to submit our results\n#filename = \n#audio, sample_rate = librosa.load(filename, res_type = 'kaiser_fast')\n#mfcc_features = librosa.load(y = audio, sr = sample_rate,n_mfcc = 40)\n#mfccs_scaled_features = np.mean(mfccs_features.T,axis = 0)\n\n#mfcc_scaled_features = mfcc","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:50.297268Z","iopub.execute_input":"2022-05-13T13:17:50.297526Z","iopub.status.idle":"2022-05-13T13:17:50.301756Z","shell.execute_reply.started":"2022-05-13T13:17:50.297497Z","shell.execute_reply":"2022-05-13T13:17:50.300634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_audio_dir = '../input/birdclef-2022/test_soundscapes/'\nfile_list = [f.split('.')[0] for f in sorted(os.listdir(test_audio_dir))]","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:50.303085Z","iopub.execute_input":"2022-05-13T13:17:50.303364Z","iopub.status.idle":"2022-05-13T13:17:50.321257Z","shell.execute_reply.started":"2022-05-13T13:17:50.303330Z","shell.execute_reply":"2022-05-13T13:17:50.320404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Number of test soundscapes:', len(file_list))","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:50.322572Z","iopub.execute_input":"2022-05-13T13:17:50.323029Z","iopub.status.idle":"2022-05-13T13:17:50.339706Z","shell.execute_reply.started":"2022-05-13T13:17:50.322990Z","shell.execute_reply":"2022-05-13T13:17:50.338766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('../input/birdclef-2022/scored_birds.json') as sbfile:\n    scored_birds = json.load(sbfile)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:50.341162Z","iopub.execute_input":"2022-05-13T13:17:50.341943Z","iopub.status.idle":"2022-05-13T13:17:50.355150Z","shell.execute_reply.started":"2022-05-13T13:17:50.341889Z","shell.execute_reply":"2022-05-13T13:17:50.354035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = {'row_id': [], 'target': []}","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:50.356852Z","iopub.execute_input":"2022-05-13T13:17:50.357399Z","iopub.status.idle":"2022-05-13T13:17:50.368255Z","shell.execute_reply.started":"2022-05-13T13:17:50.357337Z","shell.execute_reply":"2022-05-13T13:17:50.366986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Process audio files and make predictions\nfor afile in file_list:\n    \n    # Complete file path\n    path = test_audio_dir + afile + '.ogg'\n    \n    # Open file with librosa and split signal into 5-second chunks\n    # sig, rate = librosa.load(path)\n    # ...\n    \n    # Let's assume we have a list of 12 audio chunks (1min / 5s == 12 segments)\n    chunks = [[] for i in range(12)]","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:50.369516Z","iopub.execute_input":"2022-05-13T13:17:50.370504Z","iopub.status.idle":"2022-05-13T13:17:50.385891Z","shell.execute_reply.started":"2022-05-13T13:17:50.370459Z","shell.execute_reply":"2022-05-13T13:17:50.385114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"chunks","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:50.387155Z","iopub.execute_input":"2022-05-13T13:17:50.387866Z","iopub.status.idle":"2022-05-13T13:17:50.403207Z","shell.execute_reply.started":"2022-05-13T13:17:50.387828Z","shell.execute_reply":"2022-05-13T13:17:50.402064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# to check our prediction, those are the strings we need to compare:\na = 0\nfor i in range(len(y_test)):\n    if str(y_test[i])==str(predict_x[i]).replace(\".\", \"\"):\n        a=a+1\nprint(a)\nprint(a/len(y_test))","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:50.404311Z","iopub.execute_input":"2022-05-13T13:17:50.404881Z","iopub.status.idle":"2022-05-13T13:17:50.569856Z","shell.execute_reply.started":"2022-05-13T13:17:50.404841Z","shell.execute_reply":"2022-05-13T13:17:50.568829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape\npredict_x.shape\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.metrics import f1_score\n#score = f1_score(str(y_test[1]),str(predict_x[1]).replace(\".\", \"\"), average = None)\n#fil_acc_orig = accuracy_score(X_test,predict_x)\n\nX_test\n\ny\n\n","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:50.571088Z","iopub.execute_input":"2022-05-13T13:17:50.571394Z","iopub.status.idle":"2022-05-13T13:17:50.579959Z","shell.execute_reply.started":"2022-05-13T13:17:50.571358Z","shell.execute_reply":"2022-05-13T13:17:50.578976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's prepare the test file to create the submision csv","metadata":{}},{"cell_type":"code","source":"# we will divide the test audio in 12 audios of 5 seconds.\n\nfrom pydub import AudioSegment\nfrom pydub.utils import make_chunks \nsound = AudioSegment.from_file(\"../input/birdclef-2022/test_soundscapes/soundscape_453028782.ogg\")\n\nchunk_length_ms = 5000 # pydub calculates in millisec \nchunks = make_chunks(sound,chunk_length_ms) #Make chunks of one sec \nfor i, chunk in enumerate(chunks): \n    chunk_name = \"{0}.ogg\".format(i) \n    print (\"exporting\", chunk_name) \n    chunk.export(chunk_name, format=\"ogg\") \n","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:50.581579Z","iopub.execute_input":"2022-05-13T13:17:50.581895Z","iopub.status.idle":"2022-05-13T13:17:52.686911Z","shell.execute_reply.started":"2022-05-13T13:17:50.581861Z","shell.execute_reply":"2022-05-13T13:17:52.685805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lets stract the features\nimport librosa\nimport numpy as np\n\ndef features_extractor(file):\n    #load the file (audio)\n    audio, sample_rate = librosa.load(file_name, res_type='kaiser_fast') \n    #we extract mfcc\n    mfccs_features = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)\n    #in order to find out scaled feature we do mean of transpose of value\n    mfccs_scaled_features = np.mean(mfccs_features.T,axis=0)\n    return mfccs_scaled_features\n\n\nimport os\nextracted_features_=[]\nfor i in range(0,12):\n    file_name = os.path.join(os.path.abspath(\"./\"),str(i) + \".ogg\")\n    data=features_extractor(file_name)\n    extracted_features_.append([data])","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:52.688927Z","iopub.execute_input":"2022-05-13T13:17:52.689228Z","iopub.status.idle":"2022-05-13T13:17:53.932473Z","shell.execute_reply.started":"2022-05-13T13:17:52.689196Z","shell.execute_reply":"2022-05-13T13:17:53.931441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(extracted_features_)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:53.934324Z","iopub.execute_input":"2022-05-13T13:17:53.934955Z","iopub.status.idle":"2022-05-13T13:17:53.943202Z","shell.execute_reply.started":"2022-05-13T13:17:53.934893Z","shell.execute_reply":"2022-05-13T13:17:53.942208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nextracted_features_df_=pd.DataFrame(extracted_features_,columns=['feature'])","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:53.945369Z","iopub.execute_input":"2022-05-13T13:17:53.946091Z","iopub.status.idle":"2022-05-13T13:17:53.960286Z","shell.execute_reply.started":"2022-05-13T13:17:53.946036Z","shell.execute_reply":"2022-05-13T13:17:53.959061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_=np.array(extracted_features_df_['feature'].tolist())\nX_.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:53.962571Z","iopub.execute_input":"2022-05-13T13:17:53.963449Z","iopub.status.idle":"2022-05-13T13:17:53.978089Z","shell.execute_reply.started":"2022-05-13T13:17:53.963388Z","shell.execute_reply":"2022-05-13T13:17:53.977022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('../input/birdclef-2022/scored_birds.json') as sbfile:\n    scored_birds = json.load(sbfile)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:53.980514Z","iopub.execute_input":"2022-05-13T13:17:53.981447Z","iopub.status.idle":"2022-05-13T13:17:53.989701Z","shell.execute_reply.started":"2022-05-13T13:17:53.981375Z","shell.execute_reply":"2022-05-13T13:17:53.988605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scored_birds_ = to_categorical(labelencoder.fit_transform(scored_birds))","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:53.995627Z","iopub.execute_input":"2022-05-13T13:17:53.996633Z","iopub.status.idle":"2022-05-13T13:17:54.008079Z","shell.execute_reply.started":"2022-05-13T13:17:53.996556Z","shell.execute_reply":"2022-05-13T13:17:54.007021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scored_birds_","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:54.014308Z","iopub.execute_input":"2022-05-13T13:17:54.016010Z","iopub.status.idle":"2022-05-13T13:17:54.042744Z","shell.execute_reply.started":"2022-05-13T13:17:54.015946Z","shell.execute_reply":"2022-05-13T13:17:54.041510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(scored_birds)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:54.049306Z","iopub.execute_input":"2022-05-13T13:17:54.050020Z","iopub.status.idle":"2022-05-13T13:17:54.056381Z","shell.execute_reply.started":"2022-05-13T13:17:54.049970Z","shell.execute_reply":"2022-05-13T13:17:54.055590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = []\nscored_birds_.tolist()\npredict_x.tolist()\nfor i in range(len(scored_birds_)):\n    for j in range(len(predict_x)):\n        if str(scored_birds_[i]).replace(\".\", \"\") == str(predict_x[j]).replace(\".\", \"\"):\n            score.append(scored_birds[i])\nprint(score)\n","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:54.057443Z","iopub.execute_input":"2022-05-13T13:17:54.058019Z","iopub.status.idle":"2022-05-13T13:17:57.192916Z","shell.execute_reply.started":"2022-05-13T13:17:54.057973Z","shell.execute_reply":"2022-05-13T13:17:57.191859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict__=model.predict(X_)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:57.194195Z","iopub.execute_input":"2022-05-13T13:17:57.194468Z","iopub.status.idle":"2022-05-13T13:17:57.265239Z","shell.execute_reply.started":"2022-05-13T13:17:57.194434Z","shell.execute_reply":"2022-05-13T13:17:57.264520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(predict__)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:50:29.625956Z","iopub.execute_input":"2022-05-13T13:50:29.626509Z","iopub.status.idle":"2022-05-13T13:50:29.633394Z","shell.execute_reply.started":"2022-05-13T13:50:29.626444Z","shell.execute_reply":"2022-05-13T13:50:29.631978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This is where we will store our results\npred = {'row_id': [], 'target': []}\n\n# Process audio files and make predictions\nfor afile in file_list:\n    \n    # Complete file path\n    path = test_audio_dir + afile + '.ogg'\n    \n    # Open file with librosa and split signal into 5-second chunks\n    # sig, rate = librosa.load(path)\n    # ...\n    \n    # Let's assume we have a list of 12 audio chunks (1min / 5s == 12 segments)\n    chunks = [[] for i in range(12)]\n    \n    # Make prediction for each chunk\n    # Each scored bird gets a random value in our case\n    # since we don't actually have a model\n    for i in range(len(chunks)):        \n        chunk_end_time = (i + 1) * 5\n        j=0\n        for bird in scored_birds:\n            \n            # This is our random prediction score for this bird\n            score = predict__[i][j]\n            maximun = max(predict__[i])\n            j=j+1\n            # Assemble the row_id which we need to do for each scored bird\n            row_id = afile + '_' + bird + '_' + str(chunk_end_time)\n            \n            # Put the result into our prediction dict and\n            # apply a \"confidence\" threshold of 0.5\n            pred['row_id'].append(row_id)\n            pred['target'].append(True if score == maximun else False)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:51:05.853372Z","iopub.execute_input":"2022-05-13T13:51:05.853729Z","iopub.status.idle":"2022-05-13T13:51:05.865798Z","shell.execute_reply.started":"2022-05-13T13:51:05.853691Z","shell.execute_reply":"2022-05-13T13:51:05.865132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:57.279276Z","iopub.execute_input":"2022-05-13T13:17:57.279544Z","iopub.status.idle":"2022-05-13T13:17:57.310809Z","shell.execute_reply.started":"2022-05-13T13:17:57.279498Z","shell.execute_reply":"2022-05-13T13:17:57.310071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.metrics import f1_score\n#f1_score(y_true, y_pred, average=None)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:57.312062Z","iopub.execute_input":"2022-05-13T13:17:57.312900Z","iopub.status.idle":"2022-05-13T13:17:57.316294Z","shell.execute_reply.started":"2022-05-13T13:17:57.312845Z","shell.execute_reply":"2022-05-13T13:17:57.315594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Of course it is not a good way to solve the problem, we did our data analisys for something\n# and the code will be of course improved. We need to use data from all the birds, consider\n# the rating, etc\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:57.317627Z","iopub.execute_input":"2022-05-13T13:17:57.318058Z","iopub.status.idle":"2022-05-13T13:17:57.348574Z","shell.execute_reply.started":"2022-05-13T13:17:57.318025Z","shell.execute_reply":"2022-05-13T13:17:57.347510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Let's see how important are the audios with 0.o rating\ncount = 0\nprint(len(df_train))\nfor i in range(len(df_train)):\n    if df_train['rating'][i] == 0.0:\n        count = count +1\n        print(df_train['primary_label'][i],df_train['time'][i])\nprint(count)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:57.350000Z","iopub.execute_input":"2022-05-13T13:17:57.350749Z","iopub.status.idle":"2022-05-13T13:17:57.735039Z","shell.execute_reply.started":"2022-05-13T13:17:57.350705Z","shell.execute_reply":"2022-05-13T13:17:57.733940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Let's check the duration of the unescored audios and see if those are very important factors\n#in our analysis.\ncount = 0\nprint(len(df_scored))\nfor i in index_row_scored:\n    if df_scored['rating'][i] == 0.0:\n        count = count+1\n        print(df_scored['primary_label'][i],df_scored['time'][i])\nprint(count)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:57.737051Z","iopub.execute_input":"2022-05-13T13:17:57.737425Z","iopub.status.idle":"2022-05-13T13:17:57.772176Z","shell.execute_reply.started":"2022-05-13T13:17:57.737375Z","shell.execute_reply":"2022-05-13T13:17:57.771410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count = 0\nprint(len(df_scored))\nfor i in index_row_scored:\n    print(df_scored['primary_label'][i],df_scored['time'][i])\n    count = count+1\n\nprint(count)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:57.773312Z","iopub.execute_input":"2022-05-13T13:17:57.773758Z","iopub.status.idle":"2022-05-13T13:17:58.240903Z","shell.execute_reply.started":"2022-05-13T13:17:57.773701Z","shell.execute_reply":"2022-05-13T13:17:58.238618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The unrated audios doesn't look like very important data, those have average duration and\n# there are not so many of them, and are not present in the birds with too less audios.\n# We will drop them for now.","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:58.245506Z","iopub.execute_input":"2022-05-13T13:17:58.246403Z","iopub.status.idle":"2022-05-13T13:17:58.249867Z","shell.execute_reply.started":"2022-05-13T13:17:58.246364Z","shell.execute_reply":"2022-05-13T13:17:58.249056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\n\ncls = RandomForestClassifier(random_state=0)\ncls.fit(X_train, y_train)\n\nz = cls.predict_proba(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:58.250836Z","iopub.execute_input":"2022-05-13T13:17:58.251061Z","iopub.status.idle":"2022-05-13T13:17:59.787485Z","shell.execute_reply.started":"2022-05-13T13:17:58.251033Z","shell.execute_reply":"2022-05-13T13:17:59.786412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.svm import LinearSVC\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.preprocessing import StandardScaler","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:59.788744Z","iopub.execute_input":"2022-05-13T13:17:59.789157Z","iopub.status.idle":"2022-05-13T13:17:59.794625Z","shell.execute_reply.started":"2022-05-13T13:17:59.789115Z","shell.execute_reply":"2022-05-13T13:17:59.793588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\npredict_prob=model.predict(X_test)\n\npredict_classes=np.argmax(predict_prob,axis=1)\n\npredict_prob\n\nclf = make_pipeline(StandardScaler(), LinearSVC(random_state=0, tol=1e-5))","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:59.795892Z","iopub.execute_input":"2022-05-13T13:17:59.796242Z","iopub.status.idle":"2022-05-13T13:17:59.880564Z","shell.execute_reply.started":"2022-05-13T13:17:59.796199Z","shell.execute_reply":"2022-05-13T13:17:59.879619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_ = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]\ny_train_ = np.asarray(y_train_)\ny_train_.shape\nX_train.shape\nk=np.array(extracted_features_df['primary_label'].tolist())\nk = labelencoder.fit_transform(k)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:59.882065Z","iopub.execute_input":"2022-05-13T13:17:59.882430Z","iopub.status.idle":"2022-05-13T13:17:59.891693Z","shell.execute_reply.started":"2022-05-13T13:17:59.882394Z","shell.execute_reply":"2022-05-13T13:17:59.890520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf.fit(X, k)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:17:59.892997Z","iopub.execute_input":"2022-05-13T13:17:59.893722Z","iopub.status.idle":"2022-05-13T13:18:01.138100Z","shell.execute_reply.started":"2022-05-13T13:17:59.893681Z","shell.execute_reply":"2022-05-13T13:18:01.136927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(clf.named_steps['linearsvc'].coef_)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:18:01.139737Z","iopub.execute_input":"2022-05-13T13:18:01.139974Z","iopub.status.idle":"2022-05-13T13:18:01.159413Z","shell.execute_reply.started":"2022-05-13T13:18:01.139946Z","shell.execute_reply":"2022-05-13T13:18:01.158553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(clf.named_steps['linearsvc'].intercept_)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:18:01.160969Z","iopub.execute_input":"2022-05-13T13:18:01.161425Z","iopub.status.idle":"2022-05-13T13:18:01.167733Z","shell.execute_reply.started":"2022-05-13T13:18:01.161378Z","shell.execute_reply":"2022-05-13T13:18:01.166508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pre = clf.score(X,k)\npre","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:18:01.169279Z","iopub.execute_input":"2022-05-13T13:18:01.169642Z","iopub.status.idle":"2022-05-13T13:18:01.189449Z","shell.execute_reply.started":"2022-05-13T13:18:01.169597Z","shell.execute_reply":"2022-05-13T13:18:01.188390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = clf.decision_function(X)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:18:01.192219Z","iopub.execute_input":"2022-05-13T13:18:01.194249Z","iopub.status.idle":"2022-05-13T13:18:01.202223Z","shell.execute_reply.started":"2022-05-13T13:18:01.194169Z","shell.execute_reply":"2022-05-13T13:18:01.201134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:18:01.204143Z","iopub.execute_input":"2022-05-13T13:18:01.205321Z","iopub.status.idle":"2022-05-13T13:18:01.220214Z","shell.execute_reply.started":"2022-05-13T13:18:01.205267Z","shell.execute_reply":"2022-05-13T13:18:01.219102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(prediction[0])\nmax_ = []\nfor i in range(len(prediction)):\n    print(prediction[i])\n\n    ","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:18:01.222179Z","iopub.execute_input":"2022-05-13T13:18:01.224591Z","iopub.status.idle":"2022-05-13T13:18:01.814598Z","shell.execute_reply.started":"2022-05-13T13:18:01.224478Z","shell.execute_reply":"2022-05-13T13:18:01.811681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = pd.DataFrame(pred, columns = ['row_id', 'target'])\n\n# Quick sanity check\nprint(results.head()) \n    \n# Convert our results to csv\nresults.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-13T13:35:23.955921Z","iopub.execute_input":"2022-05-13T13:35:23.956401Z","iopub.status.idle":"2022-05-13T13:35:23.975342Z","shell.execute_reply.started":"2022-05-13T13:35:23.956364Z","shell.execute_reply":"2022-05-13T13:35:23.973767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}