{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":19596,"databundleVersionId":1292430,"sourceType":"competition"},{"sourceId":1262046,"sourceType":"datasetVersion","datasetId":726424},{"sourceId":1264575,"sourceType":"datasetVersion","datasetId":725893}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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)\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\nimport os\nfor 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","_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-01-11T18:52:40.896628Z","iopub.execute_input":"2024-01-11T18:52:40.897438Z","iopub.status.idle":"2024-01-11T18:52:41.316569Z","shell.execute_reply.started":"2024-01-11T18:52:40.897389Z","shell.execute_reply":"2024-01-11T18:52:41.315599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Import necessary libraries","metadata":{}},{"cell_type":"code","source":"# numpy already imported\n# pandas already imported\nimport matplotlib.pyplot as plt # for visualising data\n%matplotlib inline\nimport seaborn as sns\n\nimport IPython.display as ipd # for playing audio files\nimport soundfile as sf # for reading and writing audio files\nimport audioread # reading and processing audio files\nimport os # file and directory manipulation \nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:52:47.883777Z","iopub.execute_input":"2024-01-11T18:52:47.884154Z","iopub.status.idle":"2024-01-11T18:52:47.891491Z","shell.execute_reply.started":"2024-01-11T18:52:47.884123Z","shell.execute_reply":"2024-01-11T18:52:47.890397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install librosa==0.9.2","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:52:48.610433Z","iopub.execute_input":"2024-01-11T18:52:48.611269Z","iopub.status.idle":"2024-01-11T18:53:00.755903Z","shell.execute_reply.started":"2024-01-11T18:52:48.611236Z","shell.execute_reply":"2024-01-11T18:53:00.754710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import librosa # for audio processing and audio feature extraction","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:00.757947Z","iopub.execute_input":"2024-01-11T18:53:00.758256Z","iopub.status.idle":"2024-01-11T18:53:00.763294Z","shell.execute_reply.started":"2024-01-11T18:53:00.758225Z","shell.execute_reply":"2024-01-11T18:53:00.762406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Reading the train.csv file for Exploratory Data Analysis (EDA) to understand the structure and content of the dataset. Doing this will help in identifying patterns and anomalies(missing value and datatype issues) within the data. This can also highlight the importance of different features in the dataset and can be helpful in drawing important correlations and relationships between data columns.","metadata":{}},{"cell_type":"markdown","source":"> # EXPLORATORY DATA ANALYSIS","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/birdsong-recognition/train.csv\")\ndf","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:00.764505Z","iopub.execute_input":"2024-01-11T18:53:00.764775Z","iopub.status.idle":"2024-01-11T18:53:01.095442Z","shell.execute_reply.started":"2024-01-11T18:53:00.764750Z","shell.execute_reply":"2024-01-11T18:53:01.094495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This shows that dataset has 21375 rows and 35 columns.","metadata":{}},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:01.098274Z","iopub.execute_input":"2024-01-11T18:53:01.098948Z","iopub.status.idle":"2024-01-11T18:53:01.121693Z","shell.execute_reply.started":"2024-01-11T18:53:01.098909Z","shell.execute_reply":"2024-01-11T18:53:01.120807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:01.122834Z","iopub.execute_input":"2024-01-11T18:53:01.123150Z","iopub.status.idle":"2024-01-11T18:53:01.135647Z","shell.execute_reply.started":"2024-01-11T18:53:01.123124Z","shell.execute_reply":"2024-01-11T18:53:01.134745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This shows all the 35 columns in the dataset that we have to look into.","metadata":{}},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:01.136782Z","iopub.execute_input":"2024-01-11T18:53:01.137326Z","iopub.status.idle":"2024-01-11T18:53:01.213916Z","shell.execute_reply.started":"2024-01-11T18:53:01.137297Z","shell.execute_reply":"2024-01-11T18:53:01.212754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here, the dataset info shows that, there are some columns like playback speed, bird_seen etc. where some null values are found. We have to deal with it, further.","metadata":{}},{"cell_type":"code","source":"df['rating'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:01.215453Z","iopub.execute_input":"2024-01-11T18:53:01.216066Z","iopub.status.idle":"2024-01-11T18:53:01.222783Z","shell.execute_reply.started":"2024-01-11T18:53:01.216014Z","shell.execute_reply":"2024-01-11T18:53:01.221814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can see here, rating column has no issue of datatype and missing values. These rating values here may be representing the user experience of the quality of bird sounds recorded or may be representing that high-rated birds are the one that are more searched for. So, this column might be useful further. ","metadata":{}},{"cell_type":"code","source":"df.playback_used.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:01.223975Z","iopub.execute_input":"2024-01-11T18:53:01.224281Z","iopub.status.idle":"2024-01-11T18:53:01.235596Z","shell.execute_reply.started":"2024-01-11T18:53:01.224257Z","shell.execute_reply":"2024-01-11T18:53:01.234709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We see here that column has some nan values. As playback is just denoting whether some audio for the bird call or not, that may not be very useful for our purpose. ","metadata":{}},{"cell_type":"code","source":"df.playback_used.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:01.685265Z","iopub.execute_input":"2024-01-11T18:53:01.685722Z","iopub.status.idle":"2024-01-11T18:53:01.696320Z","shell.execute_reply.started":"2024-01-11T18:53:01.685687Z","shell.execute_reply":"2024-01-11T18:53:01.695276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Suppose, if we want to use the playback column, we need to deal with the the missing values, we can look above the most common value is 'no' so we can go for replacing the missing values with 'no'.","metadata":{}},{"cell_type":"code","source":"df.playback_used.fillna('no',inplace = True)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:02.782473Z","iopub.execute_input":"2024-01-11T18:53:02.783347Z","iopub.status.idle":"2024-01-11T18:53:02.790479Z","shell.execute_reply.started":"2024-01-11T18:53:02.783311Z","shell.execute_reply":"2024-01-11T18:53:02.789512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.playback_used.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:03.497242Z","iopub.execute_input":"2024-01-11T18:53:03.497830Z","iopub.status.idle":"2024-01-11T18:53:03.505269Z","shell.execute_reply.started":"2024-01-11T18:53:03.497796Z","shell.execute_reply":"2024-01-11T18:53:03.504350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.ebird_code.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:03.998917Z","iopub.execute_input":"2024-01-11T18:53:03.999798Z","iopub.status.idle":"2024-01-11T18:53:04.010566Z","shell.execute_reply.started":"2024-01-11T18:53:03.999763Z","shell.execute_reply":"2024-01-11T18:53:04.009619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.ebird_code.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:04.266359Z","iopub.execute_input":"2024-01-11T18:53:04.266678Z","iopub.status.idle":"2024-01-11T18:53:04.275603Z","shell.execute_reply.started":"2024-01-11T18:53:04.266651Z","shell.execute_reply":"2024-01-11T18:53:04.274651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.ebird_code.nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:04.861821Z","iopub.execute_input":"2024-01-11T18:53:04.862453Z","iopub.status.idle":"2024-01-11T18:53:04.870070Z","shell.execute_reply.started":"2024-01-11T18:53:04.862418Z","shell.execute_reply":"2024-01-11T18:53:04.869116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here, we can see that the column ebird_code has no missing values and this basically represents the some sort of code for the species of the bird. So, this column can be very usefule, while we will be classifying the sounds of bird. But as the dtype of the column is object, but we need numerical values for our model, so we can look for label encoding also.","metadata":{}},{"cell_type":"code","source":"df.channels.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:05.973758Z","iopub.execute_input":"2024-01-11T18:53:05.974151Z","iopub.status.idle":"2024-01-11T18:53:05.986584Z","shell.execute_reply.started":"2024-01-11T18:53:05.974117Z","shell.execute_reply":"2024-01-11T18:53:05.985377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.channels.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:06.229624Z","iopub.execute_input":"2024-01-11T18:53:06.229966Z","iopub.status.idle":"2024-01-11T18:53:06.238062Z","shell.execute_reply.started":"2024-01-11T18:53:06.229934Z","shell.execute_reply":"2024-01-11T18:53:06.237121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The channels column is showing here that whether the recorded sound is mono or stereo type, we can see there are no null values. This column is not very useful as we will be using librosa for audio processing which converts the stereo audio type to mono on its own.","metadata":{}},{"cell_type":"code","source":"df.date.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:06.648594Z","iopub.execute_input":"2024-01-11T18:53:06.649573Z","iopub.status.idle":"2024-01-11T18:53:06.661619Z","shell.execute_reply.started":"2024-01-11T18:53:06.649527Z","shell.execute_reply":"2024-01-11T18:53:06.660803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.date.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:06.879027Z","iopub.execute_input":"2024-01-11T18:53:06.879363Z","iopub.status.idle":"2024-01-11T18:53:06.887409Z","shell.execute_reply.started":"2024-01-11T18:53:06.879336Z","shell.execute_reply":"2024-01-11T18:53:06.886543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here, the date column can be of use in context to birds. If we go for extracting the month details from the date, we will be able to know which species of birds appear in which month or season.","metadata":{}},{"cell_type":"code","source":"df[['year', 'month', 'day']] = df['date'].str.split('-', expand=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:07.305487Z","iopub.execute_input":"2024-01-11T18:53:07.305839Z","iopub.status.idle":"2024-01-11T18:53:07.354859Z","shell.execute_reply.started":"2024-01-11T18:53:07.305806Z","shell.execute_reply":"2024-01-11T18:53:07.353904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.month.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:08.022946Z","iopub.execute_input":"2024-01-11T18:53:08.023843Z","iopub.status.idle":"2024-01-11T18:53:08.032984Z","shell.execute_reply.started":"2024-01-11T18:53:08.023808Z","shell.execute_reply":"2024-01-11T18:53:08.031974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here, we find that there is a month value '00'. This obviously has no meaning. Let's first chcek how many times this value has occured in month column.","metadata":{}},{"cell_type":"code","source":"df.month.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:08.595146Z","iopub.execute_input":"2024-01-11T18:53:08.595533Z","iopub.status.idle":"2024-01-11T18:53:08.607199Z","shell.execute_reply.started":"2024-01-11T18:53:08.595502Z","shell.execute_reply":"2024-01-11T18:53:08.606242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"month value '00' has occured the least (just 34 times). So, to correct this, we can try for replacing this value with the month value in which most birds have appeared,i.e, month '05'. ","metadata":{}},{"cell_type":"code","source":"df['month'] = df['month'].replace('00', '05')","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:10.538884Z","iopub.execute_input":"2024-01-11T18:53:10.539271Z","iopub.status.idle":"2024-01-11T18:53:10.550441Z","shell.execute_reply.started":"2024-01-11T18:53:10.539241Z","shell.execute_reply":"2024-01-11T18:53:10.549405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.month.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:11.061411Z","iopub.execute_input":"2024-01-11T18:53:11.062019Z","iopub.status.idle":"2024-01-11T18:53:11.070879Z","shell.execute_reply.started":"2024-01-11T18:53:11.061987Z","shell.execute_reply":"2024-01-11T18:53:11.069865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here, the dtype of month values is object we can convert that to numeric for feeding into model.","metadata":{}},{"cell_type":"code","source":"df = df.drop(['year', 'day'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:17.388388Z","iopub.execute_input":"2024-01-11T18:53:17.388755Z","iopub.status.idle":"2024-01-11T18:53:17.401819Z","shell.execute_reply.started":"2024-01-11T18:53:17.388726Z","shell.execute_reply":"2024-01-11T18:53:17.400791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"year and day values are not very useful so we can also drop these","metadata":{}},{"cell_type":"code","source":"df.pitch.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:18.233263Z","iopub.execute_input":"2024-01-11T18:53:18.233638Z","iopub.status.idle":"2024-01-11T18:53:18.245660Z","shell.execute_reply.started":"2024-01-11T18:53:18.233607Z","shell.execute_reply":"2024-01-11T18:53:18.244743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.pitch.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:18.570952Z","iopub.execute_input":"2024-01-11T18:53:18.571316Z","iopub.status.idle":"2024-01-11T18:53:18.579052Z","shell.execute_reply.started":"2024-01-11T18:53:18.571286Z","shell.execute_reply":"2024-01-11T18:53:18.578170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.pitch.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:18.837008Z","iopub.execute_input":"2024-01-11T18:53:18.837370Z","iopub.status.idle":"2024-01-11T18:53:18.847890Z","shell.execute_reply.started":"2024-01-11T18:53:18.837342Z","shell.execute_reply":"2024-01-11T18:53:18.846980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:19.122436Z","iopub.execute_input":"2024-01-11T18:53:19.122791Z","iopub.status.idle":"2024-01-11T18:53:19.129463Z","shell.execute_reply.started":"2024-01-11T18:53:19.122750Z","shell.execute_reply":"2024-01-11T18:53:19.128487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.speed.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:19.316406Z","iopub.execute_input":"2024-01-11T18:53:19.316745Z","iopub.status.idle":"2024-01-11T18:53:19.327167Z","shell.execute_reply.started":"2024-01-11T18:53:19.316717Z","shell.execute_reply":"2024-01-11T18:53:19.326065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.species.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:19.530490Z","iopub.execute_input":"2024-01-11T18:53:19.530831Z","iopub.status.idle":"2024-01-11T18:53:19.541943Z","shell.execute_reply.started":"2024-01-11T18:53:19.530790Z","shell.execute_reply":"2024-01-11T18:53:19.540998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.number_of_notes.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:19.713248Z","iopub.execute_input":"2024-01-11T18:53:19.713668Z","iopub.status.idle":"2024-01-11T18:53:19.721641Z","shell.execute_reply.started":"2024-01-11T18:53:19.713631Z","shell.execute_reply":"2024-01-11T18:53:19.720765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.title.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:19.995896Z","iopub.execute_input":"2024-01-11T18:53:19.996533Z","iopub.status.idle":"2024-01-11T18:53:20.007108Z","shell.execute_reply.started":"2024-01-11T18:53:19.996501Z","shell.execute_reply":"2024-01-11T18:53:20.006099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.title.nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:20.148417Z","iopub.execute_input":"2024-01-11T18:53:20.148714Z","iopub.status.idle":"2024-01-11T18:53:20.161312Z","shell.execute_reply.started":"2024-01-11T18:53:20.148688Z","shell.execute_reply":"2024-01-11T18:53:20.160460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here the title value is unique for each sound file, it seems like a unique ID for a single bird. ","metadata":{}},{"cell_type":"code","source":"df.secondary_labels.nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:20.553102Z","iopub.execute_input":"2024-01-11T18:53:20.553493Z","iopub.status.idle":"2024-01-11T18:53:20.564512Z","shell.execute_reply.started":"2024-01-11T18:53:20.553459Z","shell.execute_reply":"2024-01-11T18:53:20.563303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.secondary_labels.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:20.739788Z","iopub.execute_input":"2024-01-11T18:53:20.740145Z","iopub.status.idle":"2024-01-11T18:53:20.749421Z","shell.execute_reply.started":"2024-01-11T18:53:20.740116Z","shell.execute_reply":"2024-01-11T18:53:20.748503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Secondary labels can also be used for classification but they do not clear denote the name of the bird or there species, so using ebird_code values and species values are better labels for the classification purpose. As, this does not reveal any new info, we can also go for dropping this column.","metadata":{}},{"cell_type":"code","source":"df.bird_seen.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:21.156039Z","iopub.execute_input":"2024-01-11T18:53:21.156478Z","iopub.status.idle":"2024-01-11T18:53:21.166966Z","shell.execute_reply.started":"2024-01-11T18:53:21.156445Z","shell.execute_reply":"2024-01-11T18:53:21.165999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.bird_seen.fillna('yes', inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:21.368247Z","iopub.execute_input":"2024-01-11T18:53:21.368894Z","iopub.status.idle":"2024-01-11T18:53:21.376701Z","shell.execute_reply.started":"2024-01-11T18:53:21.368849Z","shell.execute_reply":"2024-01-11T18:53:21.375678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.bird_seen.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:21.575113Z","iopub.execute_input":"2024-01-11T18:53:21.575601Z","iopub.status.idle":"2024-01-11T18:53:21.584703Z","shell.execute_reply.started":"2024-01-11T18:53:21.575559Z","shell.execute_reply":"2024-01-11T18:53:21.583509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This column might be representing whether the bird was seen at time of recording their sounds or not","metadata":{}},{"cell_type":"code","source":"df.sci_name.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:22.200921Z","iopub.execute_input":"2024-01-11T18:53:22.201799Z","iopub.status.idle":"2024-01-11T18:53:22.213010Z","shell.execute_reply.started":"2024-01-11T18:53:22.201765Z","shell.execute_reply":"2024-01-11T18:53:22.212000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sci_name.nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:22.397636Z","iopub.execute_input":"2024-01-11T18:53:22.397991Z","iopub.status.idle":"2024-01-11T18:53:22.405736Z","shell.execute_reply.started":"2024-01-11T18:53:22.397961Z","shell.execute_reply":"2024-01-11T18:53:22.404851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sci_name.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:22.654753Z","iopub.execute_input":"2024-01-11T18:53:22.655806Z","iopub.status.idle":"2024-01-11T18:53:22.665226Z","shell.execute_reply.started":"2024-01-11T18:53:22.655764Z","shell.execute_reply":"2024-01-11T18:53:22.664373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This looks similar to species name column but the only differnece is these are not common names rather these are their scientific names.","metadata":{}},{"cell_type":"code","source":"df.location.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:23.073316Z","iopub.execute_input":"2024-01-11T18:53:23.073688Z","iopub.status.idle":"2024-01-11T18:53:23.085624Z","shell.execute_reply.started":"2024-01-11T18:53:23.073656Z","shell.execute_reply":"2024-01-11T18:53:23.084682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.location.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:23.231854Z","iopub.execute_input":"2024-01-11T18:53:23.232596Z","iopub.status.idle":"2024-01-11T18:53:23.242391Z","shell.execute_reply.started":"2024-01-11T18:53:23.232564Z","shell.execute_reply":"2024-01-11T18:53:23.241489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This column shows the exact locations where the bird sounds were observed and recorded, we can keep this column for displaying which was seen where on map.","metadata":{}},{"cell_type":"code","source":"df.latitude","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:23.896223Z","iopub.execute_input":"2024-01-11T18:53:23.896692Z","iopub.status.idle":"2024-01-11T18:53:23.904146Z","shell.execute_reply.started":"2024-01-11T18:53:23.896647Z","shell.execute_reply":"2024-01-11T18:53:23.903191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:24.104096Z","iopub.execute_input":"2024-01-11T18:53:24.104490Z","iopub.status.idle":"2024-01-11T18:53:24.185758Z","shell.execute_reply.started":"2024-01-11T18:53:24.104456Z","shell.execute_reply":"2024-01-11T18:53:24.184682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sampling_rate.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:24.332874Z","iopub.execute_input":"2024-01-11T18:53:24.333560Z","iopub.status.idle":"2024-01-11T18:53:24.341578Z","shell.execute_reply.started":"2024-01-11T18:53:24.333527Z","shell.execute_reply":"2024-01-11T18:53:24.340557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Actually, we do not need this sampling rate values. As we are using the librosa library, it keeps the sampling rate constant(22,050 Hz) by default. If we would have scipy library for audio loading and processing, then this column would have been important because its requires sampling rate to be mentioned explicitly. So, for now we can drop this column.","metadata":{}},{"cell_type":"code","source":"df = df.drop('sampling_rate', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:24.852084Z","iopub.execute_input":"2024-01-11T18:53:24.852457Z","iopub.status.idle":"2024-01-11T18:53:24.864847Z","shell.execute_reply.started":"2024-01-11T18:53:24.852428Z","shell.execute_reply":"2024-01-11T18:53:24.863736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.type.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:24.985464Z","iopub.execute_input":"2024-01-11T18:53:24.986207Z","iopub.status.idle":"2024-01-11T18:53:24.994170Z","shell.execute_reply.started":"2024-01-11T18:53:24.986172Z","shell.execute_reply":"2024-01-11T18:53:24.993186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.type.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:25.196885Z","iopub.execute_input":"2024-01-11T18:53:25.197584Z","iopub.status.idle":"2024-01-11T18:53:25.210265Z","shell.execute_reply.started":"2024-01-11T18:53:25.197550Z","shell.execute_reply":"2024-01-11T18:53:25.209254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This denotes the type of sound recorded whether its a call for a bird or bird is singing or something different, to see what all types its has we need to split it on commas.","metadata":{}},{"cell_type":"code","source":" df = df.drop('type', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:25.622030Z","iopub.execute_input":"2024-01-11T18:53:25.622441Z","iopub.status.idle":"2024-01-11T18:53:25.638273Z","shell.execute_reply.started":"2024-01-11T18:53:25.622409Z","shell.execute_reply":"2024-01-11T18:53:25.637306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.elevation.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:25.832881Z","iopub.execute_input":"2024-01-11T18:53:25.833552Z","iopub.status.idle":"2024-01-11T18:53:25.843115Z","shell.execute_reply.started":"2024-01-11T18:53:25.833519Z","shell.execute_reply":"2024-01-11T18:53:25.842070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.elevation.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:26.066970Z","iopub.execute_input":"2024-01-11T18:53:26.067665Z","iopub.status.idle":"2024-01-11T18:53:26.078517Z","shell.execute_reply.started":"2024-01-11T18:53:26.067632Z","shell.execute_reply":"2024-01-11T18:53:26.077570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here, elevation might be denoting the altitude of place from the ground level/the sea level where specific birds are being found. Even though it does not have any null values but it has some troublesome entries that need to be dealt with.","metadata":{}},{"cell_type":"code","source":"df['elevation'] = df['elevation'].str.replace(' m','', regex=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:26.503141Z","iopub.execute_input":"2024-01-11T18:53:26.504319Z","iopub.status.idle":"2024-01-11T18:53:26.530739Z","shell.execute_reply.started":"2024-01-11T18:53:26.504272Z","shell.execute_reply":"2024-01-11T18:53:26.529893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.elevation.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:26.705914Z","iopub.execute_input":"2024-01-11T18:53:26.706252Z","iopub.status.idle":"2024-01-11T18:53:26.717606Z","shell.execute_reply.started":"2024-01-11T18:53:26.706225Z","shell.execute_reply":"2024-01-11T18:53:26.716616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['elevation'] = df['elevation'].replace('?', np.nan)\ndf['elevation'] = df['elevation'].replace('0', np.nan)\ndf['elevation'] = df['elevation'].replace('-', np.nan)\ndf['elevation'] = df['elevation'].replace('Unknown', np.nan)\ndf['elevation'] = df['elevation'].replace('', np.nan)\ndf['elevation'] = df['elevation'].replace('??', np.nan)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:26.921476Z","iopub.execute_input":"2024-01-11T18:53:26.922430Z","iopub.status.idle":"2024-01-11T18:53:26.955822Z","shell.execute_reply.started":"2024-01-11T18:53:26.922390Z","shell.execute_reply":"2024-01-11T18:53:26.955017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.elevation.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:27.272416Z","iopub.execute_input":"2024-01-11T18:53:27.272760Z","iopub.status.idle":"2024-01-11T18:53:27.283589Z","shell.execute_reply.started":"2024-01-11T18:53:27.272731Z","shell.execute_reply":"2024-01-11T18:53:27.282544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.elevation.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:27.395532Z","iopub.execute_input":"2024-01-11T18:53:27.396145Z","iopub.status.idle":"2024-01-11T18:53:27.408655Z","shell.execute_reply.started":"2024-01-11T18:53:27.396107Z","shell.execute_reply":"2024-01-11T18:53:27.407559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"df['elevation'] = df['elevation'].str.replace('m','')","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:27.807285Z","iopub.execute_input":"2024-01-11T18:53:27.807658Z","iopub.status.idle":"2024-01-11T18:53:27.824571Z","shell.execute_reply.started":"2024-01-11T18:53:27.807629Z","shell.execute_reply":"2024-01-11T18:53:27.823559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.description.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:27.992696Z","iopub.execute_input":"2024-01-11T18:53:27.993605Z","iopub.status.idle":"2024-01-11T18:53:28.004620Z","shell.execute_reply.started":"2024-01-11T18:53:27.993570Z","shell.execute_reply":"2024-01-11T18:53:28.003591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.description.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:28.217121Z","iopub.execute_input":"2024-01-11T18:53:28.217435Z","iopub.status.idle":"2024-01-11T18:53:28.228350Z","shell.execute_reply.started":"2024-01-11T18:53:28.217409Z","shell.execute_reply":"2024-01-11T18:53:28.227409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop('description', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:28.416991Z","iopub.execute_input":"2024-01-11T18:53:28.417350Z","iopub.status.idle":"2024-01-11T18:53:28.431261Z","shell.execute_reply.started":"2024-01-11T18:53:28.417323Z","shell.execute_reply":"2024-01-11T18:53:28.430363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:28.690472Z","iopub.execute_input":"2024-01-11T18:53:28.690786Z","iopub.status.idle":"2024-01-11T18:53:28.699064Z","shell.execute_reply.started":"2024-01-11T18:53:28.690761Z","shell.execute_reply":"2024-01-11T18:53:28.698121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.bitrate_of_mp3","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:28.852003Z","iopub.execute_input":"2024-01-11T18:53:28.852359Z","iopub.status.idle":"2024-01-11T18:53:28.860378Z","shell.execute_reply.started":"2024-01-11T18:53:28.852329Z","shell.execute_reply":"2024-01-11T18:53:28.859391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop('bitrate_of_mp3', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:29.065906Z","iopub.execute_input":"2024-01-11T18:53:29.066273Z","iopub.status.idle":"2024-01-11T18:53:29.081392Z","shell.execute_reply.started":"2024-01-11T18:53:29.066243Z","shell.execute_reply":"2024-01-11T18:53:29.080546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.file_type.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:29.186696Z","iopub.execute_input":"2024-01-11T18:53:29.186980Z","iopub.status.idle":"2024-01-11T18:53:29.197341Z","shell.execute_reply.started":"2024-01-11T18:53:29.186956Z","shell.execute_reply":"2024-01-11T18:53:29.196350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Using librosa library has no issues with different filetypes. It is capable of loading .mp3, .wav, .mp2 and .aac files. So having differnet file type do not make any significant issue.","metadata":{}},{"cell_type":"code","source":"df.volume.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:29.663027Z","iopub.execute_input":"2024-01-11T18:53:29.664188Z","iopub.status.idle":"2024-01-11T18:53:29.675553Z","shell.execute_reply.started":"2024-01-11T18:53:29.664150Z","shell.execute_reply":"2024-01-11T18:53:29.674471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.volume.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:29.894849Z","iopub.execute_input":"2024-01-11T18:53:29.895202Z","iopub.status.idle":"2024-01-11T18:53:29.903410Z","shell.execute_reply.started":"2024-01-11T18:53:29.895172Z","shell.execute_reply":"2024-01-11T18:53:29.902386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop('volume', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:30.116010Z","iopub.execute_input":"2024-01-11T18:53:30.116376Z","iopub.status.idle":"2024-01-11T18:53:30.132211Z","shell.execute_reply.started":"2024-01-11T18:53:30.116347Z","shell.execute_reply":"2024-01-11T18:53:30.131414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:30.305365Z","iopub.execute_input":"2024-01-11T18:53:30.305671Z","iopub.status.idle":"2024-01-11T18:53:30.311999Z","shell.execute_reply.started":"2024-01-11T18:53:30.305645Z","shell.execute_reply":"2024-01-11T18:53:30.311097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.background.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:30.584895Z","iopub.execute_input":"2024-01-11T18:53:30.585669Z","iopub.status.idle":"2024-01-11T18:53:30.594127Z","shell.execute_reply.started":"2024-01-11T18:53:30.585629Z","shell.execute_reply":"2024-01-11T18:53:30.593179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop('background', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:30.741434Z","iopub.execute_input":"2024-01-11T18:53:30.742225Z","iopub.status.idle":"2024-01-11T18:53:30.756639Z","shell.execute_reply.started":"2024-01-11T18:53:30.742193Z","shell.execute_reply":"2024-01-11T18:53:30.755725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.xc_id.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:30.941758Z","iopub.execute_input":"2024-01-11T18:53:30.942077Z","iopub.status.idle":"2024-01-11T18:53:30.951989Z","shell.execute_reply.started":"2024-01-11T18:53:30.942039Z","shell.execute_reply":"2024-01-11T18:53:30.951027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.xc_id.unique","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:31.340041Z","iopub.execute_input":"2024-01-11T18:53:31.340747Z","iopub.status.idle":"2024-01-11T18:53:31.348001Z","shell.execute_reply.started":"2024-01-11T18:53:31.340710Z","shell.execute_reply":"2024-01-11T18:53:31.346891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This also seems like individual unique id given to each bird sound recording. ","metadata":{}},{"cell_type":"code","source":"df = df.drop('xc_id', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:31.531479Z","iopub.execute_input":"2024-01-11T18:53:31.531815Z","iopub.status.idle":"2024-01-11T18:53:31.546651Z","shell.execute_reply.started":"2024-01-11T18:53:31.531787Z","shell.execute_reply":"2024-01-11T18:53:31.545885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.url.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:31.746662Z","iopub.execute_input":"2024-01-11T18:53:31.747420Z","iopub.status.idle":"2024-01-11T18:53:31.759268Z","shell.execute_reply.started":"2024-01-11T18:53:31.747388Z","shell.execute_reply":"2024-01-11T18:53:31.758226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.url.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:31.945722Z","iopub.execute_input":"2024-01-11T18:53:31.946067Z","iopub.status.idle":"2024-01-11T18:53:31.955909Z","shell.execute_reply.started":"2024-01-11T18:53:31.946028Z","shell.execute_reply":"2024-01-11T18:53:31.954967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This column gives the hyperlinks to each of the audio file for every bird sound. For our model, these hyperlinks may not be that useful so we can drop this column.  ","metadata":{}},{"cell_type":"code","source":"df = df.drop('url', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:32.434818Z","iopub.execute_input":"2024-01-11T18:53:32.435317Z","iopub.status.idle":"2024-01-11T18:53:32.450290Z","shell.execute_reply.started":"2024-01-11T18:53:32.435276Z","shell.execute_reply":"2024-01-11T18:53:32.449316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:32.607410Z","iopub.execute_input":"2024-01-11T18:53:32.607805Z","iopub.status.idle":"2024-01-11T18:53:32.614343Z","shell.execute_reply.started":"2024-01-11T18:53:32.607772Z","shell.execute_reply":"2024-01-11T18:53:32.613332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.country.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:32.795719Z","iopub.execute_input":"2024-01-11T18:53:32.796008Z","iopub.status.idle":"2024-01-11T18:53:32.804328Z","shell.execute_reply.started":"2024-01-11T18:53:32.795984Z","shell.execute_reply":"2024-01-11T18:53:32.803357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This column gives info about which birds are found in a particular country. This may be useful for informational purpose to display which species are most prominently found in which countries and other informational stuff.","metadata":{}},{"cell_type":"code","source":"df.author.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:33.423396Z","iopub.execute_input":"2024-01-11T18:53:33.424185Z","iopub.status.idle":"2024-01-11T18:53:33.435935Z","shell.execute_reply.started":"2024-01-11T18:53:33.424150Z","shell.execute_reply":"2024-01-11T18:53:33.434962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.author.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:33.665126Z","iopub.execute_input":"2024-01-11T18:53:33.665462Z","iopub.status.idle":"2024-01-11T18:53:33.678830Z","shell.execute_reply.started":"2024-01-11T18:53:33.665434Z","shell.execute_reply":"2024-01-11T18:53:33.677943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.author.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:33.850429Z","iopub.execute_input":"2024-01-11T18:53:33.850782Z","iopub.status.idle":"2024-01-11T18:53:33.862570Z","shell.execute_reply.started":"2024-01-11T18:53:33.850753Z","shell.execute_reply":"2024-01-11T18:53:33.861675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This column might be showing the names of the people who have recorded a particualr a bird call/song sound. We may look for which people have done the most recordings, but that does not give any valuable information regarding our context of birds. We can go for dropping this column.","metadata":{}},{"cell_type":"code","source":"df = df.drop('author', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:34.304456Z","iopub.execute_input":"2024-01-11T18:53:34.304821Z","iopub.status.idle":"2024-01-11T18:53:34.318875Z","shell.execute_reply.started":"2024-01-11T18:53:34.304792Z","shell.execute_reply":"2024-01-11T18:53:34.317947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:34.514989Z","iopub.execute_input":"2024-01-11T18:53:34.515723Z","iopub.status.idle":"2024-01-11T18:53:34.522085Z","shell.execute_reply.started":"2024-01-11T18:53:34.515690Z","shell.execute_reply":"2024-01-11T18:53:34.521084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df.primary_label.nunique())\ndf.primary_label.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:34.690844Z","iopub.execute_input":"2024-01-11T18:53:34.691494Z","iopub.status.idle":"2024-01-11T18:53:34.704819Z","shell.execute_reply.started":"2024-01-11T18:53:34.691467Z","shell.execute_reply":"2024-01-11T18:53:34.703708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop('primary_label', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:34.926458Z","iopub.execute_input":"2024-01-11T18:53:34.927111Z","iopub.status.idle":"2024-01-11T18:53:34.941240Z","shell.execute_reply.started":"2024-01-11T18:53:34.927082Z","shell.execute_reply":"2024-01-11T18:53:34.940297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.length.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:35.086179Z","iopub.execute_input":"2024-01-11T18:53:35.086488Z","iopub.status.idle":"2024-01-11T18:53:35.096908Z","shell.execute_reply.started":"2024-01-11T18:53:35.086462Z","shell.execute_reply":"2024-01-11T18:53:35.096101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.length","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:35.348819Z","iopub.execute_input":"2024-01-11T18:53:35.349219Z","iopub.status.idle":"2024-01-11T18:53:35.357550Z","shell.execute_reply.started":"2024-01-11T18:53:35.349169Z","shell.execute_reply":"2024-01-11T18:53:35.356478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:35.591772Z","iopub.execute_input":"2024-01-11T18:53:35.592206Z","iopub.status.idle":"2024-01-11T18:53:35.600639Z","shell.execute_reply.started":"2024-01-11T18:53:35.592172Z","shell.execute_reply":"2024-01-11T18:53:35.599585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.time.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:35.796649Z","iopub.execute_input":"2024-01-11T18:53:35.797029Z","iopub.status.idle":"2024-01-11T18:53:35.808938Z","shell.execute_reply.started":"2024-01-11T18:53:35.796997Z","shell.execute_reply":"2024-01-11T18:53:35.807954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df.time.nunique())\ndf.time.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:36.008716Z","iopub.execute_input":"2024-01-11T18:53:36.009120Z","iopub.status.idle":"2024-01-11T18:53:36.020310Z","shell.execute_reply.started":"2024-01-11T18:53:36.009086Z","shell.execute_reply":"2024-01-11T18:53:36.019295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This column might be showing the time when the sound was being recorded, or might be the time when particular bird was seen. For displaying plot for informational purpose this column may be used.","metadata":{}},{"cell_type":"code","source":"df.recordist.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:36.483491Z","iopub.execute_input":"2024-01-11T18:53:36.483873Z","iopub.status.idle":"2024-01-11T18:53:36.495239Z","shell.execute_reply.started":"2024-01-11T18:53:36.483841Z","shell.execute_reply":"2024-01-11T18:53:36.494214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df.recordist.nunique())\ndf.recordist.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:36.688859Z","iopub.execute_input":"2024-01-11T18:53:36.689255Z","iopub.status.idle":"2024-01-11T18:53:36.704853Z","shell.execute_reply.started":"2024-01-11T18:53:36.689225Z","shell.execute_reply":"2024-01-11T18:53:36.703975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop('recordist', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:36.943419Z","iopub.execute_input":"2024-01-11T18:53:36.943835Z","iopub.status.idle":"2024-01-11T18:53:36.956698Z","shell.execute_reply.started":"2024-01-11T18:53:36.943803Z","shell.execute_reply":"2024-01-11T18:53:36.955849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:37.132018Z","iopub.execute_input":"2024-01-11T18:53:37.132380Z","iopub.status.idle":"2024-01-11T18:53:37.138674Z","shell.execute_reply.started":"2024-01-11T18:53:37.132353Z","shell.execute_reply":"2024-01-11T18:53:37.137795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.license.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:37.347518Z","iopub.execute_input":"2024-01-11T18:53:37.348418Z","iopub.status.idle":"2024-01-11T18:53:37.359706Z","shell.execute_reply.started":"2024-01-11T18:53:37.348374Z","shell.execute_reply":"2024-01-11T18:53:37.358686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:37.590125Z","iopub.execute_input":"2024-01-11T18:53:37.591093Z","iopub.status.idle":"2024-01-11T18:53:37.596787Z","shell.execute_reply.started":"2024-01-11T18:53:37.591035Z","shell.execute_reply":"2024-01-11T18:53:37.595958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (10,6))\n\nsns.countplot( x='rating', data = df, order = df['rating'].value_counts().index, palette = 'viridis')\nplt.title(\"Ratings distribution\")\nplt.xlabel(\"Ratings\")\nplt.ylabel(\"Count\")\nplt.xticks(rotation = 45)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:37.820850Z","iopub.execute_input":"2024-01-11T18:53:37.821633Z","iopub.status.idle":"2024-01-11T18:53:38.142605Z","shell.execute_reply.started":"2024-01-11T18:53:37.821582Z","shell.execute_reply":"2024-01-11T18:53:38.141655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['month']=df['month'].astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:38.144479Z","iopub.execute_input":"2024-01-11T18:53:38.144860Z","iopub.status.idle":"2024-01-11T18:53:38.154340Z","shell.execute_reply.started":"2024-01-11T18:53:38.144810Z","shell.execute_reply":"2024-01-11T18:53:38.153270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.month.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:38.289545Z","iopub.execute_input":"2024-01-11T18:53:38.289865Z","iopub.status.idle":"2024-01-11T18:53:38.297132Z","shell.execute_reply.started":"2024-01-11T18:53:38.289834Z","shell.execute_reply":"2024-01-11T18:53:38.296106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bird_month_counts = df.groupby(['month', 'species']).size().reset_index(name='Count')\n\n# Find the top 10 birds based on overall sightings\ntop_birds = bird_month_counts.groupby('species')['Count'].sum().nlargest(5).index\n\n# Filter the data for the top 10 birds\ntop_bird_month_counts = bird_month_counts[bird_month_counts['species'].isin(top_birds)]\n\n# Create a line plot using Seaborn\nplt.figure(figsize=(14, 8))\nsns.lineplot(x='month', y='Count', hue='species', data=top_bird_month_counts, marker='o', palette='muted')\nplt.xlabel('Month')\nplt.ylabel('Count')\nplt.title('Top 10 Bird Sightings Over Months')\nplt.legend(title='species', bbox_to_anchor=(1, 1))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:38.620519Z","iopub.execute_input":"2024-01-11T18:53:38.620879Z","iopub.status.idle":"2024-01-11T18:53:39.047619Z","shell.execute_reply.started":"2024-01-11T18:53:38.620843Z","shell.execute_reply":"2024-01-11T18:53:39.046705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:39.049040Z","iopub.execute_input":"2024-01-11T18:53:39.049335Z","iopub.status.idle":"2024-01-11T18:53:39.055248Z","shell.execute_reply.started":"2024-01-11T18:53:39.049310Z","shell.execute_reply":"2024-01-11T18:53:39.054329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # FEATURE EXTRACTION ","metadata":{}},{"cell_type":"code","source":"unique_values = df['species'].unique()[:5]\nunique_values","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:39.251103Z","iopub.execute_input":"2024-01-11T18:53:39.251802Z","iopub.status.idle":"2024-01-11T18:53:39.259486Z","shell.execute_reply.started":"2024-01-11T18:53:39.251774Z","shell.execute_reply":"2024-01-11T18:53:39.258606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfs = []\n\n# Extract 2 rows for each unique value\nfor value in unique_values:\n    # Filter rows based on the unique value\n    subset = df[df['species'] == value].head(2)\n    \n    # Append the subset to the result DataFrame\n    dfs.append(subset)\n\ndf_for_FE = pd.concat(dfs, ignore_index=True)\ndf_for_FE","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:39.421069Z","iopub.execute_input":"2024-01-11T18:53:39.421766Z","iopub.status.idle":"2024-01-11T18:53:39.473695Z","shell.execute_reply.started":"2024-01-11T18:53:39.421730Z","shell.execute_reply":"2024-01-11T18:53:39.472705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y1,sr1 = librosa.load(\"/kaggle/input/birdsong-recognition/train_audio/aldfly/XC134874.mp3\")\ny2,sr2 = librosa.load(\"/kaggle/input/birdsong-recognition/train_audio/aldfly/XC135454.mp3\")","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:39.909208Z","iopub.execute_input":"2024-01-11T18:53:39.909572Z","iopub.status.idle":"2024-01-11T18:53:41.756578Z","shell.execute_reply.started":"2024-01-11T18:53:39.909545Z","shell.execute_reply":"2024-01-11T18:53:41.755480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Audio1\")\nipd.Audio(y1, rate=sr1)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:41.758490Z","iopub.execute_input":"2024-01-11T18:53:41.758846Z","iopub.status.idle":"2024-01-11T18:53:41.791690Z","shell.execute_reply.started":"2024-01-11T18:53:41.758816Z","shell.execute_reply":"2024-01-11T18:53:41.790737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y3,sr3 = librosa.load(\"/kaggle/input/birdsong-recognition/train_audio/ameavo/XC133080.mp3\")\ny4,sr4 = librosa.load(\"/kaggle/input/birdsong-recognition/train_audio/ameavo/XC139829.mp3\")","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:42.004612Z","iopub.execute_input":"2024-01-11T18:53:42.005346Z","iopub.status.idle":"2024-01-11T18:53:42.873237Z","shell.execute_reply.started":"2024-01-11T18:53:42.005312Z","shell.execute_reply":"2024-01-11T18:53:42.872418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Audio3\")\nipd.Audio(y3, rate=sr3)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:42.875268Z","iopub.execute_input":"2024-01-11T18:53:42.875640Z","iopub.status.idle":"2024-01-11T18:53:42.892781Z","shell.execute_reply.started":"2024-01-11T18:53:42.875606Z","shell.execute_reply":"2024-01-11T18:53:42.891997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y5,sr5 = librosa.load(\"/kaggle/input/birdsong-recognition/train_audio/amebit/XC127371.mp3\")\ny6,sr6 = librosa.load(\"/kaggle/input/birdsong-recognition/train_audio/amebit/XC130058.mp3\")","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:42.893731Z","iopub.execute_input":"2024-01-11T18:53:42.893970Z","iopub.status.idle":"2024-01-11T18:53:44.139670Z","shell.execute_reply.started":"2024-01-11T18:53:42.893948Z","shell.execute_reply":"2024-01-11T18:53:44.138666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Audio5\")\nipd.Audio(y5, rate=sr5)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:44.142525Z","iopub.execute_input":"2024-01-11T18:53:44.142852Z","iopub.status.idle":"2024-01-11T18:53:44.167758Z","shell.execute_reply.started":"2024-01-11T18:53:44.142824Z","shell.execute_reply":"2024-01-11T18:53:44.166956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y7,sr7 = librosa.load(\"/kaggle/input/birdsong-recognition/train_audio/amecro/XC109768.mp3\")\ny8,sr8 = librosa.load(\"/kaggle/input/birdsong-recognition/train_audio/amecro/XC112598.mp3\")","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:44.168718Z","iopub.execute_input":"2024-01-11T18:53:44.168964Z","iopub.status.idle":"2024-01-11T18:53:48.234001Z","shell.execute_reply.started":"2024-01-11T18:53:44.168942Z","shell.execute_reply":"2024-01-11T18:53:48.233091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Audio7\")\nipd.Audio(y7, rate=sr7)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:48.235451Z","iopub.execute_input":"2024-01-11T18:53:48.235732Z","iopub.status.idle":"2024-01-11T18:53:48.264627Z","shell.execute_reply.started":"2024-01-11T18:53:48.235706Z","shell.execute_reply":"2024-01-11T18:53:48.263725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y9,sr9 = librosa.load(\"/kaggle/input/birdsong-recognition/train_audio/amegfi/XC109299.mp3\")\ny10,sr10 = librosa.load(\"/kaggle/input/birdsong-recognition/train_audio/amegfi/XC109300.mp3\")","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:48.265817Z","iopub.execute_input":"2024-01-11T18:53:48.266191Z","iopub.status.idle":"2024-01-11T18:53:56.258902Z","shell.execute_reply.started":"2024-01-11T18:53:48.266162Z","shell.execute_reply":"2024-01-11T18:53:56.258084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Audio9\")\nipd.Audio(y9, rate=sr9)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:56.260121Z","iopub.execute_input":"2024-01-11T18:53:56.260424Z","iopub.status.idle":"2024-01-11T18:53:56.366172Z","shell.execute_reply.started":"2024-01-11T18:53:56.260398Z","shell.execute_reply":"2024-01-11T18:53:56.365103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ZERO CROSSING RATE","metadata":{}},{"cell_type":"code","source":"zcr1 = librosa.feature.zero_crossing_rate(y1)\nprint(zcr1)\nprint(\"zcr1\")\nprint('max:',zcr1.max())\nprint('min:',zcr1.min())\nprint(\"-------------------------------\")\nzcr2 = librosa.feature.zero_crossing_rate(y2)\nprint(\"zcr2\")\nprint('max:',zcr2.max())\nprint('min:',zcr2.min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:56.367431Z","iopub.execute_input":"2024-01-11T18:53:56.367753Z","iopub.status.idle":"2024-01-11T18:53:56.412220Z","shell.execute_reply.started":"2024-01-11T18:53:56.367726Z","shell.execute_reply":"2024-01-11T18:53:56.411095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zcr3 = librosa.feature.zero_crossing_rate(y3)\nprint(\"zcr3\")\nprint('max:',zcr3.max())\nprint('min:',zcr3.min())\nprint(\"-------------------------------\")\nzcr4 = librosa.feature.zero_crossing_rate(y4)\nprint(\"zcr4\")\nprint('max:',zcr4.max())\nprint('min:',zcr4.min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:56.416011Z","iopub.execute_input":"2024-01-11T18:53:56.416353Z","iopub.status.idle":"2024-01-11T18:53:56.441145Z","shell.execute_reply.started":"2024-01-11T18:53:56.416324Z","shell.execute_reply":"2024-01-11T18:53:56.440096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zcr5 = librosa.feature.zero_crossing_rate(y5)\nprint(\"zcr5\")\nprint('max:',zcr5.max())\nprint('min:',zcr5.min())\nprint(\"-------------------------------\")\nzcr6 = librosa.feature.zero_crossing_rate(y6)\nprint(\"zcr6\")\nprint('max:',zcr6.max())\nprint('min:',zcr6.min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:56.442537Z","iopub.execute_input":"2024-01-11T18:53:56.442891Z","iopub.status.idle":"2024-01-11T18:53:56.475597Z","shell.execute_reply.started":"2024-01-11T18:53:56.442863Z","shell.execute_reply":"2024-01-11T18:53:56.474648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zcr7 = librosa.feature.zero_crossing_rate(y7)\nprint(\"zcr7\")\nprint('max:',zcr7.max())\nprint('min:',zcr7.min())\nprint(\"-------------------------------\")\nzcr8 = librosa.feature.zero_crossing_rate(y8)\nprint(\"zcr8\")\nprint('max:',zcr8.max())\nprint('min:',zcr8.min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:56.476860Z","iopub.execute_input":"2024-01-11T18:53:56.477222Z","iopub.status.idle":"2024-01-11T18:53:56.582701Z","shell.execute_reply.started":"2024-01-11T18:53:56.477192Z","shell.execute_reply":"2024-01-11T18:53:56.581660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zcr9 = librosa.feature.zero_crossing_rate(y9)\nprint(\"zcr9\")\nprint('max:',zcr9.max())\nprint('min:',zcr9.min())\nprint(\"-------------------------------\")\nzcr10 = librosa.feature.zero_crossing_rate(y10)\nprint(\"zcr10\")\nprint('max:',zcr10.max())\nprint('min:',zcr10.min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:56.584461Z","iopub.execute_input":"2024-01-11T18:53:56.584879Z","iopub.status.idle":"2024-01-11T18:53:56.773273Z","shell.execute_reply.started":"2024-01-11T18:53:56.584835Z","shell.execute_reply":"2024-01-11T18:53:56.772303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Observng the above zero-crossing rate values for each audio file, we can observe a pattern that two audio files of same category bird have similar ranges of zero-crossing rates. And they do show differnece in the zcr values of audio files from different bird categories. As this feature somewhat classifies the two audio groups, so this can be considered further for classification model.","metadata":{}},{"cell_type":"markdown","source":"# RMS Energy","metadata":{}},{"cell_type":"code","source":"#energy (rms)\nenergy1 = librosa.feature.rms(y=y1)\nprint(energy1)\nprint('energy1')\nprint('max:',energy1.max())\nprint('min:',energy1.min())\nprint('mean:',energy1.mean())\nprint(\"-------------------------------\")\nenergy2 = librosa.feature.rms(y=y2)\nprint('energy2')\nprint('max:',energy2.max())\nprint('min:',energy2.min())\nprint('mean:',energy2.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:56.774484Z","iopub.execute_input":"2024-01-11T18:53:56.774810Z","iopub.status.idle":"2024-01-11T18:53:56.790202Z","shell.execute_reply.started":"2024-01-11T18:53:56.774782Z","shell.execute_reply":"2024-01-11T18:53:56.789258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#energy (rms)\nenergy3 = librosa.feature.rms(y=y3)\nprint('energy3')\nprint('max:',energy3.max())\nprint('min:',energy3.min())\nprint('mean:',energy3.mean())\nprint(\"-------------------------------\")\nenergy4 = librosa.feature.rms(y=y4)\nprint('energy4')\nprint('max:',energy4.max())\nprint('min:',energy4.min())\nprint('mean:',energy4.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:56.791643Z","iopub.execute_input":"2024-01-11T18:53:56.792342Z","iopub.status.idle":"2024-01-11T18:53:56.803492Z","shell.execute_reply.started":"2024-01-11T18:53:56.792304Z","shell.execute_reply":"2024-01-11T18:53:56.802434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#energy (rms)\nenergy5 = librosa.feature.rms(y=y5)\nprint('energy5')\nprint('max:',energy5.max())\nprint('min:',energy5.min())\nprint('mean:',energy5.mean())\nprint(\"-------------------------------\")\nenergy6 = librosa.feature.rms(y=y6)\nprint('energy6')\nprint('max:',energy6.max())\nprint('min:',energy6.min())\nprint('mean:',energy6.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:56.805120Z","iopub.execute_input":"2024-01-11T18:53:56.805484Z","iopub.status.idle":"2024-01-11T18:53:56.819196Z","shell.execute_reply.started":"2024-01-11T18:53:56.805450Z","shell.execute_reply":"2024-01-11T18:53:56.818306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#energy (rms)\nenergy7 = librosa.feature.rms(y=y7)\nprint('energy7')\nprint('max:',energy7.max())\nprint('min:',energy7.min())\nprint('mean:',energy7.mean())\nprint(\"-------------------------------\")\nenergy8 = librosa.feature.rms(y=y8)\nprint('energy8')\nprint('max:',energy8.max())\nprint('min:',energy8.min())\nprint('mean:',energy8.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:56.820400Z","iopub.execute_input":"2024-01-11T18:53:56.820795Z","iopub.status.idle":"2024-01-11T18:53:56.847850Z","shell.execute_reply.started":"2024-01-11T18:53:56.820762Z","shell.execute_reply":"2024-01-11T18:53:56.846972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#energy (rms)\nenergy9 = librosa.feature.rms(y=y9)\nprint('energy9')\nprint('max:',energy9.max())\nprint('min:',energy9.min())\nprint('mean:',energy9.mean())\nprint(\"-------------------------------\")\nenergy10 = librosa.feature.rms(y=y10)\nprint('energy10')\nprint('max:',energy10.max())\nprint('min:',energy10.min())\nprint('mean:',energy10.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:56.848916Z","iopub.execute_input":"2024-01-11T18:53:56.849212Z","iopub.status.idle":"2024-01-11T18:53:56.890432Z","shell.execute_reply.started":"2024-01-11T18:53:56.849186Z","shell.execute_reply":"2024-01-11T18:53:56.889543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From above avg. values for rms energy for each audio file, we can not derive any clear pattern of values and resemblance that can be used for classifying the audio groups.","metadata":{}},{"cell_type":"markdown","source":"# Spectral Roll-off","metadata":{}},{"cell_type":"code","source":"#spectral roll-off\nS, phase = librosa.magphase(librosa.stft(y1))\nrolloff = librosa.feature.spectral_rolloff(S=S, sr=sr1, roll_percent=0.85)\nprint(\"audio1 spectral rolloff:\",rolloff.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:56.891510Z","iopub.execute_input":"2024-01-11T18:53:56.891774Z","iopub.status.idle":"2024-01-11T18:53:56.936676Z","shell.execute_reply.started":"2024-01-11T18:53:56.891750Z","shell.execute_reply":"2024-01-11T18:53:56.935683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#spectral roll-off\nS, phase = librosa.magphase(librosa.stft(y2))\nrolloff = librosa.feature.spectral_rolloff(S=S, sr=sr2, roll_percent=0.85)\nprint(\"audio2 spectral rolloff:\",rolloff.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:56.938160Z","iopub.execute_input":"2024-01-11T18:53:56.938461Z","iopub.status.idle":"2024-01-11T18:53:57.002147Z","shell.execute_reply.started":"2024-01-11T18:53:56.938434Z","shell.execute_reply":"2024-01-11T18:53:57.001137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#spectral roll-off\nS, phase = librosa.magphase(librosa.stft(y3))\nrolloff = librosa.feature.spectral_rolloff(S=S, sr=sr3, roll_percent=0.85)\nprint(\"audio3 spectral rolloff:\",rolloff.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:57.003545Z","iopub.execute_input":"2024-01-11T18:53:57.003873Z","iopub.status.idle":"2024-01-11T18:53:57.029302Z","shell.execute_reply.started":"2024-01-11T18:53:57.003843Z","shell.execute_reply":"2024-01-11T18:53:57.028410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#spectral roll-off\nS, phase = librosa.magphase(librosa.stft(y4))\nrolloff = librosa.feature.spectral_rolloff(S=S, sr=sr4, roll_percent=0.85)\nprint(\"audio4 spectral rolloff:\",rolloff.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:57.030493Z","iopub.execute_input":"2024-01-11T18:53:57.030778Z","iopub.status.idle":"2024-01-11T18:53:57.066031Z","shell.execute_reply.started":"2024-01-11T18:53:57.030752Z","shell.execute_reply":"2024-01-11T18:53:57.065124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#spectral roll-off\nS, phase = librosa.magphase(librosa.stft(y5))\nrolloff = librosa.feature.spectral_rolloff(S=S, sr=sr5, roll_percent=0.85)\nprint(\"audio5 spectral rolloff:\",rolloff.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:57.067304Z","iopub.execute_input":"2024-01-11T18:53:57.067660Z","iopub.status.idle":"2024-01-11T18:53:57.105163Z","shell.execute_reply.started":"2024-01-11T18:53:57.067628Z","shell.execute_reply":"2024-01-11T18:53:57.104175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#spectral roll-off\nS, phase = librosa.magphase(librosa.stft(y6))\nrolloff = librosa.feature.spectral_rolloff(S=S, sr=sr6, roll_percent=0.85)\nprint(\"audio6 spectral rolloff:\",rolloff.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:57.106446Z","iopub.execute_input":"2024-01-11T18:53:57.106740Z","iopub.status.idle":"2024-01-11T18:53:57.152510Z","shell.execute_reply.started":"2024-01-11T18:53:57.106714Z","shell.execute_reply":"2024-01-11T18:53:57.151492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#spectral roll-off\nS, phase = librosa.magphase(librosa.stft(y7))\nrolloff = librosa.feature.spectral_rolloff(S=S, sr=sr7, roll_percent=0.85)\nprint(\"audio7 spectral rolloff:\",rolloff.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:57.153690Z","iopub.execute_input":"2024-01-11T18:53:57.154033Z","iopub.status.idle":"2024-01-11T18:53:57.189682Z","shell.execute_reply.started":"2024-01-11T18:53:57.154001Z","shell.execute_reply":"2024-01-11T18:53:57.188705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#spectral roll-off\nS, phase = librosa.magphase(librosa.stft(y8))\nrolloff = librosa.feature.spectral_rolloff(S=S, sr=sr8, roll_percent=0.85)\nprint(\"audio8 spectral rolloff:\",rolloff.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:57.195673Z","iopub.execute_input":"2024-01-11T18:53:57.195989Z","iopub.status.idle":"2024-01-11T18:53:57.410165Z","shell.execute_reply.started":"2024-01-11T18:53:57.195962Z","shell.execute_reply":"2024-01-11T18:53:57.409190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#spectral roll-off\nS, phase = librosa.magphase(librosa.stft(y9))\nrolloff = librosa.feature.spectral_rolloff(S=S, sr=sr9, roll_percent=0.85)\nprint(\"audio9 spectral rolloff:\",rolloff.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:57.411249Z","iopub.execute_input":"2024-01-11T18:53:57.411523Z","iopub.status.idle":"2024-01-11T18:53:57.613372Z","shell.execute_reply.started":"2024-01-11T18:53:57.411498Z","shell.execute_reply":"2024-01-11T18:53:57.612430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#spectral roll-off\nS, phase = librosa.magphase(librosa.stft(y10))\nrolloff = librosa.feature.spectral_rolloff(S=S, sr=sr10, roll_percent=0.85)\nprint(\"audio10 spectral rolloff:\",rolloff.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:57.614761Z","iopub.execute_input":"2024-01-11T18:53:57.615162Z","iopub.status.idle":"2024-01-11T18:53:57.874384Z","shell.execute_reply.started":"2024-01-11T18:53:57.615126Z","shell.execute_reply":"2024-01-11T18:53:57.873237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here we see that avg. values for spectral rolloff for each audio file do not reveal any particular pattern of values and resemblance that can be further used for classifying the audio groups. They have some mixed range of values that can not classified clearly.","metadata":{}},{"cell_type":"markdown","source":"# MFCCs","metadata":{}},{"cell_type":"code","source":"mfcc1 = librosa.feature.mfcc(y=y1, sr=sr1)\nprint(mfcc1)\nprint(\"mfcc1\")\nprint(\"max of mean-mfcc:\",np.mean(mfcc1, axis=1).max())\nprint(\"min of mean-mfcc:\",np.mean(mfcc1, axis=1).min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:57.876004Z","iopub.execute_input":"2024-01-11T18:53:57.876497Z","iopub.status.idle":"2024-01-11T18:53:57.919242Z","shell.execute_reply.started":"2024-01-11T18:53:57.876455Z","shell.execute_reply":"2024-01-11T18:53:57.918008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfcc2 = librosa.feature.mfcc(y=y2, sr=sr2)\nprint(\"mfcc2\")\nprint(\"max of mean-mfcc:\",np.mean(mfcc2, axis=1).max())\nprint(\"min of mean-mfcc:\",np.mean(mfcc2, axis=1).min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:57.921638Z","iopub.execute_input":"2024-01-11T18:53:57.922162Z","iopub.status.idle":"2024-01-11T18:53:57.999956Z","shell.execute_reply.started":"2024-01-11T18:53:57.922112Z","shell.execute_reply":"2024-01-11T18:53:57.998791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfcc3 = librosa.feature.mfcc(y=y3, sr=sr3)\nprint(\"mfcc3\")\nprint(\"max of mean-mfcc:\",np.mean(mfcc3, axis=1).max())\nprint(\"min of mean-mfcc:\",np.mean(mfcc3, axis=1).min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:58.002018Z","iopub.execute_input":"2024-01-11T18:53:58.002523Z","iopub.status.idle":"2024-01-11T18:53:58.040458Z","shell.execute_reply.started":"2024-01-11T18:53:58.002476Z","shell.execute_reply":"2024-01-11T18:53:58.039197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfcc4 = librosa.feature.mfcc(y=y4, sr=sr4)\nprint(\"mfcc4\")\nprint(\"max of mean-mfcc:\",np.mean(mfcc4, axis=1).max())\nprint(\"min of mean-mfcc:\",np.mean(mfcc4, axis=1).min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:58.042445Z","iopub.execute_input":"2024-01-11T18:53:58.043740Z","iopub.status.idle":"2024-01-11T18:53:58.095284Z","shell.execute_reply.started":"2024-01-11T18:53:58.043688Z","shell.execute_reply":"2024-01-11T18:53:58.094094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfcc5 = librosa.feature.mfcc(y=y5, sr=sr5)\nprint(\"mfcc5\")\nprint(\"max of mean-mfcc:\",np.mean(mfcc5, axis=1).max())\nprint(\"min of mean-mfcc:\",np.mean(mfcc5, axis=1).min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:58.097384Z","iopub.execute_input":"2024-01-11T18:53:58.098244Z","iopub.status.idle":"2024-01-11T18:53:58.155499Z","shell.execute_reply.started":"2024-01-11T18:53:58.098194Z","shell.execute_reply":"2024-01-11T18:53:58.154218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfcc6 = librosa.feature.mfcc(y=y6, sr=sr6)\nprint(\"mfcc6\")\nprint(\"max of mean-mfcc:\",np.mean(mfcc6, axis=1).max())\nprint(\"min of mean-mfcc:\",np.mean(mfcc6, axis=1).min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:58.157691Z","iopub.execute_input":"2024-01-11T18:53:58.158603Z","iopub.status.idle":"2024-01-11T18:53:58.223308Z","shell.execute_reply.started":"2024-01-11T18:53:58.158550Z","shell.execute_reply":"2024-01-11T18:53:58.222016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfcc7 = librosa.feature.mfcc(y=y7, sr=sr7)\nprint(\"mfcc7\")\nprint(\"max of mean-mfcc:\",np.mean(mfcc7, axis=1).max())\nprint(\"min of mean-mfcc:\",np.mean(mfcc7, axis=1).min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:58.225473Z","iopub.execute_input":"2024-01-11T18:53:58.226374Z","iopub.status.idle":"2024-01-11T18:53:58.275118Z","shell.execute_reply.started":"2024-01-11T18:53:58.226326Z","shell.execute_reply":"2024-01-11T18:53:58.273926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfcc8 = librosa.feature.mfcc(y=y8, sr=sr8)\nprint(\"mfcc8\")\nprint(\"max of mean-mfcc:\",np.mean(mfcc8, axis=1).max())\nprint(\"min of mean-mfcc:\",np.mean(mfcc8, axis=1).min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:58.277333Z","iopub.execute_input":"2024-01-11T18:53:58.278264Z","iopub.status.idle":"2024-01-11T18:53:58.485408Z","shell.execute_reply.started":"2024-01-11T18:53:58.278213Z","shell.execute_reply":"2024-01-11T18:53:58.484101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfcc9 = librosa.feature.mfcc(y=y9, sr=sr9)\nprint(\"mfcc9\")\nprint(\"max of mean-mfcc:\",np.mean(mfcc9, axis=1).max())\nprint(\"min of mean-mfcc:\",np.mean(mfcc9, axis=1).min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:58.487625Z","iopub.execute_input":"2024-01-11T18:53:58.488539Z","iopub.status.idle":"2024-01-11T18:53:58.685338Z","shell.execute_reply.started":"2024-01-11T18:53:58.488487Z","shell.execute_reply":"2024-01-11T18:53:58.683968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfcc10 = librosa.feature.mfcc(y=y10, sr=sr10)\nprint(\"mfcc10\")\nprint(\"max of mean-mfcc:\",np.mean(mfcc10, axis=1).max())\nprint(\"min of mean-mfcc:\",np.mean(mfcc10, axis=1).min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:58.687594Z","iopub.execute_input":"2024-01-11T18:53:58.688474Z","iopub.status.idle":"2024-01-11T18:53:58.922280Z","shell.execute_reply.started":"2024-01-11T18:53:58.688422Z","shell.execute_reply":"2024-01-11T18:53:58.918936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here we can observe that mean mfccs values do follow some pattern. Two audio files from each categpry of bird do share some similarities in the mfcc values. There statisctical values will be helpful in classification model.","metadata":{}},{"cell_type":"markdown","source":"# Spectral Flux","metadata":{}},{"cell_type":"code","source":"#spectral flux\nonset_env = librosa.onset.onset_strength(y=y1, sr=sr1)\nprint(\"spectral flux for Audio1\")\nprint(onset_env)\nprint(onset_env.mean())\nprint(\"----------------------------------------\")\nonset_env = librosa.onset.onset_strength(y=y2, sr=sr2)\nprint(\"spectral flux for Audio2\")\nprint(onset_env.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:58.923785Z","iopub.execute_input":"2024-01-11T18:53:58.924227Z","iopub.status.idle":"2024-01-11T18:53:59.066283Z","shell.execute_reply.started":"2024-01-11T18:53:58.924190Z","shell.execute_reply":"2024-01-11T18:53:59.065098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"onset_env = librosa.onset.onset_strength(y=y3, sr=sr3)\nprint(\"spectral flux for Audio3\")\nprint(onset_env.mean())\nprint(\"----------------------------------------\")\nonset_env = librosa.onset.onset_strength(y=y4, sr=sr4)\nprint(\"spectral flux for Audio4\")\nprint(onset_env.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:59.068343Z","iopub.execute_input":"2024-01-11T18:53:59.069266Z","iopub.status.idle":"2024-01-11T18:53:59.147567Z","shell.execute_reply.started":"2024-01-11T18:53:59.069214Z","shell.execute_reply":"2024-01-11T18:53:59.146188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"onset_env = librosa.onset.onset_strength(y=y5, sr=sr5)\nprint(\"spectral flux for Audio5\")\nprint(onset_env.mean())\nprint(\"----------------------------------------\")\nonset_env = librosa.onset.onset_strength(y=y6, sr=sr6)\nprint(\"spectral flux for Audio6\")\nprint(onset_env.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:59.149796Z","iopub.execute_input":"2024-01-11T18:53:59.150681Z","iopub.status.idle":"2024-01-11T18:53:59.258287Z","shell.execute_reply.started":"2024-01-11T18:53:59.150627Z","shell.execute_reply":"2024-01-11T18:53:59.257127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"onset_env = librosa.onset.onset_strength(y=y7, sr=sr7)\nprint(\"spectral flux for Audio7\")\nprint(onset_env.mean())\nprint(\"----------------------------------------\")\nonset_env = librosa.onset.onset_strength(y=y8, sr=sr8)\nprint(\"spectral flux for Audio6\")\nprint(onset_env.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:59.260183Z","iopub.execute_input":"2024-01-11T18:53:59.260924Z","iopub.status.idle":"2024-01-11T18:53:59.508060Z","shell.execute_reply.started":"2024-01-11T18:53:59.260878Z","shell.execute_reply":"2024-01-11T18:53:59.506820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"onset_env = librosa.onset.onset_strength(y=y9, sr=sr9)\nprint(\"spectral flux for Audio9\")\nprint(onset_env.mean())\nprint(\"----------------------------------------\")\nonset_env = librosa.onset.onset_strength(y=y10, sr=sr10)\nprint(\"spectral flux for Audio10\")\nprint(onset_env.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:59.510116Z","iopub.execute_input":"2024-01-11T18:53:59.510923Z","iopub.status.idle":"2024-01-11T18:53:59.934453Z","shell.execute_reply.started":"2024-01-11T18:53:59.510873Z","shell.execute_reply":"2024-01-11T18:53:59.931107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Observing values, we can't clearly find any distinction pattern.","metadata":{}},{"cell_type":"markdown","source":"# Spectrogram","metadata":{}},{"cell_type":"markdown","source":"Observing the fourier transforms of audio files will reveal innformation about  the magnitude of the frequency components at different time frames. This will be useful for identifying the key frequency components.","metadata":{}},{"cell_type":"code","source":"spec = np.abs(librosa.stft(y1))\nmean_per_bin = np.mean(spec, axis=1)\nprint(mean_per_bin.max())\nprint(mean_per_bin.min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:59.936408Z","iopub.execute_input":"2024-01-11T18:53:59.936867Z","iopub.status.idle":"2024-01-11T18:53:59.996324Z","shell.execute_reply.started":"2024-01-11T18:53:59.936828Z","shell.execute_reply":"2024-01-11T18:53:59.995169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spec = np.abs(librosa.stft(y2))\nmean_per_bin = np.mean(spec, axis=1)\nprint(mean_per_bin.max())\nprint(mean_per_bin.min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:53:59.998172Z","iopub.execute_input":"2024-01-11T18:53:59.999070Z","iopub.status.idle":"2024-01-11T18:54:00.064532Z","shell.execute_reply.started":"2024-01-11T18:53:59.999008Z","shell.execute_reply":"2024-01-11T18:54:00.063636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spec = np.abs(librosa.stft(y3))\nmean_per_bin = np.mean(spec, axis=1)\nprint(mean_per_bin.max())\nprint(mean_per_bin.min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.065623Z","iopub.execute_input":"2024-01-11T18:54:00.065912Z","iopub.status.idle":"2024-01-11T18:54:00.080889Z","shell.execute_reply.started":"2024-01-11T18:54:00.065886Z","shell.execute_reply":"2024-01-11T18:54:00.080082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spec = np.abs(librosa.stft(y4))\nmean_per_bin = np.mean(spec, axis=1)\nprint(mean_per_bin.max())\nprint(mean_per_bin.min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.082121Z","iopub.execute_input":"2024-01-11T18:54:00.082380Z","iopub.status.idle":"2024-01-11T18:54:00.105252Z","shell.execute_reply.started":"2024-01-11T18:54:00.082357Z","shell.execute_reply":"2024-01-11T18:54:00.104396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spec = np.abs(librosa.stft(y5))\nmean_per_bin = np.mean(spec, axis=1)\nprint(mean_per_bin.max())\nprint(mean_per_bin.min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.106250Z","iopub.execute_input":"2024-01-11T18:54:00.106491Z","iopub.status.idle":"2024-01-11T18:54:00.131448Z","shell.execute_reply.started":"2024-01-11T18:54:00.106469Z","shell.execute_reply":"2024-01-11T18:54:00.130644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spec = np.abs(librosa.stft(y6))\nmean_per_bin = np.mean(spec, axis=1)\nprint(mean_per_bin.max())\nprint(mean_per_bin.min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.132429Z","iopub.execute_input":"2024-01-11T18:54:00.132687Z","iopub.status.idle":"2024-01-11T18:54:00.159620Z","shell.execute_reply.started":"2024-01-11T18:54:00.132663Z","shell.execute_reply":"2024-01-11T18:54:00.158727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spec = np.abs(librosa.stft(y7))\nmean_per_bin = np.mean(spec, axis=1)\nprint(mean_per_bin.max())\nprint(mean_per_bin.min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.160721Z","iopub.execute_input":"2024-01-11T18:54:00.162055Z","iopub.status.idle":"2024-01-11T18:54:00.183245Z","shell.execute_reply.started":"2024-01-11T18:54:00.162022Z","shell.execute_reply":"2024-01-11T18:54:00.182393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spec = np.abs(librosa.stft(y8))\nmean_per_bin = np.mean(spec, axis=1)\nprint(mean_per_bin.max())\nprint(mean_per_bin.min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.184284Z","iopub.execute_input":"2024-01-11T18:54:00.184648Z","iopub.status.idle":"2024-01-11T18:54:00.305766Z","shell.execute_reply.started":"2024-01-11T18:54:00.184613Z","shell.execute_reply":"2024-01-11T18:54:00.304779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spec = np.abs(librosa.stft(y9))\nmean_per_bin = np.mean(spec, axis=1)\nprint(mean_per_bin.max())\nprint(mean_per_bin.min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.307013Z","iopub.execute_input":"2024-01-11T18:54:00.307385Z","iopub.status.idle":"2024-01-11T18:54:00.420063Z","shell.execute_reply.started":"2024-01-11T18:54:00.307351Z","shell.execute_reply":"2024-01-11T18:54:00.419207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spec = np.abs(librosa.stft(y10))\nmean_per_bin = np.mean(spec, axis=1)\nprint(mean_per_bin.max())\nprint(mean_per_bin.min())","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.421060Z","iopub.execute_input":"2024-01-11T18:54:00.421317Z","iopub.status.idle":"2024-01-11T18:54:00.558968Z","shell.execute_reply.started":"2024-01-11T18:54:00.421293Z","shell.execute_reply":"2024-01-11T18:54:00.558090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Spectrogram for Audio 1\nD1 = librosa.amplitude_to_db(np.abs(librosa.stft(y1)), ref=np.max)\n\n# Spectrogram for Audio 2\nD2 = librosa.amplitude_to_db(np.abs(librosa.stft(y2)), ref=np.max)\n\n# Create subplots\nplt.figure(figsize=(16, 4))\n\n# Plot for Audio 1\nplt.subplot(1, 2, 1)\nlibrosa.display.specshow(D1, sr=sr1, x_axis='time', y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Spectrogram of Audio 1')\n\n# Plot for Audio 2\nplt.subplot(1, 2, 2)\nlibrosa.display.specshow(D2, sr=sr2, x_axis='time', y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Spectrogram of Audio 2')\n\n# Adjust layout for better visualization\nplt.tight_layout()\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.560101Z","iopub.execute_input":"2024-01-11T18:54:00.560493Z","iopub.status.idle":"2024-01-11T18:54:00.911745Z","shell.execute_reply.started":"2024-01-11T18:54:00.560462Z","shell.execute_reply":"2024-01-11T18:54:00.909265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D1 = librosa.amplitude_to_db(np.abs(librosa.stft(y3)), ref=np.max)\nD2 = librosa.amplitude_to_db(np.abs(librosa.stft(y4)), ref=np.max)\n\nplt.figure(figsize=(16, 4))\n\nplt.subplot(1, 2, 1)\nlibrosa.display.specshow(D1, sr=sr3, x_axis='time', y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Spectrogram of Audio 3')\n\nplt.subplot(1, 2, 2)\nlibrosa.display.specshow(D2, sr=sr4, x_axis='time', y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Spectrogram of Audio 4')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.912761Z","iopub.status.idle":"2024-01-11T18:54:00.913286Z","shell.execute_reply.started":"2024-01-11T18:54:00.912995Z","shell.execute_reply":"2024-01-11T18:54:00.913018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D1 = librosa.amplitude_to_db(np.abs(librosa.stft(y5)), ref=np.max)\nD2 = librosa.amplitude_to_db(np.abs(librosa.stft(y6)), ref=np.max)\n\nplt.figure(figsize=(16, 4))\n\nplt.subplot(1, 2, 1)\nlibrosa.display.specshow(D1, sr=sr5, x_axis='time', y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Spectrogram of Audio 5')\n\nplt.subplot(1, 2, 2)\nlibrosa.display.specshow(D2, sr=sr6, x_axis='time', y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Spectrogram of Audio 6')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.915181Z","iopub.status.idle":"2024-01-11T18:54:00.915845Z","shell.execute_reply.started":"2024-01-11T18:54:00.915604Z","shell.execute_reply":"2024-01-11T18:54:00.915628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D1 = librosa.amplitude_to_db(np.abs(librosa.stft(y7)), ref=np.max)\nD2 = librosa.amplitude_to_db(np.abs(librosa.stft(y8)), ref=np.max)\n\nplt.figure(figsize=(16, 4))\n\nplt.subplot(1, 2, 1)\nlibrosa.display.specshow(D1, sr=sr7, x_axis='time', y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Spectrogram of Audio 7')\n\nplt.subplot(1, 2, 2)\nlibrosa.display.specshow(D2, sr=sr8, x_axis='time', y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Spectrogram of Audio 8')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.917246Z","iopub.status.idle":"2024-01-11T18:54:00.917707Z","shell.execute_reply.started":"2024-01-11T18:54:00.917465Z","shell.execute_reply":"2024-01-11T18:54:00.917489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D1 = librosa.amplitude_to_db(np.abs(librosa.stft(y9)), ref=np.max)\nD2 = librosa.amplitude_to_db(np.abs(librosa.stft(y10)), ref=np.max)\n\nplt.figure(figsize=(16, 4))\n\nplt.subplot(1, 2, 1)\nlibrosa.display.specshow(D1, sr=sr9, x_axis='time', y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Spectrogram of Audio 9')\n\nplt.subplot(1, 2, 2)\nlibrosa.display.specshow(D2, sr=sr10, x_axis='time', y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Spectrogram of Audio 10')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.918897Z","iopub.status.idle":"2024-01-11T18:54:00.919391Z","shell.execute_reply.started":"2024-01-11T18:54:00.919153Z","shell.execute_reply":"2024-01-11T18:54:00.919176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MODEL","metadata":{}},{"cell_type":"code","source":"!pip install librosa==0.9.2","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.921090Z","iopub.status.idle":"2024-01-11T18:54:00.921568Z","shell.execute_reply.started":"2024-01-11T18:54:00.921328Z","shell.execute_reply":"2024-01-11T18:54:00.921351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Due to some modification in new version of librosa, the melspectrogram feature was not working properly that's why installed, older version of librosa to work with.","metadata":{}},{"cell_type":"code","source":"import cv2\nimport audioread\nimport logging\nimport os\nimport random\nimport time\nimport warnings\n\nimport librosa\nimport numpy as np\nimport pandas as pd\nimport soundfile as sf\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.utils.data as data\n\nfrom contextlib import contextmanager\nfrom pathlib import Path\nfrom typing import Optional\n\nfrom fastprogress import progress_bar\nfrom sklearn.metrics import f1_score\nfrom torchvision import models","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.923435Z","iopub.status.idle":"2024-01-11T18:54:00.923767Z","shell.execute_reply.started":"2024-01-11T18:54:00.923603Z","shell.execute_reply":"2024-01-11T18:54:00.923620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Utilities","metadata":{}},{"cell_type":"code","source":"def set_seed(seed: int = 42):\n    random.seed(seed)\n    np.random.seed(seed)\n    os.environ[\"PYTHONHASHSEED\"] = str(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)  # type: ignore\n    torch.backends.cudnn.deterministic = True  # type: ignore\n    torch.backends.cudnn.benchmark = True  # type: ignore\n    \n    \ndef get_logger(out_file=None):\n    logger = logging.getLogger()\n    formatter = logging.Formatter(\"%(asctime)s - %(levelname)s - %(message)s\")\n    logger.handlers = []\n    logger.setLevel(logging.INFO)\n\n    handler = logging.StreamHandler()\n    handler.setFormatter(formatter)\n    handler.setLevel(logging.INFO)\n    logger.addHandler(handler)\n\n    if out_file is not None:\n        fh = logging.FileHandler(out_file)\n        fh.setFormatter(formatter)\n        fh.setLevel(logging.INFO)\n        logger.addHandler(fh)\n    logger.info(\"logger set up\")\n    return logger\n    \n    \n@contextmanager\ndef timer(name: str, logger: Optional[logging.Logger] = None):\n    t0 = time.time()\n    msg = f\"[{name}] start\"\n    if logger is None:\n        print(msg)\n    else:\n        logger.info(msg)\n    yield\n\n    msg = f\"[{name}] done in {time.time() - t0:.2f} s\"\n    if logger is None:\n        print(msg)\n    else:\n        logger.info(msg)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.924759Z","iopub.status.idle":"2024-01-11T18:54:00.925127Z","shell.execute_reply.started":"2024-01-11T18:54:00.924936Z","shell.execute_reply":"2024-01-11T18:54:00.924952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logger = get_logger(\"main.log\")\nset_seed(1213)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.927113Z","iopub.status.idle":"2024-01-11T18:54:00.927541Z","shell.execute_reply.started":"2024-01-11T18:54:00.927317Z","shell.execute_reply":"2024-01-11T18:54:00.927339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Loading","metadata":{}},{"cell_type":"code","source":"TARGET_SR = 32000\nTEST = Path(\"../input/birdsong-recognition/test_audio\").exists()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.928842Z","iopub.status.idle":"2024-01-11T18:54:00.929208Z","shell.execute_reply.started":"2024-01-11T18:54:00.929011Z","shell.execute_reply":"2024-01-11T18:54:00.929026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if TEST:\n    DATA_DIR = Path(\"../input/birdsong-recognition/\")\nelse:\n    # dataset created by @shonenkov, thanks!\n    DATA_DIR = Path(\"../input/birdcall-check/\")\n    \n\ntest = pd.read_csv(DATA_DIR / \"test.csv\")\ntest_audio = DATA_DIR / \"test_audio\"\n\n\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.930496Z","iopub.status.idle":"2024-01-11T18:54:00.930848Z","shell.execute_reply.started":"2024-01-11T18:54:00.930662Z","shell.execute_reply":"2024-01-11T18:54:00.930678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv(\"../input/birdsong-recognition/sample_submission.csv\")\nsub.to_csv(\"submission.csv\", index=False)  # this will be overwritten if everything goes well","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.931976Z","iopub.status.idle":"2024-01-11T18:54:00.932380Z","shell.execute_reply.started":"2024-01-11T18:54:00.932179Z","shell.execute_reply":"2024-01-11T18:54:00.932215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ResNet(nn.Module):\n    def __init__(self, base_model_name: str, pretrained=False,\n                 num_classes=264):\n        super().__init__()\n        base_model = models.__getattribute__(base_model_name)(\n            pretrained=pretrained)\n        layers = list(base_model.children())[:-2]\n        layers.append(nn.AdaptiveMaxPool2d(1))\n        self.encoder = nn.Sequential(*layers)\n\n        in_features = base_model.fc.in_features\n\n        self.classifier = nn.Sequential(\n            nn.Linear(in_features, 1024), nn.ReLU(), nn.Dropout(p=0.2),\n            nn.Linear(1024, 1024), nn.ReLU(), nn.Dropout(p=0.2),\n            nn.Linear(1024, num_classes))\n\n    def forward(self, x):\n        batch_size = x.size(0)\n        x = self.encoder(x).view(batch_size, -1)\n        x = self.classifier(x)\n        multiclass_proba = F.softmax(x, dim=1)\n        multilabel_proba = F.sigmoid(x)\n        return {\n            \"logits\": x,\n            \"multiclass_proba\": multiclass_proba,\n            \"multilabel_proba\": multilabel_proba\n        }","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.933701Z","iopub.status.idle":"2024-01-11T18:54:00.934158Z","shell.execute_reply.started":"2024-01-11T18:54:00.933914Z","shell.execute_reply":"2024-01-11T18:54:00.933936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Parameters","metadata":{}},{"cell_type":"code","source":"model_config = {\n    \"base_model_name\": \"resnet50\",\n    \"pretrained\": False,\n    \"num_classes\": 264\n}\n\nmelspectrogram_parameters = {\n    \"n_mels\": 128,\n    \"fmin\": 20,\n    \"fmax\": 16000\n}\n\nweights_path = \"/kaggle/input/birdcall-resnet50-init-weights/best.pth\"","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.936258Z","iopub.status.idle":"2024-01-11T18:54:00.936572Z","shell.execute_reply.started":"2024-01-11T18:54:00.936417Z","shell.execute_reply":"2024-01-11T18:54:00.936432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\ndf = pd.read_csv(\"/kaggle/input/birdsong-recognition/train.csv\")\n\nunique_bird_names = df.ebird_code.unique()\nlabel_encoder = LabelEncoder()\nencoded_labels = label_encoder.fit_transform(unique_bird_names)\nBIRD_CODE = dict(zip(unique_bird_names, encoded_labels))\n\n#for bird_name, label in BIRD_CODE.items():\n#    print(f\"{bird_name}:{label}\")\n\nINV_BIRD_CODE = {v: k for k, v in BIRD_CODE.items()}\n#for bird_name, label in INV_BIRD_CODE.items():\n#    print(f\"{bird_name}:{label}\")","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.937914Z","iopub.status.idle":"2024-01-11T18:54:00.938352Z","shell.execute_reply.started":"2024-01-11T18:54:00.938129Z","shell.execute_reply":"2024-01-11T18:54:00.938150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define Dataset","metadata":{}},{"cell_type":"code","source":"def mono_to_color(X: np.ndarray,\n                  mean=None,\n                  std=None,\n                  norm_max=None,\n                  norm_min=None,\n                  eps=1e-6):\n    \n    # Stack X as [X,X,X]\n    X = np.stack([X, X, X], axis=-1)\n\n    # Standardize\n    mean = mean or X.mean()\n    X = X - mean\n    std = std or X.std()\n    Xstd = X / (std + eps)\n    _min, _max = Xstd.min(), Xstd.max()\n    norm_max = norm_max or _max\n    norm_min = norm_min or _min\n    if (_max - _min) > eps:\n        # Normalize to [0, 255]\n        V = Xstd\n        V[V < norm_min] = norm_min\n        V[V > norm_max] = norm_max\n        V = 255 * (V - norm_min) / (norm_max - norm_min)\n        V = V.astype(np.uint8)\n    else:\n        # Just zero\n        V = np.zeros_like(Xstd, dtype=np.uint8)\n    return V\n\n\nclass TestDataset(data.Dataset):\n    def __init__(self, df: pd.DataFrame, clip: np.ndarray,\n                 img_size=224, melspectrogram_parameters={}):\n        self.df = df\n        self.clip = clip\n        self.img_size = img_size\n        self.melspectrogram_parameters = melspectrogram_parameters\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx: int):\n        SR = 32000\n        sample = self.df.loc[idx, :]\n        site = sample.site\n        row_id = sample.row_id\n        \n        if site == \"site_3\":\n            y = self.clip.astype(np.float32)\n            len_y = len(y)\n            start = 0\n            end = SR * 5\n            images = []\n            while len_y > start:\n                y_batch = y[start:end].astype(np.float32)\n                if len(y_batch) != (SR * 5):\n                    break\n                start = end\n                end = end + SR * 5\n                \n                melspec = librosa.feature.melspectrogram(y_batch,\n                                                         sr=SR,\n                                                         **self.melspectrogram_parameters)\n                melspec = librosa.power_to_db(melspec).astype(np.float32)\n                image = mono_to_color(melspec)\n                height, width, _ = image.shape\n                image = cv2.resize(image, (int(width * self.img_size / height), self.img_size))\n                image = np.moveaxis(image, 2, 0)\n                image = (image / 255.0).astype(np.float32)\n                images.append(image)\n            images = np.asarray(images)\n            return images, row_id, site\n        else:\n            end_seconds = int(sample.seconds)\n            start_seconds = int(end_seconds - 5)\n            \n            start_index = SR * start_seconds\n            end_index = SR * end_seconds\n            \n            y = self.clip[start_index:end_index].astype(np.float32)\n\n            melspec = librosa.feature.melspectrogram(y, sr=SR, **self.melspectrogram_parameters)\n            melspec = librosa.power_to_db(melspec).astype(np.float32)\n\n            image = mono_to_color(melspec)\n            height, width, _ = image.shape\n            image = cv2.resize(image, (int(width * self.img_size / height), self.img_size))\n            image = np.moveaxis(image, 2, 0)\n            image = (image / 255.0).astype(np.float32)\n\n            return image, row_id, site","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:00.992455Z","iopub.execute_input":"2024-01-11T18:54:00.993127Z","iopub.status.idle":"2024-01-11T18:54:01.011314Z","shell.execute_reply.started":"2024-01-11T18:54:00.993099Z","shell.execute_reply":"2024-01-11T18:54:01.010444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction loop","metadata":{}},{"cell_type":"code","source":"def get_model(config: dict, weights_path: str):\n    model = ResNet(**config)\n    checkpoint = torch.load(weights_path)\n    model.load_state_dict(checkpoint[\"model_state_dict\"])\n    device = torch.device(\"cuda\")\n    model.to(device)\n    model.eval()\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:01.426489Z","iopub.execute_input":"2024-01-11T18:54:01.426823Z","iopub.status.idle":"2024-01-11T18:54:01.432244Z","shell.execute_reply.started":"2024-01-11T18:54:01.426796Z","shell.execute_reply":"2024-01-11T18:54:01.431295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prediction_for_clip(test_df: pd.DataFrame, \n                        clip: np.ndarray, \n                        model: ResNet, \n                        mel_params: dict, \n                        threshold=0.5):\n\n    dataset = TestDataset(df=test_df, \n                          clip=clip,\n                          img_size=224,\n                          melspectrogram_parameters=mel_params)\n    loader = data.DataLoader(dataset, batch_size=1, shuffle=False)\n    device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n    \n    model.eval()\n    prediction_dict = {}\n    for image, row_id, site in progress_bar(loader):\n        site = site[0]\n        row_id = row_id[0]\n        if site in {\"site_1\", \"site_2\"}:\n            image = image.to(device)\n\n            with torch.no_grad():\n                prediction = model(image)\n                proba = prediction[\"multilabel_proba\"].detach().cpu().numpy().reshape(-1)\n\n            events = proba >= threshold\n            labels = np.argwhere(events).reshape(-1).tolist()\n\n        else:\n            # to avoid prediction on large batch\n            image = image.squeeze(0)\n            batch_size = 16\n            whole_size = image.size(0)\n            if whole_size % batch_size == 0:\n                n_iter = whole_size // batch_size\n            else:\n                n_iter = whole_size // batch_size + 1\n                \n            all_events = set()\n            for batch_i in range(n_iter):\n                batch = image[batch_i * batch_size:(batch_i + 1) * batch_size]\n                if batch.ndim == 3:\n                    batch = batch.unsqueeze(0)\n\n                batch = batch.to(device)\n                with torch.no_grad():\n                    prediction = model(batch)\n                    proba = prediction[\"multilabel_proba\"].detach().cpu().numpy()\n                    \n                events = proba >= threshold\n                for i in range(len(events)):\n                    event = events[i, :]\n                    labels = np.argwhere(event).reshape(-1).tolist()\n                    for label in labels:\n                        all_events.add(label)\n                        \n            labels = list(all_events)\n        if len(labels) == 0:\n            prediction_dict[row_id] = \"nocall\"\n        else:\n            labels_str_list = list(map(lambda x: INV_BIRD_CODE[x], labels))\n            label_string = \" \".join(labels_str_list)\n            prediction_dict[row_id] = label_string\n    return prediction_dict","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:01.798917Z","iopub.execute_input":"2024-01-11T18:54:01.799803Z","iopub.status.idle":"2024-01-11T18:54:01.814175Z","shell.execute_reply.started":"2024-01-11T18:54:01.799767Z","shell.execute_reply":"2024-01-11T18:54:01.813223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prediction(test_df: pd.DataFrame,\n               test_audio: Path,\n               model_config: dict,\n               mel_params: dict,\n               weights_path: str,\n               threshold=0.5):\n    model = get_model(model_config, weights_path)\n    unique_audio_id = test_df.audio_id.unique()\n\n    warnings.filterwarnings(\"ignore\")\n    prediction_dfs = []\n    for audio_id in unique_audio_id:\n        with timer(f\"Loading {audio_id}\", logger):\n            clip, _ = librosa.load(test_audio / (audio_id + \".mp3\"),\n                                   sr=TARGET_SR,\n                                   mono=True)\n        \n        test_df_for_audio_id = test_df.query(\n            f\"audio_id == '{audio_id}'\").reset_index(drop=True)\n        with timer(f\"Prediction on {audio_id}\", logger):\n            prediction_dict = prediction_for_clip(test_df_for_audio_id,\n                                                  clip=clip,\n                                                  model=model,\n                                                  mel_params=mel_params,\n                                                  threshold=threshold)\n        row_id = list(prediction_dict.keys())\n        birds = list(prediction_dict.values())\n        prediction_df = pd.DataFrame({\n            \"row_id\": row_id,\n            \"birds\": birds\n        })\n        prediction_dfs.append(prediction_df)\n    \n    prediction_df = pd.concat(prediction_dfs, axis=0, sort=False).reset_index(drop=True)\n    return prediction_df","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:02.072727Z","iopub.execute_input":"2024-01-11T18:54:02.073120Z","iopub.status.idle":"2024-01-11T18:54:02.082650Z","shell.execute_reply.started":"2024-01-11T18:54:02.073087Z","shell.execute_reply":"2024-01-11T18:54:02.081542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction","metadata":{}},{"cell_type":"code","source":"submission = prediction(test_df=test,\n                        test_audio=test_audio,\n                        model_config=model_config,\n                        mel_params=melspectrogram_parameters,\n                        weights_path=weights_path,\n                        threshold=0.8)\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:02.966828Z","iopub.execute_input":"2024-01-11T18:54:02.967564Z","iopub.status.idle":"2024-01-11T18:54:31.273072Z","shell.execute_reply.started":"2024-01-11T18:54:02.967531Z","shell.execute_reply":"2024-01-11T18:54:31.271768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:31.276031Z","iopub.execute_input":"2024-01-11T18:54:31.276958Z","iopub.status.idle":"2024-01-11T18:54:31.294627Z","shell.execute_reply.started":"2024-01-11T18:54:31.276908Z","shell.execute_reply":"2024-01-11T18:54:31.292992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.row_id.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:31.297650Z","iopub.execute_input":"2024-01-11T18:54:31.298584Z","iopub.status.idle":"2024-01-11T18:54:31.311179Z","shell.execute_reply.started":"2024-01-11T18:54:31.298533Z","shell.execute_reply":"2024-01-11T18:54:31.309889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = pd.read_csv(\"/kaggle/working/submission.csv\")\n\n# Remove trailing numbers and duplicates from 'row_id' column\ndf1['row_id'] = df1['row_id'].replace(to_replace=r'_[0-9]+$', value='', regex=True)\ndf1 = df1.drop_duplicates()\nrows = df1[df1.duplicated(subset=['row_id'], keep=False) & (df1['birds'] == 'nocall')]\ndf_no_duplicates = df1.drop(rows.index)\n\ndf_no_duplicates = df_no_duplicates.reset_index(drop=True)\n\ndf_no_duplicates","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:54:31.314433Z","iopub.execute_input":"2024-01-11T18:54:31.315413Z","iopub.status.idle":"2024-01-11T18:54:31.337571Z","shell.execute_reply.started":"2024-01-11T18:54:31.315361Z","shell.execute_reply":"2024-01-11T18:54:31.336882Z"},"trusted":true},"execution_count":null,"outputs":[]}]}