{"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":"markdown","source":"# Import Modules","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\n# Preprocessing\nfrom sklearn.preprocessing import OneHotEncoder, StandardScaler\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.impute import SimpleImputer, MissingIndicator\n# Feature Engineering\nfrom sklearn.decomposition import PCA\n# Model Building\nfrom sklearn.pipeline import Pipeline,FeatureUnion\nfrom sklearn.svm import SVC\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.ensemble import RandomForestClassifier\nfrom xgboost import XGBClassifier\n# Inference\nfrom sklearn.model_selection import cross_val_score, train_test_split\nfrom sklearn.metrics import roc_auc_score,f1_score\n# Save\nimport pickle","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:52.850566Z","iopub.execute_input":"2022-07-11T18:39:52.851138Z","iopub.status.idle":"2022-07-11T18:39:54.208755Z","shell.execute_reply.started":"2022-07-11T18:39:52.851022Z","shell.execute_reply":"2022-07-11T18:39:54.207828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# List All Files","metadata":{}},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.210289Z","iopub.execute_input":"2022-07-11T18:39:54.210808Z","iopub.status.idle":"2022-07-11T18:39:54.218067Z","shell.execute_reply.started":"2022-07-11T18:39:54.210744Z","shell.execute_reply":"2022-07-11T18:39:54.216841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read Files","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/titanic/train.csv')\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.220567Z","iopub.execute_input":"2022-07-11T18:39:54.221009Z","iopub.status.idle":"2022-07-11T18:39:54.271415Z","shell.execute_reply.started":"2022-07-11T18:39:54.220966Z","shell.execute_reply":"2022-07-11T18:39:54.270346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preprocessing","metadata":{}},{"cell_type":"code","source":"y = train_df['Survived']","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.275037Z","iopub.execute_input":"2022-07-11T18:39:54.276042Z","iopub.status.idle":"2022-07-11T18:39:54.282322Z","shell.execute_reply.started":"2022-07-11T18:39:54.275992Z","shell.execute_reply":"2022-07-11T18:39:54.281406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols_to_use = ['Pclass','Sex','Age','SibSp','Parch']\nX = train_df[cols_to_use]\nX.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.283387Z","iopub.execute_input":"2022-07-11T18:39:54.283857Z","iopub.status.idle":"2022-07-11T18:39:54.303574Z","shell.execute_reply.started":"2022-07-11T18:39:54.283825Z","shell.execute_reply":"2022-07-11T18:39:54.302414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Check Missing Values","metadata":{}},{"cell_type":"code","source":"X.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.305217Z","iopub.execute_input":"2022-07-11T18:39:54.305867Z","iopub.status.idle":"2022-07-11T18:39:54.318038Z","shell.execute_reply.started":"2022-07-11T18:39:54.305819Z","shell.execute_reply":"2022-07-11T18:39:54.316922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"age_median = X['Age'].median()\nage_median","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.319817Z","iopub.execute_input":"2022-07-11T18:39:54.320259Z","iopub.status.idle":"2022-07-11T18:39:54.333208Z","shell.execute_reply.started":"2022-07-11T18:39:54.320215Z","shell.execute_reply":"2022-07-11T18:39:54.332236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fill Missing values","metadata":{}},{"cell_type":"code","source":"X['Age'] = X['Age'].fillna(age_median)\nX.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.335662Z","iopub.execute_input":"2022-07-11T18:39:54.336388Z","iopub.status.idle":"2022-07-11T18:39:54.347073Z","shell.execute_reply.started":"2022-07-11T18:39:54.336352Z","shell.execute_reply":"2022-07-11T18:39:54.345906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.349401Z","iopub.execute_input":"2022-07-11T18:39:54.350109Z","iopub.status.idle":"2022-07-11T18:39:54.362901Z","shell.execute_reply.started":"2022-07-11T18:39:54.350060Z","shell.execute_reply":"2022-07-11T18:39:54.361759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Adding Extra Feature","metadata":{}},{"cell_type":"code","source":"temp = X.copy()\ntemp['Is_alone'] = 0\ntemp.loc[X['SibSp'] > 0, 'Is_alone'] = 1\ntemp.loc[X['Parch'] > 0, 'Is_alone'] = 1\ntemp.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.364566Z","iopub.execute_input":"2022-07-11T18:39:54.364997Z","iopub.status.idle":"2022-07-11T18:39:54.400084Z","shell.execute_reply.started":"2022-07-11T18:39:54.364948Z","shell.execute_reply":"2022-07-11T18:39:54.398941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = temp.copy()\nX.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.401228Z","iopub.execute_input":"2022-07-11T18:39:54.401645Z","iopub.status.idle":"2022-07-11T18:39:54.413876Z","shell.execute_reply.started":"2022-07-11T18:39:54.401613Z","shell.execute_reply":"2022-07-11T18:39:54.413141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Drop `SibSp` and `Parch`","metadata":{}},{"cell_type":"code","source":"X = X.drop(['SibSp','Parch'],axis = 1)\nX.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.415045Z","iopub.execute_input":"2022-07-11T18:39:54.415448Z","iopub.status.idle":"2022-07-11T18:39:54.430576Z","shell.execute_reply.started":"2022-07-11T18:39:54.415420Z","shell.execute_reply":"2022-07-11T18:39:54.429850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Scale Data","metadata":{}},{"cell_type":"code","source":"num_feat = ['Age']\ncat_feat = ['Pclass','Sex','Is_alone']\n\nnumeric_transformer = StandardScaler()\ncategorical_transformer = OneHotEncoder(handle_unknown='ignore',sparse = False)\n\npreprocessor = ColumnTransformer(\n    transformers=[\n        ('num', numeric_transformer, num_feat),\n        ('cat', categorical_transformer, cat_feat)\n    ]\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.431507Z","iopub.execute_input":"2022-07-11T18:39:54.432085Z","iopub.status.idle":"2022-07-11T18:39:54.437114Z","shell.execute_reply.started":"2022-07-11T18:39:54.432054Z","shell.execute_reply":"2022-07-11T18:39:54.436161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_transformed = preprocessor.fit_transform(X)\nX_transformed.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.438476Z","iopub.execute_input":"2022-07-11T18:39:54.438769Z","iopub.status.idle":"2022-07-11T18:39:54.457997Z","shell.execute_reply.started":"2022-07-11T18:39:54.438741Z","shell.execute_reply":"2022-07-11T18:39:54.456759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train","metadata":{}},{"cell_type":"code","source":"dtree_pipe = Pipeline([('prep',preprocessor),('tree',DecisionTreeClassifier())])\ntree = DecisionTreeClassifier()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.459602Z","iopub.execute_input":"2022-07-11T18:39:54.460215Z","iopub.status.idle":"2022-07-11T18:39:54.469718Z","shell.execute_reply.started":"2022-07-11T18:39:54.460168Z","shell.execute_reply":"2022-07-11T18:39:54.468733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = cross_val_score(estimator = tree,X = X_transformed,y = y,scoring = 'f1')\nnp.mean(res),np.std(res)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.471290Z","iopub.execute_input":"2022-07-11T18:39:54.471740Z","iopub.status.idle":"2022-07-11T18:39:54.509540Z","shell.execute_reply.started":"2022-07-11T18:39:54.471694Z","shell.execute_reply":"2022-07-11T18:39:54.508378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tree.fit(X_transformed,y)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.510848Z","iopub.execute_input":"2022-07-11T18:39:54.511132Z","iopub.status.idle":"2022-07-11T18:39:54.520545Z","shell.execute_reply.started":"2022-07-11T18:39:54.511105Z","shell.execute_reply":"2022-07-11T18:39:54.519402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Save Model","metadata":{}},{"cell_type":"code","source":"model_dict = {\"tree_model\": tree, \"preprocess\" : preprocessor}\npickle.dump(model_dict,open('tree.pkl','wb'))","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.521764Z","iopub.execute_input":"2022-07-11T18:39:54.523328Z","iopub.status.idle":"2022-07-11T18:39:54.529235Z","shell.execute_reply.started":"2022-07-11T18:39:54.523179Z","shell.execute_reply":"2022-07-11T18:39:54.528256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"sample = pd.read_csv('/kaggle/input/titanic/gender_submission.csv')\nsample.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.530490Z","iopub.execute_input":"2022-07-11T18:39:54.530785Z","iopub.status.idle":"2022-07-11T18:39:54.552889Z","shell.execute_reply.started":"2022-07-11T18:39:54.530756Z","shell.execute_reply":"2022-07-11T18:39:54.551971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv('/kaggle/input/titanic/test.csv')\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.554018Z","iopub.execute_input":"2022-07-11T18:39:54.554418Z","iopub.status.idle":"2022-07-11T18:39:54.579659Z","shell.execute_reply.started":"2022-07-11T18:39:54.554381Z","shell.execute_reply":"2022-07-11T18:39:54.578694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = test_df[cols_to_use]\n#temp = X.copy()\nX_test['Is_alone'] = 0\nX_test.loc[X_test['SibSp'] > 0, 'Is_alone'] = 1\nX_test.loc[X_test['Parch'] > 0, 'Is_alone'] = 1\nX_test = X_test.drop(['SibSp','Parch'],axis = 1)\nX_test['Age'] = X_test['Age'].fillna(age_median)\nprint(X_test.isna().sum())\nX_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.580997Z","iopub.execute_input":"2022-07-11T18:39:54.581313Z","iopub.status.idle":"2022-07-11T18:39:54.607411Z","shell.execute_reply.started":"2022-07-11T18:39:54.581277Z","shell.execute_reply":"2022-07-11T18:39:54.606710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_proc = preprocessor.transform(X_test)\nX_test_proc.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.608469Z","iopub.execute_input":"2022-07-11T18:39:54.608922Z","iopub.status.idle":"2022-07-11T18:39:54.618124Z","shell.execute_reply.started":"2022-07-11T18:39:54.608889Z","shell.execute_reply":"2022-07-11T18:39:54.617223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = tree.predict(X_test_proc)\nsubmission = pd.DataFrame({'PassengerId':test_df['PassengerId'],'Survived':y_pred})\nsubmission.to_csv('tree_submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:39:54.621235Z","iopub.execute_input":"2022-07-11T18:39:54.621826Z","iopub.status.idle":"2022-07-11T18:39:54.632353Z","shell.execute_reply.started":"2022-07-11T18:39:54.621755Z","shell.execute_reply":"2022-07-11T18:39:54.631285Z"},"trusted":true},"execution_count":null,"outputs":[]}]}