{"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-08T20:24:12.220156Z","iopub.execute_input":"2023-01-08T20:24:12.220772Z","iopub.status.idle":"2023-01-08T20:24:12.231225Z","shell.execute_reply.started":"2023-01-08T20:24:12.220726Z","shell.execute_reply":"2023-01-08T20:24:12.229283Z"},"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-08T20:24:12.234021Z","iopub.execute_input":"2023-01-08T20:24:12.235037Z","iopub.status.idle":"2023-01-08T20:24:22.040030Z","shell.execute_reply.started":"2023-01-08T20:24:12.234931Z","shell.execute_reply":"2023-01-08T20:24:22.038846Z"},"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-08T20:24:22.042628Z","iopub.execute_input":"2023-01-08T20:24:22.043395Z","iopub.status.idle":"2023-01-08T20:24:22.050047Z","shell.execute_reply.started":"2023-01-08T20:24:22.043346Z","shell.execute_reply":"2023-01-08T20:24:22.048954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T20:24:22.051819Z","iopub.execute_input":"2023-01-08T20:24:22.052160Z","iopub.status.idle":"2023-01-08T20:24:22.165659Z","shell.execute_reply.started":"2023-01-08T20:24:22.052129Z","shell.execute_reply":"2023-01-08T20:24:22.164525Z"},"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-08T20:24:22.168262Z","iopub.execute_input":"2023-01-08T20:24:22.168645Z","iopub.status.idle":"2023-01-08T20:24:22.279320Z","shell.execute_reply.started":"2023-01-08T20:24:22.168604Z","shell.execute_reply":"2023-01-08T20:24:22.277608Z"},"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-08T20:24:22.281200Z","iopub.execute_input":"2023-01-08T20:24:22.282063Z","iopub.status.idle":"2023-01-08T20:24:23.153389Z","shell.execute_reply.started":"2023-01-08T20:24:22.282008Z","shell.execute_reply":"2023-01-08T20:24:23.151997Z"},"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-08T20:24:23.155535Z","iopub.execute_input":"2023-01-08T20:24:23.156056Z","iopub.status.idle":"2023-01-08T20:24:23.384602Z","shell.execute_reply.started":"2023-01-08T20:24:23.156006Z","shell.execute_reply":"2023-01-08T20:24:23.383649Z"},"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-08T20:24:23.386063Z","iopub.execute_input":"2023-01-08T20:24:23.386800Z","iopub.status.idle":"2023-01-08T20:24:24.159910Z","shell.execute_reply.started":"2023-01-08T20:24:23.386755Z","shell.execute_reply":"2023-01-08T20:24:24.158548Z"},"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-08T20:24:24.161963Z","iopub.execute_input":"2023-01-08T20:24:24.162439Z","iopub.status.idle":"2023-01-08T20:24:24.226643Z","shell.execute_reply.started":"2023-01-08T20:24:24.162342Z","shell.execute_reply":"2023-01-08T20:24:24.225031Z"},"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-08T20:24:24.228927Z","iopub.execute_input":"2023-01-08T20:24:24.229396Z","iopub.status.idle":"2023-01-08T20:24:24.261916Z","shell.execute_reply.started":"2023-01-08T20:24:24.229349Z","shell.execute_reply":"2023-01-08T20:24:24.260483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2023-01-08T20:24:24.266981Z","iopub.execute_input":"2023-01-08T20:24:24.267427Z","iopub.status.idle":"2023-01-08T20:24:24.278197Z","shell.execute_reply.started":"2023-01-08T20:24:24.267381Z","shell.execute_reply":"2023-01-08T20:24:24.276673Z"},"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-08T20:24:24.280159Z","iopub.execute_input":"2023-01-08T20:24:24.280624Z","iopub.status.idle":"2023-01-08T20:25:02.660024Z","shell.execute_reply.started":"2023-01-08T20:24:24.280586Z","shell.execute_reply":"2023-01-08T20:25:02.657864Z"},"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-08T20:25:02.668291Z","iopub.execute_input":"2023-01-08T20:25:02.668981Z","iopub.status.idle":"2023-01-08T20:25:02.835079Z","shell.execute_reply.started":"2023-01-08T20:25:02.668921Z","shell.execute_reply":"2023-01-08T20:25:02.832848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = lr.predict(test_x.values)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T20:25:02.836245Z","iopub.execute_input":"2023-01-08T20:25:02.836917Z","iopub.status.idle":"2023-01-08T20:25:02.945102Z","shell.execute_reply.started":"2023-01-08T20:25:02.836876Z","shell.execute_reply":"2023-01-08T20:25:02.943023Z"},"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-08T20:25:02.947757Z","iopub.execute_input":"2023-01-08T20:25:02.948698Z","iopub.status.idle":"2023-01-08T20:25:03.204924Z","shell.execute_reply.started":"2023-01-08T20:25:02.948645Z","shell.execute_reply":"2023-01-08T20:25:03.203633Z"},"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-08T20:25:03.206260Z","iopub.execute_input":"2023-01-08T20:25:03.206649Z","iopub.status.idle":"2023-01-08T20:25:03.216150Z","shell.execute_reply.started":"2023-01-08T20:25:03.206613Z","shell.execute_reply":"2023-01-08T20:25:03.214654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T20:25:03.218360Z","iopub.execute_input":"2023-01-08T20:25:03.218915Z","iopub.status.idle":"2023-01-08T20:25:03.232087Z","shell.execute_reply.started":"2023-01-08T20:25:03.218874Z","shell.execute_reply":"2023-01-08T20:25:03.230703Z"},"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-08T20:25:03.234137Z","iopub.execute_input":"2023-01-08T20:25:03.234612Z","iopub.status.idle":"2023-01-08T20:25:03.969052Z","shell.execute_reply.started":"2023-01-08T20:25:03.234574Z","shell.execute_reply":"2023-01-08T20:25:03.967799Z"},"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-08T20:25:03.970504Z","iopub.execute_input":"2023-01-08T20:25:03.970858Z","iopub.status.idle":"2023-01-08T20:26:32.529575Z","shell.execute_reply.started":"2023-01-08T20:25:03.970827Z","shell.execute_reply":"2023-01-08T20:26:32.528090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = rf.predict(test_x.values)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T20:26:32.531342Z","iopub.execute_input":"2023-01-08T20:26:32.531860Z","iopub.status.idle":"2023-01-08T20:26:34.525070Z","shell.execute_reply.started":"2023-01-08T20:26:32.531809Z","shell.execute_reply":"2023-01-08T20:26:34.523645Z"},"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-08T20:26:34.527273Z","iopub.execute_input":"2023-01-08T20:26:34.528532Z","iopub.status.idle":"2023-01-08T20:26:34.541193Z","shell.execute_reply.started":"2023-01-08T20:26:34.528450Z","shell.execute_reply":"2023-01-08T20:26:34.539781Z"},"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-08T20:26:34.543416Z","iopub.execute_input":"2023-01-08T20:26:34.544779Z","iopub.status.idle":"2023-01-08T20:26:35.330898Z","shell.execute_reply.started":"2023-01-08T20:26:34.544720Z","shell.execute_reply":"2023-01-08T20:26:35.329489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T20:26:35.332668Z","iopub.execute_input":"2023-01-08T20:26:35.333395Z","iopub.status.idle":"2023-01-08T20:26:35.345188Z","shell.execute_reply.started":"2023-01-08T20:26:35.333345Z","shell.execute_reply":"2023-01-08T20:26:35.344176Z"},"trusted":true},"execution_count":null,"outputs":[]}]}