{"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-08T15:39:36.109416Z","iopub.execute_input":"2023-01-08T15:39:36.110253Z","iopub.status.idle":"2023-01-08T15:39:36.140362Z","shell.execute_reply.started":"2023-01-08T15:39:36.110153Z","shell.execute_reply":"2023-01-08T15:39:36.139460Z"},"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-08T15:40:01.646222Z","iopub.execute_input":"2023-01-08T15:40:01.646684Z","iopub.status.idle":"2023-01-08T15:40:19.787646Z","shell.execute_reply.started":"2023-01-08T15:40:01.646643Z","shell.execute_reply":"2023-01-08T15:40:19.786477Z"},"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-08T15:40:39.411652Z","iopub.execute_input":"2023-01-08T15:40:39.412070Z","iopub.status.idle":"2023-01-08T15:40:39.418228Z","shell.execute_reply.started":"2023-01-08T15:40:39.412034Z","shell.execute_reply":"2023-01-08T15:40:39.417081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## check null values\ntrain_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:40:50.160827Z","iopub.execute_input":"2023-01-08T15:40:50.161220Z","iopub.status.idle":"2023-01-08T15:40:50.260626Z","shell.execute_reply.started":"2023-01-08T15:40:50.161181Z","shell.execute_reply":"2023-01-08T15:40:50.259382Z"},"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-08T15:41:04.169630Z","iopub.execute_input":"2023-01-08T15:41:04.170108Z","iopub.status.idle":"2023-01-08T15:41:04.253845Z","shell.execute_reply.started":"2023-01-08T15:41:04.170072Z","shell.execute_reply":"2023-01-08T15:41:04.252588Z"},"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() ## data seems balanced","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:41:13.701109Z","iopub.execute_input":"2023-01-08T15:41:13.701600Z","iopub.status.idle":"2023-01-08T15:41:14.566520Z","shell.execute_reply.started":"2023-01-08T15:41:13.701563Z","shell.execute_reply":"2023-01-08T15:41:14.565358Z"},"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-08T15:41:28.616608Z","iopub.execute_input":"2023-01-08T15:41:28.617073Z","iopub.status.idle":"2023-01-08T15:41:28.843792Z","shell.execute_reply.started":"2023-01-08T15:41:28.617034Z","shell.execute_reply":"2023-01-08T15:41:28.842583Z"},"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-08T15:42:20.198298Z","iopub.execute_input":"2023-01-08T15:42:20.198747Z","iopub.status.idle":"2023-01-08T15:42:20.932045Z","shell.execute_reply.started":"2023-01-08T15:42:20.198708Z","shell.execute_reply":"2023-01-08T15:42:20.930588Z"},"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-08T15:42:25.286806Z","iopub.execute_input":"2023-01-08T15:42:25.288107Z","iopub.status.idle":"2023-01-08T15:42:25.406388Z","shell.execute_reply.started":"2023-01-08T15:42:25.288062Z","shell.execute_reply":"2023-01-08T15:42:25.402755Z"},"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-08T15:42:28.168899Z","iopub.execute_input":"2023-01-08T15:42:28.169346Z","iopub.status.idle":"2023-01-08T15:42:28.195012Z","shell.execute_reply.started":"2023-01-08T15:42:28.169309Z","shell.execute_reply":"2023-01-08T15:42:28.193662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:42:37.843729Z","iopub.execute_input":"2023-01-08T15:42:37.844202Z","iopub.status.idle":"2023-01-08T15:42:37.850037Z","shell.execute_reply.started":"2023-01-08T15:42:37.844157Z","shell.execute_reply":"2023-01-08T15:42:37.848553Z"},"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-08T15:42:47.236478Z","iopub.execute_input":"2023-01-08T15:42:47.236896Z","iopub.status.idle":"2023-01-08T15:43:33.499566Z","shell.execute_reply.started":"2023-01-08T15:42:47.236865Z","shell.execute_reply":"2023-01-08T15:43:33.494912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## submission\ntest_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-08T15:43:44.468554Z","iopub.execute_input":"2023-01-08T15:43:44.468939Z","iopub.status.idle":"2023-01-08T15:43:44.627801Z","shell.execute_reply.started":"2023-01-08T15:43:44.468908Z","shell.execute_reply":"2023-01-08T15:43:44.626599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = lr.predict(test_x.values)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:44:18.752894Z","iopub.execute_input":"2023-01-08T15:44:18.753330Z","iopub.status.idle":"2023-01-08T15:44:18.859195Z","shell.execute_reply.started":"2023-01-08T15:44:18.753294Z","shell.execute_reply":"2023-01-08T15:44:18.857471Z"},"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-08T15:44:46.015045Z","iopub.execute_input":"2023-01-08T15:44:46.015479Z","iopub.status.idle":"2023-01-08T15:44:46.227902Z","shell.execute_reply.started":"2023-01-08T15:44:46.015440Z","shell.execute_reply":"2023-01-08T15:44:46.226824Z"},"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-08T15:44:56.777317Z","iopub.execute_input":"2023-01-08T15:44:56.777794Z","iopub.status.idle":"2023-01-08T15:44:56.786161Z","shell.execute_reply.started":"2023-01-08T15:44:56.777757Z","shell.execute_reply":"2023-01-08T15:44:56.784779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:45:07.473952Z","iopub.execute_input":"2023-01-08T15:45:07.474397Z","iopub.status.idle":"2023-01-08T15:45:07.485686Z","shell.execute_reply.started":"2023-01-08T15:45:07.474360Z","shell.execute_reply":"2023-01-08T15:45:07.484762Z"},"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-08T15:45:18.575846Z","iopub.execute_input":"2023-01-08T15:45:18.576372Z","iopub.status.idle":"2023-01-08T15:45:19.305411Z","shell.execute_reply.started":"2023-01-08T15:45:18.576324Z","shell.execute_reply":"2023-01-08T15:45:19.304334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from 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-08T15:46:26.733083Z","iopub.execute_input":"2023-01-08T15:46:26.733474Z","iopub.status.idle":"2023-01-08T15:47:46.689154Z","shell.execute_reply.started":"2023-01-08T15:46:26.733442Z","shell.execute_reply":"2023-01-08T15:47:46.687656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = rf.predict(test_x.values)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:48:04.591329Z","iopub.execute_input":"2023-01-08T15:48:04.591735Z","iopub.status.idle":"2023-01-08T15:48:06.580685Z","shell.execute_reply.started":"2023-01-08T15:48:04.591703Z","shell.execute_reply":"2023-01-08T15:48:06.579453Z"},"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-08T15:48:14.551391Z","iopub.execute_input":"2023-01-08T15:48:14.552210Z","iopub.status.idle":"2023-01-08T15:48:14.560025Z","shell.execute_reply.started":"2023-01-08T15:48:14.552168Z","shell.execute_reply":"2023-01-08T15:48:14.558934Z"},"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-08T15:48:16.437670Z","iopub.execute_input":"2023-01-08T15:48:16.438205Z","iopub.status.idle":"2023-01-08T15:48:17.199362Z","shell.execute_reply.started":"2023-01-08T15:48:16.438158Z","shell.execute_reply":"2023-01-08T15:48:17.197718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:48:20.408930Z","iopub.execute_input":"2023-01-08T15:48:20.410163Z","iopub.status.idle":"2023-01-08T15:48:20.419590Z","shell.execute_reply.started":"2023-01-08T15:48:20.410064Z","shell.execute_reply":"2023-01-08T15:48:20.418780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T15:48:25.627819Z","iopub.execute_input":"2023-01-08T15:48:25.628220Z","iopub.status.idle":"2023-01-08T15:48:25.639025Z","shell.execute_reply.started":"2023-01-08T15:48:25.628188Z","shell.execute_reply":"2023-01-08T15:48:25.637788Z"},"trusted":true},"execution_count":null,"outputs":[]}]}