{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier\nfrom sklearn.neural_network import MLPClassifier\nimport matplotlib.pyplot as plt ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-28T13:33:59.756799Z","iopub.execute_input":"2023-04-28T13:33:59.757206Z","iopub.status.idle":"2023-04-28T13:34:00.669927Z","shell.execute_reply.started":"2023-04-28T13:33:59.757173Z","shell.execute_reply":"2023-04-28T13:34:00.668460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Studying datasets:","metadata":{}},{"cell_type":"markdown","source":"## df1:","metadata":{}},{"cell_type":"code","source":"df1 = pd.read_csv('/kaggle/input/birdclef-2023/eBird_Taxonomy_v2021.csv')\ndf1","metadata":{"execution":{"iopub.status.busy":"2023-04-28T13:34:00.674912Z","iopub.execute_input":"2023-04-28T13:34:00.675401Z","iopub.status.idle":"2023-04-28T13:34:00.806926Z","shell.execute_reply.started":"2023-04-28T13:34:00.675325Z","shell.execute_reply":"2023-04-28T13:34:00.804254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.info()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T13:34:00.808553Z","iopub.execute_input":"2023-04-28T13:34:00.810046Z","iopub.status.idle":"2023-04-28T13:34:00.843595Z","shell.execute_reply.started":"2023-04-28T13:34:00.809999Z","shell.execute_reply":"2023-04-28T13:34:00.842094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## df2:","metadata":{}},{"cell_type":"code","source":"df2 = pd.read_csv('/kaggle/input/birdclef-2023/train_metadata.csv')\ndf2","metadata":{"execution":{"iopub.status.busy":"2023-04-28T13:34:00.847463Z","iopub.execute_input":"2023-04-28T13:34:00.847848Z","iopub.status.idle":"2023-04-28T13:34:00.989535Z","shell.execute_reply.started":"2023-04-28T13:34:00.847813Z","shell.execute_reply":"2023-04-28T13:34:00.988198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.info()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T13:34:00.990819Z","iopub.execute_input":"2023-04-28T13:34:00.991141Z","iopub.status.idle":"2023-04-28T13:34:01.014550Z","shell.execute_reply.started":"2023-04-28T13:34:00.991112Z","shell.execute_reply":"2023-04-28T13:34:01.012856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Feature engineering:","metadata":{}},{"cell_type":"code","source":"# How looking now df1['SPECIES_GROUP']\nfor value in df1['SPECIES_GROUP']:\n    print(value)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T13:34:01.016324Z","iopub.execute_input":"2023-04-28T13:34:01.016822Z","iopub.status.idle":"2023-04-28T13:34:01.138553Z","shell.execute_reply.started":"2023-04-28T13:34:01.016782Z","shell.execute_reply":"2023-04-28T13:34:01.137622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# What df1['SPECIES_GROUP'] looks like afterwards\nlist1 = []\ndf1['FAMILY'].fillna('0',inplace=True)\nfor value in df1['FAMILY']:\n    if value != '0':\n        list1.append(value.split(' (')[1][:-1])\n    else:\n        list1.append(value)\ndf1['SPECIES_GROUP'] = list1\ndf1['SPECIES_GROUP']","metadata":{"execution":{"iopub.status.busy":"2023-04-28T13:34:01.139495Z","iopub.execute_input":"2023-04-28T13:34:01.139814Z","iopub.status.idle":"2023-04-28T13:34:01.167303Z","shell.execute_reply.started":"2023-04-28T13:34:01.139784Z","shell.execute_reply":"2023-04-28T13:34:01.165793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# How looking now df1['FAMILY']\nfor value in df1['FAMILY']:\n    print(value)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T13:34:01.169211Z","iopub.execute_input":"2023-04-28T13:34:01.169781Z","iopub.status.idle":"2023-04-28T13:34:01.303575Z","shell.execute_reply.started":"2023-04-28T13:34:01.169728Z","shell.execute_reply":"2023-04-28T13:34:01.302052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# What df1['FAMILY'] looks like afterwards\nlist1 = []\nfor value in df1['FAMILY']:\n    if value != '0':\n        list1.append(value.split(' (')[0])\n    else:\n        list1.append(value)\ndf1['FAMILY'] = list1\ndf1['FAMILY']","metadata":{"execution":{"iopub.status.busy":"2023-04-28T13:34:01.304958Z","iopub.execute_input":"2023-04-28T13:34:01.305305Z","iopub.status.idle":"2023-04-28T13:34:01.328608Z","shell.execute_reply.started":"2023-04-28T13:34:01.305273Z","shell.execute_reply":"2023-04-28T13:34:01.327531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.fillna('0',inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T13:34:01.334040Z","iopub.execute_input":"2023-04-28T13:34:01.334569Z","iopub.status.idle":"2023-04-28T13:34:01.349401Z","shell.execute_reply.started":"2023-04-28T13:34:01.334526Z","shell.execute_reply":"2023-04-28T13:34:01.347807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for column in df1:\n    df1[column] = LabelEncoder().fit_transform(df1[column].values)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T13:34:01.351265Z","iopub.execute_input":"2023-04-28T13:34:01.351787Z","iopub.status.idle":"2023-04-28T13:34:01.518932Z","shell.execute_reply.started":"2023-04-28T13:34:01.351727Z","shell.execute_reply":"2023-04-28T13:34:01.517620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1","metadata":{"execution":{"iopub.status.busy":"2023-04-28T13:34:01.520530Z","iopub.execute_input":"2023-04-28T13:34:01.520980Z","iopub.status.idle":"2023-04-28T13:34:01.539670Z","shell.execute_reply.started":"2023-04-28T13:34:01.520935Z","shell.execute_reply":"2023-04-28T13:34:01.538245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for column in df2:\n    df2[column] = LabelEncoder().fit_transform(df2[column].values)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T13:34:01.541186Z","iopub.execute_input":"2023-04-28T13:34:01.541663Z","iopub.status.idle":"2023-04-28T13:34:01.684195Z","shell.execute_reply.started":"2023-04-28T13:34:01.541575Z","shell.execute_reply":"2023-04-28T13:34:01.682691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2","metadata":{"execution":{"iopub.status.busy":"2023-04-28T13:34:01.686579Z","iopub.execute_input":"2023-04-28T13:34:01.687064Z","iopub.status.idle":"2023-04-28T13:34:01.706188Z","shell.execute_reply.started":"2023-04-28T13:34:01.687013Z","shell.execute_reply":"2023-04-28T13:34:01.705025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_full = df1.join(df2,how = 'left')\ndf_full","metadata":{"execution":{"iopub.status.busy":"2023-04-28T13:34:01.707767Z","iopub.execute_input":"2023-04-28T13:34:01.708119Z","iopub.status.idle":"2023-04-28T13:34:01.749867Z","shell.execute_reply.started":"2023-04-28T13:34:01.708085Z","shell.execute_reply":"2023-04-28T13:34:01.748438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## This common_name column will be used as a prediction for other MLs.","metadata":{}},{"cell_type":"code","source":"train_X,test_X,train_y,test_y = train_test_split(df_full.drop('common_name',axis=1),df_full['common_name'])\nMLs=dict()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T13:34:01.751297Z","iopub.execute_input":"2023-04-28T13:34:01.751688Z","iopub.status.idle":"2023-04-28T13:34:01.771118Z","shell.execute_reply.started":"2023-04-28T13:34:01.751652Z","shell.execute_reply":"2023-04-28T13:34:01.770112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DecisionTree = DecisionTreeClassifier(random_state=0)\nDecisionTree.fit(train_X,train_y)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:20:58.531500Z","iopub.execute_input":"2023-04-28T14:20:58.532143Z","iopub.status.idle":"2023-04-28T14:20:59.439387Z","shell.execute_reply.started":"2023-04-28T14:20:58.532103Z","shell.execute_reply":"2023-04-28T14:20:59.438083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('DecisionTree score: ',str(DecisionTree.score(test_X,test_y)))\nMLs['DecisionTree'] = DecisionTree.score(test_X,test_y)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:20:59.445314Z","iopub.execute_input":"2023-04-28T14:20:59.445953Z","iopub.status.idle":"2023-04-28T14:20:59.465326Z","shell.execute_reply.started":"2023-04-28T14:20:59.445908Z","shell.execute_reply":"2023-04-28T14:20:59.463932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DecisionTree1 = DecisionTreeClassifier(max_depth=52,random_state=0)\nDecisionTree1.fit(train_X,train_y)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:20:59.467089Z","iopub.execute_input":"2023-04-28T14:20:59.468092Z","iopub.status.idle":"2023-04-28T14:21:00.389739Z","shell.execute_reply.started":"2023-04-28T14:20:59.468045Z","shell.execute_reply":"2023-04-28T14:21:00.388421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('DecisionTree score: ',str(DecisionTree1.score(test_X,test_y)))\nMLs['DecisionTree1'] = DecisionTree1.score(test_X,test_y)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:21:00.391672Z","iopub.execute_input":"2023-04-28T14:21:00.392003Z","iopub.status.idle":"2023-04-28T14:21:00.413158Z","shell.execute_reply.started":"2023-04-28T14:21:00.391973Z","shell.execute_reply":"2023-04-28T14:21:00.411851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"RandomForest = RandomForestClassifier(random_state=0)\nRandomForest.fit(train_X,train_y)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:21:00.415253Z","iopub.execute_input":"2023-04-28T14:21:00.416130Z","iopub.status.idle":"2023-04-28T14:21:13.127119Z","shell.execute_reply.started":"2023-04-28T14:21:00.416090Z","shell.execute_reply":"2023-04-28T14:21:13.125950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('RandomForest score: ',str(RandomForest.score(test_X,test_y)))\nMLs['RandomForest'] = RandomForest.score(test_X,test_y)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:21:13.128770Z","iopub.execute_input":"2023-04-28T14:21:13.129236Z","iopub.status.idle":"2023-04-28T14:21:14.192683Z","shell.execute_reply.started":"2023-04-28T14:21:13.129187Z","shell.execute_reply":"2023-04-28T14:21:14.191524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"RandomForest1 = RandomForestClassifier(n_estimators=1000,max_depth=100,random_state=0)\nRandomForest1.fit(train_X,train_y)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:21:14.194589Z","iopub.execute_input":"2023-04-28T14:21:14.195089Z","iopub.status.idle":"2023-04-28T14:23:20.467838Z","shell.execute_reply.started":"2023-04-28T14:21:14.195027Z","shell.execute_reply":"2023-04-28T14:23:20.466700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('RandomForest score: ',str(RandomForest1.score(test_X,test_y)))\nMLs['RandomForest1'] = RandomForest1.score(test_X,test_y)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:23:20.469279Z","iopub.execute_input":"2023-04-28T14:23:20.469620Z","iopub.status.idle":"2023-04-28T14:23:30.014792Z","shell.execute_reply.started":"2023-04-28T14:23:20.469590Z","shell.execute_reply":"2023-04-28T14:23:30.013798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"GradientBoosting = GradientBoostingClassifier(random_state=0)\nGradientBoosting.fit(train_X,train_y)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T14:23:30.016312Z","iopub.execute_input":"2023-04-28T14:23:30.016623Z","iopub.status.idle":"2023-04-28T15:03:14.561673Z","shell.execute_reply.started":"2023-04-28T14:23:30.016594Z","shell.execute_reply":"2023-04-28T15:03:14.560499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('GradientBoosting score: ',str(GradientBoosting.score(test_X,test_y)))\nMLs['GradientBoosting'] = GradientBoosting.score(test_X,test_y)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T15:03:14.563329Z","iopub.execute_input":"2023-04-28T15:03:14.563704Z","iopub.status.idle":"2023-04-28T15:03:17.765712Z","shell.execute_reply.started":"2023-04-28T15:03:14.563671Z","shell.execute_reply":"2023-04-28T15:03:17.764650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"GradientBoosting1 = GradientBoostingClassifier(random_state=0, max_depth=5,n_estimators=100)\nGradientBoosting1.fit(train_X,train_y)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T15:03:17.767227Z","iopub.execute_input":"2023-04-28T15:03:17.767737Z","iopub.status.idle":"2023-04-28T15:42:03.149943Z","shell.execute_reply.started":"2023-04-28T15:03:17.767691Z","shell.execute_reply":"2023-04-28T15:42:03.148718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('GradientBoosting score: ',str(GradientBoosting1.score(test_X,test_y)))\nMLs['GradientBoosting1'] = GradientBoosting1.score(test_X,test_y)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T15:42:03.151300Z","iopub.execute_input":"2023-04-28T15:42:03.151688Z","iopub.status.idle":"2023-04-28T15:42:06.779353Z","shell.execute_reply.started":"2023-04-28T15:42:03.151655Z","shell.execute_reply":"2023-04-28T15:42:06.778411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MLP = MLPClassifier()\nMLP.fit(train_X,train_y)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T15:42:06.781118Z","iopub.execute_input":"2023-04-28T15:42:06.781577Z","iopub.status.idle":"2023-04-28T15:43:37.698086Z","shell.execute_reply.started":"2023-04-28T15:42:06.781532Z","shell.execute_reply":"2023-04-28T15:43:37.696886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('MLP score: ',str(MLP.score(test_X,test_y)))\nMLs['MLP'] = MLP.score(test_X,test_y)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T15:43:37.699929Z","iopub.execute_input":"2023-04-28T15:43:37.700651Z","iopub.status.idle":"2023-04-28T15:43:37.870553Z","shell.execute_reply.started":"2023-04-28T15:43:37.700605Z","shell.execute_reply":"2023-04-28T15:43:37.869260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MLP1 = MLPClassifier(max_iter=1000,alpha=10, random_state=0)\nMLP1.fit(train_X,train_y)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T15:43:37.872389Z","iopub.execute_input":"2023-04-28T15:43:37.873116Z","iopub.status.idle":"2023-04-28T15:46:02.650622Z","shell.execute_reply.started":"2023-04-28T15:43:37.873068Z","shell.execute_reply":"2023-04-28T15:46:02.649014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('MLP score: ',str(MLP1.score(test_X,test_y)))\nMLs['MLP1'] = MLP1.score(test_X,test_y)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T15:46:02.652674Z","iopub.execute_input":"2023-04-28T15:46:02.654060Z","iopub.status.idle":"2023-04-28T15:46:02.792004Z","shell.execute_reply.started":"2023-04-28T15:46:02.653994Z","shell.execute_reply":"2023-04-28T15:46:02.790395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(30,10))\nplt.title('Scores MLs:')\nplt.bar(MLs.keys(),MLs.values())","metadata":{"execution":{"iopub.status.busy":"2023-04-28T15:54:24.904956Z","iopub.execute_input":"2023-04-28T15:54:24.905400Z","iopub.status.idle":"2023-04-28T15:54:25.319204Z","shell.execute_reply.started":"2023-04-28T15:54:24.905361Z","shell.execute_reply":"2023-04-28T15:54:25.318237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best=[ key for key,value in zip(MLs.keys(),MLs.values()) if value == max(MLs.values())]\nprint('The best ML:',best[0])","metadata":{"execution":{"iopub.status.busy":"2023-04-28T16:00:11.098267Z","iopub.execute_input":"2023-04-28T16:00:11.098708Z","iopub.status.idle":"2023-04-28T16:00:11.105536Z","shell.execute_reply.started":"2023-04-28T16:00:11.098671Z","shell.execute_reply":"2023-04-28T16:00:11.104264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}