{"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)\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-07T14:10:40.541242Z","iopub.execute_input":"2023-01-07T14:10:40.541860Z","iopub.status.idle":"2023-01-07T14:10:40.584006Z","shell.execute_reply.started":"2023-01-07T14:10:40.541697Z","shell.execute_reply":"2023-01-07T14:10:40.583026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/emsi-5iir-ds/train.csv\")\ntest_df = pd.read_csv(\"/kaggle/input/emsi-5iir-ds/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:12:14.860287Z","iopub.execute_input":"2023-01-07T14:12:14.860693Z","iopub.status.idle":"2023-01-07T14:12:33.621290Z","shell.execute_reply.started":"2023-01-07T14:12:14.860660Z","shell.execute_reply":"2023-01-07T14:12:33.619822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Size of the train_df :\",train_df.shape[0])\nprint(\"Size of the test_df: \",test_df.shape[0])","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:12:55.618665Z","iopub.execute_input":"2023-01-07T14:12:55.619193Z","iopub.status.idle":"2023-01-07T14:12:55.627031Z","shell.execute_reply.started":"2023-01-07T14:12:55.619155Z","shell.execute_reply":"2023-01-07T14:12:55.625426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## check null values\ntrain_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:13:27.817445Z","iopub.execute_input":"2023-01-07T14:13:27.817918Z","iopub.status.idle":"2023-01-07T14:13:27.928532Z","shell.execute_reply.started":"2023-01-07T14:13:27.817882Z","shell.execute_reply":"2023-01-07T14:13:27.927062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Total columns are \",train_df.shape[1])\nnum_cols = train_df.select_dtypes([\"int\",\"float\"])\nprint(\"Number of numerical columns are : \",num_cols.shape[1])\ncat_cols = train_df.select_dtypes([\"object\"])\nprint(\"Number of categorical columns are : \",cat_cols.shape[1])","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:13:59.656018Z","iopub.execute_input":"2023-01-07T14:13:59.656550Z","iopub.status.idle":"2023-01-07T14:13:59.752247Z","shell.execute_reply.started":"2023-01-07T14:13:59.656509Z","shell.execute_reply":"2023-01-07T14:13:59.750668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n\ntarget = train_df.target\nsns.countplot(x=target.value_counts())\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:16:04.677032Z","iopub.execute_input":"2023-01-07T14:16:04.677575Z","iopub.status.idle":"2023-01-07T14:16:05.584192Z","shell.execute_reply.started":"2023-01-07T14:16:04.677536Z","shell.execute_reply":"2023-01-07T14:16:05.582778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection  import train_test_split\nfrom sklearn.linear_model import LogisticRegression","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:16:22.357993Z","iopub.execute_input":"2023-01-07T14:16:22.359347Z","iopub.status.idle":"2023-01-07T14:16:22.364488Z","shell.execute_reply.started":"2023-01-07T14:16:22.359300Z","shell.execute_reply":"2023-01-07T14:16:22.363269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x, cv_x, y_train, y_cv  = train_test_split(num_cols,train_df.target,stratify=train_df.target)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:18:15.442890Z","iopub.execute_input":"2023-01-07T14:18:15.443338Z","iopub.status.idle":"2023-01-07T14:18:16.240733Z","shell.execute_reply.started":"2023-01-07T14:18:15.443306Z","shell.execute_reply":"2023-01-07T14:18:16.239046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x.drop(['target',\"id\"],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:18:35.068611Z","iopub.execute_input":"2023-01-07T14:18:35.069455Z","iopub.status.idle":"2023-01-07T14:18:35.104642Z","shell.execute_reply.started":"2023-01-07T14:18:35.069407Z","shell.execute_reply":"2023-01-07T14:18:35.099669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv_x.drop([\"target\",\"id\"],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:19:00.081492Z","iopub.execute_input":"2023-01-07T14:19:00.081903Z","iopub.status.idle":"2023-01-07T14:19:00.110157Z","shell.execute_reply.started":"2023-01-07T14:19:00.081870Z","shell.execute_reply":"2023-01-07T14:19:00.108733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:19:10.356898Z","iopub.execute_input":"2023-01-07T14:19:10.357311Z","iopub.status.idle":"2023-01-07T14:19:10.362568Z","shell.execute_reply.started":"2023-01-07T14:19:10.357276Z","shell.execute_reply":"2023-01-07T14:19:10.361539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr = LogisticRegression(max_iter=500)\nlr.fit(train_x.values, y_train.values)\npred = lr.predict(train_x.values)\nprint(\"The train accuracy : \",accuracy_score(y_train.values,pred))\npred  = lr.predict(cv_x.values)\nprint(\"The cv accuracy : \",accuracy_score(y_cv.values, pred))","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:19:23.060743Z","iopub.execute_input":"2023-01-07T14:19:23.061155Z","iopub.status.idle":"2023-01-07T14:19:50.718746Z","shell.execute_reply.started":"2023-01-07T14:19:23.061124Z","shell.execute_reply":"2023-01-07T14:19:50.717557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_x = test_df.select_dtypes([\"int\",\"float\"])\ntest_x.shape\ntest_id = test_x.id.values\ntest_x.drop(\"id\",axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:23:18.670490Z","iopub.execute_input":"2023-01-07T14:23:18.671026Z","iopub.status.idle":"2023-01-07T14:23:18.914842Z","shell.execute_reply.started":"2023-01-07T14:23:18.670985Z","shell.execute_reply":"2023-01-07T14:23:18.913198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = lr.predict(test_x.values)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:23:49.218246Z","iopub.execute_input":"2023-01-07T14:23:49.218703Z","iopub.status.idle":"2023-01-07T14:23:49.401483Z","shell.execute_reply.started":"2023-01-07T14:23:49.218667Z","shell.execute_reply":"2023-01-07T14:23:49.399703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(\"/kaggle/input/emsi-5iir-ds/sample_submission.csv\")\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:25:08.811046Z","iopub.execute_input":"2023-01-07T14:25:08.811694Z","iopub.status.idle":"2023-01-07T14:25:09.053784Z","shell.execute_reply.started":"2023-01-07T14:25:08.811641Z","shell.execute_reply":"2023-01-07T14:25:09.052452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame({\n    \"id\" : test_id,\n    \"target\": pred\n})","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:25:22.118598Z","iopub.execute_input":"2023-01-07T14:25:22.119065Z","iopub.status.idle":"2023-01-07T14:25:22.127984Z","shell.execute_reply.started":"2023-01-07T14:25:22.119027Z","shell.execute_reply":"2023-01-07T14:25:22.126961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:25:33.657735Z","iopub.execute_input":"2023-01-07T14:25:33.658155Z","iopub.status.idle":"2023-01-07T14:25:33.668608Z","shell.execute_reply.started":"2023-01-07T14:25:33.658124Z","shell.execute_reply":"2023-01-07T14:25:33.667659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:25:45.639287Z","iopub.execute_input":"2023-01-07T14:25:45.639832Z","iopub.status.idle":"2023-01-07T14:25:46.425352Z","shell.execute_reply.started":"2023-01-07T14:25:45.639781Z","shell.execute_reply":"2023-01-07T14:25:46.423740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## random forest\nfrom sklearn.ensemble import RandomForestClassifier\nrf = RandomForestClassifier(n_estimators=20,max_depth=10).fit(train_x.values, y_train.values)\npred = rf.predict(train_x.values)\nprint(\"The accuaracy score : \",accuracy_score(y_train.values, pred))\npred = rf.predict(cv_x.values)\nprint(\"The accuaracy score of cv : \",accuracy_score(y_cv.values, pred))","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:26:24.146196Z","iopub.execute_input":"2023-01-07T14:26:24.146672Z","iopub.status.idle":"2023-01-07T14:28:11.471996Z","shell.execute_reply.started":"2023-01-07T14:26:24.146631Z","shell.execute_reply":"2023-01-07T14:28:11.470845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = rf.predict(test_x.values)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:28:48.733142Z","iopub.execute_input":"2023-01-07T14:28:48.733606Z","iopub.status.idle":"2023-01-07T14:28:50.855769Z","shell.execute_reply.started":"2023-01-07T14:28:48.733561Z","shell.execute_reply":"2023-01-07T14:28:50.854345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame({\n    \"id\" : test_id,\n    \"target\": pred\n})","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:29:08.657783Z","iopub.execute_input":"2023-01-07T14:29:08.658256Z","iopub.status.idle":"2023-01-07T14:29:08.667328Z","shell.execute_reply.started":"2023-01-07T14:29:08.658214Z","shell.execute_reply":"2023-01-07T14:29:08.665797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:29:20.777892Z","iopub.execute_input":"2023-01-07T14:29:20.778288Z","iopub.status.idle":"2023-01-07T14:29:21.514483Z","shell.execute_reply.started":"2023-01-07T14:29:20.778257Z","shell.execute_reply":"2023-01-07T14:29:21.513183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T14:29:31.291950Z","iopub.execute_input":"2023-01-07T14:29:31.293033Z","iopub.status.idle":"2023-01-07T14:29:31.305607Z","shell.execute_reply.started":"2023-01-07T14:29:31.292988Z","shell.execute_reply":"2023-01-07T14:29:31.304118Z"},"trusted":true},"execution_count":null,"outputs":[]}]}