{"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":"none","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":false}},"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","execution":{"iopub.status.busy":"2024-01-11T17:57:54.78886Z","iopub.execute_input":"2024-01-11T17:57:54.789238Z","iopub.status.idle":"2024-01-11T17:58:00.600834Z","shell.execute_reply.started":"2024-01-11T17:57:54.789206Z","shell.execute_reply":"2024-01-11T17:58:00.599405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"EDA","metadata":{}},{"cell_type":"code","source":"df=pd.read_csv(\"/kaggle/input/birdsong-recognition/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:00.603706Z","iopub.execute_input":"2024-01-11T17:58:00.605135Z","iopub.status.idle":"2024-01-11T17:58:01.101142Z","shell.execute_reply.started":"2024-01-11T17:58:00.605097Z","shell.execute_reply":"2024-01-11T17:58:01.099465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:01.103419Z","iopub.execute_input":"2024-01-11T17:58:01.103872Z","iopub.status.idle":"2024-01-11T17:58:01.928718Z","shell.execute_reply.started":"2024-01-11T17:58:01.103832Z","shell.execute_reply":"2024-01-11T17:58:01.92761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_columns', None)\npd.set_option('display.expand_frame_repr', False)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:01.931567Z","iopub.execute_input":"2024-01-11T17:58:01.931975Z","iopub.status.idle":"2024-01-11T17:58:01.938116Z","shell.execute_reply.started":"2024-01-11T17:58:01.931938Z","shell.execute_reply":"2024-01-11T17:58:01.936749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:01.939452Z","iopub.execute_input":"2024-01-11T17:58:01.939793Z","iopub.status.idle":"2024-01-11T17:58:01.953049Z","shell.execute_reply.started":"2024-01-11T17:58:01.939763Z","shell.execute_reply":"2024-01-11T17:58:01.951945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:01.954432Z","iopub.execute_input":"2024-01-11T17:58:01.954852Z","iopub.status.idle":"2024-01-11T17:58:02.016274Z","shell.execute_reply.started":"2024-01-11T17:58:01.954824Z","shell.execute_reply":"2024-01-11T17:58:02.01474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns_with_nulls = df.columns[df.isnull().any()]\nprint(columns_with_nulls)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.017637Z","iopub.execute_input":"2024-01-11T17:58:02.017906Z","iopub.status.idle":"2024-01-11T17:58:02.05514Z","shell.execute_reply.started":"2024-01-11T17:58:02.017879Z","shell.execute_reply":"2024-01-11T17:58:02.053318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.056554Z","iopub.execute_input":"2024-01-11T17:58:02.056928Z","iopub.status.idle":"2024-01-11T17:58:02.068466Z","shell.execute_reply.started":"2024-01-11T17:58:02.056897Z","shell.execute_reply":"2024-01-11T17:58:02.067451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.069879Z","iopub.execute_input":"2024-01-11T17:58:02.070389Z","iopub.status.idle":"2024-01-11T17:58:02.106997Z","shell.execute_reply.started":"2024-01-11T17:58:02.070354Z","shell.execute_reply":"2024-01-11T17:58:02.105671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['rating'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.110949Z","iopub.execute_input":"2024-01-11T17:58:02.11153Z","iopub.status.idle":"2024-01-11T17:58:02.119454Z","shell.execute_reply.started":"2024-01-11T17:58:02.111499Z","shell.execute_reply":"2024-01-11T17:58:02.118178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['playback_used'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.121384Z","iopub.execute_input":"2024-01-11T17:58:02.122131Z","iopub.status.idle":"2024-01-11T17:58:02.135864Z","shell.execute_reply.started":"2024-01-11T17:58:02.122095Z","shell.execute_reply":"2024-01-11T17:58:02.134242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['playback_used'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.137236Z","iopub.execute_input":"2024-01-11T17:58:02.138128Z","iopub.status.idle":"2024-01-11T17:58:02.150462Z","shell.execute_reply.started":"2024-01-11T17:58:02.138097Z","shell.execute_reply":"2024-01-11T17:58:02.148136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['playback_used'].fillna('no', inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.152754Z","iopub.execute_input":"2024-01-11T17:58:02.153223Z","iopub.status.idle":"2024-01-11T17:58:02.16453Z","shell.execute_reply.started":"2024-01-11T17:58:02.153185Z","shell.execute_reply":"2024-01-11T17:58:02.163404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['playback_used'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.167962Z","iopub.execute_input":"2024-01-11T17:58:02.168309Z","iopub.status.idle":"2024-01-11T17:58:02.181284Z","shell.execute_reply.started":"2024-01-11T17:58:02.168276Z","shell.execute_reply":"2024-01-11T17:58:02.180038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le = LabelEncoder()\ndf['playback_used_encoded'] = le.fit_transform(df['playback_used'])\nprint(df[['playback_used', 'playback_used_encoded']])","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.182552Z","iopub.execute_input":"2024-01-11T17:58:02.183Z","iopub.status.idle":"2024-01-11T17:58:02.20527Z","shell.execute_reply.started":"2024-01-11T17:58:02.182972Z","shell.execute_reply":"2024-01-11T17:58:02.20385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.206552Z","iopub.execute_input":"2024-01-11T17:58:02.206838Z","iopub.status.idle":"2024-01-11T17:58:02.228149Z","shell.execute_reply.started":"2024-01-11T17:58:02.206811Z","shell.execute_reply":"2024-01-11T17:58:02.227084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop('playback_used', axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.229713Z","iopub.execute_input":"2024-01-11T17:58:02.230006Z","iopub.status.idle":"2024-01-11T17:58:02.246132Z","shell.execute_reply.started":"2024-01-11T17:58:02.22998Z","shell.execute_reply":"2024-01-11T17:58:02.244832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.247291Z","iopub.execute_input":"2024-01-11T17:58:02.247626Z","iopub.status.idle":"2024-01-11T17:58:02.281014Z","shell.execute_reply.started":"2024-01-11T17:58:02.2476Z","shell.execute_reply":"2024-01-11T17:58:02.27912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['ebird_code'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.282349Z","iopub.execute_input":"2024-01-11T17:58:02.282702Z","iopub.status.idle":"2024-01-11T17:58:02.296649Z","shell.execute_reply.started":"2024-01-11T17:58:02.282671Z","shell.execute_reply":"2024-01-11T17:58:02.295556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['ebird_code'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.29825Z","iopub.execute_input":"2024-01-11T17:58:02.299441Z","iopub.status.idle":"2024-01-11T17:58:02.312322Z","shell.execute_reply.started":"2024-01-11T17:58:02.299377Z","shell.execute_reply":"2024-01-11T17:58:02.310733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['channels'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.315485Z","iopub.execute_input":"2024-01-11T17:58:02.315899Z","iopub.status.idle":"2024-01-11T17:58:02.327859Z","shell.execute_reply.started":"2024-01-11T17:58:02.315838Z","shell.execute_reply":"2024-01-11T17:58:02.326432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['channels'] = df['channels'].astype(str).str[0].astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.329792Z","iopub.execute_input":"2024-01-11T17:58:02.330306Z","iopub.status.idle":"2024-01-11T17:58:02.358053Z","shell.execute_reply.started":"2024-01-11T17:58:02.330265Z","shell.execute_reply":"2024-01-11T17:58:02.356888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['channels'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.359158Z","iopub.execute_input":"2024-01-11T17:58:02.361212Z","iopub.status.idle":"2024-01-11T17:58:02.37883Z","shell.execute_reply.started":"2024-01-11T17:58:02.36116Z","shell.execute_reply":"2024-01-11T17:58:02.3774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['date'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.38012Z","iopub.execute_input":"2024-01-11T17:58:02.380885Z","iopub.status.idle":"2024-01-11T17:58:02.394977Z","shell.execute_reply.started":"2024-01-11T17:58:02.380843Z","shell.execute_reply":"2024-01-11T17:58:02.393831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['date'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.396664Z","iopub.execute_input":"2024-01-11T17:58:02.397914Z","iopub.status.idle":"2024-01-11T17:58:02.411597Z","shell.execute_reply.started":"2024-01-11T17:58:02.397856Z","shell.execute_reply":"2024-01-11T17:58:02.410434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['year'] = df['date'].apply(lambda x: x.split('-')[0]).astype(int)\ndf['month'] = df['date'].apply(lambda x: x.split('-')[1]).astype(int)\ndf['day_of_month'] = df['date'].apply(lambda x: x.split('-')[2]).astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.413097Z","iopub.execute_input":"2024-01-11T17:58:02.414209Z","iopub.status.idle":"2024-01-11T17:58:02.454173Z","shell.execute_reply.started":"2024-01-11T17:58:02.414159Z","shell.execute_reply":"2024-01-11T17:58:02.452655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop('date', axis=1, inplace=True)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.455546Z","iopub.execute_input":"2024-01-11T17:58:02.455925Z","iopub.status.idle":"2024-01-11T17:58:02.493417Z","shell.execute_reply.started":"2024-01-11T17:58:02.455888Z","shell.execute_reply":"2024-01-11T17:58:02.492416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.502179Z","iopub.execute_input":"2024-01-11T17:58:02.50256Z","iopub.status.idle":"2024-01-11T17:58:02.539542Z","shell.execute_reply.started":"2024-01-11T17:58:02.502529Z","shell.execute_reply":"2024-01-11T17:58:02.538269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['pitch'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.540719Z","iopub.execute_input":"2024-01-11T17:58:02.541105Z","iopub.status.idle":"2024-01-11T17:58:02.548429Z","shell.execute_reply.started":"2024-01-11T17:58:02.541075Z","shell.execute_reply":"2024-01-11T17:58:02.547582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['pitch'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.550219Z","iopub.execute_input":"2024-01-11T17:58:02.550861Z","iopub.status.idle":"2024-01-11T17:58:02.566734Z","shell.execute_reply.started":"2024-01-11T17:58:02.550819Z","shell.execute_reply":"2024-01-11T17:58:02.565258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['duration'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.568819Z","iopub.execute_input":"2024-01-11T17:58:02.570092Z","iopub.status.idle":"2024-01-11T17:58:02.579045Z","shell.execute_reply.started":"2024-01-11T17:58:02.570046Z","shell.execute_reply":"2024-01-11T17:58:02.577113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['speed'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.580171Z","iopub.execute_input":"2024-01-11T17:58:02.58053Z","iopub.status.idle":"2024-01-11T17:58:02.595583Z","shell.execute_reply.started":"2024-01-11T17:58:02.580499Z","shell.execute_reply":"2024-01-11T17:58:02.59426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['speed'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.596925Z","iopub.execute_input":"2024-01-11T17:58:02.597235Z","iopub.status.idle":"2024-01-11T17:58:02.608587Z","shell.execute_reply.started":"2024-01-11T17:58:02.597207Z","shell.execute_reply":"2024-01-11T17:58:02.607404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['species'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.610264Z","iopub.execute_input":"2024-01-11T17:58:02.61095Z","iopub.status.idle":"2024-01-11T17:58:02.623089Z","shell.execute_reply.started":"2024-01-11T17:58:02.610912Z","shell.execute_reply":"2024-01-11T17:58:02.621425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['sci_name'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.624384Z","iopub.execute_input":"2024-01-11T17:58:02.62574Z","iopub.status.idle":"2024-01-11T17:58:02.636237Z","shell.execute_reply.started":"2024-01-11T17:58:02.625703Z","shell.execute_reply":"2024-01-11T17:58:02.635315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['number_of_notes'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.63801Z","iopub.execute_input":"2024-01-11T17:58:02.638691Z","iopub.status.idle":"2024-01-11T17:58:02.649374Z","shell.execute_reply.started":"2024-01-11T17:58:02.638654Z","shell.execute_reply":"2024-01-11T17:58:02.648295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['title'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.650358Z","iopub.execute_input":"2024-01-11T17:58:02.650832Z","iopub.status.idle":"2024-01-11T17:58:02.671527Z","shell.execute_reply.started":"2024-01-11T17:58:02.650805Z","shell.execute_reply":"2024-01-11T17:58:02.669472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop('title', axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.672959Z","iopub.execute_input":"2024-01-11T17:58:02.674073Z","iopub.status.idle":"2024-01-11T17:58:02.692163Z","shell.execute_reply.started":"2024-01-11T17:58:02.674035Z","shell.execute_reply":"2024-01-11T17:58:02.688845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['secondary_labels'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.693751Z","iopub.execute_input":"2024-01-11T17:58:02.69441Z","iopub.status.idle":"2024-01-11T17:58:02.708775Z","shell.execute_reply.started":"2024-01-11T17:58:02.694376Z","shell.execute_reply":"2024-01-11T17:58:02.707876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['bird_seen'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.709981Z","iopub.execute_input":"2024-01-11T17:58:02.71053Z","iopub.status.idle":"2024-01-11T17:58:02.721906Z","shell.execute_reply.started":"2024-01-11T17:58:02.710498Z","shell.execute_reply":"2024-01-11T17:58:02.721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['bird_seen'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.722885Z","iopub.execute_input":"2024-01-11T17:58:02.72376Z","iopub.status.idle":"2024-01-11T17:58:02.739443Z","shell.execute_reply.started":"2024-01-11T17:58:02.723729Z","shell.execute_reply":"2024-01-11T17:58:02.738417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['sci_name'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.740682Z","iopub.execute_input":"2024-01-11T17:58:02.741145Z","iopub.status.idle":"2024-01-11T17:58:02.75215Z","shell.execute_reply.started":"2024-01-11T17:58:02.741115Z","shell.execute_reply":"2024-01-11T17:58:02.751357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['location'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.753752Z","iopub.execute_input":"2024-01-11T17:58:02.754528Z","iopub.status.idle":"2024-01-11T17:58:02.767094Z","shell.execute_reply.started":"2024-01-11T17:58:02.754492Z","shell.execute_reply":"2024-01-11T17:58:02.766002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['latitude'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.769229Z","iopub.execute_input":"2024-01-11T17:58:02.770686Z","iopub.status.idle":"2024-01-11T17:58:02.780838Z","shell.execute_reply.started":"2024-01-11T17:58:02.770644Z","shell.execute_reply":"2024-01-11T17:58:02.779901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df[df['latitude'] != 'Not specified']\ndf = df[df['longitude'] != 'Not specified']","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.78215Z","iopub.execute_input":"2024-01-11T17:58:02.782568Z","iopub.status.idle":"2024-01-11T17:58:02.830064Z","shell.execute_reply.started":"2024-01-11T17:58:02.782534Z","shell.execute_reply":"2024-01-11T17:58:02.82851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['latitude'] = df['latitude'].astype(float)\ndf['longitude'] = df['longitude'].astype(float)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.834469Z","iopub.execute_input":"2024-01-11T17:58:02.835123Z","iopub.status.idle":"2024-01-11T17:58:02.84667Z","shell.execute_reply.started":"2024-01-11T17:58:02.835081Z","shell.execute_reply":"2024-01-11T17:58:02.845542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.848062Z","iopub.execute_input":"2024-01-11T17:58:02.849122Z","iopub.status.idle":"2024-01-11T17:58:02.900614Z","shell.execute_reply.started":"2024-01-11T17:58:02.849082Z","shell.execute_reply":"2024-01-11T17:58:02.899623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['sampling_rate'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.902554Z","iopub.execute_input":"2024-01-11T17:58:02.902952Z","iopub.status.idle":"2024-01-11T17:58:02.91457Z","shell.execute_reply.started":"2024-01-11T17:58:02.902921Z","shell.execute_reply":"2024-01-11T17:58:02.913544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['sampling_rate'] = df['sampling_rate'].apply(lambda x:x.split(' ')[0]).astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.915844Z","iopub.execute_input":"2024-01-11T17:58:02.916564Z","iopub.status.idle":"2024-01-11T17:58:02.933003Z","shell.execute_reply.started":"2024-01-11T17:58:02.916533Z","shell.execute_reply":"2024-01-11T17:58:02.93105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['sampling_rate'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.934669Z","iopub.execute_input":"2024-01-11T17:58:02.935015Z","iopub.status.idle":"2024-01-11T17:58:02.944045Z","shell.execute_reply.started":"2024-01-11T17:58:02.934991Z","shell.execute_reply":"2024-01-11T17:58:02.943171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['type'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.946569Z","iopub.execute_input":"2024-01-11T17:58:02.947577Z","iopub.status.idle":"2024-01-11T17:58:02.959284Z","shell.execute_reply.started":"2024-01-11T17:58:02.947536Z","shell.execute_reply":"2024-01-11T17:58:02.958021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['type'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.960936Z","iopub.execute_input":"2024-01-11T17:58:02.961709Z","iopub.status.idle":"2024-01-11T17:58:02.972456Z","shell.execute_reply.started":"2024-01-11T17:58:02.961674Z","shell.execute_reply":"2024-01-11T17:58:02.971089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"adjusted_type = df['type'].apply(lambda x:x.split(',')).reset_index().explode(\"type\")\nadjusted_type = adjusted_type['type'].apply(lambda x: x.strip().lower()).reset_index()\nadjusted_type['type'] = adjusted_type['type'].replace('calls','call')\nadjusted_type['type'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:02.973732Z","iopub.execute_input":"2024-01-11T17:58:02.975114Z","iopub.status.idle":"2024-01-11T17:58:03.031148Z","shell.execute_reply.started":"2024-01-11T17:58:02.975037Z","shell.execute_reply":"2024-01-11T17:58:03.030005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:03.032529Z","iopub.execute_input":"2024-01-11T17:58:03.033038Z","iopub.status.idle":"2024-01-11T17:58:03.063182Z","shell.execute_reply.started":"2024-01-11T17:58:03.033007Z","shell.execute_reply":"2024-01-11T17:58:03.061257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['elevation'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:03.064441Z","iopub.execute_input":"2024-01-11T17:58:03.064812Z","iopub.status.idle":"2024-01-11T17:58:03.077306Z","shell.execute_reply.started":"2024-01-11T17:58:03.064781Z","shell.execute_reply":"2024-01-11T17:58:03.07575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['elevation'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:03.078857Z","iopub.execute_input":"2024-01-11T17:58:03.079294Z","iopub.status.idle":"2024-01-11T17:58:03.097026Z","shell.execute_reply.started":"2024-01-11T17:58:03.079256Z","shell.execute_reply":"2024-01-11T17:58:03.095447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['elevation'] = df['elevation'].replace(['? m','Unknown m', ' m'], '99999')\n","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:03.098023Z","iopub.execute_input":"2024-01-11T17:58:03.098403Z","iopub.status.idle":"2024-01-11T17:58:03.110862Z","shell.execute_reply.started":"2024-01-11T17:58:03.098369Z","shell.execute_reply":"2024-01-11T17:58:03.109905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['elevation'] = df['elevation'].str.replace(',','')","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:03.111893Z","iopub.execute_input":"2024-01-11T17:58:03.112281Z","iopub.status.idle":"2024-01-11T17:58:03.130025Z","shell.execute_reply.started":"2024-01-11T17:58:03.112248Z","shell.execute_reply":"2024-01-11T17:58:03.128606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['elevation'] = df['elevation'].str.replace('.','')","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:03.131839Z","iopub.execute_input":"2024-01-11T17:58:03.132258Z","iopub.status.idle":"2024-01-11T17:58:03.15105Z","shell.execute_reply.started":"2024-01-11T17:58:03.132222Z","shell.execute_reply":"2024-01-11T17:58:03.149866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['elevation'] = pd.to_numeric(df['elevation'],errors='coerce')\nsum_elevation = 0\ncount_valid_values = 0\n\nfor index, row in df.iterrows():\n  if row['elevation']!= 99999:\n    sum_elevation != row['elevation']\n    count_valid_values +=1\n    mean_elevation = sum_elevation / count_valid_values\n    mean_elevation","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:03.152164Z","iopub.execute_input":"2024-01-11T17:58:03.153036Z","iopub.status.idle":"2024-01-11T17:58:04.355517Z","shell.execute_reply.started":"2024-01-11T17:58:03.153005Z","shell.execute_reply":"2024-01-11T17:58:04.354469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['elevation'].mean()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.356653Z","iopub.execute_input":"2024-01-11T17:58:04.3574Z","iopub.status.idle":"2024-01-11T17:58:04.364106Z","shell.execute_reply.started":"2024-01-11T17:58:04.357366Z","shell.execute_reply":"2024-01-11T17:58:04.362523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['elevation'] = df['elevation'].replace(['99999'],mean_elevation)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.365849Z","iopub.execute_input":"2024-01-11T17:58:04.366271Z","iopub.status.idle":"2024-01-11T17:58:04.378404Z","shell.execute_reply.started":"2024-01-11T17:58:04.366234Z","shell.execute_reply":"2024-01-11T17:58:04.37724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop('description', axis=1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.379602Z","iopub.execute_input":"2024-01-11T17:58:04.380749Z","iopub.status.idle":"2024-01-11T17:58:04.399139Z","shell.execute_reply.started":"2024-01-11T17:58:04.380716Z","shell.execute_reply":"2024-01-11T17:58:04.39764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['bitrate_of_mp3'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.400839Z","iopub.execute_input":"2024-01-11T17:58:04.401662Z","iopub.status.idle":"2024-01-11T17:58:04.416742Z","shell.execute_reply.started":"2024-01-11T17:58:04.401626Z","shell.execute_reply":"2024-01-11T17:58:04.415788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['bitrate_of_mp3'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.418114Z","iopub.execute_input":"2024-01-11T17:58:04.418452Z","iopub.status.idle":"2024-01-11T17:58:04.434095Z","shell.execute_reply.started":"2024-01-11T17:58:04.418416Z","shell.execute_reply":"2024-01-11T17:58:04.433411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[df['bitrate_of_mp3'] == 0]","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.435301Z","iopub.execute_input":"2024-01-11T17:58:04.435789Z","iopub.status.idle":"2024-01-11T17:58:04.459387Z","shell.execute_reply.started":"2024-01-11T17:58:04.435756Z","shell.execute_reply":"2024-01-11T17:58:04.458414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop(df[df['bitrate_of_mp3']==3].index, inplace =True)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.460774Z","iopub.execute_input":"2024-01-11T17:58:04.461136Z","iopub.status.idle":"2024-01-11T17:58:04.479995Z","shell.execute_reply.started":"2024-01-11T17:58:04.461102Z","shell.execute_reply":"2024-01-11T17:58:04.478355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[df['bitrate_of_mp3'].isna()]","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.48128Z","iopub.execute_input":"2024-01-11T17:58:04.482655Z","iopub.status.idle":"2024-01-11T17:58:04.515556Z","shell.execute_reply.started":"2024-01-11T17:58:04.482613Z","shell.execute_reply":"2024-01-11T17:58:04.514064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['bitrate_of_mp3'] =df['bitrate_of_mp3'].fillna('0')","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.517477Z","iopub.execute_input":"2024-01-11T17:58:04.517829Z","iopub.status.idle":"2024-01-11T17:58:04.526432Z","shell.execute_reply.started":"2024-01-11T17:58:04.517798Z","shell.execute_reply":"2024-01-11T17:58:04.525153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['bitrate_of_mp3'] = df['bitrate_of_mp3'].apply(lambda x: x.split(' ')[0]).astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.528111Z","iopub.execute_input":"2024-01-11T17:58:04.529226Z","iopub.status.idle":"2024-01-11T17:58:04.54876Z","shell.execute_reply.started":"2024-01-11T17:58:04.529126Z","shell.execute_reply":"2024-01-11T17:58:04.547011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m = df['bitrate_of_mp3'].mean()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.549782Z","iopub.execute_input":"2024-01-11T17:58:04.550107Z","iopub.status.idle":"2024-01-11T17:58:04.558096Z","shell.execute_reply.started":"2024-01-11T17:58:04.550042Z","shell.execute_reply":"2024-01-11T17:58:04.557303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['bitrate_of_mp3'] = df['bitrate_of_mp3'].replace(0,m)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.558968Z","iopub.execute_input":"2024-01-11T17:58:04.559257Z","iopub.status.idle":"2024-01-11T17:58:04.573586Z","shell.execute_reply.started":"2024-01-11T17:58:04.559231Z","shell.execute_reply":"2024-01-11T17:58:04.572137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.575077Z","iopub.execute_input":"2024-01-11T17:58:04.575425Z","iopub.status.idle":"2024-01-11T17:58:04.605739Z","shell.execute_reply.started":"2024-01-11T17:58:04.575393Z","shell.execute_reply":"2024-01-11T17:58:04.604405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['file_type'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.606803Z","iopub.execute_input":"2024-01-11T17:58:04.607143Z","iopub.status.idle":"2024-01-11T17:58:04.61989Z","shell.execute_reply.started":"2024-01-11T17:58:04.607115Z","shell.execute_reply":"2024-01-11T17:58:04.619024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['volume'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.62083Z","iopub.execute_input":"2024-01-11T17:58:04.62122Z","iopub.status.idle":"2024-01-11T17:58:04.637196Z","shell.execute_reply.started":"2024-01-11T17:58:04.621186Z","shell.execute_reply":"2024-01-11T17:58:04.636227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['background'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.638973Z","iopub.execute_input":"2024-01-11T17:58:04.639504Z","iopub.status.idle":"2024-01-11T17:58:04.653057Z","shell.execute_reply.started":"2024-01-11T17:58:04.639472Z","shell.execute_reply":"2024-01-11T17:58:04.652035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['background'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.654241Z","iopub.execute_input":"2024-01-11T17:58:04.654548Z","iopub.status.idle":"2024-01-11T17:58:04.667019Z","shell.execute_reply.started":"2024-01-11T17:58:04.654522Z","shell.execute_reply":"2024-01-11T17:58:04.665064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop('background', axis=1, inplace= True)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.668166Z","iopub.execute_input":"2024-01-11T17:58:04.669199Z","iopub.status.idle":"2024-01-11T17:58:04.680315Z","shell.execute_reply.started":"2024-01-11T17:58:04.669165Z","shell.execute_reply":"2024-01-11T17:58:04.678888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['xc_id'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.681704Z","iopub.execute_input":"2024-01-11T17:58:04.682494Z","iopub.status.idle":"2024-01-11T17:58:04.694917Z","shell.execute_reply.started":"2024-01-11T17:58:04.68246Z","shell.execute_reply":"2024-01-11T17:58:04.693427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop('xc_id',axis=1, inplace= True)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.696739Z","iopub.execute_input":"2024-01-11T17:58:04.697424Z","iopub.status.idle":"2024-01-11T17:58:04.714553Z","shell.execute_reply.started":"2024-01-11T17:58:04.697384Z","shell.execute_reply":"2024-01-11T17:58:04.712501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['url'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.715875Z","iopub.execute_input":"2024-01-11T17:58:04.716248Z","iopub.status.idle":"2024-01-11T17:58:04.736081Z","shell.execute_reply.started":"2024-01-11T17:58:04.716211Z","shell.execute_reply":"2024-01-11T17:58:04.734877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop('url', axis=1, inplace= True)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.749184Z","iopub.execute_input":"2024-01-11T17:58:04.7497Z","iopub.status.idle":"2024-01-11T17:58:04.763498Z","shell.execute_reply.started":"2024-01-11T17:58:04.749668Z","shell.execute_reply":"2024-01-11T17:58:04.762107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['country'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.765177Z","iopub.execute_input":"2024-01-11T17:58:04.765883Z","iopub.status.idle":"2024-01-11T17:58:04.780668Z","shell.execute_reply.started":"2024-01-11T17:58:04.765846Z","shell.execute_reply":"2024-01-11T17:58:04.779916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['author'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.78251Z","iopub.execute_input":"2024-01-11T17:58:04.782942Z","iopub.status.idle":"2024-01-11T17:58:04.797613Z","shell.execute_reply.started":"2024-01-11T17:58:04.78289Z","shell.execute_reply":"2024-01-11T17:58:04.796097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['author'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.799175Z","iopub.execute_input":"2024-01-11T17:58:04.799625Z","iopub.status.idle":"2024-01-11T17:58:04.808277Z","shell.execute_reply.started":"2024-01-11T17:58:04.79959Z","shell.execute_reply":"2024-01-11T17:58:04.807231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['recordist'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.809841Z","iopub.execute_input":"2024-01-11T17:58:04.810383Z","iopub.status.idle":"2024-01-11T17:58:04.820572Z","shell.execute_reply.started":"2024-01-11T17:58:04.810316Z","shell.execute_reply":"2024-01-11T17:58:04.819389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[df['author'] != df['recordist']]","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.823113Z","iopub.execute_input":"2024-01-11T17:58:04.823611Z","iopub.status.idle":"2024-01-11T17:58:04.842206Z","shell.execute_reply.started":"2024-01-11T17:58:04.823572Z","shell.execute_reply":"2024-01-11T17:58:04.841208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop('author', axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.843663Z","iopub.execute_input":"2024-01-11T17:58:04.844217Z","iopub.status.idle":"2024-01-11T17:58:04.861064Z","shell.execute_reply.started":"2024-01-11T17:58:04.844183Z","shell.execute_reply":"2024-01-11T17:58:04.860036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['primary_label'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.862165Z","iopub.execute_input":"2024-01-11T17:58:04.862584Z","iopub.status.idle":"2024-01-11T17:58:04.878457Z","shell.execute_reply.started":"2024-01-11T17:58:04.862553Z","shell.execute_reply":"2024-01-11T17:58:04.877143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['length'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.879631Z","iopub.execute_input":"2024-01-11T17:58:04.880737Z","iopub.status.idle":"2024-01-11T17:58:04.89003Z","shell.execute_reply.started":"2024-01-11T17:58:04.880699Z","shell.execute_reply":"2024-01-11T17:58:04.888942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['min_value'] = df['length'].str.extract(r'(\\d+)')\ndf['min_value'] = pd.to_numeric(df['min_value'],errors='coerce')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.891319Z","iopub.execute_input":"2024-01-11T17:58:04.891697Z","iopub.status.idle":"2024-01-11T17:58:04.947757Z","shell.execute_reply.started":"2024-01-11T17:58:04.891671Z","shell.execute_reply":"2024-01-11T17:58:04.946672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['max_value'] = df['length'].str.extract(r'(\\d+)')\ndf['max_value'] = pd.to_numeric(df['max_value'],errors='coerce')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:04.949129Z","iopub.execute_input":"2024-01-11T17:58:04.949788Z","iopub.status.idle":"2024-01-11T17:58:05.004628Z","shell.execute_reply.started":"2024-01-11T17:58:04.949748Z","shell.execute_reply":"2024-01-11T17:58:05.002964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['min_value'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:05.006111Z","iopub.execute_input":"2024-01-11T17:58:05.008294Z","iopub.status.idle":"2024-01-11T17:58:05.016769Z","shell.execute_reply.started":"2024-01-11T17:58:05.008245Z","shell.execute_reply":"2024-01-11T17:58:05.014489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['max_value'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:05.020324Z","iopub.execute_input":"2024-01-11T17:58:05.020692Z","iopub.status.idle":"2024-01-11T17:58:05.032681Z","shell.execute_reply.started":"2024-01-11T17:58:05.020662Z","shell.execute_reply":"2024-01-11T17:58:05.031244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['time'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:05.033844Z","iopub.execute_input":"2024-01-11T17:58:05.034457Z","iopub.status.idle":"2024-01-11T17:58:05.048533Z","shell.execute_reply.started":"2024-01-11T17:58:05.034425Z","shell.execute_reply":"2024-01-11T17:58:05.04651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop('time', axis=1, inplace= True)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:05.050178Z","iopub.execute_input":"2024-01-11T17:58:05.051527Z","iopub.status.idle":"2024-01-11T17:58:05.062776Z","shell.execute_reply.started":"2024-01-11T17:58:05.051484Z","shell.execute_reply":"2024-01-11T17:58:05.061726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['license'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:05.063798Z","iopub.execute_input":"2024-01-11T17:58:05.064887Z","iopub.status.idle":"2024-01-11T17:58:05.079001Z","shell.execute_reply.started":"2024-01-11T17:58:05.064852Z","shell.execute_reply":"2024-01-11T17:58:05.078362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le = LabelEncoder()\ndf['license_encoded'] = le.fit_transform(df['license'])\nencoded_unique_values = df['license_encoded'].unique()\nlabel_swaping = dict(zip(df['license'], df['license_encoded']))","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:05.080192Z","iopub.execute_input":"2024-01-11T17:58:05.081004Z","iopub.status.idle":"2024-01-11T17:58:05.097303Z","shell.execute_reply.started":"2024-01-11T17:58:05.080976Z","shell.execute_reply":"2024-01-11T17:58:05.09602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop('license', axis=1, inplace= True)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:05.099056Z","iopub.execute_input":"2024-01-11T17:58:05.100965Z","iopub.status.idle":"2024-01-11T17:58:05.11806Z","shell.execute_reply.started":"2024-01-11T17:58:05.100921Z","shell.execute_reply":"2024-01-11T17:58:05.116352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:05.119462Z","iopub.execute_input":"2024-01-11T17:58:05.12039Z","iopub.status.idle":"2024-01-11T17:58:05.145925Z","shell.execute_reply.started":"2024-01-11T17:58:05.120326Z","shell.execute_reply":"2024-01-11T17:58:05.144521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:05.148558Z","iopub.execute_input":"2024-01-11T17:58:05.149636Z","iopub.status.idle":"2024-01-11T17:58:05.205267Z","shell.execute_reply.started":"2024-01-11T17:58:05.149597Z","shell.execute_reply":"2024-01-11T17:58:05.203892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(style=\"white\")\nplt.figure(figsize=(10,6))\n\nsns.countplot(x='rating', data=df, order=df['rating'].value_counts().index,palette='viridis')\n\nplt.title('Distribution Of Ratings')\nplt.xlabel('Rating')\nplt.ylabel('Count')\n\nplt.xticks(rotation=45)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:05.2067Z","iopub.execute_input":"2024-01-11T17:58:05.207106Z","iopub.status.idle":"2024-01-11T17:58:05.552555Z","shell.execute_reply.started":"2024-01-11T17:58:05.207075Z","shell.execute_reply":"2024-01-11T17:58:05.551663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(style=\"ticks\")\nplt.figure(figsize=(10,6))\n\nsns.countplot(y='playback_used_encoded', data=df, palette='Set2')\n\nplt.title('Distribution Of Playback Used')\nplt.xlabel('Count')\nplt.ylabel('Playback Used')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:05.554277Z","iopub.execute_input":"2024-01-11T17:58:05.554843Z","iopub.status.idle":"2024-01-11T17:58:05.713166Z","shell.execute_reply.started":"2024-01-11T17:58:05.554809Z","shell.execute_reply":"2024-01-11T17:58:05.712305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,6))\n\nsns.countplot(x='rating', hue='playback_used_encoded', data=df, palette='pastel')\n\nplt.title('Ratings Versus Playback Used')\nplt.xlabel('Rating')\nplt.ylabel('Count')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:05.714528Z","iopub.execute_input":"2024-01-11T17:58:05.715048Z","iopub.status.idle":"2024-01-11T17:58:06.013481Z","shell.execute_reply.started":"2024-01-11T17:58:05.715006Z","shell.execute_reply":"2024-01-11T17:58:06.012139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"values_count = df['number_of_notes'].value_counts()\n\ncustom_colors = [(239/255, 71/255, 111/255, 1),\n                 (247/255,140/255,107/255, 1),\n                 (255/255, 209/255, 102//255, 1),\n                 (6/255, 214/255, 160/255, 1),\n                 (17/255, 138/255, 178/255, 1)]\nplt.figure(figsize=(6,6))\nplt.pie(values_count, labels= None, autopct='%1.1f%%', startangle=90, colors=custom_colors, wedgeprops=dict(width=0.3))\n\nplt.gca().set_facecolor('#07384C')\n\nplt.title(f'Distribution of number of notes', fontsize=15)\n\nplt.legend(values_count.index, title='categories', loc='upper right',bbox_to_anchor=(1,0,0.5,1))\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:06.014548Z","iopub.execute_input":"2024-01-11T17:58:06.014831Z","iopub.status.idle":"2024-01-11T17:58:06.23731Z","shell.execute_reply.started":"2024-01-11T17:58:06.014805Z","shell.execute_reply":"2024-01-11T17:58:06.236502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"FE","metadata":{}},{"cell_type":"code","source":"import librosa # for audio analysis\nimport librosa.display\nimport soundfile as sf # for reading and writing sound files\nimport IPython.display as ipd","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:06.238641Z","iopub.execute_input":"2024-01-11T17:58:06.239128Z","iopub.status.idle":"2024-01-11T17:58:06.314315Z","shell.execute_reply.started":"2024-01-11T17:58:06.239094Z","shell.execute_reply":"2024-01-11T17:58:06.312493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio1_1 = \"/kaggle/input/birdsong-recognition/train_audio/canwar/XC250587.mp3\"\naudio1_2 = \"/kaggle/input/birdsong-recognition/train_audio/canwar/XC421848.mp3\"\naudio2_1 = \"/kaggle/input/birdsong-recognition/train_audio/grnher/XC372647.mp3\"\naudio2_2 = \"/kaggle/input/birdsong-recognition/train_audio/grnher/XC218817.mp3\"\naudio3_1 = \"/kaggle/input/birdsong-recognition/train_audio/buggna/XC455035.mp3\"\naudio3_2 = \"/kaggle/input/birdsong-recognition/train_audio/buggna/XC302322.mp3\"\naudio4_1 = \"/kaggle/input/birdsong-recognition/train_audio/pilwoo/XC156471.mp3\"\naudio4_2 = \"/kaggle/input/birdsong-recognition/train_audio/pilwoo/XC113384.mp3\"\naudio5_1 = \"/kaggle/input/birdsong-recognition/train_audio/wesgre/XC387824.mp3\"\naudio5_2 = \"/kaggle/input/birdsong-recognition/train_audio/wesgre/XC292677.mp3\"","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:06.31658Z","iopub.execute_input":"2024-01-11T17:58:06.316963Z","iopub.status.idle":"2024-01-11T17:58:06.323858Z","shell.execute_reply.started":"2024-01-11T17:58:06.316931Z","shell.execute_reply":"2024-01-11T17:58:06.322253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y1, sr1 = librosa.load(audio1_1) # y1 = np array of audio signals; sr = sample rate (no. of data points (samples) played back each second -- Hz)\ny2, sr2 = librosa.load(audio1_2)\ny3, sr3 = librosa.load(audio2_1)\ny4, sr4 = librosa.load(audio2_2)\ny5, sr5 = librosa.load(audio3_1)\ny6, sr6 = librosa.load(audio3_2)\ny7, sr7 = librosa.load(audio4_1)\ny8, sr8 = librosa.load(audio4_2)\ny9, sr9 = librosa.load(audio5_1)\ny10, sr10 = librosa.load(audio5_2)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:06.325054Z","iopub.execute_input":"2024-01-11T17:58:06.326833Z","iopub.status.idle":"2024-01-11T17:58:17.227443Z","shell.execute_reply.started":"2024-01-11T17:58:06.326775Z","shell.execute_reply":"2024-01-11T17:58:17.225847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D = librosa.amplitude_to_db(np.abs(librosa.stft(y1)), ref=np.max)\n\n# Plot the spectrogram\nplt.figure(figsize=(8, 4))\nlibrosa.display.specshow(D, sr=sr1, x_axis='time', y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Spectrogram of the Audio Signal')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:17.229406Z","iopub.execute_input":"2024-01-11T17:58:17.230131Z","iopub.status.idle":"2024-01-11T17:58:18.400951Z","shell.execute_reply.started":"2024-01-11T17:58:17.23009Z","shell.execute_reply":"2024-01-11T17:58:18.39961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D = librosa.amplitude_to_db(np.abs(librosa.stft(y2)), ref=np.max)\n\n# Plot the spectrogram\nplt.figure(figsize=(8, 4))\nlibrosa.display.specshow(D, sr=sr2, x_axis='time', y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Spectrogram of the Audio Signal')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:18.403048Z","iopub.execute_input":"2024-01-11T17:58:18.403708Z","iopub.status.idle":"2024-01-11T17:58:19.23384Z","shell.execute_reply.started":"2024-01-11T17:58:18.403673Z","shell.execute_reply":"2024-01-11T17:58:19.232315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D = librosa.amplitude_to_db(np.abs(librosa.stft(y3)), ref=np.max)\n\n# Plot the spectrogram\nplt.figure(figsize=(8, 4))\nlibrosa.display.specshow(D, sr=sr3, x_axis='time', y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Spectrogram of the Audio Signal')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:19.23539Z","iopub.execute_input":"2024-01-11T17:58:19.236589Z","iopub.status.idle":"2024-01-11T17:58:19.774952Z","shell.execute_reply.started":"2024-01-11T17:58:19.236541Z","shell.execute_reply":"2024-01-11T17:58:19.772294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D = librosa.amplitude_to_db(np.abs(librosa.stft(y4)), ref=np.max)\n\n# Plot the spectrogram\nplt.figure(figsize=(8, 4))\nlibrosa.display.specshow(D, sr=sr4, x_axis='time', y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Spectrogram of the Audio Signal')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:19.776173Z","iopub.execute_input":"2024-01-11T17:58:19.776603Z","iopub.status.idle":"2024-01-11T17:58:20.465628Z","shell.execute_reply.started":"2024-01-11T17:58:19.776562Z","shell.execute_reply":"2024-01-11T17:58:20.464565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D = librosa.amplitude_to_db(np.abs(librosa.stft(y5)), ref=np.max)\n\n# Plot the spectrogram\nplt.figure(figsize=(8, 4))\nlibrosa.display.specshow(D, sr=sr5, x_axis='time', y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Spectrogram of the Audio Signal')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:20.466805Z","iopub.execute_input":"2024-01-11T17:58:20.467118Z","iopub.status.idle":"2024-01-11T17:58:21.063691Z","shell.execute_reply.started":"2024-01-11T17:58:20.46709Z","shell.execute_reply":"2024-01-11T17:58:21.062503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D = librosa.amplitude_to_db(np.abs(librosa.stft(y6)), ref=np.max)\n\n# Plot the spectrogram\nplt.figure(figsize=(8, 4))\nlibrosa.display.specshow(D, sr=sr6, x_axis='time', y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Spectrogram of the Audio Signal')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:21.065584Z","iopub.execute_input":"2024-01-11T17:58:21.065997Z","iopub.status.idle":"2024-01-11T17:58:26.223512Z","shell.execute_reply.started":"2024-01-11T17:58:21.065963Z","shell.execute_reply":"2024-01-11T17:58:26.22198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D = librosa.amplitude_to_db(np.abs(librosa.stft(y7)), ref=np.max)\n\n# Plot the spectrogram\nplt.figure(figsize=(8, 4))\nlibrosa.display.specshow(D, sr=sr7, x_axis='time', y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Spectrogram of the Audio Signal')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:26.224931Z","iopub.execute_input":"2024-01-11T17:58:26.225279Z","iopub.status.idle":"2024-01-11T17:58:30.296447Z","shell.execute_reply.started":"2024-01-11T17:58:26.225251Z","shell.execute_reply":"2024-01-11T17:58:30.295164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D = librosa.amplitude_to_db(np.abs(librosa.stft(y8)), ref=np.max)\n\n# Plot the spectrogram\nplt.figure(figsize=(8, 4))\nlibrosa.display.specshow(D, sr=sr8, x_axis='time', y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Spectrogram of the Audio Signal')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:30.298253Z","iopub.execute_input":"2024-01-11T17:58:30.298846Z","iopub.status.idle":"2024-01-11T17:58:32.239846Z","shell.execute_reply.started":"2024-01-11T17:58:30.298814Z","shell.execute_reply":"2024-01-11T17:58:32.238393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D = librosa.amplitude_to_db(np.abs(librosa.stft(y9)), ref=np.max)\n\n# Plot the spectrogram\nplt.figure(figsize=(8, 4))\nlibrosa.display.specshow(D, sr=sr9, x_axis='time', y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Spectrogram of the Audio Signal')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:32.241664Z","iopub.execute_input":"2024-01-11T17:58:32.242183Z","iopub.status.idle":"2024-01-11T17:58:34.021062Z","shell.execute_reply.started":"2024-01-11T17:58:32.242151Z","shell.execute_reply":"2024-01-11T17:58:34.019916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D = librosa.amplitude_to_db(np.abs(librosa.stft(y10)), ref=np.max)\n\n# Plot the spectrogram\nplt.figure(figsize=(8, 4))\nlibrosa.display.specshow(D, sr=sr10, x_axis='time', y_axis='log')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Spectrogram of the Audio Signal')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:34.022672Z","iopub.execute_input":"2024-01-11T17:58:34.023244Z","iopub.status.idle":"2024-01-11T17:58:35.174497Z","shell.execute_reply.started":"2024-01-11T17:58:34.023214Z","shell.execute_reply":"2024-01-11T17:58:35.172745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mel_signal = librosa.feature.melspectrogram(y=y3, sr=sr3, hop_length=2, n_fft=200)\nspectrogram = np.abs(mel_signal)\npower_to_db = librosa.power_to_db(spectrogram, ref=np.max)\nplt.figure(figsize=(7, 6))\nlibrosa.display.specshow(power_to_db, sr=sr3, x_axis='time', y_axis='mel', cmap='magma', hop_length=2)\n\nplt.colorbar(label='dB')\nplt.title('Mel-Spectrogram (dB)', fontdict=dict(size=18))\nplt.xlabel('Time', fontdict=dict(size=15))\nplt.ylabel('Frequency', fontdict=dict(size=15))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:35.176374Z","iopub.execute_input":"2024-01-11T17:58:35.177352Z","iopub.status.idle":"2024-01-11T17:58:45.397692Z","shell.execute_reply.started":"2024-01-11T17:58:35.17728Z","shell.execute_reply":"2024-01-11T17:58:45.396373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mel_signal = librosa.feature.melspectrogram(y=y4, sr=sr4, hop_length=2, n_fft=200)\nspectrogram = np.abs(mel_signal)\npower_to_db = librosa.power_to_db(spectrogram, ref=np.max)\nplt.figure(figsize=(7, 6))\nlibrosa.display.specshow(power_to_db, sr=sr4, x_axis='time', y_axis='mel', cmap='magma', hop_length=2)\n\nplt.colorbar(label='dB')\nplt.title('Mel-Spectrogram (dB)', fontdict=dict(size=18))\nplt.xlabel('Time', fontdict=dict(size=15))\nplt.ylabel('Frequency', fontdict=dict(size=15))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:58:45.399603Z","iopub.execute_input":"2024-01-11T17:58:45.399934Z","iopub.status.idle":"2024-01-11T17:59:00.72059Z","shell.execute_reply.started":"2024-01-11T17:58:45.399908Z","shell.execute_reply":"2024-01-11T17:59:00.719389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2 #image processing\nimport audioread #reading and processing audio files\nimport logging #record events and errors for debugging and monitoring\nimport os #file and directory manipulation\nimport random #shuffling data\nimport time #calculate time taken or introduce delays\nimport warnings #issuing warning messages about potential issues\nimport librosa\nimport numpy as np\nimport pandas as pd\nimport soundfile as sf #reading and writing audio files\nimport torch #core module of PyTorch\nimport torch.nn as nn #building and training neural networks\nimport torch.nn.functional as F #for element-wise functions\nimport torch.utils.data as data #for loading and batching data during training\nfrom contextlib import contextmanager #to ensure that resources are properly managed\nfrom pathlib import Path #for working with paths in a more object-oriented way\nfrom typing import Optional #type hint\nfrom fastprogress import progress_bar\nfrom sklearn.metrics import f1_score\nfrom torchvision import models #provides pre-trained models","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:59:00.722042Z","iopub.execute_input":"2024-01-11T17:59:00.722785Z","iopub.status.idle":"2024-01-11T17:59:03.24453Z","shell.execute_reply.started":"2024-01-11T17:59:00.722751Z","shell.execute_reply":"2024-01-11T17:59:03.242817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed (seed: int = 42): #sets random seed for different libraries\n    random.seed (seed)\n    np.random.seed (seed)\n    os.environ[ \"PYTHONHASHSEED\"] = str(seed) #for hash functions\n    torch.manual_seed (seed) #random of pytorch\n    torch.cuda.manual_seed (seed) #type: ignore #same but for GPU\n    torch.backends.cudnn.deterministic = True #type: ignore #deterministic mode\n    torch.backends. cudnn.benchmark = True # type: ignore #optimize performance\n\ndef get_logger (out_file=None) :\n    logger = logging.getLogger()\n    formatter = logging. Formatter(\"%(asctime)s - %(levelname)s - %(message)s\") #timestamp, log level, mess\n    logger.handlers = [] #object that transfers log\n    logger.setLevel (logging. INFO) #capture only INFO level\n    handler = logging. StreamHandler() #to the console\n    handler. setFormatter (formatter)\n    handler.setLevel (logging. INFO)\n    logger.addHandler (handler)\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","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:59:03.245314Z","iopub.status.idle":"2024-01-11T17:59:03.245721Z","shell.execute_reply.started":"2024-01-11T17:59:03.245543Z","shell.execute_reply":"2024-01-11T17:59:03.245563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@contextmanager #measure execution time\ndef timer (name: str, logger: Optional [logging.Logger] = None):\n    t0 = time.time() #current 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)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:59:03.250356Z","iopub.status.idle":"2024-01-11T17:59:03.250949Z","shell.execute_reply.started":"2024-01-11T17:59:03.250696Z","shell.execute_reply":"2024-01-11T17:59:03.250721Z"},"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-11T17:59:03.252946Z","iopub.status.idle":"2024-01-11T17:59:03.253545Z","shell.execute_reply.started":"2024-01-11T17:59:03.253243Z","shell.execute_reply":"2024-01-11T17:59:03.253267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TARGET_SR = 32000","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:59:03.255042Z","iopub.status.idle":"2024-01-11T17:59:03.255625Z","shell.execute_reply.started":"2024-01-11T17:59:03.255357Z","shell.execute_reply":"2024-01-11T17:59:03.255382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(\"/kaggle/input/birdcall-check/test.csv\")\ntest_audio = \"/kaggle/input/birdcall-check/test_audio\"\n\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:59:03.256881Z","iopub.status.idle":"2024-01-11T17:59:03.257305Z","shell.execute_reply.started":"2024-01-11T17:59:03.257109Z","shell.execute_reply":"2024-01-11T17:59:03.257129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ResNet (nn. Module): #base class\n    def __init__(self, base_model_name: str, pretrained=False, #constructor #weights\n                num_classes=264): #constructor method\n        super().__init__() #initializing the base class\n        base_model = models.__getattribute__(base_model_name) (\n            pretrained-pretrained)\n        layers = list (base_model.children())[:-2] #except pooling and dense\n        layers.append(nn.AdaptiveMaxPool2d(1))\n        self.encoder = nn.Sequential(*layers)\n\n        in_features = base_model.fc.in_features #number of input 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    def forward(self,x):\n        batch_size = x.size(0)\n        x = self.encoder (x).view (batch_size, -1) #1D tensor\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        }\n","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:59:03.258638Z","iopub.status.idle":"2024-01-11T17:59:03.259087Z","shell.execute_reply.started":"2024-01-11T17:59:03.258865Z","shell.execute_reply":"2024-01-11T17:59:03.258884Z"},"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 = \"../input/birdcall-resnet50-init-weights/best.pth\"","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:59:03.261247Z","iopub.status.idle":"2024-01-11T17:59:03.261721Z","shell.execute_reply.started":"2024-01-11T17:59:03.261501Z","shell.execute_reply":"2024-01-11T17:59:03.261522Z"},"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()\n\nlabel_encoder = LabelEncoder()\n\nencoded_labels = label_encoder.fit_transform(unique_bird_names)\n\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-11T17:59:03.262671Z","iopub.status.idle":"2024-01-11T17:59:03.263087Z","shell.execute_reply.started":"2024-01-11T17:59:03.262875Z","shell.execute_reply":"2024-01-11T17:59:03.262894Z"},"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    X = np.stack([X,X,X], axis=-1)\n    \n    #Standardize\n    mean = mean or X.mean()\n    X = X - mean\n    std = srd 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    \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        V = np.zeros_like(Xstd, dtype=np.uint8)\n    return V\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\n                \n            ","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:59:03.264624Z","iopub.status.idle":"2024-01-11T17:59:03.265024Z","shell.execute_reply.started":"2024-01-11T17:59:03.264831Z","shell.execute_reply":"2024-01-11T17:59:03.264852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model(config: dict, weights_path: str):\n    model = ResNet(**config)\n    checkpoint = torch.load(weights_path, map_location=torch.device('cpu'))\n    model.load_state_dict(checkpoint[\"model_state_dict\"])\n    device = torch.device('cpu')\n    model.to(device)\n    model.eval\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:59:03.266027Z","iopub.status.idle":"2024-01-11T17:59:03.266447Z","shell.execute_reply.started":"2024-01-11T17:59:03.266224Z","shell.execute_reply":"2024-01-11T17:59:03.266243Z"},"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, batchsize=1, shuffle=False)\n    \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                prediciton = model(image)\n                proba = prediction[\"multilabel_proba\"].detach().cpu().numpy().reshape(-1)\n\n                \n            events = proba >= threshold\n            labels = np.argwhere(events).reshape(-1).tolist()\n        else:\n\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 // batach_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                    prediciton = 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-11T17:59:03.26743Z","iopub.status.idle":"2024-01-11T17:59:03.267821Z","shell.execute_reply.started":"2024-01-11T17:59:03.267625Z","shell.execute_reply":"2024-01-11T17:59:03.267643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install resampy","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:59:03.269322Z","iopub.status.idle":"2024-01-11T17:59:03.26972Z","shell.execute_reply.started":"2024-01-11T17:59:03.269554Z","shell.execute_reply":"2024-01-11T17:59:03.269572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import resampy","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:59:03.27047Z","iopub.status.idle":"2024-01-11T17:59:03.270855Z","shell.execute_reply.started":"2024-01-11T17:59:03.270664Z","shell.execute_reply":"2024-01-11T17:59:03.270682Z"},"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    predictions_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                                 res_type= \"kaiser_fast\")\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 = prediciton_for_clip(test_df_for_audio_id,\n                                                      clip=clip,\n                                                      model=model,\n                                                      mel_params=mel_params,\n                                                      threshold=threshold)\n                \n            row_id = list(prediction_dict.keys())\n            birds = list(prediction_dict.values())\n            prediciton_df = pd.DataFrame({\n                \"row_id\": row_id,\n                \"birds\" : birds\n            })\n            prediction_dfs.append(prediciton_df)\n    \n    prediction_df = pd.concat(predition_dfs, axis=0, sort=False).reset_index(drop=True)\n    return prediciton_df\n","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:59:03.272284Z","iopub.status.idle":"2024-01-11T17:59:03.272722Z","shell.execute_reply.started":"2024-01-11T17:59:03.272514Z","shell.execute_reply":"2024-01-11T17:59:03.272533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-11T17:59:03.274294Z","iopub.status.idle":"2024-01-11T17:59:03.274743Z","shell.execute_reply.started":"2024-01-11T17:59:03.274533Z","shell.execute_reply":"2024-01-11T17:59:03.274553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}