{"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":"# 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)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import train_test_split\n\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":"2023-01-03T17:06:22.484063Z","iopub.execute_input":"2023-01-03T17:06:22.484658Z","iopub.status.idle":"2023-01-03T17:06:22.518735Z","shell.execute_reply.started":"2023-01-03T17:06:22.484602Z","shell.execute_reply":"2023-01-03T17:06:22.517499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test=pd.read_csv('/kaggle/input/emsi-5iir-ds/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:06:22.521226Z","iopub.execute_input":"2023-01-03T17:06:22.522435Z","iopub.status.idle":"2023-01-03T17:06:27.225297Z","shell.execute_reply.started":"2023-01-03T17:06:22.522364Z","shell.execute_reply":"2023-01-03T17:06:27.223538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv(\"/kaggle/input/emsi-5iir-ds/sample_submission.csv\")\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:06:27.227005Z","iopub.execute_input":"2023-01-03T17:06:27.227526Z","iopub.status.idle":"2023-01-03T17:06:27.351999Z","shell.execute_reply.started":"2023-01-03T17:06:27.227462Z","shell.execute_reply":"2023-01-03T17:06:27.350747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:06:27.354536Z","iopub.execute_input":"2023-01-03T17:06:27.355154Z","iopub.status.idle":"2023-01-03T17:06:27.389601Z","shell.execute_reply.started":"2023-01-03T17:06:27.355089Z","shell.execute_reply":"2023-01-03T17:06:27.388112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df=pd.read_csv('/kaggle/input/emsi-5iir-ds/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:06:27.395679Z","iopub.execute_input":"2023-01-03T17:06:27.396094Z","iopub.status.idle":"2023-01-03T17:06:33.618496Z","shell.execute_reply.started":"2023-01-03T17:06:27.396059Z","shell.execute_reply":"2023-01-03T17:06:33.617322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:06:33.620237Z","iopub.execute_input":"2023-01-03T17:06:33.620739Z","iopub.status.idle":"2023-01-03T17:06:33.651730Z","shell.execute_reply.started":"2023-01-03T17:06:33.620679Z","shell.execute_reply":"2023-01-03T17:06:33.650417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:06:33.653776Z","iopub.execute_input":"2023-01-03T17:06:33.654312Z","iopub.status.idle":"2023-01-03T17:06:33.685261Z","shell.execute_reply.started":"2023-01-03T17:06:33.654264Z","shell.execute_reply":"2023-01-03T17:06:33.683959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df_test.shape, train_df.shape)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:06:33.687202Z","iopub.execute_input":"2023-01-03T17:06:33.687644Z","iopub.status.idle":"2023-01-03T17:06:33.698025Z","shell.execute_reply.started":"2023-01-03T17:06:33.687565Z","shell.execute_reply":"2023-01-03T17:06:33.696105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.isnull().sum().sum(),train_df.isnull().sum().sum() ","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:06:33.699951Z","iopub.execute_input":"2023-01-03T17:06:33.700474Z","iopub.status.idle":"2023-01-03T17:06:33.868057Z","shell.execute_reply.started":"2023-01-03T17:06:33.700422Z","shell.execute_reply":"2023-01-03T17:06:33.866620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.duplicated().sum(),train_df.duplicated().sum()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:06:33.869405Z","iopub.execute_input":"2023-01-03T17:06:33.869933Z","iopub.status.idle":"2023-01-03T17:06:39.288849Z","shell.execute_reply.started":"2023-01-03T17:06:33.869865Z","shell.execute_reply":"2023-01-03T17:06:39.287403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.describe()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:06:39.290400Z","iopub.execute_input":"2023-01-03T17:06:39.290912Z","iopub.status.idle":"2023-01-03T17:06:40.279337Z","shell.execute_reply.started":"2023-01-03T17:06:39.290860Z","shell.execute_reply":"2023-01-03T17:06:40.277933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df.dtypes)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:06:40.280753Z","iopub.execute_input":"2023-01-03T17:06:40.282802Z","iopub.status.idle":"2023-01-03T17:06:40.292196Z","shell.execute_reply.started":"2023-01-03T17:06:40.282740Z","shell.execute_reply":"2023-01-03T17:06:40.290879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['f_27']","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:06:40.294035Z","iopub.execute_input":"2023-01-03T17:06:40.294419Z","iopub.status.idle":"2023-01-03T17:06:40.313392Z","shell.execute_reply.started":"2023-01-03T17:06:40.294382Z","shell.execute_reply":"2023-01-03T17:06:40.311829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"float_features = [f for f in train_df.columns if train_df[f].dtype == 'float64']\n\n","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:06:40.318715Z","iopub.execute_input":"2023-01-03T17:06:40.319218Z","iopub.status.idle":"2023-01-03T17:06:40.327080Z","shell.execute_reply.started":"2023-01-03T17:06:40.319165Z","shell.execute_reply":"2023-01-03T17:06:40.325164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 12))\nsns.heatmap(train_df[float_features + ['target']].corr(), center=0, annot=True, fmt='.2f')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:06:40.328596Z","iopub.execute_input":"2023-01-03T17:06:40.328988Z","iopub.status.idle":"2023-01-03T17:06:42.773951Z","shell.execute_reply.started":"2023-01-03T17:06:40.328944Z","shell.execute_reply":"2023-01-03T17:06:42.772512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"int_features = [f for f in train_df.columns if train_df[f].dtype == 'int64' and f != 'id' and f!= 'target']","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:06:42.775336Z","iopub.execute_input":"2023-01-03T17:06:42.775737Z","iopub.status.idle":"2023-01-03T17:06:42.783827Z","shell.execute_reply.started":"2023-01-03T17:06:42.775703Z","shell.execute_reply":"2023-01-03T17:06:42.782221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 12))\nsns.heatmap(train_df[int_features + ['target']].corr(), center=0, annot=True, fmt='.2f')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:06:42.785376Z","iopub.execute_input":"2023-01-03T17:06:42.785914Z","iopub.status.idle":"2023-01-03T17:06:44.690807Z","shell.execute_reply.started":"2023-01-03T17:06:42.785872Z","shell.execute_reply":"2023-01-03T17:06:44.689346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.f_27.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:06:44.692455Z","iopub.execute_input":"2023-01-03T17:06:44.692993Z","iopub.status.idle":"2023-01-03T17:06:45.720012Z","shell.execute_reply.started":"2023-01-03T17:06:44.692931Z","shell.execute_reply":"2023-01-03T17:06:45.718796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(10):\n    print(f'Position {i}')\n    tg = train_df.groupby(train_df.f_27.str.get(i))\n    temp = pd.DataFrame({'size': tg.size(), 'probability': tg.target.mean().round(2)})\n    print(temp)\n    print()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:06:45.721219Z","iopub.execute_input":"2023-01-03T17:06:45.721616Z","iopub.status.idle":"2023-01-03T17:06:51.429432Z","shell.execute_reply.started":"2023-01-03T17:06:45.721562Z","shell.execute_reply":"2023-01-03T17:06:51.428165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_characters = train_df.f_27.apply(lambda s: len(set(s))).rename('unique_characters')\ntg = train_df.groupby(unique_characters)\ntemp = pd.DataFrame({'size': tg.size(), 'probability': tg.target.mean().round(2)})\nprint(temp)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:06:51.431491Z","iopub.execute_input":"2023-01-03T17:06:51.431911Z","iopub.status.idle":"2023-01-03T17:06:52.279045Z","shell.execute_reply.started":"2023-01-03T17:06:51.431867Z","shell.execute_reply":"2023-01-03T17:06:52.277516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for df in [train_df, df_test]:\n    for i in range(10):\n        df[f'ch{i}'] = df.f_27.str.get(i).apply(ord) - ord('A')\n    df[\"unique_characters\"] = df.f_27.apply(lambda s: len(set(s)))","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:06:52.280834Z","iopub.execute_input":"2023-01-03T17:06:52.281676Z","iopub.status.idle":"2023-01-03T17:07:06.324195Z","shell.execute_reply.started":"2023-01-03T17:06:52.281621Z","shell.execute_reply":"2023-01-03T17:07:06.322903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:07:06.325718Z","iopub.execute_input":"2023-01-03T17:07:06.326231Z","iopub.status.idle":"2023-01-03T17:07:06.355238Z","shell.execute_reply.started":"2023-01-03T17:07:06.326175Z","shell.execute_reply":"2023-01-03T17:07:06.354297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.describe()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:07:06.356889Z","iopub.execute_input":"2023-01-03T17:07:06.357523Z","iopub.status.idle":"2023-01-03T17:07:07.696949Z","shell.execute_reply.started":"2023-01-03T17:07:06.357482Z","shell.execute_reply":"2023-01-03T17:07:07.695100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train2_df=pd.read_csv('/kaggle/input/emsi-5iir-ds/train.csv')\ntest2_df=pd.read_csv('/kaggle/input/emsi-5iir-ds/test.csv')\n","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:07:07.698700Z","iopub.execute_input":"2023-01-03T17:07:07.699079Z","iopub.status.idle":"2023-01-03T17:07:18.063883Z","shell.execute_reply.started":"2023-01-03T17:07:07.699043Z","shell.execute_reply":"2023-01-03T17:07:18.062666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for df in [train2_df,test2_df]:\n    for i in range(10):\n        df[f'ch{i}'] = df.f_27.str.get(i).apply(ord) - ord('A')\n    df[\"unique_characters\"] = df.f_27.apply(lambda s: len(set(s)))","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:07:18.065441Z","iopub.execute_input":"2023-01-03T17:07:18.065843Z","iopub.status.idle":"2023-01-03T17:07:32.094427Z","shell.execute_reply.started":"2023-01-03T17:07:18.065806Z","shell.execute_reply":"2023-01-03T17:07:32.093135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train2_df = train2_df.drop(labels=['f_27'],axis=1)\ntest2_df = test2_df.drop(labels=['f_27'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:07:32.095811Z","iopub.execute_input":"2023-01-03T17:07:32.096141Z","iopub.status.idle":"2023-01-03T17:07:32.502308Z","shell.execute_reply.started":"2023-01-03T17:07:32.096110Z","shell.execute_reply":"2023-01-03T17:07:32.501089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(train2_df.drop(columns=['target']),train2_df['target'],test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:07:32.503637Z","iopub.execute_input":"2023-01-03T17:07:32.504004Z","iopub.status.idle":"2023-01-03T17:07:33.231264Z","shell.execute_reply.started":"2023-01-03T17:07:32.503970Z","shell.execute_reply":"2023-01-03T17:07:33.230069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf=RandomForestClassifier(n_estimators=100)\n\nclf.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:07:33.232852Z","iopub.execute_input":"2023-01-03T17:07:33.233212Z","iopub.status.idle":"2023-01-03T17:07:33.238258Z","shell.execute_reply.started":"2023-01-03T17:07:33.233179Z","shell.execute_reply":"2023-01-03T17:07:33.237111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=clf.predict(X_test)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:07:33.239731Z","iopub.execute_input":"2023-01-03T17:07:33.240099Z","iopub.status.idle":"2023-01-03T17:07:33.269457Z","shell.execute_reply.started":"2023-01-03T17:07:33.240064Z","shell.execute_reply":"2023-01-03T17:07:33.266938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import metrics\nprint(\"Accuracy:\",metrics.accuracy_score(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:07:33.270918Z","iopub.status.idle":"2023-01-03T17:07:33.271562Z","shell.execute_reply.started":"2023-01-03T17:07:33.271241Z","shell.execute_reply":"2023-01-03T17:07:33.271272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=clf.predict(test2_df)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:07:33.273444Z","iopub.status.idle":"2023-01-03T17:07:33.274111Z","shell.execute_reply.started":"2023-01-03T17:07:33.273855Z","shell.execute_reply":"2023-01-03T17:07:33.273886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'id':test2_df['id'],'target':y_pred})","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:07:33.276001Z","iopub.status.idle":"2023-01-03T17:07:33.276680Z","shell.execute_reply.started":"2023-01-03T17:07:33.276317Z","shell.execute_reply":"2023-01-03T17:07:33.276349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:07:33.278460Z","iopub.status.idle":"2023-01-03T17:07:33.279134Z","shell.execute_reply.started":"2023-01-03T17:07:33.278803Z","shell.execute_reply":"2023-01-03T17:07:33.278838Z"},"trusted":true},"execution_count":null,"outputs":[]}]}