{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","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"}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport librosa\nfrom sklearn.preprocessing import LabelEncoder\nimport librosa.display\nimport soundfile as sf \nimport IPython.display as ipd \nfrom matplotlib.animation import FuncAnimation","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:04.981634Z","iopub.execute_input":"2024-01-10T20:17:04.982009Z","iopub.status.idle":"2024-01-10T20:17:04.987867Z","shell.execute_reply.started":"2024-01-10T20:17:04.981981Z","shell.execute_reply":"2024-01-10T20:17:04.986671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/birdsong-recognition/train.csv')\npd.set_option('display.max_column' , None)\npd.set_option('display.max_rows', None)  \npd.set_option('display.max_columns', None)\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:04.997873Z","iopub.execute_input":"2024-01-10T20:17:04.998768Z","iopub.status.idle":"2024-01-10T20:17:05.361411Z","shell.execute_reply.started":"2024-01-10T20:17:04.998732Z","shell.execute_reply":"2024-01-10T20:17:05.360433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:05.363096Z","iopub.execute_input":"2024-01-10T20:17:05.363392Z","iopub.status.idle":"2024-01-10T20:17:05.408988Z","shell.execute_reply.started":"2024-01-10T20:17:05.363366Z","shell.execute_reply":"2024-01-10T20:17:05.407846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"null_columns = data.columns[data.isnull().any()]\nnull_columns\n#identifying the null columns","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:05.410184Z","iopub.execute_input":"2024-01-10T20:17:05.410717Z","iopub.status.idle":"2024-01-10T20:17:05.448861Z","shell.execute_reply.started":"2024-01-10T20:17:05.410687Z","shell.execute_reply":"2024-01-10T20:17:05.447945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['rating'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:05.450982Z","iopub.execute_input":"2024-01-10T20:17:05.451286Z","iopub.status.idle":"2024-01-10T20:17:05.459269Z","shell.execute_reply.started":"2024-01-10T20:17:05.451258Z","shell.execute_reply":"2024-01-10T20:17:05.458531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(style=\"whitegrid\")  # Set the background style\n\n# Define a custom color palette\ncustom_palette = sns.color_palette(\"viridis\", n_colors=len(data['rating'].unique()))\n\nsns.countplot(x='rating', data=data, order=data['rating'].value_counts().index, palette=custom_palette)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:05.460986Z","iopub.execute_input":"2024-01-10T20:17:05.461875Z","iopub.status.idle":"2024-01-10T20:17:05.821082Z","shell.execute_reply.started":"2024-01-10T20:17:05.461845Z","shell.execute_reply":"2024-01-10T20:17:05.820060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['playback_used'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:05.822064Z","iopub.execute_input":"2024-01-10T20:17:05.822363Z","iopub.status.idle":"2024-01-10T20:17:05.830560Z","shell.execute_reply.started":"2024-01-10T20:17:05.822338Z","shell.execute_reply":"2024-01-10T20:17:05.829314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['playback_used'].value_counts(dropna=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:05.831715Z","iopub.execute_input":"2024-01-10T20:17:05.831992Z","iopub.status.idle":"2024-01-10T20:17:05.843138Z","shell.execute_reply.started":"2024-01-10T20:17:05.831967Z","shell.execute_reply":"2024-01-10T20:17:05.842103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['playback_used'].fillna('no' , inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:05.845204Z","iopub.execute_input":"2024-01-10T20:17:05.845632Z","iopub.status.idle":"2024-01-10T20:17:05.854847Z","shell.execute_reply.started":"2024-01-10T20:17:05.845596Z","shell.execute_reply":"2024-01-10T20:17:05.853852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playback_used_count = data['playback_used'].value_counts()\n\ncolors = plt.cm.Set1.colors\n\nplt.figure(figsize=(4, 4))\nplt.pie(playback_used_count, labels=playback_used_count.index, autopct='%1.1f%%', startangle=90, colors=colors, wedgeprops=dict(width=0.4))\n\nplt.title('Distribution of Playback Used', fontsize=16)\n\nplt.legend(playback_used_count.index, loc='best', bbox_to_anchor=(0.8, 0.8))\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:05.855765Z","iopub.execute_input":"2024-01-10T20:17:05.856569Z","iopub.status.idle":"2024-01-10T20:17:06.116668Z","shell.execute_reply.started":"2024-01-10T20:17:05.856541Z","shell.execute_reply":"2024-01-10T20:17:06.115320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['ebird_code'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:06.121792Z","iopub.execute_input":"2024-01-10T20:17:06.122239Z","iopub.status.idle":"2024-01-10T20:17:06.132523Z","shell.execute_reply.started":"2024-01-10T20:17:06.122200Z","shell.execute_reply":"2024-01-10T20:17:06.131016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['channels'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:06.134042Z","iopub.execute_input":"2024-01-10T20:17:06.134789Z","iopub.status.idle":"2024-01-10T20:17:06.145245Z","shell.execute_reply.started":"2024-01-10T20:17:06.134736Z","shell.execute_reply":"2024-01-10T20:17:06.144184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"channels_count = data['channels'].value_counts()\nplt.pie(channels_count, labels=channels_count.index, autopct='%1.1f%%', startangle=90)\nplt.title('Pie Plot of channels')\nplt.legend(channels_count.index, title='Channels', loc='upper right')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:06.146905Z","iopub.execute_input":"2024-01-10T20:17:06.147813Z","iopub.status.idle":"2024-01-10T20:17:06.362880Z","shell.execute_reply.started":"2024-01-10T20:17:06.147770Z","shell.execute_reply":"2024-01-10T20:17:06.361769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['channels'] = data['channels'].astype(str).str[0].astype(int)\ndata['channels'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:06.364617Z","iopub.execute_input":"2024-01-10T20:17:06.365220Z","iopub.status.idle":"2024-01-10T20:17:06.408549Z","shell.execute_reply.started":"2024-01-10T20:17:06.365177Z","shell.execute_reply":"2024-01-10T20:17:06.407515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['date'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:06.412654Z","iopub.execute_input":"2024-01-10T20:17:06.413168Z","iopub.status.idle":"2024-01-10T20:17:06.428533Z","shell.execute_reply.started":"2024-01-10T20:17:06.413127Z","shell.execute_reply":"2024-01-10T20:17:06.427317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['year'] = data['date'].apply(lambda x : x.split('-')[0]).astype(int)\ndata['month'] = data['date'].apply(lambda x : x.split('-')[1]).astype(int)\ndata['day'] = data['date'].apply(lambda x : x.split('-')[2]).astype(int)\ndata = data.drop(['date'] , axis=1)\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:06.429926Z","iopub.execute_input":"2024-01-10T20:17:06.430307Z","iopub.status.idle":"2024-01-10T20:17:06.512260Z","shell.execute_reply.started":"2024-01-10T20:17:06.430274Z","shell.execute_reply":"2024-01-10T20:17:06.511233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(6,6))\nsns.countplot(x='year', data=data)\n\nplt.xticks(rotation =90)\nplt.title('Count Plot of Years')\nplt.xlabel('Year')\nplt.ylabel('Count')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:06.513654Z","iopub.execute_input":"2024-01-10T20:17:06.513986Z","iopub.status.idle":"2024-01-10T20:17:07.288833Z","shell.execute_reply.started":"2024-01-10T20:17:06.513958Z","shell.execute_reply":"2024-01-10T20:17:07.287894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(6,6))\nsns.countplot(x='month', data=data)\n\nplt.xticks(rotation =90)\nplt.title('Count Plot of months')\nplt.xlabel('Year')\nplt.ylabel('Count')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:07.290448Z","iopub.execute_input":"2024-01-10T20:17:07.291021Z","iopub.status.idle":"2024-01-10T20:17:07.652651Z","shell.execute_reply.started":"2024-01-10T20:17:07.290989Z","shell.execute_reply":"2024-01-10T20:17:07.651567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(6,6))\nsns.countplot(x='day', data=data)\n\nplt.xticks(rotation =90)\nplt.title('Count Plot of months')\nplt.xlabel('Year')\nplt.ylabel('Count')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:07.654098Z","iopub.execute_input":"2024-01-10T20:17:07.654848Z","iopub.status.idle":"2024-01-10T20:17:08.481139Z","shell.execute_reply.started":"2024-01-10T20:17:07.654810Z","shell.execute_reply":"2024-01-10T20:17:08.480071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['pitch'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:08.482696Z","iopub.execute_input":"2024-01-10T20:17:08.483119Z","iopub.status.idle":"2024-01-10T20:17:08.491956Z","shell.execute_reply.started":"2024-01-10T20:17:08.483081Z","shell.execute_reply":"2024-01-10T20:17:08.490879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(6,6))\nsns.countplot(x='pitch', data=data)\n\nplt.xticks(rotation =90)\nplt.title('Plot of Pitche')\nplt.xlabel('pitch')\nplt.ylabel('Count')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:08.493418Z","iopub.execute_input":"2024-01-10T20:17:08.494300Z","iopub.status.idle":"2024-01-10T20:17:08.849887Z","shell.execute_reply.started":"2024-01-10T20:17:08.494260Z","shell.execute_reply":"2024-01-10T20:17:08.848844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['duration'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:08.851031Z","iopub.execute_input":"2024-01-10T20:17:08.851340Z","iopub.status.idle":"2024-01-10T20:17:08.860221Z","shell.execute_reply.started":"2024-01-10T20:17:08.851312Z","shell.execute_reply":"2024-01-10T20:17:08.859036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.drop(['duration'] , axis=1 , inplace=True)\ndata['speed'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:08.861312Z","iopub.execute_input":"2024-01-10T20:17:08.861635Z","iopub.status.idle":"2024-01-10T20:17:08.881771Z","shell.execute_reply.started":"2024-01-10T20:17:08.861608Z","shell.execute_reply":"2024-01-10T20:17:08.880849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(6,6))\nsns.countplot(x='speed', data=data)\n\nplt.xticks(rotation =90)\nplt.title('Plot of speed')\nplt.xlabel('speed')\nplt.ylabel('Count')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:08.882960Z","iopub.execute_input":"2024-01-10T20:17:08.883264Z","iopub.status.idle":"2024-01-10T20:17:09.243181Z","shell.execute_reply.started":"2024-01-10T20:17:08.883236Z","shell.execute_reply":"2024-01-10T20:17:09.242006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.drop(['pitch'] , axis=1)\ndata = data.drop(['speed'] , axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:09.244312Z","iopub.execute_input":"2024-01-10T20:17:09.244653Z","iopub.status.idle":"2024-01-10T20:17:09.272781Z","shell.execute_reply.started":"2024-01-10T20:17:09.244623Z","shell.execute_reply":"2024-01-10T20:17:09.271548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['species'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:09.274189Z","iopub.execute_input":"2024-01-10T20:17:09.274474Z","iopub.status.idle":"2024-01-10T20:17:09.282545Z","shell.execute_reply.started":"2024-01-10T20:17:09.274448Z","shell.execute_reply":"2024-01-10T20:17:09.281544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(data['number_of_notes'].unique())\ndata['number_of_notes'].value_counts(dropna=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:09.283646Z","iopub.execute_input":"2024-01-10T20:17:09.284043Z","iopub.status.idle":"2024-01-10T20:17:09.297010Z","shell.execute_reply.started":"2024-01-10T20:17:09.283985Z","shell.execute_reply":"2024-01-10T20:17:09.295844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(6,6))\nsns.countplot(x='number_of_notes', data=data)\n\nplt.xticks(rotation =90)\nplt.title('Plot of notes')\nplt.xlabel('number_of_notes')\nplt.ylabel('Count')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:09.298735Z","iopub.execute_input":"2024-01-10T20:17:09.299780Z","iopub.status.idle":"2024-01-10T20:17:09.650071Z","shell.execute_reply.started":"2024-01-10T20:17:09.299730Z","shell.execute_reply":"2024-01-10T20:17:09.649022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['bird_seen'].value_counts(dropna=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:09.660640Z","iopub.execute_input":"2024-01-10T20:17:09.661434Z","iopub.status.idle":"2024-01-10T20:17:09.668965Z","shell.execute_reply.started":"2024-01-10T20:17:09.661398Z","shell.execute_reply":"2024-01-10T20:17:09.667890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.fillna('yes' , inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:09.670742Z","iopub.execute_input":"2024-01-10T20:17:09.671277Z","iopub.status.idle":"2024-01-10T20:17:09.733499Z","shell.execute_reply.started":"2024-01-10T20:17:09.671232Z","shell.execute_reply":"2024-01-10T20:17:09.732599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(4,4))\nsns.countplot(x='bird_seen', data=data)\n\nplt.xticks(rotation =90)\nplt.title('Plot of does the bird seen')\nplt.xlabel('bird seen')\nplt.ylabel('Count')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:09.734554Z","iopub.execute_input":"2024-01-10T20:17:09.734816Z","iopub.status.idle":"2024-01-10T20:17:10.031838Z","shell.execute_reply.started":"2024-01-10T20:17:09.734792Z","shell.execute_reply":"2024-01-10T20:17:10.030273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(data['latitude'].nunique())\nprint(data['longitude'].nunique())","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:10.032934Z","iopub.execute_input":"2024-01-10T20:17:10.033263Z","iopub.status.idle":"2024-01-10T20:17:10.043195Z","shell.execute_reply.started":"2024-01-10T20:17:10.033236Z","shell.execute_reply":"2024-01-10T20:17:10.042086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data[data['latitude'] != 'Not specified']\ndata = data[data['longitude'] != 'Not specified']\ndata['latitude']  = data['latitude'].astype(float)\ndata['longitude']  = data['longitude'].astype(float)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:10.044632Z","iopub.execute_input":"2024-01-10T20:17:10.045308Z","iopub.status.idle":"2024-01-10T20:17:10.104333Z","shell.execute_reply.started":"2024-01-10T20:17:10.045267Z","shell.execute_reply":"2024-01-10T20:17:10.103473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['sampling_rate'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:10.105648Z","iopub.execute_input":"2024-01-10T20:17:10.106026Z","iopub.status.idle":"2024-01-10T20:17:10.116295Z","shell.execute_reply.started":"2024-01-10T20:17:10.105990Z","shell.execute_reply":"2024-01-10T20:17:10.115278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['sampling_rate(in Hz)'] = data['sampling_rate'].apply(lambda x : x.split(' ')[0]).astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:10.117537Z","iopub.execute_input":"2024-01-10T20:17:10.117903Z","iopub.status.idle":"2024-01-10T20:17:10.135669Z","shell.execute_reply.started":"2024-01-10T20:17:10.117875Z","shell.execute_reply":"2024-01-10T20:17:10.134728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))  \n\nax = sns.countplot(x='sampling_rate(in Hz)', data=data)\n\nfor p in ax.patches:\n    ax.annotate(f'{p.get_height()}', (p.get_x() + p.get_width() / 2., p.get_height()),\n                ha='center', va='center', xytext=(0, 10), textcoords='offset points', fontsize=8)\n\nplt.xticks(rotation=90)\nplt.title('Plot of sampling rate')\nplt.xlabel('Sampling rate')\nplt.ylabel('Count')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:10.137381Z","iopub.execute_input":"2024-01-10T20:17:10.138615Z","iopub.status.idle":"2024-01-10T20:17:10.575865Z","shell.execute_reply.started":"2024-01-10T20:17:10.138573Z","shell.execute_reply":"2024-01-10T20:17:10.574616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['type'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:10.577638Z","iopub.execute_input":"2024-01-10T20:17:10.578060Z","iopub.status.idle":"2024-01-10T20:17:10.587035Z","shell.execute_reply.started":"2024-01-10T20:17:10.578018Z","shell.execute_reply":"2024-01-10T20:17:10.585908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.drop(['type'] , axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:10.589076Z","iopub.execute_input":"2024-01-10T20:17:10.589537Z","iopub.status.idle":"2024-01-10T20:17:10.605043Z","shell.execute_reply.started":"2024-01-10T20:17:10.589467Z","shell.execute_reply":"2024-01-10T20:17:10.604188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['elevation'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:10.606775Z","iopub.execute_input":"2024-01-10T20:17:10.607191Z","iopub.status.idle":"2024-01-10T20:17:10.616033Z","shell.execute_reply.started":"2024-01-10T20:17:10.607150Z","shell.execute_reply":"2024-01-10T20:17:10.615095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['elevation'] = data['elevation'].replace(['? m' , 'Unknown m'  ,' m'] , '99999')","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:10.617856Z","iopub.execute_input":"2024-01-10T20:17:10.618299Z","iopub.status.idle":"2024-01-10T20:17:10.629861Z","shell.execute_reply.started":"2024-01-10T20:17:10.618261Z","shell.execute_reply":"2024-01-10T20:17:10.628911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport re\n\n# Assuming 'elevation' is the name of the column in your DataFrame\ndata['elevation'] = data['elevation'].astype(str).str.replace('~', '').str.replace('-', '')\n\ndef extract_numeric(value):\n    try:\n        numeric_part = float(re.search(r'\\d+\\.\\d+|\\d+', value).group())\n        return int(numeric_part)\n    except (ValueError, AttributeError):\n        return None  # or any other default value you want to use for non-numeric entries\n\ndata['elevation'] = data['elevation'].apply(extract_numeric)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:10.631261Z","iopub.execute_input":"2024-01-10T20:17:10.631610Z","iopub.status.idle":"2024-01-10T20:17:10.691922Z","shell.execute_reply.started":"2024-01-10T20:17:10.631580Z","shell.execute_reply":"2024-01-10T20:17:10.690746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['elevation'] = data['elevation'].astype(float).fillna(0)  # Convert to float and fill NaN with 0\ndata['elevation'] = round(data['elevation']).astype(int)  # Round and convert to int\n","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:10.693573Z","iopub.execute_input":"2024-01-10T20:17:10.693885Z","iopub.status.idle":"2024-01-10T20:17:10.700339Z","shell.execute_reply.started":"2024-01-10T20:17:10.693857Z","shell.execute_reply":"2024-01-10T20:17:10.699408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['elevation'] = round(data['elevation']).astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:10.701375Z","iopub.execute_input":"2024-01-10T20:17:10.701695Z","iopub.status.idle":"2024-01-10T20:17:10.710785Z","shell.execute_reply.started":"2024-01-10T20:17:10.701668Z","shell.execute_reply":"2024-01-10T20:17:10.709842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data['elevation'] = data['elevation'].apply(lambda x : x.split(' ')[0]).astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:10.712147Z","iopub.execute_input":"2024-01-10T20:17:10.712429Z","iopub.status.idle":"2024-01-10T20:17:10.721425Z","shell.execute_reply.started":"2024-01-10T20:17:10.712405Z","shell.execute_reply":"2024-01-10T20:17:10.720420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['elevation'] = pd.to_numeric(data['elevation'] ,errors='coerce')\nsum_elev = 0\ncount_valid_values = 0\n\nfor index  , row in data.iterrows():\n    if row['elevation'] != 99999:\n        sum_elev += row['elevation']\n        count_valid_values += 1\n\nmean_elev =  sum_elev/count_valid_values\nmean_elev ","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:10.722653Z","iopub.execute_input":"2024-01-10T20:17:10.722972Z","iopub.status.idle":"2024-01-10T20:17:12.071994Z","shell.execute_reply.started":"2024-01-10T20:17:10.722944Z","shell.execute_reply":"2024-01-10T20:17:12.070957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['elevation'] = data['elevation'].replace(99999, 'mean_elev')\n\ndata['elevation'] = pd.to_numeric(data['elevation'], errors='coerce').fillna(0).astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:12.073511Z","iopub.execute_input":"2024-01-10T20:17:12.074100Z","iopub.status.idle":"2024-01-10T20:17:12.090737Z","shell.execute_reply.started":"2024-01-10T20:17:12.074061Z","shell.execute_reply":"2024-01-10T20:17:12.089524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['bitrate_of_mp3'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:12.091920Z","iopub.execute_input":"2024-01-10T20:17:12.092233Z","iopub.status.idle":"2024-01-10T20:17:12.102901Z","shell.execute_reply.started":"2024-01-10T20:17:12.092207Z","shell.execute_reply":"2024-01-10T20:17:12.101694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndata['bitrate_of_mp3'] = pd.to_numeric(data['bitrate_of_mp3'].str.extract('(\\d+)')[0], errors='coerce')\n\nmean_value = data['bitrate_of_mp3'].mean(skipna=True)\n\ndata['bitrate_of_mp3'] = data['bitrate_of_mp3'].fillna(mean_value).astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:12.104249Z","iopub.execute_input":"2024-01-10T20:17:12.105374Z","iopub.status.idle":"2024-01-10T20:17:12.161764Z","shell.execute_reply.started":"2024-01-10T20:17:12.105334Z","shell.execute_reply":"2024-01-10T20:17:12.160945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['bitrate_of_mp3'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:12.163114Z","iopub.execute_input":"2024-01-10T20:17:12.163770Z","iopub.status.idle":"2024-01-10T20:17:12.170899Z","shell.execute_reply.started":"2024-01-10T20:17:12.163733Z","shell.execute_reply":"2024-01-10T20:17:12.169883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.drop(data[data['bitrate_of_mp3'] == 3 ].index , inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:12.172613Z","iopub.execute_input":"2024-01-10T20:17:12.172992Z","iopub.status.idle":"2024-01-10T20:17:12.190620Z","shell.execute_reply.started":"2024-01-10T20:17:12.172956Z","shell.execute_reply":"2024-01-10T20:17:12.189708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data[data['bitrate_of_mp3'].isna()]","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:12.192086Z","iopub.execute_input":"2024-01-10T20:17:12.192803Z","iopub.status.idle":"2024-01-10T20:17:12.209718Z","shell.execute_reply.started":"2024-01-10T20:17:12.192762Z","shell.execute_reply":"2024-01-10T20:17:12.208559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['file_type'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:12.210885Z","iopub.execute_input":"2024-01-10T20:17:12.211780Z","iopub.status.idle":"2024-01-10T20:17:12.221680Z","shell.execute_reply.started":"2024-01-10T20:17:12.211747Z","shell.execute_reply":"2024-01-10T20:17:12.220781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(4, 6))  \n\nax = sns.countplot(x='file_type', data=data)\n\nfor p in ax.patches:\n    ax.annotate(f'{p.get_height()}', (p.get_x() + p.get_width() / 2., p.get_height()),\n                ha='center', va='center', xytext=(0, 10), textcoords='offset points', fontsize=8)\n\nplt.xticks(rotation=90)\nplt.title('Plot of file_type')\nplt.xlabel('file_type')\nplt.ylabel('Count')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:12.222785Z","iopub.execute_input":"2024-01-10T20:17:12.223584Z","iopub.status.idle":"2024-01-10T20:17:12.582286Z","shell.execute_reply.started":"2024-01-10T20:17:12.223552Z","shell.execute_reply":"2024-01-10T20:17:12.581447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['volume'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:12.583513Z","iopub.execute_input":"2024-01-10T20:17:12.584209Z","iopub.status.idle":"2024-01-10T20:17:12.592132Z","shell.execute_reply.started":"2024-01-10T20:17:12.584173Z","shell.execute_reply":"2024-01-10T20:17:12.591405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(4, 6))  \n\nax = sns.countplot(x='volume', data=data)\n\nfor p in ax.patches:\n    ax.annotate(f'{p.get_height()}', (p.get_x() + p.get_width() / 2., p.get_height()),\n                ha='center', va='center', xytext=(0, 10), textcoords='offset points', fontsize=8)\n\nplt.xticks(rotation=90)\nplt.title('Plot of file_type')\nplt.xlabel('file_type')\nplt.ylabel('Count')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:12.593343Z","iopub.execute_input":"2024-01-10T20:17:12.593671Z","iopub.status.idle":"2024-01-10T20:17:12.965079Z","shell.execute_reply.started":"2024-01-10T20:17:12.593642Z","shell.execute_reply":"2024-01-10T20:17:12.963863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.drop(['volume'] , axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:12.966448Z","iopub.execute_input":"2024-01-10T20:17:12.966805Z","iopub.status.idle":"2024-01-10T20:17:12.981519Z","shell.execute_reply.started":"2024-01-10T20:17:12.966776Z","shell.execute_reply":"2024-01-10T20:17:12.980660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['background'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:12.982855Z","iopub.execute_input":"2024-01-10T20:17:12.983168Z","iopub.status.idle":"2024-01-10T20:17:12.993659Z","shell.execute_reply.started":"2024-01-10T20:17:12.983142Z","shell.execute_reply":"2024-01-10T20:17:12.992512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.drop(['background'] , axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:12.995289Z","iopub.execute_input":"2024-01-10T20:17:12.996205Z","iopub.status.idle":"2024-01-10T20:17:13.010248Z","shell.execute_reply.started":"2024-01-10T20:17:12.996165Z","shell.execute_reply":"2024-01-10T20:17:13.009146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['xc_id'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:13.012053Z","iopub.execute_input":"2024-01-10T20:17:13.012610Z","iopub.status.idle":"2024-01-10T20:17:13.021023Z","shell.execute_reply.started":"2024-01-10T20:17:13.012570Z","shell.execute_reply":"2024-01-10T20:17:13.020250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.drop(['xc_id'] , axis=1)\ndata = data.drop(['url'] , axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:13.022075Z","iopub.execute_input":"2024-01-10T20:17:13.022441Z","iopub.status.idle":"2024-01-10T20:17:13.046177Z","shell.execute_reply.started":"2024-01-10T20:17:13.022415Z","shell.execute_reply":"2024-01-10T20:17:13.045313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['country'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:13.047641Z","iopub.execute_input":"2024-01-10T20:17:13.048793Z","iopub.status.idle":"2024-01-10T20:17:13.057810Z","shell.execute_reply.started":"2024-01-10T20:17:13.048749Z","shell.execute_reply":"2024-01-10T20:17:13.056970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['country'] = data['country'].str.strip(\"'[]\").str.replace(\"'\", \"\").str.split(\", \")\n\n# Flatten the list of country\nall_country = [country for sublist in data['country'].dropna() for country in sublist]\n\n# Create a DataFrame with country counts\ncountry_counts = pd.Series(all_country).value_counts().reset_index()\ncountry_counts.columns = ['Country', 'Count']\n\n# Sort the DataFrame by 'Count' in descending order\ncountry_counts_sorted = country_counts.sort_values(by='Count', ascending=False)\n\n# Plot the countplot for the top 10 country\nplt.figure(figsize=(12, 6))\nsns.barplot(x='Country', y='Count', data=country_counts_sorted.head(10))\nplt.title('Top 10 country by Count')\nplt.xlabel('Country')\nplt.ylabel('Count')\nplt.xticks(rotation=45, ha='right')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:13.059296Z","iopub.execute_input":"2024-01-10T20:17:13.059942Z","iopub.status.idle":"2024-01-10T20:17:13.484284Z","shell.execute_reply.started":"2024-01-10T20:17:13.059904Z","shell.execute_reply":"2024-01-10T20:17:13.483178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(data['author'].nunique())\nprint(data['recordist'].nunique())","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:13.485609Z","iopub.execute_input":"2024-01-10T20:17:13.485940Z","iopub.status.idle":"2024-01-10T20:17:13.495570Z","shell.execute_reply.started":"2024-01-10T20:17:13.485881Z","shell.execute_reply":"2024-01-10T20:17:13.494700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.drop(['author'] , axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:13.497116Z","iopub.execute_input":"2024-01-10T20:17:13.497580Z","iopub.status.idle":"2024-01-10T20:17:13.513569Z","shell.execute_reply.started":"2024-01-10T20:17:13.497541Z","shell.execute_reply":"2024-01-10T20:17:13.512786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['primary_label'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:13.514559Z","iopub.execute_input":"2024-01-10T20:17:13.515251Z","iopub.status.idle":"2024-01-10T20:17:13.526410Z","shell.execute_reply.started":"2024-01-10T20:17:13.515214Z","shell.execute_reply":"2024-01-10T20:17:13.525509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.drop(['primary_label'] , axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:13.527606Z","iopub.execute_input":"2024-01-10T20:17:13.527983Z","iopub.status.idle":"2024-01-10T20:17:13.542257Z","shell.execute_reply.started":"2024-01-10T20:17:13.527946Z","shell.execute_reply":"2024-01-10T20:17:13.541542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['length'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:13.543439Z","iopub.execute_input":"2024-01-10T20:17:13.544211Z","iopub.status.idle":"2024-01-10T20:17:13.552321Z","shell.execute_reply.started":"2024-01-10T20:17:13.544171Z","shell.execute_reply":"2024-01-10T20:17:13.551295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['length'] = data['length'].replace('Not specified', '0-0(s)')\ndata['length'] = data['length'].fillna('0-0(s)')\ndata[['min_length', 'max_length']] = data['length'].str.extract(r'(\\d+)-(\\d+)\\(s\\)')\ndata[['min_length', 'max_length']] = data[['min_length', 'max_length']].astype(float)\ndata = data.drop(['length'] , axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:13.553909Z","iopub.execute_input":"2024-01-10T20:17:13.554531Z","iopub.status.idle":"2024-01-10T20:17:13.614710Z","shell.execute_reply.started":"2024-01-10T20:17:13.554476Z","shell.execute_reply":"2024-01-10T20:17:13.613862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['time'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:13.616447Z","iopub.execute_input":"2024-01-10T20:17:13.616875Z","iopub.status.idle":"2024-01-10T20:17:13.624976Z","shell.execute_reply.started":"2024-01-10T20:17:13.616838Z","shell.execute_reply":"2024-01-10T20:17:13.623847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['time'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:13.626796Z","iopub.execute_input":"2024-01-10T20:17:13.627499Z","iopub.status.idle":"2024-01-10T20:17:13.637703Z","shell.execute_reply.started":"2024-01-10T20:17:13.627440Z","shell.execute_reply":"2024-01-10T20:17:13.636508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['license'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:13.639298Z","iopub.execute_input":"2024-01-10T20:17:13.639701Z","iopub.status.idle":"2024-01-10T20:17:13.650077Z","shell.execute_reply.started":"2024-01-10T20:17:13.639663Z","shell.execute_reply":"2024-01-10T20:17:13.649006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le  = LabelEncoder()\ndata['license'] = le.fit_transform(data['license'])\ndata['license'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:13.651347Z","iopub.execute_input":"2024-01-10T20:17:13.651646Z","iopub.status.idle":"2024-01-10T20:17:13.663429Z","shell.execute_reply.started":"2024-01-10T20:17:13.651620Z","shell.execute_reply":"2024-01-10T20:17:13.662320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:13.665895Z","iopub.execute_input":"2024-01-10T20:17:13.666527Z","iopub.status.idle":"2024-01-10T20:17:13.694084Z","shell.execute_reply.started":"2024-01-10T20:17:13.666470Z","shell.execute_reply":"2024-01-10T20:17:13.693318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Feature Engineering\n","metadata":{}},{"cell_type":"code","source":"# #to print the unquie entries indices\n# unique_entries = data['ebird_code'].drop_duplicates()\n\n# for index in unique_entries.index:\n#     print(f\"Index: {index}, Value: {unique_entries[index]}\")","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:13.695335Z","iopub.execute_input":"2024-01-10T20:17:13.696400Z","iopub.status.idle":"2024-01-10T20:17:13.702658Z","shell.execute_reply.started":"2024-01-10T20:17:13.696357Z","shell.execute_reply":"2024-01-10T20:17:13.701814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = data.iloc[[0 ,1 , 1147 , 1148 , 4754 , 4755 , 7221 , 7222 , 21088 , 21089]].reset_index(drop=True)\ntrain.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:13.703678Z","iopub.execute_input":"2024-01-10T20:17:13.704239Z","iopub.status.idle":"2024-01-10T20:17:13.739450Z","shell.execute_reply.started":"2024-01-10T20:17:13.704200Z","shell.execute_reply":"2024-01-10T20:17:13.738423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_directory = '/kaggle/input/birdsong-recognition/train_audio/'\n\n# Create a new column 'file_location' by combining 'category' and 'file_name'\naudio = base_directory + train['ebird_code'] + '/' + train['filename']\naudio","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:13.740575Z","iopub.execute_input":"2024-01-10T20:17:13.740897Z","iopub.status.idle":"2024-01-10T20:17:13.749361Z","shell.execute_reply.started":"2024-01-10T20:17:13.740871Z","shell.execute_reply":"2024-01-10T20:17:13.748364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_list = []\nsr_list = []\n\n# Loop through the 'audio' column and load each audio file\nfor audio_file in audio:\n    y_, sr_ = librosa.load(audio_file)\n    y_list.append(y_)\n    sr_list.append(sr_)\n\n# Add 'y' and 'sr' columns to the DataFrame\ntrain['y'] = y_list\ntrain['sr'] = sr_list","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:13.750654Z","iopub.execute_input":"2024-01-10T20:17:13.751526Z","iopub.status.idle":"2024-01-10T20:17:25.016845Z","shell.execute_reply.started":"2024-01-10T20:17:13.751461Z","shell.execute_reply":"2024-01-10T20:17:25.015887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:25.018311Z","iopub.execute_input":"2024-01-10T20:17:25.019109Z","iopub.status.idle":"2024-01-10T20:17:25.023235Z","shell.execute_reply.started":"2024-01-10T20:17:25.019074Z","shell.execute_reply":"2024-01-10T20:17:25.022282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(10):\n    print(f\"Audio {i}: {train.loc[i, 'ebird_code']}\")\n    ipd.display(ipd.Audio(train.loc[i, 'y'], rate=train.loc[i, 'sr']))","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:25.024303Z","iopub.execute_input":"2024-01-10T20:17:25.025097Z","iopub.status.idle":"2024-01-10T20:17:25.405500Z","shell.execute_reply.started":"2024-01-10T20:17:25.025046Z","shell.execute_reply":"2024-01-10T20:17:25.404518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_files = 10\n\n# Configure the grid layout\nnum_rows = 2\nnum_cols = num_files // num_rows\n\n# Create subplots\nfig, axes = plt.subplots(num_rows, num_cols, figsize=(15, 5))\n\n# Display audio files in a grid layout\nfor i, ax in enumerate(axes.flatten()):\n    if i < num_files:\n        ax.set_title(f\"Audio {i}: {train.loc[i, 'species']}\")\n        ax.plot(train.loc[i, 'y'])  # You might want to use plot here based on the structure of your 'y'\n        ax.axis('off')  # Turn off axis for a cleaner look\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:25.406767Z","iopub.execute_input":"2024-01-10T20:17:25.407405Z","iopub.status.idle":"2024-01-10T20:17:28.004375Z","shell.execute_reply.started":"2024-01-10T20:17:25.407374Z","shell.execute_reply":"2024-01-10T20:17:28.003363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" train['mfcc'] = None\n\n# Loop through the DataFrame and calculate MFCCs for each audio file\nfor i in range(len(train)):\n    y = train.loc[i, 'y']\n    sr = train.loc[i, 'sr']\n    mfcc = librosa.feature.mfcc(y=y, sr=sr)\n    train.at[i, 'mfcc'] = mfcc","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:28.005550Z","iopub.execute_input":"2024-01-10T20:17:28.005868Z","iopub.status.idle":"2024-01-10T20:17:30.243382Z","shell.execute_reply.started":"2024-01-10T20:17:28.005839Z","shell.execute_reply":"2024-01-10T20:17:30.242045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['mel_spectrogram'] = None\n\n# Loop through the DataFrame and calculate Mel-spectrograms for each audio file\nfor i in range(len(train)):\n    y = train.loc[i, 'y']\n    sr = train.loc[i, 'sr']\n    \n    # Calculate Mel-spectrogram\n    mel_signal = librosa.feature.melspectrogram(y=y, sr=sr, hop_length=2, n_fft=200)\n    spectrogram = np.abs(mel_signal)\n    power_to_db = librosa.power_to_db(spectrogram, ref=np.max)\n    \n    # Store the result in the 'mel_spectrogram' column\n    train.at[i, 'mel_spectrogram'] = power_to_db\n\n# Display Mel-spectrograms for the first 10 audio files\nnum_files_to_display = 10\nfor i in range(num_files_to_display):\n    mel_spectrogram = train.loc[i, 'mel_spectrogram']\n    \n    # Plot the Mel-spectrogram\n    plt.figure(figsize=(7, 6))\n    librosa.display.specshow(mel_spectrogram, sr=train.loc[i, 'sr'], x_axis='time', y_axis='mel', cmap='magma', hop_length=2)\n    \n    plt.colorbar(label='dB')\n    plt.title(f'Mel-Spectrogram for {train.loc[i, \"ebird_code\"]}', fontdict=dict(size=18))\n    plt.xlabel('Time', fontdict=dict(size=15))\n    plt.ylabel('Frequency', fontdict=dict(size=15))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:30.245525Z","iopub.execute_input":"2024-01-10T20:17:30.246399Z","iopub.status.idle":"2024-01-10T20:17:51.074819Z","shell.execute_reply.started":"2024-01-10T20:17:30.246354Z","shell.execute_reply":"2024-01-10T20:17:51.072636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['zcr'] = None\n\n# Loop through the DataFrame and calculate zero-crossing rates for each audio file\nfor i in range(len(train)):\n    y = train.loc[i, 'y']\n    zcr = librosa.feature.zero_crossing_rate(y)\n    train.at[i, 'zcr'] = zcr\n\n# Plot Zero Crossing Rate for all audio files\nnum_files_to_plot = 10  # Change this based on the number of files you want to plot\nplt.figure(figsize=(15, 10))\n\nfor i in range(num_files_to_plot):\n    zcr_values = train.loc[i, 'zcr'][0]\n    plt.subplot(4, 3, i+1)  # Assuming you want a 2x5 grid for the first 10 files\n    plt.plot(zcr_values, label='ZCR')\n    plt.title(f'ZCR - {train.loc[i, \"ebird_code\"]}')\n    plt.legend()\n    plt.xlabel('Frame Index')\n    plt.ylabel('Zero Crossing Rate')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:18:37.874464Z","iopub.execute_input":"2024-01-10T20:18:37.875552Z","iopub.status.idle":"2024-01-10T20:18:40.985593Z","shell.execute_reply.started":"2024-01-10T20:18:37.875514Z","shell.execute_reply":"2024-01-10T20:18:40.984545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['rms_energy'] = None\n\nfor i in range(len(train)):\n    y = train.loc[i, 'y']\n    rms_energy = librosa.feature.rms(y=y)\n    train.at[i, 'rms_energy'] = rms_energy\n\n# Plot RMS Energy for all audio files\nnum_files_to_plot = 10  # Change this based on the number of files you want to plot\nplt.figure(figsize=(15, 10))\n\nfor i in range(num_files_to_plot):\n    rms_energy_values = train.loc[i, 'rms_energy'][0]\n    plt.subplot(4, 3, i+1)  # Assuming you want a 2x5 grid for the first 10 files\n    plt.plot(rms_energy_values, label='RMS Energy')\n    plt.title(f'RMS Energy - {train.loc[i, \"ebird_code\"]}')\n    plt.legend()\n    plt.xlabel('Frame Index')\n    plt.ylabel('RMS Energy')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:51.078933Z","iopub.status.idle":"2024-01-10T20:17:51.079822Z","shell.execute_reply.started":"2024-01-10T20:17:51.079536Z","shell.execute_reply":"2024-01-10T20:17:51.079562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_spectral_rolloff(y, sr, title):\n    S, phase = librosa.magphase(librosa.stft(y))\n    rolloff = librosa.feature.spectral_rolloff(S=S, sr=sr, roll_percent=0.99)\n\n    # Plot Spectral Rolloff\n    plt.plot(rolloff[0], label='Spectral Rolloff')\n    plt.title(f'{title}')\n    plt.legend()\n\n# Plot Spectral Rolloff for all audio files\nnum_files_to_plot = 10  # Change this based on the number of files you want to plot\nplt.figure(figsize=(15, 10))\n\nfor i in range(num_files_to_plot):\n    plt.subplot(4, 3, i+1)  # Assuming you want a 2x5 grid for the first 10 files\n    plot_spectral_rolloff(train.loc[i, 'y'], train.loc[i, 'sr'], f'{train.loc[i, \"ebird_code\"]}')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:51.081380Z","iopub.status.idle":"2024-01-10T20:17:51.082254Z","shell.execute_reply.started":"2024-01-10T20:17:51.081980Z","shell.execute_reply":"2024-01-10T20:17:51.082006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_spectral_flux(y, sr, title):\n    onset_env = librosa.onset.onset_strength(y=y, sr=sr)\n\n    # Plot Spectral Flux\n    plt.plot(onset_env, label='Spectral Flux')\n    plt.title(f'{title}')\n    plt.legend()\n\n# Plot Spectral Flux for all audio files\nnum_files_to_plot = 10  # Change this based on the number of files you want to plot\nplt.figure(figsize=(15, 10))\n\nfor i in range(num_files_to_plot):\n    plt.subplot(4, 3, i+1)  # Assuming you want a 2x5 grid for the first 10 files\n    plot_spectral_flux(train.loc[i, 'y'], train.loc[i, 'sr'], f'{train.loc[i, \"ebird_code\"]}')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:17:51.083867Z","iopub.status.idle":"2024-01-10T20:17:51.084741Z","shell.execute_reply.started":"2024-01-10T20:17:51.084436Z","shell.execute_reply":"2024-01-10T20:17:51.084463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Model Buliding","metadata":{}},{"cell_type":"code","source":"import cv2\nimport audioread\nimport logging\nimport os\nimport random\nimport time\nimport warnings\n\n\nimport soundfile as sf\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.utils.data as data\n\nfrom contextlib import contextmanager\nfrom pathlib import Path\nfrom typing import Optional\n\nfrom fastprogress import progress_bar\nfrom sklearn.metrics import f1_score\nfrom torchvision import models","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:23:51.167343Z","iopub.execute_input":"2024-01-10T20:23:51.167760Z","iopub.status.idle":"2024-01-10T20:23:51.584848Z","shell.execute_reply.started":"2024-01-10T20:23:51.167727Z","shell.execute_reply":"2024-01-10T20:23:51.583506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed(seed: int = 42):\n    random.seed(seed)\n    np.random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    torch.manual_seed(seed)\n    torch.backends.cudnn.benchmark = True\n\ndef get_logger(out_file=None):\n    logger = logging.getLogger()\n    formatter = logging.Formatter(\"%(asctime)s - %(levelname)s - %(message)s\")\n    logger.handlers = []\n    logger.setLevel(logging.INFO)\n    \n    handler = logging.StreamHandler()\n    handler.setFormatter(formatter)\n    handler.setLevel(logging.INFO)\n    logger.addHandler(handler)\n    \n    if out_file is not None:\n        fh = logging.FileHandler(out_file)\n        fh.setFormatter(formatter)\n        fh.setLevel(logging.INFO)\n        logger.addHandler(fh)\n    logger.info(\"Logger set up\")\n    return logger\n\ndef timer(name: str, logger: Optional[logging.Logger] = None):\n    t0 = time.time()\n    msg = f\"[{name}] start\"\n    if logger is None:\n        print(msg)\n    else:\n        logger.info(msg)\n        \n    yield\n    \n    msg = f\"[{name}] done in {time.time() - t0:.2f} s\"\n    if logger is None:\n        print(msg)\n    else:\n        logger.info(msg)","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:50:27.832608Z","iopub.execute_input":"2024-01-10T20:50:27.833664Z","iopub.status.idle":"2024-01-10T20:50:27.842736Z","shell.execute_reply.started":"2024-01-10T20:50:27.833625Z","shell.execute_reply":"2024-01-10T20:50:27.841720Z"},"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-10T20:51:13.833231Z","iopub.execute_input":"2024-01-10T20:51:13.833622Z","iopub.status.idle":"2024-01-10T20:51:13.844409Z","shell.execute_reply.started":"2024-01-10T20:51:13.833592Z","shell.execute_reply":"2024-01-10T20:51:13.843176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TARGET_SR = 32000","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:51:43.156016Z","iopub.execute_input":"2024-01-10T20:51:43.156689Z","iopub.status.idle":"2024-01-10T20:51:43.161703Z","shell.execute_reply.started":"2024-01-10T20:51:43.156642Z","shell.execute_reply":"2024-01-10T20:51:43.160400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/birdsong-recognition/test.csv')\ntest_audio = \"/kaggle/input/birdcall-check/test_audio\"\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-10T20:54:37.067471Z","iopub.execute_input":"2024-01-10T20:54:37.067879Z","iopub.status.idle":"2024-01-10T20:54:37.083296Z","shell.execute_reply.started":"2024-01-10T20:54:37.067848Z","shell.execute_reply":"2024-01-10T20:54:37.082285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ResNet(nn.Module):  # Fixed typo: should be nn.Module, not nn.module\n    def __init__(self, base_model_name: str, pretrained=False, num_classes=10):  # Assuming num_classes as 10, replace with the desired value\n        super().__init__()\n        base_model = getattr(models, base_model_name)(pretrained=pretrained)  # Fixed typo: __getattribute__ to getattr\n        layers = list(base_model.children())[:-2]\n        layers.append(nn.AdaptiveMaxPool2d(1))\n        self.encoder = nn.Sequential(*layers)\n    \n        in_features = base_model.fc.in_features\n        self.classifier = nn.Sequential(\n            nn.Linear(in_features, 1024), nn.ReLU(), nn.Dropout(p=0.2),\n            nn.Linear(1024, 1024), nn.ReLU(), nn.Dropout(p=0.2),\n            nn.Linear(1024, num_classes)\n        )\n\n    def forward(self, x):\n        batch_size = x.size(0)\n        x = self.encoder(x).view(batch_size, -1)\n        x = self.classifier(x)\n        multiclass_proba = F.softmax(x, dim=1)\n        multilabel_proba = torch.sigmoid(x)  # Fixed typo: F.sigmoid to torch.sigmoid\n        return {\n            \"logits\": x,\n            \"multiclass_proba\": multiclass_proba,\n            \"multilabel_proba\": multilabel_proba\n        }","metadata":{"execution":{"iopub.status.busy":"2024-01-10T21:06:34.126594Z","iopub.execute_input":"2024-01-10T21:06:34.127474Z","iopub.status.idle":"2024-01-10T21:06:34.136753Z","shell.execute_reply.started":"2024-01-10T21:06:34.127439Z","shell.execute_reply":"2024-01-10T21:06:34.135542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_config = {\n    \"base_model_name\" : \"resnet50\",\n    \"pretrained\" : False ,\n    \"num_classes\" : 264\n}\n\nmelspectogram_parameters = {\n    \"n_mels\" : 128,\n    \"fmin\" : 20 ,\n    \"fmax\" : 16000\n}\nweights_path = ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_bird_names = data['ebird_code'].unique()\n\nlabel_encoder = LabelEncoder()\nencoded_labels = label.encoder.fit_transform(unique_bird_names)\nBIRD_CODE = dict ( zip(unique_bird_names , encoded_labels))\n\nIN_BIRD_CODE = {v:k for k , v in BIRD_CODE.items()}","metadata":{"execution":{"iopub.status.busy":"2024-01-10T21:14:09.658435Z","iopub.execute_input":"2024-01-10T21:14:09.659190Z","iopub.status.idle":"2024-01-10T21:14:09.712740Z","shell.execute_reply.started":"2024-01-10T21:14:09.659155Z","shell.execute_reply":"2024-01-10T21:14:09.711524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-01-10T21:14:39.455364Z","iopub.execute_input":"2024-01-10T21:14:39.455793Z","iopub.status.idle":"2024-01-10T21:14:39.487910Z","shell.execute_reply.started":"2024-01-10T21:14:39.455760Z","shell.execute_reply":"2024-01-10T21:14:39.486446Z"},"trusted":true},"execution_count":null,"outputs":[]}]}