{"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":7401724,"sourceType":"datasetVersion","datasetId":4304019},{"sourceId":1262046,"sourceType":"datasetVersion","datasetId":726424}],"dockerImageVersionId":30635,"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-14T17:11:16.199959Z","iopub.execute_input":"2024-01-14T17:11:16.200332Z","iopub.status.idle":"2024-01-14T17:11:22.932925Z","shell.execute_reply.started":"2024-01-14T17:11:16.200301Z","shell.execute_reply":"2024-01-14T17:11:22.931904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-14T17:11:22.934839Z","iopub.execute_input":"2024-01-14T17:11:22.935271Z","iopub.status.idle":"2024-01-14T17:11:23.281705Z","shell.execute_reply.started":"2024-01-14T17:11:22.935241Z","shell.execute_reply":"2024-01-14T17:11:23.280667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:23.283111Z","iopub.execute_input":"2024-01-14T17:11:23.283553Z","iopub.status.idle":"2024-01-14T17:11:23.322151Z","shell.execute_reply.started":"2024-01-14T17:11:23.283515Z","shell.execute_reply":"2024-01-14T17:11:23.321133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_columns',None)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:23.324332Z","iopub.execute_input":"2024-01-14T17:11:23.324631Z","iopub.status.idle":"2024-01-14T17:11:23.329170Z","shell.execute_reply.started":"2024-01-14T17:11:23.324604Z","shell.execute_reply":"2024-01-14T17:11:23.327983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.expand_frame_repr', False)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:23.330779Z","iopub.execute_input":"2024-01-14T17:11:23.331189Z","iopub.status.idle":"2024-01-14T17:11:23.339077Z","shell.execute_reply.started":"2024-01-14T17:11:23.331152Z","shell.execute_reply":"2024-01-14T17:11:23.338285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:23.340354Z","iopub.execute_input":"2024-01-14T17:11:23.340955Z","iopub.status.idle":"2024-01-14T17:11:23.371421Z","shell.execute_reply.started":"2024-01-14T17:11:23.340917Z","shell.execute_reply":"2024-01-14T17:11:23.370214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:23.372704Z","iopub.execute_input":"2024-01-14T17:11:23.373081Z","iopub.status.idle":"2024-01-14T17:11:23.428854Z","shell.execute_reply.started":"2024-01-14T17:11:23.373048Z","shell.execute_reply":"2024-01-14T17:11:23.427860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['rating'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:23.430109Z","iopub.execute_input":"2024-01-14T17:11:23.430430Z","iopub.status.idle":"2024-01-14T17:11:23.437927Z","shell.execute_reply.started":"2024-01-14T17:11:23.430402Z","shell.execute_reply":"2024-01-14T17:11:23.437027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.playback_used.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:23.439220Z","iopub.execute_input":"2024-01-14T17:11:23.440146Z","iopub.status.idle":"2024-01-14T17:11:23.451398Z","shell.execute_reply.started":"2024-01-14T17:11:23.440082Z","shell.execute_reply":"2024-01-14T17:11:23.450451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.playback_used.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:23.456206Z","iopub.execute_input":"2024-01-14T17:11:23.456515Z","iopub.status.idle":"2024-01-14T17:11:23.466675Z","shell.execute_reply.started":"2024-01-14T17:11:23.456487Z","shell.execute_reply":"2024-01-14T17:11:23.465633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.playback_used.fillna('no',inplace = True)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:23.467738Z","iopub.execute_input":"2024-01-14T17:11:23.468048Z","iopub.status.idle":"2024-01-14T17:11:23.476916Z","shell.execute_reply.started":"2024-01-14T17:11:23.468020Z","shell.execute_reply":"2024-01-14T17:11:23.476154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.playback_used.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:23.477998Z","iopub.execute_input":"2024-01-14T17:11:23.478493Z","iopub.status.idle":"2024-01-14T17:11:23.488393Z","shell.execute_reply.started":"2024-01-14T17:11:23.478464Z","shell.execute_reply":"2024-01-14T17:11:23.487507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.channels.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:23.490749Z","iopub.execute_input":"2024-01-14T17:11:23.491167Z","iopub.status.idle":"2024-01-14T17:11:23.503143Z","shell.execute_reply.started":"2024-01-14T17:11:23.491109Z","shell.execute_reply":"2024-01-14T17:11:23.502095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.channels.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:23.504494Z","iopub.execute_input":"2024-01-14T17:11:23.505145Z","iopub.status.idle":"2024-01-14T17:11:23.515936Z","shell.execute_reply.started":"2024-01-14T17:11:23.505087Z","shell.execute_reply":"2024-01-14T17:11:23.515169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.date.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:23.516958Z","iopub.execute_input":"2024-01-14T17:11:23.517744Z","iopub.status.idle":"2024-01-14T17:11:23.527614Z","shell.execute_reply.started":"2024-01-14T17:11:23.517714Z","shell.execute_reply":"2024-01-14T17:11:23.526774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.date.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:23.529003Z","iopub.execute_input":"2024-01-14T17:11:23.529296Z","iopub.status.idle":"2024-01-14T17:11:23.537882Z","shell.execute_reply.started":"2024-01-14T17:11:23.529270Z","shell.execute_reply":"2024-01-14T17:11:23.536941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[['year', 'month', 'day']] = df['date'].str.split('-', expand=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:23.539251Z","iopub.execute_input":"2024-01-14T17:11:23.539798Z","iopub.status.idle":"2024-01-14T17:11:23.580731Z","shell.execute_reply.started":"2024-01-14T17:11:23.539769Z","shell.execute_reply":"2024-01-14T17:11:23.579689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.month.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:23.582142Z","iopub.execute_input":"2024-01-14T17:11:23.582750Z","iopub.status.idle":"2024-01-14T17:11:23.591827Z","shell.execute_reply.started":"2024-01-14T17:11:23.582709Z","shell.execute_reply":"2024-01-14T17:11:23.590627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.month.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:23.593515Z","iopub.execute_input":"2024-01-14T17:11:23.594347Z","iopub.status.idle":"2024-01-14T17:11:23.606897Z","shell.execute_reply.started":"2024-01-14T17:11:23.594306Z","shell.execute_reply":"2024-01-14T17:11:23.606189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['month'] = df['month'].replace('00', '05')","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:23.608066Z","iopub.execute_input":"2024-01-14T17:11:23.608417Z","iopub.status.idle":"2024-01-14T17:11:23.622853Z","shell.execute_reply.started":"2024-01-14T17:11:23.608387Z","shell.execute_reply":"2024-01-14T17:11:23.621983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop(['year', 'day'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:23.624338Z","iopub.execute_input":"2024-01-14T17:11:23.624683Z","iopub.status.idle":"2024-01-14T17:11:23.644710Z","shell.execute_reply.started":"2024-01-14T17:11:23.624656Z","shell.execute_reply":"2024-01-14T17:11:23.643607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.ebird_code.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:23.646211Z","iopub.execute_input":"2024-01-14T17:11:23.646650Z","iopub.status.idle":"2024-01-14T17:11:23.658497Z","shell.execute_reply.started":"2024-01-14T17:11:23.646612Z","shell.execute_reply":"2024-01-14T17:11:23.657232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.ebird_code.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:23.660204Z","iopub.execute_input":"2024-01-14T17:11:23.660928Z","iopub.status.idle":"2024-01-14T17:11:23.669955Z","shell.execute_reply.started":"2024-01-14T17:11:23.660887Z","shell.execute_reply":"2024-01-14T17:11:23.669194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\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-14T17:11:23.671143Z","iopub.execute_input":"2024-01-14T17:11:23.671947Z","iopub.status.idle":"2024-01-14T17:11:24.495687Z","shell.execute_reply.started":"2024-01-14T17:11:23.671916Z","shell.execute_reply":"2024-01-14T17:11:24.494889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.ebird_code.nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.496973Z","iopub.execute_input":"2024-01-14T17:11:24.497505Z","iopub.status.idle":"2024-01-14T17:11:24.504847Z","shell.execute_reply.started":"2024-01-14T17:11:24.497472Z","shell.execute_reply":"2024-01-14T17:11:24.504093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.month.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.505981Z","iopub.execute_input":"2024-01-14T17:11:24.506707Z","iopub.status.idle":"2024-01-14T17:11:24.519224Z","shell.execute_reply.started":"2024-01-14T17:11:24.506661Z","shell.execute_reply":"2024-01-14T17:11:24.518341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.pitch.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.520370Z","iopub.execute_input":"2024-01-14T17:11:24.520738Z","iopub.status.idle":"2024-01-14T17:11:24.532049Z","shell.execute_reply.started":"2024-01-14T17:11:24.520710Z","shell.execute_reply":"2024-01-14T17:11:24.531099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.pitch.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.539486Z","iopub.execute_input":"2024-01-14T17:11:24.539804Z","iopub.status.idle":"2024-01-14T17:11:24.546998Z","shell.execute_reply.started":"2024-01-14T17:11:24.539776Z","shell.execute_reply":"2024-01-14T17:11:24.546075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.pitch.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.548418Z","iopub.execute_input":"2024-01-14T17:11:24.548706Z","iopub.status.idle":"2024-01-14T17:11:24.563173Z","shell.execute_reply.started":"2024-01-14T17:11:24.548681Z","shell.execute_reply":"2024-01-14T17:11:24.562408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.speed.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.564364Z","iopub.execute_input":"2024-01-14T17:11:24.564878Z","iopub.status.idle":"2024-01-14T17:11:24.575067Z","shell.execute_reply.started":"2024-01-14T17:11:24.564846Z","shell.execute_reply":"2024-01-14T17:11:24.574199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.species.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.576613Z","iopub.execute_input":"2024-01-14T17:11:24.577136Z","iopub.status.idle":"2024-01-14T17:11:24.590271Z","shell.execute_reply.started":"2024-01-14T17:11:24.577095Z","shell.execute_reply":"2024-01-14T17:11:24.589162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.title.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.591585Z","iopub.execute_input":"2024-01-14T17:11:24.591993Z","iopub.status.idle":"2024-01-14T17:11:24.611229Z","shell.execute_reply.started":"2024-01-14T17:11:24.591965Z","shell.execute_reply":"2024-01-14T17:11:24.609970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.title.nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.612846Z","iopub.execute_input":"2024-01-14T17:11:24.613314Z","iopub.status.idle":"2024-01-14T17:11:24.627840Z","shell.execute_reply.started":"2024-01-14T17:11:24.613275Z","shell.execute_reply":"2024-01-14T17:11:24.626787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.number_of_notes.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.629251Z","iopub.execute_input":"2024-01-14T17:11:24.630431Z","iopub.status.idle":"2024-01-14T17:11:24.639170Z","shell.execute_reply.started":"2024-01-14T17:11:24.630390Z","shell.execute_reply":"2024-01-14T17:11:24.638165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.secondary_labels.nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.640348Z","iopub.execute_input":"2024-01-14T17:11:24.640645Z","iopub.status.idle":"2024-01-14T17:11:24.650611Z","shell.execute_reply.started":"2024-01-14T17:11:24.640619Z","shell.execute_reply":"2024-01-14T17:11:24.649646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.secondary_labels.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.651557Z","iopub.execute_input":"2024-01-14T17:11:24.651883Z","iopub.status.idle":"2024-01-14T17:11:24.662434Z","shell.execute_reply.started":"2024-01-14T17:11:24.651847Z","shell.execute_reply":"2024-01-14T17:11:24.661418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.bird_seen.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.663562Z","iopub.execute_input":"2024-01-14T17:11:24.664286Z","iopub.status.idle":"2024-01-14T17:11:24.673920Z","shell.execute_reply.started":"2024-01-14T17:11:24.664254Z","shell.execute_reply":"2024-01-14T17:11:24.672863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.bird_seen.fillna('yes', inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.674980Z","iopub.execute_input":"2024-01-14T17:11:24.675313Z","iopub.status.idle":"2024-01-14T17:11:24.685491Z","shell.execute_reply.started":"2024-01-14T17:11:24.675285Z","shell.execute_reply":"2024-01-14T17:11:24.684607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.bird_seen.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.686469Z","iopub.execute_input":"2024-01-14T17:11:24.686762Z","iopub.status.idle":"2024-01-14T17:11:24.698040Z","shell.execute_reply.started":"2024-01-14T17:11:24.686736Z","shell.execute_reply":"2024-01-14T17:11:24.697372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sci_name.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.699548Z","iopub.execute_input":"2024-01-14T17:11:24.699837Z","iopub.status.idle":"2024-01-14T17:11:24.711884Z","shell.execute_reply.started":"2024-01-14T17:11:24.699812Z","shell.execute_reply":"2024-01-14T17:11:24.710737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sci_name.nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.713522Z","iopub.execute_input":"2024-01-14T17:11:24.714062Z","iopub.status.idle":"2024-01-14T17:11:24.721157Z","shell.execute_reply.started":"2024-01-14T17:11:24.714032Z","shell.execute_reply":"2024-01-14T17:11:24.720259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sci_name.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.722735Z","iopub.execute_input":"2024-01-14T17:11:24.723023Z","iopub.status.idle":"2024-01-14T17:11:24.733797Z","shell.execute_reply.started":"2024-01-14T17:11:24.722998Z","shell.execute_reply":"2024-01-14T17:11:24.732836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.location.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.734956Z","iopub.execute_input":"2024-01-14T17:11:24.735267Z","iopub.status.idle":"2024-01-14T17:11:24.746970Z","shell.execute_reply.started":"2024-01-14T17:11:24.735240Z","shell.execute_reply":"2024-01-14T17:11:24.746266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.location.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.748219Z","iopub.execute_input":"2024-01-14T17:11:24.748715Z","iopub.status.idle":"2024-01-14T17:11:24.758562Z","shell.execute_reply.started":"2024-01-14T17:11:24.748681Z","shell.execute_reply":"2024-01-14T17:11:24.757488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.latitude","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.759763Z","iopub.execute_input":"2024-01-14T17:11:24.760741Z","iopub.status.idle":"2024-01-14T17:11:24.769806Z","shell.execute_reply.started":"2024-01-14T17:11:24.760701Z","shell.execute_reply":"2024-01-14T17:11:24.769024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.770918Z","iopub.execute_input":"2024-01-14T17:11:24.771492Z","iopub.status.idle":"2024-01-14T17:11:24.817341Z","shell.execute_reply.started":"2024-01-14T17:11:24.771463Z","shell.execute_reply":"2024-01-14T17:11:24.816616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sampling_rate.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.818321Z","iopub.execute_input":"2024-01-14T17:11:24.819049Z","iopub.status.idle":"2024-01-14T17:11:24.825478Z","shell.execute_reply.started":"2024-01-14T17:11:24.819019Z","shell.execute_reply":"2024-01-14T17:11:24.824731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop('sampling_rate', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.826334Z","iopub.execute_input":"2024-01-14T17:11:24.826934Z","iopub.status.idle":"2024-01-14T17:11:24.844413Z","shell.execute_reply.started":"2024-01-14T17:11:24.826908Z","shell.execute_reply":"2024-01-14T17:11:24.843542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" df = df.drop('type', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.845524Z","iopub.execute_input":"2024-01-14T17:11:24.846211Z","iopub.status.idle":"2024-01-14T17:11:24.863548Z","shell.execute_reply.started":"2024-01-14T17:11:24.846177Z","shell.execute_reply":"2024-01-14T17:11:24.862528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.864732Z","iopub.execute_input":"2024-01-14T17:11:24.865028Z","iopub.status.idle":"2024-01-14T17:11:24.913846Z","shell.execute_reply.started":"2024-01-14T17:11:24.865002Z","shell.execute_reply":"2024-01-14T17:11:24.912823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.elevation.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.914769Z","iopub.execute_input":"2024-01-14T17:11:24.915128Z","iopub.status.idle":"2024-01-14T17:11:24.923972Z","shell.execute_reply.started":"2024-01-14T17:11:24.915088Z","shell.execute_reply":"2024-01-14T17:11:24.923079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.elevation.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.925458Z","iopub.execute_input":"2024-01-14T17:11:24.925846Z","iopub.status.idle":"2024-01-14T17:11:24.940067Z","shell.execute_reply.started":"2024-01-14T17:11:24.925808Z","shell.execute_reply":"2024-01-14T17:11:24.938922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['elevation'] = df['elevation'].str.replace(' m','', regex=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.941334Z","iopub.execute_input":"2024-01-14T17:11:24.941630Z","iopub.status.idle":"2024-01-14T17:11:24.967339Z","shell.execute_reply.started":"2024-01-14T17:11:24.941604Z","shell.execute_reply":"2024-01-14T17:11:24.966180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.elevation.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.968747Z","iopub.execute_input":"2024-01-14T17:11:24.969087Z","iopub.status.idle":"2024-01-14T17:11:24.977704Z","shell.execute_reply.started":"2024-01-14T17:11:24.969059Z","shell.execute_reply":"2024-01-14T17:11:24.976587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['elevation'] = df['elevation'].replace('?', np.nan)\ndf['elevation'] = df['elevation'].replace('0', np.nan)\ndf['elevation'] = df['elevation'].replace('-', np.nan)\ndf['elevation'] = df['elevation'].replace('Unknown', np.nan)\ndf['elevation'] = df['elevation'].replace('', np.nan)\ndf['elevation'] = df['elevation'].replace('??', np.nan)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:24.979256Z","iopub.execute_input":"2024-01-14T17:11:24.979613Z","iopub.status.idle":"2024-01-14T17:11:25.000706Z","shell.execute_reply.started":"2024-01-14T17:11:24.979585Z","shell.execute_reply":"2024-01-14T17:11:24.999910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.elevation.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.001745Z","iopub.execute_input":"2024-01-14T17:11:25.002336Z","iopub.status.idle":"2024-01-14T17:11:25.011228Z","shell.execute_reply.started":"2024-01-14T17:11:25.002299Z","shell.execute_reply":"2024-01-14T17:11:25.010084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['elevation'] = df['elevation'].str.replace('m', '')\n","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.013674Z","iopub.execute_input":"2024-01-14T17:11:25.014003Z","iopub.status.idle":"2024-01-14T17:11:25.029325Z","shell.execute_reply.started":"2024-01-14T17:11:25.013974Z","shell.execute_reply":"2024-01-14T17:11:25.028191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['elevation'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.030613Z","iopub.execute_input":"2024-01-14T17:11:25.031313Z","iopub.status.idle":"2024-01-14T17:11:25.039378Z","shell.execute_reply.started":"2024-01-14T17:11:25.031281Z","shell.execute_reply":"2024-01-14T17:11:25.038362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.description.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.040499Z","iopub.execute_input":"2024-01-14T17:11:25.040830Z","iopub.status.idle":"2024-01-14T17:11:25.055506Z","shell.execute_reply.started":"2024-01-14T17:11:25.040800Z","shell.execute_reply":"2024-01-14T17:11:25.054320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.057174Z","iopub.execute_input":"2024-01-14T17:11:25.057897Z","iopub.status.idle":"2024-01-14T17:11:25.086448Z","shell.execute_reply.started":"2024-01-14T17:11:25.057858Z","shell.execute_reply":"2024-01-14T17:11:25.085328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.description.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.087893Z","iopub.execute_input":"2024-01-14T17:11:25.088288Z","iopub.status.idle":"2024-01-14T17:11:25.098222Z","shell.execute_reply.started":"2024-01-14T17:11:25.088259Z","shell.execute_reply":"2024-01-14T17:11:25.097540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop('description', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.099693Z","iopub.execute_input":"2024-01-14T17:11:25.100732Z","iopub.status.idle":"2024-01-14T17:11:25.114139Z","shell.execute_reply.started":"2024-01-14T17:11:25.100692Z","shell.execute_reply":"2024-01-14T17:11:25.113154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.bitrate_of_mp3","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.115617Z","iopub.execute_input":"2024-01-14T17:11:25.116247Z","iopub.status.idle":"2024-01-14T17:11:25.124442Z","shell.execute_reply.started":"2024-01-14T17:11:25.116208Z","shell.execute_reply":"2024-01-14T17:11:25.123606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop('bitrate_of_mp3', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.125479Z","iopub.execute_input":"2024-01-14T17:11:25.125993Z","iopub.status.idle":"2024-01-14T17:11:25.145219Z","shell.execute_reply.started":"2024-01-14T17:11:25.125954Z","shell.execute_reply":"2024-01-14T17:11:25.144271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.file_type.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.146325Z","iopub.execute_input":"2024-01-14T17:11:25.146635Z","iopub.status.idle":"2024-01-14T17:11:25.155395Z","shell.execute_reply.started":"2024-01-14T17:11:25.146608Z","shell.execute_reply":"2024-01-14T17:11:25.154419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['volume'].info()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.156655Z","iopub.execute_input":"2024-01-14T17:11:25.157547Z","iopub.status.idle":"2024-01-14T17:11:25.171177Z","shell.execute_reply.started":"2024-01-14T17:11:25.157506Z","shell.execute_reply":"2024-01-14T17:11:25.170429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop('volume', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.172251Z","iopub.execute_input":"2024-01-14T17:11:25.172680Z","iopub.status.idle":"2024-01-14T17:11:25.193293Z","shell.execute_reply.started":"2024-01-14T17:11:25.172653Z","shell.execute_reply":"2024-01-14T17:11:25.192387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.194582Z","iopub.execute_input":"2024-01-14T17:11:25.194923Z","iopub.status.idle":"2024-01-14T17:11:25.221629Z","shell.execute_reply.started":"2024-01-14T17:11:25.194885Z","shell.execute_reply":"2024-01-14T17:11:25.220692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.223013Z","iopub.execute_input":"2024-01-14T17:11:25.223639Z","iopub.status.idle":"2024-01-14T17:11:25.263131Z","shell.execute_reply.started":"2024-01-14T17:11:25.223609Z","shell.execute_reply":"2024-01-14T17:11:25.262145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['background'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.264792Z","iopub.execute_input":"2024-01-14T17:11:25.265232Z","iopub.status.idle":"2024-01-14T17:11:25.275234Z","shell.execute_reply.started":"2024-01-14T17:11:25.265191Z","shell.execute_reply":"2024-01-14T17:11:25.274154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop('background', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.276526Z","iopub.execute_input":"2024-01-14T17:11:25.276849Z","iopub.status.idle":"2024-01-14T17:11:25.291668Z","shell.execute_reply.started":"2024-01-14T17:11:25.276822Z","shell.execute_reply":"2024-01-14T17:11:25.290759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.xc_id.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.292873Z","iopub.execute_input":"2024-01-14T17:11:25.293204Z","iopub.status.idle":"2024-01-14T17:11:25.302341Z","shell.execute_reply.started":"2024-01-14T17:11:25.293176Z","shell.execute_reply":"2024-01-14T17:11:25.301137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop('xc_id', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.303614Z","iopub.execute_input":"2024-01-14T17:11:25.303919Z","iopub.status.idle":"2024-01-14T17:11:25.321013Z","shell.execute_reply.started":"2024-01-14T17:11:25.303892Z","shell.execute_reply":"2024-01-14T17:11:25.320080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.322214Z","iopub.execute_input":"2024-01-14T17:11:25.322663Z","iopub.status.idle":"2024-01-14T17:11:25.350136Z","shell.execute_reply.started":"2024-01-14T17:11:25.322566Z","shell.execute_reply":"2024-01-14T17:11:25.349077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop('url', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.351579Z","iopub.execute_input":"2024-01-14T17:11:25.352527Z","iopub.status.idle":"2024-01-14T17:11:25.368496Z","shell.execute_reply.started":"2024-01-14T17:11:25.352487Z","shell.execute_reply":"2024-01-14T17:11:25.367557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop('author', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.369928Z","iopub.execute_input":"2024-01-14T17:11:25.370347Z","iopub.status.idle":"2024-01-14T17:11:25.387054Z","shell.execute_reply.started":"2024-01-14T17:11:25.370318Z","shell.execute_reply":"2024-01-14T17:11:25.386054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop('primary_label', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.388374Z","iopub.execute_input":"2024-01-14T17:11:25.388779Z","iopub.status.idle":"2024-01-14T17:11:25.403467Z","shell.execute_reply.started":"2024-01-14T17:11:25.388733Z","shell.execute_reply":"2024-01-14T17:11:25.402437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['length'].info()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.404659Z","iopub.execute_input":"2024-01-14T17:11:25.404972Z","iopub.status.idle":"2024-01-14T17:11:25.415441Z","shell.execute_reply.started":"2024-01-14T17:11:25.404937Z","shell.execute_reply":"2024-01-14T17:11:25.414640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['length']","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.416462Z","iopub.execute_input":"2024-01-14T17:11:25.417282Z","iopub.status.idle":"2024-01-14T17:11:25.425014Z","shell.execute_reply.started":"2024-01-14T17:11:25.417244Z","shell.execute_reply":"2024-01-14T17:11:25.423995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.time.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.426167Z","iopub.execute_input":"2024-01-14T17:11:25.426926Z","iopub.status.idle":"2024-01-14T17:11:25.440323Z","shell.execute_reply.started":"2024-01-14T17:11:25.426896Z","shell.execute_reply":"2024-01-14T17:11:25.439046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.time.nunique","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.441366Z","iopub.execute_input":"2024-01-14T17:11:25.441745Z","iopub.status.idle":"2024-01-14T17:11:25.448959Z","shell.execute_reply.started":"2024-01-14T17:11:25.441707Z","shell.execute_reply":"2024-01-14T17:11:25.448245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.time.unique","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.461284Z","iopub.execute_input":"2024-01-14T17:11:25.461633Z","iopub.status.idle":"2024-01-14T17:11:25.470445Z","shell.execute_reply.started":"2024-01-14T17:11:25.461605Z","shell.execute_reply":"2024-01-14T17:11:25.469472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.471583Z","iopub.execute_input":"2024-01-14T17:11:25.471886Z","iopub.status.idle":"2024-01-14T17:11:25.500157Z","shell.execute_reply.started":"2024-01-14T17:11:25.471858Z","shell.execute_reply":"2024-01-14T17:11:25.499252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop('recordist', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.501508Z","iopub.execute_input":"2024-01-14T17:11:25.501819Z","iopub.status.idle":"2024-01-14T17:11:25.511891Z","shell.execute_reply.started":"2024-01-14T17:11:25.501791Z","shell.execute_reply":"2024-01-14T17:11:25.511186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.license.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.512846Z","iopub.execute_input":"2024-01-14T17:11:25.513155Z","iopub.status.idle":"2024-01-14T17:11:25.522295Z","shell.execute_reply.started":"2024-01-14T17:11:25.513106Z","shell.execute_reply":"2024-01-14T17:11:25.521274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.523669Z","iopub.execute_input":"2024-01-14T17:11:25.523976Z","iopub.status.idle":"2024-01-14T17:11:25.530138Z","shell.execute_reply.started":"2024-01-14T17:11:25.523950Z","shell.execute_reply":"2024-01-14T17:11:25.529274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.531487Z","iopub.execute_input":"2024-01-14T17:11:25.532303Z","iopub.status.idle":"2024-01-14T17:11:25.540337Z","shell.execute_reply.started":"2024-01-14T17:11:25.532274Z","shell.execute_reply":"2024-01-14T17:11:25.539298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (16,8))\n\nsns.countplot( x='rating', data = df, order = df['rating'].value_counts().index, palette = 'viridis')\nplt.title(\"Ratings distribution\")\nplt.xlabel(\"Ratings\")\nplt.ylabel(\"Count\")\nplt.xticks(rotation = 45)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.541735Z","iopub.execute_input":"2024-01-14T17:11:25.542384Z","iopub.status.idle":"2024-01-14T17:11:25.890102Z","shell.execute_reply.started":"2024-01-14T17:11:25.542347Z","shell.execute_reply":"2024-01-14T17:11:25.889048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['month']=df['month'].astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.891568Z","iopub.execute_input":"2024-01-14T17:11:25.891988Z","iopub.status.idle":"2024-01-14T17:11:25.901927Z","shell.execute_reply.started":"2024-01-14T17:11:25.891950Z","shell.execute_reply":"2024-01-14T17:11:25.900871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.903194Z","iopub.execute_input":"2024-01-14T17:11:25.903490Z","iopub.status.idle":"2024-01-14T17:11:25.943478Z","shell.execute_reply.started":"2024-01-14T17:11:25.903464Z","shell.execute_reply":"2024-01-14T17:11:25.942477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bird_month_counts = df.groupby(['month', 'species']).size().reset_index(name='Count')\n\n# Find the top 10 birds based on overall sightings\ntop_birds = bird_month_counts.groupby('species')['Count'].sum().nlargest(5).index\n\n# Filter the data for the top 10 birds\ntop_bird_month_counts = bird_month_counts[bird_month_counts['species'].isin(top_birds)]\n\n# Create a line plot using Seaborn\nplt.figure(figsize=(14, 8))\nsns.lineplot(x='month', y='Count', hue='species', data=top_bird_month_counts, marker='o', palette='muted')\nplt.xlabel('Month')\nplt.ylabel('Count')\nplt.title('Top 10 Bird Sightings Over Months')\nplt.legend(title='species', bbox_to_anchor=(1, 1))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:25.947172Z","iopub.execute_input":"2024-01-14T17:11:25.947492Z","iopub.status.idle":"2024-01-14T17:11:26.366528Z","shell.execute_reply.started":"2024-01-14T17:11:25.947465Z","shell.execute_reply":"2024-01-14T17:11:26.365719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:26.367544Z","iopub.execute_input":"2024-01-14T17:11:26.368414Z","iopub.status.idle":"2024-01-14T17:11:26.391613Z","shell.execute_reply.started":"2024-01-14T17:11:26.368379Z","shell.execute_reply":"2024-01-14T17:11:26.390773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (20,4))\n\nsns.countplot( x='country', data = df, order = df['country'].value_counts().index, palette = 'viridis')\nplt.title(\"country distribution\")\nplt.xlabel(\"country\")\nplt.ylabel(\"Count\")\nplt.xticks(rotation = 45)\nplt.xticks(fontsize=6)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:26.392650Z","iopub.execute_input":"2024-01-14T17:11:26.393439Z","iopub.status.idle":"2024-01-14T17:11:27.320461Z","shell.execute_reply.started":"2024-01-14T17:11:26.393405Z","shell.execute_reply":"2024-01-14T17:11:27.319476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature Extraction","metadata":{}},{"cell_type":"code","source":"unique_values = df['species'].unique()[:5]\nunique_values","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:27.321569Z","iopub.execute_input":"2024-01-14T17:11:27.321861Z","iopub.status.idle":"2024-01-14T17:11:27.330177Z","shell.execute_reply.started":"2024-01-14T17:11:27.321834Z","shell.execute_reply":"2024-01-14T17:11:27.329058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfs = []\n\n# Extracting 2 rows for each unique value\nfor value in unique_values:\n    # Filter rows based on the unique value\n    subset = df[df['species'] == value].head(2)\n    \n    # Append the subset to the result DataFrame\n    dfs.append(subset)\n\ndf_for_FE = pd.concat(dfs, ignore_index=True)\ndf_for_FE","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:27.331713Z","iopub.execute_input":"2024-01-14T17:11:27.332530Z","iopub.status.idle":"2024-01-14T17:11:27.375213Z","shell.execute_reply.started":"2024-01-14T17:11:27.332489Z","shell.execute_reply":"2024-01-14T17:11:27.374270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install librosa==0.9.2","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:27.378731Z","iopub.execute_input":"2024-01-14T17:11:27.379033Z","iopub.status.idle":"2024-01-14T17:11:40.392380Z","shell.execute_reply.started":"2024-01-14T17:11:27.379006Z","shell.execute_reply":"2024-01-14T17:11:40.391205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt # for visualising data\n%matplotlib inline\nimport seaborn as sns\n\nimport IPython.display as ipd # for playing audio files\nimport soundfile as sf # for reading and writing audio files\nimport audioread # reading and processing audio files\nimport os # file and directory manipulation \nimport warnings\nimport librosa\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:40.395088Z","iopub.execute_input":"2024-01-14T17:11:40.396161Z","iopub.status.idle":"2024-01-14T17:11:42.176606Z","shell.execute_reply.started":"2024-01-14T17:11:40.396111Z","shell.execute_reply":"2024-01-14T17:11:42.175556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y1,sr1 = librosa.load(\"/kaggle/input/birdsong-recognition/train_audio/aldfly/XC2628.mp3\")\ny2,sr2 = librosa.load(\"/kaggle/input/birdsong-recognition/train_audio/aldfly/XC16967.mp3\")\ny3,sr3 = librosa.load(\"/kaggle/input/birdsong-recognition/train_audio/ameavo/XC99571.mp3\")\ny4,sr4 = librosa.load(\"/kaggle/input/birdsong-recognition/train_audio/ameavo/XC133080.mp3\")\ny5,sr5 = librosa.load(\"/kaggle/input/birdsong-recognition/train_audio/amebit/XC127371.mp3\")\ny6,sr6 = librosa.load(\"/kaggle/input/birdsong-recognition/train_audio/amebit/XC130058.mp3\")\ny7,sr7 = librosa.load(\"/kaggle/input/birdsong-recognition/train_audio/amecro/XC51410.mp3\")\ny8,sr8 = librosa.load(\"/kaggle/input/birdsong-recognition/train_audio/amecro/XC109768.mp3\")\ny9,sr9 = librosa.load(\"/kaggle/input/birdsong-recognition/train_audio/amegfi/XC17120.mp3\")\ny10,sr10 = librosa.load(\"/kaggle/input/birdsong-recognition/train_audio/amegfi/XC31023.mp3\")","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:42.177971Z","iopub.execute_input":"2024-01-14T17:11:42.178304Z","iopub.status.idle":"2024-01-14T17:11:55.903704Z","shell.execute_reply.started":"2024-01-14T17:11:42.178274Z","shell.execute_reply":"2024-01-14T17:11:55.902675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Audio1\")\nipd.Audio(y1, rate=sr1)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:55.905448Z","iopub.execute_input":"2024-01-14T17:11:55.905782Z","iopub.status.idle":"2024-01-14T17:11:55.931815Z","shell.execute_reply.started":"2024-01-14T17:11:55.905753Z","shell.execute_reply":"2024-01-14T17:11:55.930682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Audio3\")\nipd.Audio(y3, rate=sr3)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:55.933080Z","iopub.execute_input":"2024-01-14T17:11:55.933470Z","iopub.status.idle":"2024-01-14T17:11:55.958853Z","shell.execute_reply.started":"2024-01-14T17:11:55.933436Z","shell.execute_reply":"2024-01-14T17:11:55.957875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Audio5\")\nipd.Audio(y5, rate=sr5)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:55.960097Z","iopub.execute_input":"2024-01-14T17:11:55.960493Z","iopub.status.idle":"2024-01-14T17:11:55.993700Z","shell.execute_reply.started":"2024-01-14T17:11:55.960458Z","shell.execute_reply":"2024-01-14T17:11:55.992727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Audio7\")\nipd.Audio(y7, rate=sr7)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:55.995007Z","iopub.execute_input":"2024-01-14T17:11:55.995352Z","iopub.status.idle":"2024-01-14T17:11:56.190801Z","shell.execute_reply.started":"2024-01-14T17:11:55.995323Z","shell.execute_reply":"2024-01-14T17:11:56.188629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import librosa.display","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:56.193022Z","iopub.execute_input":"2024-01-14T17:11:56.193399Z","iopub.status.idle":"2024-01-14T17:11:56.200992Z","shell.execute_reply.started":"2024-01-14T17:11:56.193364Z","shell.execute_reply":"2024-01-14T17:11:56.199811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot spectrograms using STFT\nplt.figure(figsize=(15, 10))\n\nplt.subplot(5, 2, 1)\nD1 = librosa.amplitude_to_db(np.abs(librosa.stft(y1)), ref=np.max)\nlibrosa.display.specshow(D1, sr=sr1, x_axis='time', y_axis='log')\nplt.title('y1')\n\nplt.subplot(5, 2, 2)\nD2 = librosa.amplitude_to_db(np.abs(librosa.stft(y2)), ref=np.max)\nlibrosa.display.specshow(D2, sr=sr2, x_axis='time', y_axis='log')\nplt.title('y2')\n\nplt.subplot(5, 2, 3)\nD3 = librosa.amplitude_to_db(np.abs(librosa.stft(y3)), ref=np.max)\nlibrosa.display.specshow(D3, sr=sr3, x_axis='time', y_axis='log')\nplt.title('y3')\n\nplt.subplot(5, 2, 4)\nD4 = librosa.amplitude_to_db(np.abs(librosa.stft(y4)), ref=np.max)\nlibrosa.display.specshow(D4, sr=sr4, x_axis='time', y_axis='log')\nplt.title('y4')\n\nplt.subplot(5, 2, 5)\nD5 = librosa.amplitude_to_db(np.abs(librosa.stft(y5)), ref=np.max)\nlibrosa.display.specshow(D5, sr=sr5, x_axis='time', y_axis='log')\nplt.title('y5')\n\nplt.subplot(5, 2, 6)\nD6 = librosa.amplitude_to_db(np.abs(librosa.stft(y6)), ref=np.max)\nlibrosa.display.specshow(D6, sr=sr6, x_axis='time', y_axis='log')\nplt.title('y6')\n\nplt.subplot(5, 2, 7)\nD7 = librosa.amplitude_to_db(np.abs(librosa.stft(y7)), ref=np.max)\nlibrosa.display.specshow(D7, sr=sr7, x_axis='time', y_axis='log')\nplt.title('y7')\n\nplt.subplot(5, 2, 8)\nD8 = librosa.amplitude_to_db(np.abs(librosa.stft(y8)), ref=np.max)\nlibrosa.display.specshow(D8, sr=sr8, x_axis='time', y_axis='log')\nplt.title('y8')\n\nplt.subplot(5, 2, 9)\nD9 = librosa.amplitude_to_db(np.abs(librosa.stft(y9)), ref=np.max)\nlibrosa.display.specshow(D9, sr=sr9, x_axis='time', y_axis='log')\nplt.title('y9')\n\nplt.subplot(5, 2, 10)\nD10 = librosa.amplitude_to_db(np.abs(librosa.stft(y10)), ref=np.max)\nlibrosa.display.specshow(D10, sr=sr10, x_axis='time', y_axis='log')\nplt.title('y10')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:11:56.202816Z","iopub.execute_input":"2024-01-14T17:11:56.203562Z","iopub.status.idle":"2024-01-14T17:12:09.748237Z","shell.execute_reply.started":"2024-01-14T17:11:56.203513Z","shell.execute_reply":"2024-01-14T17:12:09.747005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 10))\n\nplt.subplot(5, 2, 1)\nlibrosa.display.specshow(librosa.power_to_db(librosa.feature.melspectrogram(y1, sr=sr1), ref=np.max), y_axis='mel', x_axis='time')\nplt.title('y1')\n\nplt.subplot(5, 2, 2)\nlibrosa.display.specshow(librosa.power_to_db(librosa.feature.melspectrogram(y2, sr=sr2), ref=np.max), y_axis='mel', x_axis='time')\nplt.title('y2')\n\nplt.subplot(5, 2, 3)\nlibrosa.display.specshow(librosa.power_to_db(librosa.feature.melspectrogram(y3, sr=sr3), ref=np.max), y_axis='mel', x_axis='time')\nplt.title('y3')\n\nplt.subplot(5, 2, 4)\nlibrosa.display.specshow(librosa.power_to_db(librosa.feature.melspectrogram(y4, sr=sr4), ref=np.max), y_axis='mel', x_axis='time')\nplt.title('y4')\n\nplt.subplot(5, 2, 5)\nlibrosa.display.specshow(librosa.power_to_db(librosa.feature.melspectrogram(y5, sr=sr5), ref=np.max), y_axis='mel', x_axis='time')\nplt.title('y5')\n\nplt.subplot(5, 2, 6)\nlibrosa.display.specshow(librosa.power_to_db(librosa.feature.melspectrogram(y6, sr=sr6), ref=np.max), y_axis='mel', x_axis='time')\nplt.title('y6')\n\nplt.subplot(5, 2, 7)\nlibrosa.display.specshow(librosa.power_to_db(librosa.feature.melspectrogram(y7, sr=sr7), ref=np.max), y_axis='mel', x_axis='time')\nplt.title('y7')\n\nplt.subplot(5, 2, 8)\nlibrosa.display.specshow(librosa.power_to_db(librosa.feature.melspectrogram(y8, sr=sr8), ref=np.max), y_axis='mel', x_axis='time')\nplt.title('y8')\n\nplt.subplot(5, 2, 9)\nlibrosa.display.specshow(librosa.power_to_db(librosa.feature.melspectrogram(y9, sr=sr9), ref=np.max), y_axis='mel', x_axis='time')\nplt.title('y9')\n\nplt.subplot(5, 2, 10)\nlibrosa.display.specshow(librosa.power_to_db(librosa.feature.melspectrogram(y10, sr=sr10), ref=np.max), y_axis='mel', x_axis='time')\nplt.title('y10')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:12:09.749668Z","iopub.execute_input":"2024-01-14T17:12:09.750026Z","iopub.status.idle":"2024-01-14T17:12:14.352318Z","shell.execute_reply.started":"2024-01-14T17:12:09.749993Z","shell.execute_reply":"2024-01-14T17:12:14.350897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_spectral_centroid(y, sr, subplot_index, title):\n    plt.subplot(5, 2, subplot_index)\n    centroids = librosa.feature.spectral_centroid(y=y, sr=sr)[0]\n    centroids = centroids + 1e-10  # Add a small constant to avoid division by zero\n    times = librosa.times_like(centroids)\n    plt.plot(times, centroids)\n    plt.title(title)\n    plt.xlabel('Time (s)')\n    plt.ylabel('Spectral Centroid')\n\nplot_spectral_centroid(y1, sr1, 1, 'XC2628 - Spectral Centroid')\nplot_spectral_centroid(y2, sr2, 2, 'XC16967 - Spectral Centroid')\nplot_spectral_centroid(y3, sr3, 3, 'XC99571 - Spectral Centroid')\nplot_spectral_centroid(y4, sr4, 4, 'XC133080 - Spectral Centroid')\nplot_spectral_centroid(y5, sr5, 5, 'XC127371 - Spectral Centroid')\nplot_spectral_centroid(y6, sr6, 6, 'XC130058 - Spectral Centroid')\nplot_spectral_centroid(y7, sr7, 7, 'XC51410 - Spectral Centroid')\nplot_spectral_centroid(y8, sr8, 8, 'XC109768 - Spectral Centroid')\nplot_spectral_centroid(y9, sr9, 9, 'XC17120 - Spectral Centroid')\nplot_spectral_centroid(y10, sr10, 10, 'XC31023 - Spectral Centroid')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:12:14.353743Z","iopub.execute_input":"2024-01-14T17:12:14.354085Z","iopub.status.idle":"2024-01-14T17:12:16.455375Z","shell.execute_reply.started":"2024-01-14T17:12:14.354053Z","shell.execute_reply":"2024-01-14T17:12:16.454298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# List of audio file paths\naudio_files = [\n    \"/kaggle/input/birdsong-recognition/train_audio/aldfly/XC2628.mp3\",\n    \"/kaggle/input/birdsong-recognition/train_audio/aldfly/XC16967.mp3\",\n    \"/kaggle/input/birdsong-recognition/train_audio/ameavo/XC99571.mp3\",\n    \"/kaggle/input/birdsong-recognition/train_audio/ameavo/XC133080.mp3\",\n    \"/kaggle/input/birdsong-recognition/train_audio/amebit/XC127371.mp3\",\n    \"/kaggle/input/birdsong-recognition/train_audio/amebit/XC130058.mp3\",\n    \"/kaggle/input/birdsong-recognition/train_audio/amecro/XC51410.mp3\",\n    \"/kaggle/input/birdsong-recognition/train_audio/amecro/XC109768.mp3\",\n    \"/kaggle/input/birdsong-recognition/train_audio/amegfi/XC17120.mp3\",\n    \"/kaggle/input/birdsong-recognition/train_audio/amegfi/XC31023.mp3\",\n]\ndef calculate_magnitude_phase(y, sr):\n    stft_result = librosa.stft(y)\n    magnitude, phase = librosa.magphase(stft_result)\n    return magnitude, phase\n\n# Create a 2x5 subplot grid\nfig, axs = plt.subplots(2, 5, figsize=(15, 6))\naxs = axs.flatten()\n\nfor i, audio_path in enumerate(audio_files):\n    # Load audio\n    y, sr = librosa.load(audio_path)\n\n    # Compute spectral centroid\n    centroid = librosa.feature.spectral_centroid(y=y, sr=sr)\n\n    # Compute magnitude and phase\n    magnitude, phase = calculate_magnitude_phase(y, sr)\n\n    # Plot spectrogram with spectral centroid\n    times = librosa.times_like(centroid)\n    librosa.display.specshow(librosa.amplitude_to_db(magnitude, ref=np.max),\n                             y_axis='log', x_axis='time', ax=axs[i])\n    axs[i].plot(times, centroid.T, label='Spectral centroid', color='w')\n    axs[i].legend(loc='upper right')\n    axs[i].set(title=f'log Power spectrogram - {audio_path.split(\"/\")[-1][:-4]}')\n\n# Adjust layout\nplt.tight_layout()\n\n# Show the plots\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:14:40.529533Z","iopub.execute_input":"2024-01-14T17:14:40.529950Z","iopub.status.idle":"2024-01-14T17:15:05.299733Z","shell.execute_reply.started":"2024-01-14T17:14:40.529919Z","shell.execute_reply":"2024-01-14T17:15:05.298871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zcr1 = librosa.feature.zero_crossing_rate(y1)\nprint(zcr1)\nprint(\"zcr1\")\nprint('max:',zcr1.max())\nprint('min:',zcr1.min())\nprint(\"-------------------------------\")\nzcr2 = librosa.feature.zero_crossing_rate(y2)\nprint(\"zcr2\")\nprint('max:',zcr2.max())\nprint('min:',zcr2.min())","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:15:42.527747Z","iopub.execute_input":"2024-01-14T17:15:42.528764Z","iopub.status.idle":"2024-01-14T17:15:42.732606Z","shell.execute_reply.started":"2024-01-14T17:15:42.528723Z","shell.execute_reply":"2024-01-14T17:15:42.731536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zcr3 = librosa.feature.zero_crossing_rate(y3)\nprint(\"zcr3\")\nprint('max:',zcr3.max())\nprint('min:',zcr3.min())\nprint(\"-------------------------------\")\nzcr4 = librosa.feature.zero_crossing_rate(y4)\nprint(\"zcr4\")\nprint('max:',zcr4.max())\nprint('min:',zcr4.min())","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:15:47.213534Z","iopub.execute_input":"2024-01-14T17:15:47.213909Z","iopub.status.idle":"2024-01-14T17:15:47.232308Z","shell.execute_reply.started":"2024-01-14T17:15:47.213878Z","shell.execute_reply":"2024-01-14T17:15:47.231305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zcr5 = librosa.feature.zero_crossing_rate(y5)\nprint(\"zcr5\")\nprint('max:',zcr5.max())\nprint('min:',zcr5.min())\nprint(\"-------------------------------\")\nzcr6 = librosa.feature.zero_crossing_rate(y6)\nprint(\"zcr6\")\nprint('max:',zcr6.max())\nprint('min:',zcr6.min())","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:12:18.870452Z","iopub.status.idle":"2024-01-14T17:12:18.870832Z","shell.execute_reply.started":"2024-01-14T17:12:18.870648Z","shell.execute_reply":"2024-01-14T17:12:18.870666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zcr7 = librosa.feature.zero_crossing_rate(y7)\nprint(\"zcr7\")\nprint('max:',zcr7.max())\nprint('min:',zcr7.min())\nprint(\"-------------------------------\")\nzcr8 = librosa.feature.zero_crossing_rate(y8)\nprint(\"zcr8\")\nprint('max:',zcr8.max())\nprint('min:',zcr8.min())","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:15:50.084450Z","iopub.execute_input":"2024-01-14T17:15:50.084808Z","iopub.status.idle":"2024-01-14T17:15:50.238675Z","shell.execute_reply.started":"2024-01-14T17:15:50.084780Z","shell.execute_reply":"2024-01-14T17:15:50.237631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zcr9 = librosa.feature.zero_crossing_rate(y9)\nprint(\"zcr9\")\nprint('max:',zcr9.max())\nprint('min:',zcr9.min())\nprint(\"-------------------------------\")\nzcr10 = librosa.feature.zero_crossing_rate(y10)\nprint(\"zcr10\")\nprint('max:',zcr10.max())\nprint('min:',zcr10.min())","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:15:51.742590Z","iopub.execute_input":"2024-01-14T17:15:51.743066Z","iopub.status.idle":"2024-01-14T17:15:51.799699Z","shell.execute_reply.started":"2024-01-14T17:15:51.743026Z","shell.execute_reply":"2024-01-14T17:15:51.798570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# RMS ENERGY","metadata":{}},{"cell_type":"code","source":"#energy (rms)\nenergy1 = librosa.feature.rms(y=y1)\nprint(energy1)\nprint('energy1')\nprint('max:',energy1.max())\nprint('min:',energy1.min())\nprint('mean:',energy1.mean())\nprint(\"-------------------------------\")\nenergy2 = librosa.feature.rms(y=y2)\nprint('energy2')\nprint('max:',energy2.max())\nprint('min:',energy2.min())\nprint('mean:',energy2.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:15:54.049394Z","iopub.execute_input":"2024-01-14T17:15:54.050053Z","iopub.status.idle":"2024-01-14T17:15:54.068268Z","shell.execute_reply.started":"2024-01-14T17:15:54.050007Z","shell.execute_reply":"2024-01-14T17:15:54.067182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#energy (rms)\nenergy3 = librosa.feature.rms(y=y3)\nprint('energy3')\nprint('max:',energy3.max())\nprint('min:',energy3.min())\nprint('mean:',energy3.mean())\nprint(\"-------------------------------\")\nenergy4 = librosa.feature.rms(y=y4)\nprint('energy4')\nprint('max:',energy4.max())\nprint('min:',energy4.min())\nprint('mean:',energy4.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:15:57.390943Z","iopub.execute_input":"2024-01-14T17:15:57.391776Z","iopub.status.idle":"2024-01-14T17:15:57.403836Z","shell.execute_reply.started":"2024-01-14T17:15:57.391718Z","shell.execute_reply":"2024-01-14T17:15:57.402446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#energy (rms)\nenergy5 = librosa.feature.rms(y=y5)\nprint('energy5')\nprint('max:',energy5.max())\nprint('min:',energy5.min())\nprint('mean:',energy5.mean())\nprint(\"-------------------------------\")\nenergy6 = librosa.feature.rms(y=y6)\nprint('energy6')\nprint('max:',energy6.max())\nprint('min:',energy6.min())\nprint('mean:',energy6.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:12:18.877222Z","iopub.status.idle":"2024-01-14T17:12:18.877542Z","shell.execute_reply.started":"2024-01-14T17:12:18.877382Z","shell.execute_reply":"2024-01-14T17:12:18.877397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#energy (rms)\nenergy7 = librosa.feature.rms(y=y7)\nprint('energy7')\nprint('max:',energy7.max())\nprint('min:',energy7.min())\nprint('mean:',energy7.mean())\nprint(\"-------------------------------\")\nenergy8 = librosa.feature.rms(y=y8)\nprint('energy8')\nprint('max:',energy8.max())\nprint('min:',energy8.min())\nprint('mean:',energy8.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:15:59.663787Z","iopub.execute_input":"2024-01-14T17:15:59.664179Z","iopub.status.idle":"2024-01-14T17:15:59.704880Z","shell.execute_reply.started":"2024-01-14T17:15:59.664139Z","shell.execute_reply":"2024-01-14T17:15:59.704165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#energy (rms)\nenergy9 = librosa.feature.rms(y=y9)\nprint('energy9')\nprint('max:',energy9.max())\nprint('min:',energy9.min())\nprint('mean:',energy9.mean())\nprint(\"-------------------------------\")\nenergy10 = librosa.feature.rms(y=y10)\nprint('energy10')\nprint('max:',energy10.max())\nprint('min:',energy10.min())\nprint('mean:',energy10.mean())","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:16:01.885268Z","iopub.execute_input":"2024-01-14T17:16:01.885673Z","iopub.status.idle":"2024-01-14T17:16:01.903301Z","shell.execute_reply.started":"2024-01-14T17:16:01.885638Z","shell.execute_reply":"2024-01-14T17:16:01.902178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MFCC","metadata":{}},{"cell_type":"code","source":"mfcc1 = librosa.feature.mfcc(y=y1, sr=sr1)\nprint(mfcc1)\nprint(\"mfcc1\")\nprint(\"max of mean-mfcc:\",np.mean(mfcc1, axis=1).max())\nprint(\"min of mean-mfcc:\",np.mean(mfcc1, axis=1).min())\nmfcc2 = librosa.feature.mfcc(y=y2, sr=sr2)\nprint(\"mfcc2\")\nprint(\"max of mean-mfcc:\",np.mean(mfcc2, axis=1).max())\nprint(\"min of mean-mfcc:\",np.mean(mfcc2, axis=1).min())\nmfcc3 = librosa.feature.mfcc(y=y3, sr=sr3)\nprint(\"mfcc3\")\nprint(\"max of mean-mfcc:\",np.mean(mfcc3, axis=1).max())\nprint(\"min of mean-mfcc:\",np.mean(mfcc3, axis=1).min())\nmfcc4 = librosa.feature.mfcc(y=y4, sr=sr4)\nprint(\"mfcc4\")\nprint(\"max of mean-mfcc:\",np.mean(mfcc4, axis=1).max())\nprint(\"min of mean-mfcc:\",np.mean(mfcc4, axis=1).min())\nmfcc5 = librosa.feature.mfcc(y=y5, sr=sr5)\nprint(\"mfcc5\")\nprint(\"max of mean-mfcc:\",np.mean(mfcc5, axis=1).max())\nprint(\"min of mean-mfcc:\",np.mean(mfcc5, axis=1).min())\nmfcc6 = librosa.feature.mfcc(y=y6, sr=sr6)\nprint(\"mfcc6\")\nprint(\"max of mean-mfcc:\",np.mean(mfcc6, axis=1).max())\nprint(\"min of mean-mfcc:\",np.mean(mfcc6, axis=1).min())\nmfcc7 = librosa.feature.mfcc(y=y7, sr=sr7)\nprint(\"mfcc7\")\nprint(\"max of mean-mfcc:\",np.mean(mfcc7, axis=1).max())\nprint(\"min of mean-mfcc:\",np.mean(mfcc7, axis=1).min())\nmfcc8 = librosa.feature.mfcc(y=y8, sr=sr8)\nprint(\"mfcc8\")\nprint(\"max of mean-mfcc:\",np.mean(mfcc8, axis=1).max())\nprint(\"min of mean-mfcc:\",np.mean(mfcc8, axis=1).min())\nmfcc9 = librosa.feature.mfcc(y=y9, sr=sr9)\nprint(\"mfcc9\")\nprint(\"max of mean-mfcc:\",np.mean(mfcc9, axis=1).max())\nprint(\"min of mean-mfcc:\",np.mean(mfcc9, axis=1).min())\nmfcc10 = librosa.feature.mfcc(y=y10, sr=sr10)\nprint(\"mfcc10\")\nprint(\"max of mean-mfcc:\",np.mean(mfcc10, axis=1).max())\nprint(\"min of mean-mfcc:\",np.mean(mfcc10, axis=1).min())","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:16:04.515152Z","iopub.execute_input":"2024-01-14T17:16:04.516245Z","iopub.status.idle":"2024-01-14T17:16:05.307640Z","shell.execute_reply.started":"2024-01-14T17:16:04.516204Z","shell.execute_reply":"2024-01-14T17:16:05.306541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compute pitches using the Harmonic-Percussive source separation\ny_harmonic1, y_percussive1 = librosa.effects.hpss(y1)\ny_harmonic2, y_percussive2 = librosa.effects.hpss(y2)\ny_harmonic3, y_percussive3 = librosa.effects.hpss(y3)\ny_harmonic4, y_percussive4 = librosa.effects.hpss(y4)\ny_harmonic5, y_percussive5 = librosa.effects.hpss(y5)\ny_harmonic6, y_percussive6 = librosa.effects.hpss(y6)\ny_harmonic7, y_percussive7 = librosa.effects.hpss(y7)\ny_harmonic8, y_percussive8 = librosa.effects.hpss(y8)\ny_harmonic9, y_percussive9 = librosa.effects.hpss(y9)\ny_harmonic10, y_percussive10 = librosa.effects.hpss(y10)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:16:07.571721Z","iopub.execute_input":"2024-01-14T17:16:07.572097Z","iopub.status.idle":"2024-01-14T17:16:28.958279Z","shell.execute_reply.started":"2024-01-14T17:16:07.572067Z","shell.execute_reply":"2024-01-14T17:16:28.957105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compute pitches\npitches1, magnitudes1 = librosa.core.piptrack(y=y_harmonic1, sr=sr1)\npitches2, magnitudes2 = librosa.core.piptrack(y=y_harmonic2, sr=sr2)\npitches3, magnitudes3 = librosa.core.piptrack(y=y_harmonic3, sr=sr3)\npitches4, magnitudes4 = librosa.core.piptrack(y=y_harmonic4, sr=sr4)\npitches5, magnitudes5 = librosa.core.piptrack(y=y_harmonic5, sr=sr5)\npitches6, magnitudes6 = librosa.core.piptrack(y=y_harmonic6, sr=sr6)\npitches7, magnitudes7 = librosa.core.piptrack(y=y_harmonic7, sr=sr7)\npitches8, magnitudes8 = librosa.core.piptrack(y=y_harmonic8, sr=sr8)\npitches9, magnitudes9 = librosa.core.piptrack(y=y_harmonic9, sr=sr9)\npitches10, magnitudes10 = librosa.core.piptrack(y=y_harmonic10, sr=sr10)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:16:35.165388Z","iopub.execute_input":"2024-01-14T17:16:35.165783Z","iopub.status.idle":"2024-01-14T17:16:35.945502Z","shell.execute_reply.started":"2024-01-14T17:16:35.165749Z","shell.execute_reply":"2024-01-14T17:16:35.944583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot pitches\nplt.figure(figsize=(12, 16))\n\nplt.subplot(5, 2, 1)\nlibrosa.display.specshow(librosa.amplitude_to_db(magnitudes1, ref=np.max), y_axis='log', x_axis='time')\nplt.title('XC2628')\n\nplt.subplot(5, 2, 2)\nlibrosa.display.specshow(librosa.amplitude_to_db(magnitudes2, ref=np.max), y_axis='log', x_axis='time')\nplt.title('XC16967')\n\nplt.subplot(5, 2, 3)\nlibrosa.display.specshow(librosa.amplitude_to_db(magnitudes3, ref=np.max), y_axis='log', x_axis='time')\nplt.title('XC99571')\n\nplt.subplot(5, 2, 4)\nlibrosa.display.specshow(librosa.amplitude_to_db(magnitudes4, ref=np.max), y_axis='log', x_axis='time')\nplt.title('XC133080')\n\nplt.subplot(5, 2, 5)\nlibrosa.display.specshow(librosa.amplitude_to_db(magnitudes5, ref=np.max), y_axis='log', x_axis='time')\nplt.title('XC127371')\n\nplt.subplot(5, 2, 6)\nlibrosa.display.specshow(librosa.amplitude_to_db(magnitudes6, ref=np.max), y_axis='log', x_axis='time')\nplt.title('XC130058')\n\nplt.subplot(5, 2, 7)\nlibrosa.display.specshow(librosa.amplitude_to_db(magnitudes7, ref=np.max), y_axis='log', x_axis='time')\nplt.title('XC51410')\n\nplt.subplot(5, 2, 8)\nlibrosa.display.specshow(librosa.amplitude_to_db(magnitudes8, ref=np.max), y_axis='log', x_axis='time')\nplt.title('XC109768')\n\nplt.subplot(5, 2, 9)\nlibrosa.display.specshow(librosa.amplitude_to_db(magnitudes9, ref=np.max), y_axis='log', x_axis='time')\nplt.title('XC17120')\n\nplt.subplot(5, 2, 10)\nlibrosa.display.specshow(librosa.amplitude_to_db(magnitudes10, ref=np.max), y_axis='log', x_axis='time')\nplt.title('XC31023')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:16:39.290307Z","iopub.execute_input":"2024-01-14T17:16:39.290671Z","iopub.status.idle":"2024-01-14T17:16:51.924552Z","shell.execute_reply.started":"2024-01-14T17:16:39.290643Z","shell.execute_reply":"2024-01-14T17:16:51.923490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"import cv2\nimport audioread\nimport logging\nimport os\nimport random\nimport time\nimport warnings\n\nimport librosa\nimport numpy as np\nimport pandas as pd\nimport soundfile as sf\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.utils.data as data\n\nfrom contextlib import contextmanager\nfrom pathlib import Path\nfrom typing import Optional\n\nfrom fastprogress import progress_bar\nfrom sklearn.metrics import f1_score\nfrom torchvision import models","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:16:57.622962Z","iopub.execute_input":"2024-01-14T17:16:57.623364Z","iopub.status.idle":"2024-01-14T17:16:59.527208Z","shell.execute_reply.started":"2024-01-14T17:16:57.623331Z","shell.execute_reply":"2024-01-14T17:16:59.526066Z"},"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    \ndef get_logger(out_file=None):\n    logger = logging.getLogger()\n    formatter = logging.Formatter(\"%(asctime)s - %(levelname)s - %(message)s\")\n    logger.handlers = []\n    logger.setLevel(logging.INFO)\n\n    handler = logging.StreamHandler()\n    handler.setFormatter(formatter)\n    handler.setLevel(logging.INFO)\n    logger.addHandler(handler)\n\n    if out_file is not None:\n        fh = logging.FileHandler(out_file)\n        fh.setFormatter(formatter)\n        fh.setLevel(logging.INFO)\n        logger.addHandler(fh)\n    logger.info(\"logger set up\")\n    return logger\n    \n    \n@contextmanager\ndef timer(name: str, logger: Optional[logging.Logger] = None):\n    t0 = time.time()\n    msg = f\"[{name}] start\"\n    if logger is None:\n        print(msg)\n    else:\n        logger.info(msg)\n    yield\n\n    msg = f\"[{name}] done in {time.time() - t0:.2f} s\"\n    if logger is None:\n        print(msg)\n    else:\n        logger.info(msg)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T17:35:31.146972Z","iopub.execute_input":"2024-01-14T17:35:31.147431Z","iopub.status.idle":"2024-01-14T17:35:31.157016Z","shell.execute_reply.started":"2024-01-14T17:35:31.147399Z","shell.execute_reply":"2024-01-14T17:35:31.155907Z"},"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-14T18:06:41.425230Z","iopub.execute_input":"2024-01-14T18:06:41.425666Z","iopub.status.idle":"2024-01-14T18:06:41.433460Z","shell.execute_reply.started":"2024-01-14T18:06:41.425629Z","shell.execute_reply":"2024-01-14T18:06:41.432422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TARGET_SR = 32000\nTEST = Path(\"/kaggle/input/weights/Document from Ashutosh Anand Mehta\")","metadata":{"execution":{"iopub.status.busy":"2024-01-14T18:06:43.834776Z","iopub.execute_input":"2024-01-14T18:06:43.835163Z","iopub.status.idle":"2024-01-14T18:06:43.841576Z","shell.execute_reply.started":"2024-01-14T18:06:43.835131Z","shell.execute_reply":"2024-01-14T18:06:43.840452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if TEST:\n    DATA_DIR = Path(\"../input/birdsong-recognition/\")\nelse:\n    # dataset created by @shonenkov, thanks!\n    DATA_DIR = Path(\"../input/birdcall-check/\")\n    \n\ntest = pd.read_csv(DATA_DIR / \"/kaggle/input/birdcall-check/test.csv\")\ntest_audio = DATA_DIR / \"/kaggle/input/birdcall-check/test_audio\"\n\n","metadata":{"execution":{"iopub.status.busy":"2024-01-14T18:08:13.724062Z","iopub.execute_input":"2024-01-14T18:08:13.724648Z","iopub.status.idle":"2024-01-14T18:08:13.734066Z","shell.execute_reply.started":"2024-01-14T18:08:13.724609Z","shell.execute_reply":"2024-01-14T18:08:13.733202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T18:06:50.801047Z","iopub.execute_input":"2024-01-14T18:06:50.801481Z","iopub.status.idle":"2024-01-14T18:06:50.814980Z","shell.execute_reply.started":"2024-01-14T18:06:50.801446Z","shell.execute_reply":"2024-01-14T18:06:50.813729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv(\"../input/birdsong-recognition/sample_submission.csv\")\nsub.to_csv(\"submission.csv\", index=False)  # this will be overwritten if everything goes well","metadata":{"execution":{"iopub.status.busy":"2024-01-14T18:06:53.413470Z","iopub.execute_input":"2024-01-14T18:06:53.413848Z","iopub.status.idle":"2024-01-14T18:06:53.423324Z","shell.execute_reply.started":"2024-01-14T18:06:53.413817Z","shell.execute_reply":"2024-01-14T18:06:53.422086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ResNet(nn.Module):\n    def __init__(self, base_model_name: str, pretrained=False,\n                 num_classes=264):\n        super().__init__()\n        base_model = models.__getattribute__(base_model_name)(\n            pretrained=pretrained)\n        layers = list(base_model.children())[:-2]\n        layers.append(nn.AdaptiveMaxPool2d(1))\n        self.encoder = nn.Sequential(*layers)\n\n        in_features = base_model.fc.in_features\n\n        self.classifier = nn.Sequential(\n            nn.Linear(in_features, 1024), nn.ReLU(), nn.Dropout(p=0.2),\n            nn.Linear(1024, 1024), nn.ReLU(), nn.Dropout(p=0.2),\n            nn.Linear(1024, num_classes))\n\n    def forward(self, x):\n        batch_size = x.size(0)\n        x = self.encoder(x).view(batch_size, -1)\n        x = self.classifier(x)\n        multiclass_proba = F.softmax(x, dim=1)\n        multilabel_proba = F.sigmoid(x)\n        return {\n            \"logits\": x,\n            \"multiclass_proba\": multiclass_proba,\n            \"multilabel_proba\": multilabel_proba\n        }","metadata":{"execution":{"iopub.status.busy":"2024-01-14T18:06:54.903216Z","iopub.execute_input":"2024-01-14T18:06:54.903622Z","iopub.status.idle":"2024-01-14T18:06:54.912787Z","shell.execute_reply.started":"2024-01-14T18:06:54.903584Z","shell.execute_reply":"2024-01-14T18:06:54.911930Z"},"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\nmelspectrogram_parameters = {\n    \"n_mels\": 128,\n    \"fmin\": 20,\n    \"fmax\": 16000\n}\n\nweights_path = \"/kaggle/input/weights/Document from Ashutosh Anand Mehta\"","metadata":{"execution":{"iopub.status.busy":"2024-01-14T18:06:56.957358Z","iopub.execute_input":"2024-01-14T18:06:56.957724Z","iopub.status.idle":"2024-01-14T18:06:56.962301Z","shell.execute_reply.started":"2024-01-14T18:06:56.957695Z","shell.execute_reply":"2024-01-14T18:06:56.961447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\ndf = pd.read_csv(\"/kaggle/input/birdsong-recognition/train.csv\")\n\nunique_bird_names = df.ebird_code.unique()\nlabel_encoder = LabelEncoder()\nencoded_labels = label_encoder.fit_transform(unique_bird_names)\nBIRD_CODE = dict(zip(unique_bird_names, encoded_labels))\n\n#for bird_name, label in BIRD_CODE.items():\n#    print(f\"{bird_name}:{label}\")\n\nINV_BIRD_CODE = {v: k for k, v in BIRD_CODE.items()}\n#for bird_name, label in INV_BIRD_CODE.items():\n#    print(f\"{bird_name}:{label}\")","metadata":{"execution":{"iopub.status.busy":"2024-01-14T18:06:59.125494Z","iopub.execute_input":"2024-01-14T18:06:59.125914Z","iopub.status.idle":"2024-01-14T18:06:59.430405Z","shell.execute_reply.started":"2024-01-14T18:06:59.125869Z","shell.execute_reply":"2024-01-14T18:06:59.429550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mono_to_color(X: np.ndarray,\n                  mean=None,\n                  std=None,\n                  norm_max=None,\n                  norm_min=None,\n                  eps=1e-6):\n    \n    # Stack X as [X,X,X]\n    X = np.stack([X, X, X], axis=-1)\n\n    # Standardize\n    mean = mean or X.mean()\n    X = X - mean\n    std = std or X.std()\n    Xstd = X / (std + eps)\n    _min, _max = Xstd.min(), Xstd.max()\n    norm_max = norm_max or _max\n    norm_min = norm_min or _min\n    if (_max - _min) > eps:\n        # Normalize to [0, 255]\n        V = Xstd\n        V[V < norm_min] = norm_min\n        V[V > norm_max] = norm_max\n        V = 255 * (V - norm_min) / (norm_max - norm_min)\n        V = V.astype(np.uint8)\n    else:\n        # Just zero\n        V = np.zeros_like(Xstd, dtype=np.uint8)\n    return V\n\n\nclass TestDataset(data.Dataset):\n    def __init__(self, df: pd.DataFrame, clip: np.ndarray,\n                 img_size=224, melspectrogram_parameters={}):\n        self.df = df\n        self.clip = clip\n        self.img_size = img_size\n        self.melspectrogram_parameters = melspectrogram_parameters\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx: int):\n        SR = 32000\n        sample = self.df.loc[idx, :]\n        site = sample.site\n        row_id = sample.row_id\n        \n        if site == \"site_3\":\n            y = self.clip.astype(np.float32)\n            len_y = len(y)\n            start = 0\n            end = SR * 5\n            images = []\n            while len_y > start:\n                y_batch = y[start:end].astype(np.float32)\n                if len(y_batch) != (SR * 5):\n                    break\n                start = end\n                end = end + SR * 5\n                \n                melspec = librosa.feature.melspectrogram(y_batch,\n                                                         sr=SR,\n                                                         **self.melspectrogram_parameters)\n                melspec = librosa.power_to_db(melspec).astype(np.float32)\n                image = mono_to_color(melspec)\n                height, width, _ = image.shape\n                image = cv2.resize(image, (int(width * self.img_size / height), self.img_size))\n                image = np.moveaxis(image, 2, 0)\n                image = (image / 255.0).astype(np.float32)\n                images.append(image)\n            images = np.asarray(images)\n            return images, row_id, site\n        else:\n            end_seconds = int(sample.seconds)\n            start_seconds = int(end_seconds - 5)\n            \n            start_index = SR * start_seconds\n            end_index = SR * end_seconds\n            \n            y = self.clip[start_index:end_index].astype(np.float32)\n\n            melspec = librosa.feature.melspectrogram(y, sr=SR, **self.melspectrogram_parameters)\n            melspec = librosa.power_to_db(melspec).astype(np.float32)\n\n            image = mono_to_color(melspec)\n            height, width, _ = image.shape\n            image = cv2.resize(image, (int(width * self.img_size / height), self.img_size))\n            image = np.moveaxis(image, 2, 0)\n            image = (image / 255.0).astype(np.float32)\n\n            return image, row_id, site","metadata":{"execution":{"iopub.status.busy":"2024-01-14T18:07:01.091722Z","iopub.execute_input":"2024-01-14T18:07:01.092093Z","iopub.status.idle":"2024-01-14T18:07:01.109681Z","shell.execute_reply.started":"2024-01-14T18:07:01.092063Z","shell.execute_reply":"2024-01-14T18:07:01.108809Z"},"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-14T18:07:03.081551Z","iopub.execute_input":"2024-01-14T18:07:03.082535Z","iopub.status.idle":"2024-01-14T18:07:03.088332Z","shell.execute_reply.started":"2024-01-14T18:07:03.082497Z","shell.execute_reply":"2024-01-14T18:07:03.087277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prediction_for_clip(test_df: pd.DataFrame, \n                        clip: np.ndarray, \n                        model: ResNet, \n                        mel_params: dict, \n                        threshold=0.5):\n\n    dataset = TestDataset(df=test_df, \n                          clip=clip,\n                          img_size=224,\n                          melspectrogram_parameters=mel_params)\n    loader = data.DataLoader(dataset, batch_size=1, shuffle=False)\n    device = torch.device('cpu')\n    \n    model.eval()\n    prediction_dict = {}\n    for image, row_id, site in progress_bar(loader):\n        site = site[0]\n        row_id = row_id[0]\n        if site in {\"site_1\", \"site_2\"}:\n            image = image.to(device)\n\n            with torch.no_grad():\n                prediction = model(image)\n                proba = prediction[\"multilabel_proba\"].detach().cpu().numpy().reshape(-1)\n\n            events = proba >= threshold\n            labels = np.argwhere(events).reshape(-1).tolist()\n\n        else:\n            # to avoid prediction on large batch\n            image = image.squeeze(0)\n            batch_size = 16\n            whole_size = image.size(0)\n            if whole_size % batch_size == 0:\n                n_iter = whole_size // batch_size\n            else:\n                n_iter = whole_size // batch_size + 1\n                \n            all_events = set()\n            for batch_i in range(n_iter):\n                batch = image[batch_i * batch_size:(batch_i + 1) * batch_size]\n                if batch.ndim == 3:\n                    batch = batch.unsqueeze(0)\n\n                batch = batch.to(device)\n                with torch.no_grad():\n                    prediction = model(batch)\n                    proba = prediction[\"multilabel_proba\"].detach().cpu().numpy()\n                    \n                events = proba >= threshold\n                for i in range(len(events)):\n                    event = events[i, :]\n                    labels = np.argwhere(event).reshape(-1).tolist()\n                    for label in labels:\n                        all_events.add(label)\n                        \n            labels = list(all_events)\n        if len(labels) == 0:\n            prediction_dict[row_id] = \"nocall\"\n        else:\n            labels_str_list = list(map(lambda x: INV_BIRD_CODE[x], labels))\n            label_string = \" \".join(labels_str_list)\n            prediction_dict[row_id] = label_string\n    return prediction_dict","metadata":{"execution":{"iopub.status.busy":"2024-01-14T18:07:05.010330Z","iopub.execute_input":"2024-01-14T18:07:05.011015Z","iopub.status.idle":"2024-01-14T18:07:05.024635Z","shell.execute_reply.started":"2024-01-14T18:07:05.010965Z","shell.execute_reply":"2024-01-14T18:07:05.023526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prediction(test_df: pd.DataFrame,\n               test_audio: Path,\n               model_config: dict,\n               mel_params: dict,\n               weights_path: str,\n               threshold=0.5):\n    model = get_model(model_config, weights_path)\n    unique_audio_id = test_df.audio_id.unique()\n\n    warnings.filterwarnings(\"ignore\")\n    prediction_dfs = []\n    for audio_id in unique_audio_id:\n        with timer(f\"Loading {audio_id}\", logger):\n            clip, _ = librosa.load(test_audio / (audio_id + \".mp3\"),\n                                   sr=TARGET_SR,\n                                   mono=True)\n        \n        test_df_for_audio_id = test_df.query(\n            f\"audio_id == '{audio_id}'\").reset_index(drop=True)\n        with timer(f\"Prediction on {audio_id}\", logger):\n            prediction_dict = prediction_for_clip(test_df_for_audio_id,\n                                                  clip=clip,\n                                                  model=model,\n                                                  mel_params=mel_params,\n                                                  threshold=threshold)\n        row_id = list(prediction_dict.keys())\n        birds = list(prediction_dict.values())\n        prediction_df = pd.DataFrame({\n            \"row_id\": row_id,\n            \"birds\": birds\n        })\n        prediction_dfs.append(prediction_df)\n    \n    prediction_df = pd.concat(prediction_dfs, axis=0, sort=False).reset_index(drop=True)\n    return prediction_df","metadata":{"execution":{"iopub.status.busy":"2024-01-14T18:07:06.985383Z","iopub.execute_input":"2024-01-14T18:07:06.985785Z","iopub.status.idle":"2024-01-14T18:07:06.993940Z","shell.execute_reply.started":"2024-01-14T18:07:06.985751Z","shell.execute_reply":"2024-01-14T18:07:06.992881Z"},"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)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-14T18:08:19.641935Z","iopub.execute_input":"2024-01-14T18:08:19.642336Z","iopub.status.idle":"2024-01-14T18:09:26.529008Z","shell.execute_reply.started":"2024-01-14T18:08:19.642302Z","shell.execute_reply":"2024-01-14T18:09:26.527970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T18:09:50.468634Z","iopub.execute_input":"2024-01-14T18:09:50.469641Z","iopub.status.idle":"2024-01-14T18:09:50.475834Z","shell.execute_reply.started":"2024-01-14T18:09:50.469601Z","shell.execute_reply":"2024-01-14T18:09:50.474692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2024-01-14T18:09:56.262180Z","iopub.execute_input":"2024-01-14T18:09:56.262556Z","iopub.status.idle":"2024-01-14T18:09:56.275204Z","shell.execute_reply.started":"2024-01-14T18:09:56.262525Z","shell.execute_reply":"2024-01-14T18:09:56.274126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.row_id.unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T18:10:00.551794Z","iopub.execute_input":"2024-01-14T18:10:00.552723Z","iopub.status.idle":"2024-01-14T18:10:00.560658Z","shell.execute_reply.started":"2024-01-14T18:10:00.552682Z","shell.execute_reply":"2024-01-14T18:10:00.559564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = pd.read_csv(\"/kaggle/working/submission.csv\")\n\n# Remove trailing numbers and duplicates from 'row_id' column\ndf1['row_id'] = df1['row_id'].replace(to_replace=r'_[0-9]+$', value='', regex=True)\ndf1 = df1.drop_duplicates()\nrows = df1[df1.duplicated(subset=['row_id'], keep=False) & (df1['birds'] == 'nocall')]\ndf_no_duplicates = df1.drop(rows.index)\n\ndf_no_duplicates = df_no_duplicates.reset_index(drop=True)\n\ndf_no_duplicates","metadata":{"execution":{"iopub.status.busy":"2024-01-14T18:10:04.492809Z","iopub.execute_input":"2024-01-14T18:10:04.493212Z","iopub.status.idle":"2024-01-14T18:10:04.515309Z","shell.execute_reply.started":"2024-01-14T18:10:04.493178Z","shell.execute_reply":"2024-01-14T18:10:04.514180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}