{"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:06:04.686613Z","iopub.execute_input":"2024-01-11T17:06:04.687162Z","iopub.status.idle":"2024-01-11T17:06:08.239636Z","shell.execute_reply.started":"2024-01-11T17:06:04.687115Z","shell.execute_reply":"2024-01-11T17:06:08.238768Z"},"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:06:08.241478Z","iopub.execute_input":"2024-01-11T17:06:08.242182Z","iopub.status.idle":"2024-01-11T17:06:08.835834Z","shell.execute_reply.started":"2024-01-11T17:06:08.242148Z","shell.execute_reply":"2024-01-11T17:06:08.834464Z"},"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:06:08.837519Z","iopub.execute_input":"2024-01-11T17:06:08.837877Z","iopub.status.idle":"2024-01-11T17:06:09.826434Z","shell.execute_reply.started":"2024-01-11T17:06:08.837846Z","shell.execute_reply":"2024-01-11T17:06:09.825271Z"},"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:06:09.828623Z","iopub.execute_input":"2024-01-11T17:06:09.829282Z","iopub.status.idle":"2024-01-11T17:06:09.834616Z","shell.execute_reply.started":"2024-01-11T17:06:09.829248Z","shell.execute_reply":"2024-01-11T17:06:09.833574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:09.835688Z","iopub.execute_input":"2024-01-11T17:06:09.836346Z","iopub.status.idle":"2024-01-11T17:06:09.850416Z","shell.execute_reply.started":"2024-01-11T17:06:09.836315Z","shell.execute_reply":"2024-01-11T17:06:09.849209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:10.03232Z","iopub.execute_input":"2024-01-11T17:06:10.032709Z","iopub.status.idle":"2024-01-11T17:06:10.142675Z","shell.execute_reply.started":"2024-01-11T17:06:10.032677Z","shell.execute_reply":"2024-01-11T17:06:10.141875Z"},"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:06:11.022759Z","iopub.execute_input":"2024-01-11T17:06:11.023335Z","iopub.status.idle":"2024-01-11T17:06:11.109279Z","shell.execute_reply.started":"2024-01-11T17:06:11.023286Z","shell.execute_reply":"2024-01-11T17:06:11.107927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:12.203307Z","iopub.execute_input":"2024-01-11T17:06:12.203749Z","iopub.status.idle":"2024-01-11T17:06:12.210999Z","shell.execute_reply.started":"2024-01-11T17:06:12.203712Z","shell.execute_reply":"2024-01-11T17:06:12.210133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:13.341044Z","iopub.execute_input":"2024-01-11T17:06:13.341687Z","iopub.status.idle":"2024-01-11T17:06:13.372231Z","shell.execute_reply.started":"2024-01-11T17:06:13.341651Z","shell.execute_reply":"2024-01-11T17:06:13.371107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['rating'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:13.909468Z","iopub.execute_input":"2024-01-11T17:06:13.909879Z","iopub.status.idle":"2024-01-11T17:06:13.919144Z","shell.execute_reply.started":"2024-01-11T17:06:13.909845Z","shell.execute_reply":"2024-01-11T17:06:13.917777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['playback_used'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:14.409278Z","iopub.execute_input":"2024-01-11T17:06:14.409656Z","iopub.status.idle":"2024-01-11T17:06:14.418976Z","shell.execute_reply.started":"2024-01-11T17:06:14.409626Z","shell.execute_reply":"2024-01-11T17:06:14.417541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['playback_used'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:14.97523Z","iopub.execute_input":"2024-01-11T17:06:14.975823Z","iopub.status.idle":"2024-01-11T17:06:14.989287Z","shell.execute_reply.started":"2024-01-11T17:06:14.97579Z","shell.execute_reply":"2024-01-11T17:06:14.987175Z"},"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:06:15.47064Z","iopub.execute_input":"2024-01-11T17:06:15.471247Z","iopub.status.idle":"2024-01-11T17:06:15.482206Z","shell.execute_reply.started":"2024-01-11T17:06:15.471209Z","shell.execute_reply":"2024-01-11T17:06:15.480802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['playback_used'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:15.935339Z","iopub.execute_input":"2024-01-11T17:06:15.935754Z","iopub.status.idle":"2024-01-11T17:06:15.944767Z","shell.execute_reply.started":"2024-01-11T17:06:15.935718Z","shell.execute_reply":"2024-01-11T17:06:15.943551Z"},"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:06:16.080817Z","iopub.execute_input":"2024-01-11T17:06:16.081222Z","iopub.status.idle":"2024-01-11T17:06:16.101177Z","shell.execute_reply.started":"2024-01-11T17:06:16.081189Z","shell.execute_reply":"2024-01-11T17:06:16.100044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:16.485555Z","iopub.execute_input":"2024-01-11T17:06:16.48682Z","iopub.status.idle":"2024-01-11T17:06:16.513364Z","shell.execute_reply.started":"2024-01-11T17:06:16.48678Z","shell.execute_reply":"2024-01-11T17:06:16.512157Z"},"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:06:16.883779Z","iopub.execute_input":"2024-01-11T17:06:16.884168Z","iopub.status.idle":"2024-01-11T17:06:16.901921Z","shell.execute_reply.started":"2024-01-11T17:06:16.884137Z","shell.execute_reply":"2024-01-11T17:06:16.901045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:17.318164Z","iopub.execute_input":"2024-01-11T17:06:17.31958Z","iopub.status.idle":"2024-01-11T17:06:17.354797Z","shell.execute_reply.started":"2024-01-11T17:06:17.319514Z","shell.execute_reply":"2024-01-11T17:06:17.353589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['ebird_code'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:17.743186Z","iopub.execute_input":"2024-01-11T17:06:17.744086Z","iopub.status.idle":"2024-01-11T17:06:17.759427Z","shell.execute_reply.started":"2024-01-11T17:06:17.744044Z","shell.execute_reply":"2024-01-11T17:06:17.758029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['ebird_code'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:18.036636Z","iopub.execute_input":"2024-01-11T17:06:18.037071Z","iopub.status.idle":"2024-01-11T17:06:18.045632Z","shell.execute_reply.started":"2024-01-11T17:06:18.037038Z","shell.execute_reply":"2024-01-11T17:06:18.044507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['channels'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:18.345238Z","iopub.execute_input":"2024-01-11T17:06:18.345624Z","iopub.status.idle":"2024-01-11T17:06:18.354851Z","shell.execute_reply.started":"2024-01-11T17:06:18.345593Z","shell.execute_reply":"2024-01-11T17:06:18.353759Z"},"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:06:18.612644Z","iopub.execute_input":"2024-01-11T17:06:18.613077Z","iopub.status.idle":"2024-01-11T17:06:18.647079Z","shell.execute_reply.started":"2024-01-11T17:06:18.613043Z","shell.execute_reply":"2024-01-11T17:06:18.646051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['channels'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:18.920579Z","iopub.execute_input":"2024-01-11T17:06:18.920986Z","iopub.status.idle":"2024-01-11T17:06:18.933448Z","shell.execute_reply.started":"2024-01-11T17:06:18.920938Z","shell.execute_reply":"2024-01-11T17:06:18.932278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['date'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:19.177521Z","iopub.execute_input":"2024-01-11T17:06:19.178143Z","iopub.status.idle":"2024-01-11T17:06:19.188154Z","shell.execute_reply.started":"2024-01-11T17:06:19.178098Z","shell.execute_reply":"2024-01-11T17:06:19.187044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['date'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:19.486567Z","iopub.execute_input":"2024-01-11T17:06:19.487001Z","iopub.status.idle":"2024-01-11T17:06:19.497327Z","shell.execute_reply.started":"2024-01-11T17:06:19.486951Z","shell.execute_reply":"2024-01-11T17:06:19.496002Z"},"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:06:19.694916Z","iopub.execute_input":"2024-01-11T17:06:19.695345Z","iopub.status.idle":"2024-01-11T17:06:19.758536Z","shell.execute_reply.started":"2024-01-11T17:06:19.69531Z","shell.execute_reply":"2024-01-11T17:06:19.755224Z"},"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:06:19.867689Z","iopub.execute_input":"2024-01-11T17:06:19.868137Z","iopub.status.idle":"2024-01-11T17:06:19.914977Z","shell.execute_reply.started":"2024-01-11T17:06:19.868092Z","shell.execute_reply":"2024-01-11T17:06:19.913549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:20.070209Z","iopub.execute_input":"2024-01-11T17:06:20.070623Z","iopub.status.idle":"2024-01-11T17:06:20.152666Z","shell.execute_reply.started":"2024-01-11T17:06:20.070589Z","shell.execute_reply":"2024-01-11T17:06:20.151479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['pitch'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:20.343122Z","iopub.execute_input":"2024-01-11T17:06:20.343518Z","iopub.status.idle":"2024-01-11T17:06:20.353636Z","shell.execute_reply.started":"2024-01-11T17:06:20.343488Z","shell.execute_reply":"2024-01-11T17:06:20.352295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['pitch'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:20.534029Z","iopub.execute_input":"2024-01-11T17:06:20.534454Z","iopub.status.idle":"2024-01-11T17:06:20.546666Z","shell.execute_reply.started":"2024-01-11T17:06:20.534418Z","shell.execute_reply":"2024-01-11T17:06:20.545414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['duration'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:20.74116Z","iopub.execute_input":"2024-01-11T17:06:20.741582Z","iopub.status.idle":"2024-01-11T17:06:20.752912Z","shell.execute_reply.started":"2024-01-11T17:06:20.741547Z","shell.execute_reply":"2024-01-11T17:06:20.752083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['speed'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:21.26964Z","iopub.execute_input":"2024-01-11T17:06:21.270056Z","iopub.status.idle":"2024-01-11T17:06:21.279217Z","shell.execute_reply.started":"2024-01-11T17:06:21.270023Z","shell.execute_reply":"2024-01-11T17:06:21.277909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['speed'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:21.464083Z","iopub.execute_input":"2024-01-11T17:06:21.46447Z","iopub.status.idle":"2024-01-11T17:06:21.47737Z","shell.execute_reply.started":"2024-01-11T17:06:21.464439Z","shell.execute_reply":"2024-01-11T17:06:21.476202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['species'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:21.664487Z","iopub.execute_input":"2024-01-11T17:06:21.664896Z","iopub.status.idle":"2024-01-11T17:06:21.674528Z","shell.execute_reply.started":"2024-01-11T17:06:21.664861Z","shell.execute_reply":"2024-01-11T17:06:21.673404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['sci_name'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:21.871048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['number_of_notes'].unique()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['title'].nunique()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop('title', axis=1, inplace=True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['secondary_labels'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:24.934112Z","iopub.execute_input":"2024-01-11T17:06:24.934937Z","iopub.status.idle":"2024-01-11T17:06:24.944999Z","shell.execute_reply.started":"2024-01-11T17:06:24.934901Z","shell.execute_reply":"2024-01-11T17:06:24.94424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['bird_seen'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:25.149226Z","iopub.execute_input":"2024-01-11T17:06:25.150022Z","iopub.status.idle":"2024-01-11T17:06:25.158942Z","shell.execute_reply.started":"2024-01-11T17:06:25.149984Z","shell.execute_reply":"2024-01-11T17:06:25.157418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['bird_seen'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:25.410147Z","iopub.execute_input":"2024-01-11T17:06:25.410557Z","iopub.status.idle":"2024-01-11T17:06:25.422341Z","shell.execute_reply.started":"2024-01-11T17:06:25.410526Z","shell.execute_reply":"2024-01-11T17:06:25.421211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['sci_name'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:26.976953Z","iopub.execute_input":"2024-01-11T17:06:26.977371Z","iopub.status.idle":"2024-01-11T17:06:26.987138Z","shell.execute_reply.started":"2024-01-11T17:06:26.977339Z","shell.execute_reply":"2024-01-11T17:06:26.985799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['location'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:27.289924Z","iopub.execute_input":"2024-01-11T17:06:27.290758Z","iopub.status.idle":"2024-01-11T17:06:27.304039Z","shell.execute_reply.started":"2024-01-11T17:06:27.290664Z","shell.execute_reply":"2024-01-11T17:06:27.302505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['latitude'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:28.495394Z","iopub.execute_input":"2024-01-11T17:06:28.496052Z","iopub.status.idle":"2024-01-11T17:06:28.508999Z","shell.execute_reply.started":"2024-01-11T17:06:28.496006Z","shell.execute_reply":"2024-01-11T17:06:28.507769Z"},"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:06:28.730803Z","iopub.execute_input":"2024-01-11T17:06:28.731558Z","iopub.status.idle":"2024-01-11T17:06:28.801352Z","shell.execute_reply.started":"2024-01-11T17:06:28.731515Z","shell.execute_reply":"2024-01-11T17:06:28.800269Z"},"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:06:29.701514Z","iopub.execute_input":"2024-01-11T17:06:29.701917Z","iopub.status.idle":"2024-01-11T17:06:29.717857Z","shell.execute_reply.started":"2024-01-11T17:06:29.701885Z","shell.execute_reply":"2024-01-11T17:06:29.717051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:29.985426Z","iopub.execute_input":"2024-01-11T17:06:29.986855Z","iopub.status.idle":"2024-01-11T17:06:30.035615Z","shell.execute_reply.started":"2024-01-11T17:06:29.986806Z","shell.execute_reply":"2024-01-11T17:06:30.034838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['sampling_rate'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:30.178279Z","iopub.execute_input":"2024-01-11T17:06:30.178673Z","iopub.status.idle":"2024-01-11T17:06:30.188015Z","shell.execute_reply.started":"2024-01-11T17:06:30.17864Z","shell.execute_reply":"2024-01-11T17:06:30.186857Z"},"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:06:30.388196Z","iopub.execute_input":"2024-01-11T17:06:30.388798Z","iopub.status.idle":"2024-01-11T17:06:30.41131Z","shell.execute_reply.started":"2024-01-11T17:06:30.388763Z","shell.execute_reply":"2024-01-11T17:06:30.409901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['sampling_rate'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:31.348849Z","iopub.execute_input":"2024-01-11T17:06:31.34928Z","iopub.status.idle":"2024-01-11T17:06:31.35847Z","shell.execute_reply.started":"2024-01-11T17:06:31.349244Z","shell.execute_reply":"2024-01-11T17:06:31.357043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['type'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:31.558388Z","iopub.execute_input":"2024-01-11T17:06:31.558808Z","iopub.status.idle":"2024-01-11T17:06:31.56846Z","shell.execute_reply.started":"2024-01-11T17:06:31.558774Z","shell.execute_reply":"2024-01-11T17:06:31.567274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['type'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:31.714906Z","iopub.execute_input":"2024-01-11T17:06:31.715341Z","iopub.status.idle":"2024-01-11T17:06:31.724102Z","shell.execute_reply.started":"2024-01-11T17:06:31.715307Z","shell.execute_reply":"2024-01-11T17:06:31.723225Z"},"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:06:31.917774Z","iopub.execute_input":"2024-01-11T17:06:31.918178Z","iopub.status.idle":"2024-01-11T17:06:31.990603Z","shell.execute_reply.started":"2024-01-11T17:06:31.918139Z","shell.execute_reply":"2024-01-11T17:06:31.989505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:32.092733Z","iopub.execute_input":"2024-01-11T17:06:32.093174Z","iopub.status.idle":"2024-01-11T17:06:32.13044Z","shell.execute_reply.started":"2024-01-11T17:06:32.093128Z","shell.execute_reply":"2024-01-11T17:06:32.129183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['elevation'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:33.66639Z","iopub.execute_input":"2024-01-11T17:06:33.666782Z","iopub.status.idle":"2024-01-11T17:06:33.677546Z","shell.execute_reply.started":"2024-01-11T17:06:33.666752Z","shell.execute_reply":"2024-01-11T17:06:33.676647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['elevation'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:33.679445Z","iopub.execute_input":"2024-01-11T17:06:33.680603Z","iopub.status.idle":"2024-01-11T17:06:33.694275Z","shell.execute_reply.started":"2024-01-11T17:06:33.680557Z","shell.execute_reply":"2024-01-11T17:06:33.693397Z"},"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:06:33.850019Z","iopub.execute_input":"2024-01-11T17:06:33.850413Z","iopub.status.idle":"2024-01-11T17:06:33.864767Z","shell.execute_reply.started":"2024-01-11T17:06:33.850381Z","shell.execute_reply":"2024-01-11T17:06:33.863384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['elevation'] = df['elevation'].str.replace(',','')","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:34.063547Z","iopub.execute_input":"2024-01-11T17:06:34.063944Z","iopub.status.idle":"2024-01-11T17:06:34.082626Z","shell.execute_reply.started":"2024-01-11T17:06:34.063913Z","shell.execute_reply":"2024-01-11T17:06:34.081317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['elevation'] = df['elevation'].str.replace('.','')","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:34.263385Z","iopub.execute_input":"2024-01-11T17:06:34.263775Z","iopub.status.idle":"2024-01-11T17:06:34.281661Z","shell.execute_reply.started":"2024-01-11T17:06:34.263743Z","shell.execute_reply":"2024-01-11T17:06:34.280315Z"},"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:06:35.317308Z","iopub.execute_input":"2024-01-11T17:06:35.317695Z","iopub.status.idle":"2024-01-11T17:06:36.931201Z","shell.execute_reply.started":"2024-01-11T17:06:35.317666Z","shell.execute_reply":"2024-01-11T17:06:36.930107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['elevation'].mean()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:36.932984Z","iopub.execute_input":"2024-01-11T17:06:36.933359Z","iopub.status.idle":"2024-01-11T17:06:36.941929Z","shell.execute_reply.started":"2024-01-11T17:06:36.933327Z","shell.execute_reply":"2024-01-11T17:06:36.940791Z"},"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:06:36.94336Z","iopub.execute_input":"2024-01-11T17:06:36.943737Z","iopub.status.idle":"2024-01-11T17:06:36.958002Z","shell.execute_reply.started":"2024-01-11T17:06:36.943704Z","shell.execute_reply":"2024-01-11T17:06:36.956787Z"},"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:06:36.960496Z","iopub.execute_input":"2024-01-11T17:06:36.961332Z","iopub.status.idle":"2024-01-11T17:06:36.977519Z","shell.execute_reply.started":"2024-01-11T17:06:36.961292Z","shell.execute_reply":"2024-01-11T17:06:36.976293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['bitrate_of_mp3'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:36.979553Z","iopub.execute_input":"2024-01-11T17:06:36.980046Z","iopub.status.idle":"2024-01-11T17:06:36.990565Z","shell.execute_reply.started":"2024-01-11T17:06:36.979998Z","shell.execute_reply":"2024-01-11T17:06:36.989304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['bitrate_of_mp3'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:36.992785Z","iopub.execute_input":"2024-01-11T17:06:36.993331Z","iopub.status.idle":"2024-01-11T17:06:37.00742Z","shell.execute_reply.started":"2024-01-11T17:06:36.993295Z","shell.execute_reply":"2024-01-11T17:06:37.006439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[df['bitrate_of_mp3'] == 0]","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:38.28957Z","iopub.execute_input":"2024-01-11T17:06:38.289947Z","iopub.status.idle":"2024-01-11T17:06:38.312032Z","shell.execute_reply.started":"2024-01-11T17:06:38.289916Z","shell.execute_reply":"2024-01-11T17:06:38.310822Z"},"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:06:38.496125Z","iopub.execute_input":"2024-01-11T17:06:38.496541Z","iopub.status.idle":"2024-01-11T17:06:38.519388Z","shell.execute_reply.started":"2024-01-11T17:06:38.496509Z","shell.execute_reply":"2024-01-11T17:06:38.518386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[df['bitrate_of_mp3'].isna()]","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:38.717822Z","iopub.execute_input":"2024-01-11T17:06:38.718265Z","iopub.status.idle":"2024-01-11T17:06:38.760595Z","shell.execute_reply.started":"2024-01-11T17:06:38.718228Z","shell.execute_reply":"2024-01-11T17:06:38.759819Z"},"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:06:38.88643Z","iopub.execute_input":"2024-01-11T17:06:38.887241Z","iopub.status.idle":"2024-01-11T17:06:38.895902Z","shell.execute_reply.started":"2024-01-11T17:06:38.887205Z","shell.execute_reply":"2024-01-11T17:06:38.894906Z"},"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:06:39.085755Z","iopub.execute_input":"2024-01-11T17:06:39.086176Z","iopub.status.idle":"2024-01-11T17:06:39.111129Z","shell.execute_reply.started":"2024-01-11T17:06:39.086141Z","shell.execute_reply":"2024-01-11T17:06:39.110029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m = df['bitrate_of_mp3'].mean()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:41.103757Z","iopub.execute_input":"2024-01-11T17:06:41.104156Z","iopub.status.idle":"2024-01-11T17:06:41.109603Z","shell.execute_reply.started":"2024-01-11T17:06:41.104124Z","shell.execute_reply":"2024-01-11T17:06:41.108357Z"},"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:06:41.363302Z","iopub.execute_input":"2024-01-11T17:06:41.363724Z","iopub.status.idle":"2024-01-11T17:06:41.373106Z","shell.execute_reply.started":"2024-01-11T17:06:41.363688Z","shell.execute_reply":"2024-01-11T17:06:41.372038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:41.552294Z","iopub.execute_input":"2024-01-11T17:06:41.552895Z","iopub.status.idle":"2024-01-11T17:06:41.58902Z","shell.execute_reply.started":"2024-01-11T17:06:41.552862Z","shell.execute_reply":"2024-01-11T17:06:41.587829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['file_type'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:41.723807Z","iopub.execute_input":"2024-01-11T17:06:41.724527Z","iopub.status.idle":"2024-01-11T17:06:41.733897Z","shell.execute_reply.started":"2024-01-11T17:06:41.724491Z","shell.execute_reply":"2024-01-11T17:06:41.732565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['volume'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:42.834189Z","iopub.execute_input":"2024-01-11T17:06:42.83533Z","iopub.status.idle":"2024-01-11T17:06:42.844752Z","shell.execute_reply.started":"2024-01-11T17:06:42.835294Z","shell.execute_reply":"2024-01-11T17:06:42.843739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['background'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:43.087248Z","iopub.execute_input":"2024-01-11T17:06:43.087708Z","iopub.status.idle":"2024-01-11T17:06:43.09864Z","shell.execute_reply.started":"2024-01-11T17:06:43.087599Z","shell.execute_reply":"2024-01-11T17:06:43.097429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['background'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:43.273874Z","iopub.execute_input":"2024-01-11T17:06:43.274266Z","iopub.status.idle":"2024-01-11T17:06:43.287291Z","shell.execute_reply.started":"2024-01-11T17:06:43.274234Z","shell.execute_reply":"2024-01-11T17:06:43.286144Z"},"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:06:43.442582Z","iopub.execute_input":"2024-01-11T17:06:43.443831Z","iopub.status.idle":"2024-01-11T17:06:43.456939Z","shell.execute_reply.started":"2024-01-11T17:06:43.443775Z","shell.execute_reply":"2024-01-11T17:06:43.455804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['xc_id'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:44.525535Z","iopub.execute_input":"2024-01-11T17:06:44.525917Z","iopub.status.idle":"2024-01-11T17:06:44.536024Z","shell.execute_reply.started":"2024-01-11T17:06:44.525886Z","shell.execute_reply":"2024-01-11T17:06:44.534804Z"},"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:06:44.892994Z","iopub.execute_input":"2024-01-11T17:06:44.894137Z","iopub.status.idle":"2024-01-11T17:06:44.911296Z","shell.execute_reply.started":"2024-01-11T17:06:44.894093Z","shell.execute_reply":"2024-01-11T17:06:44.910168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['url'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:45.20609Z","iopub.execute_input":"2024-01-11T17:06:45.206727Z","iopub.status.idle":"2024-01-11T17:06:45.220895Z","shell.execute_reply.started":"2024-01-11T17:06:45.206693Z","shell.execute_reply":"2024-01-11T17:06:45.219893Z"},"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:06:45.532113Z","iopub.execute_input":"2024-01-11T17:06:45.532551Z","iopub.status.idle":"2024-01-11T17:06:45.546215Z","shell.execute_reply.started":"2024-01-11T17:06:45.532519Z","shell.execute_reply":"2024-01-11T17:06:45.544806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['country'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:46.113932Z","iopub.execute_input":"2024-01-11T17:06:46.115074Z","iopub.status.idle":"2024-01-11T17:06:46.12579Z","shell.execute_reply.started":"2024-01-11T17:06:46.115037Z","shell.execute_reply":"2024-01-11T17:06:46.124691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['author'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:46.507617Z","iopub.execute_input":"2024-01-11T17:06:46.508063Z","iopub.status.idle":"2024-01-11T17:06:46.525393Z","shell.execute_reply.started":"2024-01-11T17:06:46.508027Z","shell.execute_reply":"2024-01-11T17:06:46.524193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['author'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:46.835205Z","iopub.execute_input":"2024-01-11T17:06:46.835591Z","iopub.status.idle":"2024-01-11T17:06:46.845082Z","shell.execute_reply.started":"2024-01-11T17:06:46.835559Z","shell.execute_reply":"2024-01-11T17:06:46.843837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['recordist'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:47.212198Z","iopub.execute_input":"2024-01-11T17:06:47.212627Z","iopub.status.idle":"2024-01-11T17:06:47.223276Z","shell.execute_reply.started":"2024-01-11T17:06:47.21259Z","shell.execute_reply":"2024-01-11T17:06:47.221921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[df['author'] != df['recordist']]","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:47.682574Z","iopub.execute_input":"2024-01-11T17:06:47.683445Z","iopub.status.idle":"2024-01-11T17:06:47.709326Z","shell.execute_reply.started":"2024-01-11T17:06:47.683404Z","shell.execute_reply":"2024-01-11T17:06:47.708076Z"},"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:06:48.0644Z","iopub.execute_input":"2024-01-11T17:06:48.064929Z","iopub.status.idle":"2024-01-11T17:06:48.079692Z","shell.execute_reply.started":"2024-01-11T17:06:48.064885Z","shell.execute_reply":"2024-01-11T17:06:48.07825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['primary_label'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:48.483447Z","iopub.execute_input":"2024-01-11T17:06:48.483848Z","iopub.status.idle":"2024-01-11T17:06:48.495932Z","shell.execute_reply.started":"2024-01-11T17:06:48.483815Z","shell.execute_reply":"2024-01-11T17:06:48.494495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['length'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:48.873878Z","iopub.execute_input":"2024-01-11T17:06:48.87439Z","iopub.status.idle":"2024-01-11T17:06:48.884349Z","shell.execute_reply.started":"2024-01-11T17:06:48.874352Z","shell.execute_reply":"2024-01-11T17:06:48.883182Z"},"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:06:49.325135Z","iopub.execute_input":"2024-01-11T17:06:49.325743Z","iopub.status.idle":"2024-01-11T17:06:49.409622Z","shell.execute_reply.started":"2024-01-11T17:06:49.325707Z","shell.execute_reply":"2024-01-11T17:06:49.408209Z"},"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:06:49.774142Z","iopub.execute_input":"2024-01-11T17:06:49.774589Z","iopub.status.idle":"2024-01-11T17:06:49.859294Z","shell.execute_reply.started":"2024-01-11T17:06:49.774553Z","shell.execute_reply":"2024-01-11T17:06:49.858038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['min_value'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:50.203614Z","iopub.execute_input":"2024-01-11T17:06:50.204816Z","iopub.status.idle":"2024-01-11T17:06:50.21106Z","shell.execute_reply.started":"2024-01-11T17:06:50.204772Z","shell.execute_reply":"2024-01-11T17:06:50.210294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['max_value'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:50.649114Z","iopub.execute_input":"2024-01-11T17:06:50.649505Z","iopub.status.idle":"2024-01-11T17:06:50.658249Z","shell.execute_reply.started":"2024-01-11T17:06:50.649475Z","shell.execute_reply":"2024-01-11T17:06:50.656899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['time'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:51.042117Z","iopub.execute_input":"2024-01-11T17:06:51.04319Z","iopub.status.idle":"2024-01-11T17:06:51.053684Z","shell.execute_reply.started":"2024-01-11T17:06:51.04314Z","shell.execute_reply":"2024-01-11T17:06:51.052171Z"},"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:06:51.47661Z","iopub.execute_input":"2024-01-11T17:06:51.477046Z","iopub.status.idle":"2024-01-11T17:06:51.489403Z","shell.execute_reply.started":"2024-01-11T17:06:51.477007Z","shell.execute_reply":"2024-01-11T17:06:51.488069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['license'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:51.944662Z","iopub.execute_input":"2024-01-11T17:06:51.945084Z","iopub.status.idle":"2024-01-11T17:06:51.955644Z","shell.execute_reply.started":"2024-01-11T17:06:51.945045Z","shell.execute_reply":"2024-01-11T17:06:51.954699Z"},"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:06:52.296952Z","iopub.execute_input":"2024-01-11T17:06:52.297401Z","iopub.status.idle":"2024-01-11T17:06:52.321817Z","shell.execute_reply.started":"2024-01-11T17:06:52.297364Z","shell.execute_reply":"2024-01-11T17:06:52.320746Z"},"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:06:52.670848Z","iopub.execute_input":"2024-01-11T17:06:52.6713Z","iopub.status.idle":"2024-01-11T17:06:52.689334Z","shell.execute_reply.started":"2024-01-11T17:06:52.671256Z","shell.execute_reply":"2024-01-11T17:06:52.687905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:53.078314Z","iopub.execute_input":"2024-01-11T17:06:53.079272Z","iopub.status.idle":"2024-01-11T17:06:53.114449Z","shell.execute_reply.started":"2024-01-11T17:06:53.07922Z","shell.execute_reply":"2024-01-11T17:06:53.11322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:06:53.981597Z","iopub.execute_input":"2024-01-11T17:06:53.982024Z","iopub.status.idle":"2024-01-11T17:06:54.054373Z","shell.execute_reply.started":"2024-01-11T17:06:53.98199Z","shell.execute_reply":"2024-01-11T17:06:54.053123Z"},"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:06:54.445388Z","iopub.execute_input":"2024-01-11T17:06:54.445795Z","iopub.status.idle":"2024-01-11T17:06:54.910196Z","shell.execute_reply.started":"2024-01-11T17:06:54.445763Z","shell.execute_reply":"2024-01-11T17:06:54.908904Z"},"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:06:54.963415Z","iopub.execute_input":"2024-01-11T17:06:54.964434Z","iopub.status.idle":"2024-01-11T17:06:55.227145Z","shell.execute_reply.started":"2024-01-11T17:06:54.964389Z","shell.execute_reply":"2024-01-11T17:06:55.226027Z"},"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:06:55.598817Z","iopub.execute_input":"2024-01-11T17:06:55.599644Z","iopub.status.idle":"2024-01-11T17:06:56.034009Z","shell.execute_reply.started":"2024-01-11T17:06:55.599596Z","shell.execute_reply":"2024-01-11T17:06:56.032527Z"},"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:06:56.106361Z","iopub.execute_input":"2024-01-11T17:06:56.106794Z","iopub.status.idle":"2024-01-11T17:06:56.427234Z","shell.execute_reply.started":"2024-01-11T17:06:56.106758Z","shell.execute_reply":"2024-01-11T17:06:56.425822Z"},"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:06:57.207094Z","iopub.execute_input":"2024-01-11T17:06:57.20752Z","iopub.status.idle":"2024-01-11T17:06:57.269835Z","shell.execute_reply.started":"2024-01-11T17:06:57.207484Z","shell.execute_reply":"2024-01-11T17:06:57.268237Z"},"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:06:57.730081Z","iopub.execute_input":"2024-01-11T17:06:57.731397Z","iopub.status.idle":"2024-01-11T17:06:57.739031Z","shell.execute_reply.started":"2024-01-11T17:06:57.73134Z","shell.execute_reply":"2024-01-11T17:06:57.737631Z"},"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:06:58.213041Z","iopub.execute_input":"2024-01-11T17:06:58.213434Z","iopub.status.idle":"2024-01-11T17:07:12.610777Z","shell.execute_reply.started":"2024-01-11T17:06:58.213404Z","shell.execute_reply":"2024-01-11T17:07:12.60957Z"},"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:07:12.612788Z","iopub.execute_input":"2024-01-11T17:07:12.613497Z","iopub.status.idle":"2024-01-11T17:07:14.299355Z","shell.execute_reply.started":"2024-01-11T17:07:12.61345Z","shell.execute_reply":"2024-01-11T17:07:14.298006Z"},"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-11T16:41:42.776566Z","iopub.execute_input":"2024-01-11T16:41:42.776939Z","iopub.status.idle":"2024-01-11T16:41:44.018535Z","shell.execute_reply.started":"2024-01-11T16:41:42.776892Z","shell.execute_reply":"2024-01-11T16:41:44.01749Z"},"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-11T16:41:44.019908Z","iopub.execute_input":"2024-01-11T16:41:44.020983Z","iopub.status.idle":"2024-01-11T16:41:45.756275Z","shell.execute_reply.started":"2024-01-11T16:41:44.020918Z","shell.execute_reply":"2024-01-11T16:41:45.754991Z"},"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-11T16:41:45.757647Z","iopub.execute_input":"2024-01-11T16:41:45.758523Z","iopub.status.idle":"2024-01-11T16:41:46.778137Z","shell.execute_reply.started":"2024-01-11T16:41:45.758479Z","shell.execute_reply":"2024-01-11T16:41:46.776815Z"},"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-11T16:41:46.780015Z","iopub.execute_input":"2024-01-11T16:41:46.780398Z","iopub.status.idle":"2024-01-11T16:41:47.643613Z","shell.execute_reply.started":"2024-01-11T16:41:46.780364Z","shell.execute_reply":"2024-01-11T16:41:47.642442Z"},"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-11T16:41:47.645546Z","iopub.execute_input":"2024-01-11T16:41:47.646359Z","iopub.status.idle":"2024-01-11T16:41:55.76355Z","shell.execute_reply.started":"2024-01-11T16:41:47.646312Z","shell.execute_reply":"2024-01-11T16:41:55.762555Z"},"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-11T16:41:55.765185Z","iopub.execute_input":"2024-01-11T16:41:55.765746Z","iopub.status.idle":"2024-01-11T16:42:01.943385Z","shell.execute_reply.started":"2024-01-11T16:41:55.765709Z","shell.execute_reply":"2024-01-11T16:42:01.942274Z"},"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-11T16:42:01.945112Z","iopub.execute_input":"2024-01-11T16:42:01.945476Z","iopub.status.idle":"2024-01-11T16:42:04.503271Z","shell.execute_reply.started":"2024-01-11T16:42:01.945443Z","shell.execute_reply":"2024-01-11T16:42:04.502265Z"},"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-11T16:42:04.504608Z","iopub.execute_input":"2024-01-11T16:42:04.505436Z","iopub.status.idle":"2024-01-11T16:42:06.869613Z","shell.execute_reply.started":"2024-01-11T16:42:04.505399Z","shell.execute_reply":"2024-01-11T16:42:06.868561Z"},"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-11T16:42:06.871085Z","iopub.execute_input":"2024-01-11T16:42:06.871624Z","iopub.status.idle":"2024-01-11T16:42:08.129208Z","shell.execute_reply.started":"2024-01-11T16:42:06.87159Z","shell.execute_reply":"2024-01-11T16:42:08.127851Z"},"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-11T16:42:08.130969Z","iopub.execute_input":"2024-01-11T16:42:08.131396Z","iopub.status.idle":"2024-01-11T16:42:22.257281Z","shell.execute_reply.started":"2024-01-11T16:42:08.131358Z","shell.execute_reply":"2024-01-11T16:42:22.256021Z"},"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-11T16:42:22.259265Z","iopub.execute_input":"2024-01-11T16:42:22.259749Z","iopub.status.idle":"2024-01-11T16:42:45.954502Z","shell.execute_reply.started":"2024-01-11T16:42:22.259696Z","shell.execute_reply":"2024-01-11T16:42:45.953294Z"},"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\nimport resampy","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:09:38.600224Z","iopub.execute_input":"2024-01-11T17:09:38.600766Z","iopub.status.idle":"2024-01-11T17:09:38.612583Z","shell.execute_reply.started":"2024-01-11T17:09:38.600722Z","shell.execute_reply":"2024-01-11T17:09:38.610725Z"},"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:09:39.412866Z","iopub.execute_input":"2024-01-11T17:09:39.413305Z","iopub.status.idle":"2024-01-11T17:09:39.424277Z","shell.execute_reply.started":"2024-01-11T17:09:39.413272Z","shell.execute_reply":"2024-01-11T17:09:39.42313Z"},"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:09:39.739062Z","iopub.execute_input":"2024-01-11T17:09:39.739723Z","iopub.status.idle":"2024-01-11T17:09:39.74732Z","shell.execute_reply.started":"2024-01-11T17:09:39.739689Z","shell.execute_reply":"2024-01-11T17:09:39.74605Z"},"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:09:39.990851Z","iopub.execute_input":"2024-01-11T17:09:39.991315Z","iopub.status.idle":"2024-01-11T17:09:40.001106Z","shell.execute_reply.started":"2024-01-11T17:09:39.991277Z","shell.execute_reply":"2024-01-11T17:09:39.99964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TARGET_SR = 32000","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:09:40.180936Z","iopub.execute_input":"2024-01-11T17:09:40.18138Z","iopub.status.idle":"2024-01-11T17:09:40.186334Z","shell.execute_reply.started":"2024-01-11T17:09:40.181347Z","shell.execute_reply":"2024-01-11T17:09:40.185347Z"},"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:09:40.390422Z","iopub.execute_input":"2024-01-11T17:09:40.390815Z","iopub.status.idle":"2024-01-11T17:09:40.409764Z","shell.execute_reply.started":"2024-01-11T17:09:40.390783Z","shell.execute_reply":"2024-01-11T17:09:40.408803Z"},"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:09:40.565482Z","iopub.execute_input":"2024-01-11T17:09:40.56629Z","iopub.status.idle":"2024-01-11T17:09:40.577795Z","shell.execute_reply.started":"2024-01-11T17:09:40.566239Z","shell.execute_reply":"2024-01-11T17:09:40.576732Z"},"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:09:40.928557Z","iopub.execute_input":"2024-01-11T17:09:40.928944Z","iopub.status.idle":"2024-01-11T17:09:40.935204Z","shell.execute_reply.started":"2024-01-11T17:09:40.928913Z","shell.execute_reply":"2024-01-11T17:09:40.933873Z"},"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:09:41.04911Z","iopub.execute_input":"2024-01-11T17:09:41.049797Z","iopub.status.idle":"2024-01-11T17:09:41.517832Z","shell.execute_reply.started":"2024-01-11T17:09:41.049763Z","shell.execute_reply":"2024-01-11T17:09:41.516676Z"},"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:09:41.520531Z","iopub.execute_input":"2024-01-11T17:09:41.520914Z","iopub.status.idle":"2024-01-11T17:09:41.546261Z","shell.execute_reply.started":"2024-01-11T17:09:41.52088Z","shell.execute_reply":"2024-01-11T17:09:41.545085Z"},"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:09:41.548419Z","iopub.execute_input":"2024-01-11T17:09:41.548864Z","iopub.status.idle":"2024-01-11T17:09:41.564148Z","shell.execute_reply.started":"2024-01-11T17:09:41.548824Z","shell.execute_reply":"2024-01-11T17:09:41.562984Z"},"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:09:41.745391Z","iopub.execute_input":"2024-01-11T17:09:41.746198Z","iopub.status.idle":"2024-01-11T17:09:41.762374Z","shell.execute_reply.started":"2024-01-11T17:09:41.746152Z","shell.execute_reply":"2024-01-11T17:09:41.761341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install resampy","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:09:41.894976Z","iopub.execute_input":"2024-01-11T17:09:41.896047Z","iopub.status.idle":"2024-01-11T17:09:56.427626Z","shell.execute_reply.started":"2024-01-11T17:09:41.896006Z","shell.execute_reply":"2024-01-11T17:09:56.425945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import resampy","metadata":{"execution":{"iopub.status.busy":"2024-01-11T17:09:56.430609Z","iopub.execute_input":"2024-01-11T17:09:56.431153Z","iopub.status.idle":"2024-01-11T17:09:56.436991Z","shell.execute_reply.started":"2024-01-11T17:09:56.431111Z","shell.execute_reply":"2024-01-11T17:09:56.435451Z"},"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:09:56.438788Z","iopub.execute_input":"2024-01-11T17:09:56.439278Z","iopub.status.idle":"2024-01-11T17:09:56.452766Z","shell.execute_reply.started":"2024-01-11T17:09:56.439237Z","shell.execute_reply":"2024-01-11T17:09:56.45193Z"},"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:09:56.455668Z","iopub.execute_input":"2024-01-11T17:09:56.456357Z","iopub.status.idle":"2024-01-11T17:09:57.319736Z","shell.execute_reply.started":"2024-01-11T17:09:56.456322Z","shell.execute_reply":"2024-01-11T17:09:57.317924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}