{"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":"# [Titanic - Machine Learning from Disaster](https://www.kaggle.com/c/titanic/data)","metadata":{"papermill":{"duration":0.011443,"end_time":"2022-08-11T18:04:25.616957","exception":false,"start_time":"2022-08-11T18:04:25.605514","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"# Imports","metadata":{"papermill":{"duration":0.009767,"end_time":"2022-08-11T18:04:25.636981","exception":false,"start_time":"2022-08-11T18:04:25.627214","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport re\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.preprocessing import OneHotEncoder, MinMaxScaler\nfrom sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score, accuracy_score\nfrom sklearn import linear_model","metadata":{"papermill":{"duration":1.313573,"end_time":"2022-08-11T18:04:26.960591","exception":false,"start_time":"2022-08-11T18:04:25.647018","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:00.200461Z","iopub.execute_input":"2022-08-11T18:24:00.201006Z","iopub.status.idle":"2022-08-11T18:24:01.544944Z","shell.execute_reply.started":"2022-08-11T18:24:00.200889Z","shell.execute_reply":"2022-08-11T18:24:01.544084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Reading data","metadata":{"papermill":{"duration":0.009766,"end_time":"2022-08-11T18:04:26.981959","exception":false,"start_time":"2022-08-11T18:04:26.972193","status":"completed"},"tags":[]}},{"cell_type":"code","source":"path = '../input/titanic/'\n\ntrain_data = pd.read_csv(path + 'train.csv')\ntrain_data.head(3)","metadata":{"papermill":{"duration":0.068922,"end_time":"2022-08-11T18:04:27.060715","exception":false,"start_time":"2022-08-11T18:04:26.991793","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:01.546724Z","iopub.execute_input":"2022-08-11T18:24:01.547275Z","iopub.status.idle":"2022-08-11T18:24:01.584879Z","shell.execute_reply.started":"2022-08-11T18:24:01.547242Z","shell.execute_reply":"2022-08-11T18:24:01.584137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv(path + 'test.csv')\ntest_data.head(3)","metadata":{"papermill":{"duration":0.038975,"end_time":"2022-08-11T18:04:27.110147","exception":false,"start_time":"2022-08-11T18:04:27.071172","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:01.586963Z","iopub.execute_input":"2022-08-11T18:24:01.587237Z","iopub.status.idle":"2022-08-11T18:24:01.608546Z","shell.execute_reply.started":"2022-08-11T18:24:01.587213Z","shell.execute_reply":"2022-08-11T18:24:01.607390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_data.info())","metadata":{"papermill":{"duration":0.04086,"end_time":"2022-08-11T18:04:27.161467","exception":false,"start_time":"2022-08-11T18:04:27.120607","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:01.610729Z","iopub.execute_input":"2022-08-11T18:24:01.610995Z","iopub.status.idle":"2022-08-11T18:24:01.635573Z","shell.execute_reply.started":"2022-08-11T18:24:01.610973Z","shell.execute_reply":"2022-08-11T18:24:01.634885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.info()","metadata":{"papermill":{"duration":0.028353,"end_time":"2022-08-11T18:04:27.200795","exception":false,"start_time":"2022-08-11T18:04:27.172442","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:01.638030Z","iopub.execute_input":"2022-08-11T18:24:01.639185Z","iopub.status.idle":"2022-08-11T18:24:01.652611Z","shell.execute_reply.started":"2022-08-11T18:24:01.639150Z","shell.execute_reply":"2022-08-11T18:24:01.651953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.describe(include=np.object_)","metadata":{"papermill":{"duration":0.034606,"end_time":"2022-08-11T18:04:27.246768","exception":false,"start_time":"2022-08-11T18:04:27.212162","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:01.655345Z","iopub.execute_input":"2022-08-11T18:24:01.656211Z","iopub.status.idle":"2022-08-11T18:24:01.676803Z","shell.execute_reply.started":"2022-08-11T18:24:01.656164Z","shell.execute_reply":"2022-08-11T18:24:01.675981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.describe()","metadata":{"papermill":{"duration":0.056461,"end_time":"2022-08-11T18:04:27.314078","exception":false,"start_time":"2022-08-11T18:04:27.257617","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:01.677987Z","iopub.execute_input":"2022-08-11T18:24:01.678653Z","iopub.status.idle":"2022-08-11T18:24:01.707610Z","shell.execute_reply.started":"2022-08-11T18:24:01.678628Z","shell.execute_reply":"2022-08-11T18:24:01.706716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.describe(include=np.object_)","metadata":{"papermill":{"duration":0.043971,"end_time":"2022-08-11T18:04:27.374381","exception":false,"start_time":"2022-08-11T18:04:27.330410","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:01.708869Z","iopub.execute_input":"2022-08-11T18:24:01.709213Z","iopub.status.idle":"2022-08-11T18:24:01.726080Z","shell.execute_reply.started":"2022-08-11T18:24:01.709182Z","shell.execute_reply":"2022-08-11T18:24:01.725429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.describe()","metadata":{"papermill":{"duration":0.047377,"end_time":"2022-08-11T18:04:27.433070","exception":false,"start_time":"2022-08-11T18:04:27.385693","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:01.726853Z","iopub.execute_input":"2022-08-11T18:24:01.727422Z","iopub.status.idle":"2022-08-11T18:24:01.759952Z","shell.execute_reply.started":"2022-08-11T18:24:01.727397Z","shell.execute_reply":"2022-08-11T18:24:01.759333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualization","metadata":{"papermill":{"duration":0.011424,"end_time":"2022-08-11T18:04:27.456024","exception":false,"start_time":"2022-08-11T18:04:27.444600","status":"completed"},"tags":[]}},{"cell_type":"code","source":"corr = train_data.corr()\nfig, ax = plt.subplots(figsize=(10,10)) \nax = sns.heatmap(corr, annot=True, linewidth=0.5)\nax.set_title(\"Correlation matrix\")\nplt.show()","metadata":{"papermill":{"duration":0.497974,"end_time":"2022-08-11T18:04:27.965427","exception":false,"start_time":"2022-08-11T18:04:27.467453","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:01.762862Z","iopub.execute_input":"2022-08-11T18:24:01.763182Z","iopub.status.idle":"2022-08-11T18:24:02.144733Z","shell.execute_reply.started":"2022-08-11T18:24:01.763158Z","shell.execute_reply":"2022-08-11T18:24:02.143706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ax = sns.pairplot(train_data.replace({\"Survived\": {0:\"No\", 1:\"Yes\"}}), hue=\"Survived\", plot_kws={\"alpha\":0.5}, diag_kind=\"hist\")\nax.fig.suptitle(\"Pairplots\", y=1.08)","metadata":{"papermill":{"duration":11.002012,"end_time":"2022-08-11T18:04:38.979832","exception":false,"start_time":"2022-08-11T18:04:27.977820","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:02.145860Z","iopub.execute_input":"2022-08-11T18:24:02.146215Z","iopub.status.idle":"2022-08-11T18:24:11.147823Z","shell.execute_reply.started":"2022-08-11T18:24:02.146182Z","shell.execute_reply":"2022-08-11T18:24:11.146943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Filling NaN","metadata":{"papermill":{"duration":0.017915,"end_time":"2022-08-11T18:04:39.016129","exception":false,"start_time":"2022-08-11T18:04:38.998214","status":"completed"},"tags":[]}},{"cell_type":"code","source":"print(train_data.isnull().sum())\nprint(40*\"=\")\nprint(test_data.isnull().sum())","metadata":{"papermill":{"duration":0.034144,"end_time":"2022-08-11T18:04:39.068636","exception":false,"start_time":"2022-08-11T18:04:39.034492","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:11.148873Z","iopub.execute_input":"2022-08-11T18:24:11.149292Z","iopub.status.idle":"2022-08-11T18:24:11.161311Z","shell.execute_reply.started":"2022-08-11T18:24:11.149260Z","shell.execute_reply":"2022-08-11T18:24:11.159971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Age","metadata":{"papermill":{"duration":0.01857,"end_time":"2022-08-11T18:04:39.106193","exception":false,"start_time":"2022-08-11T18:04:39.087623","status":"completed"},"tags":[]}},{"cell_type":"code","source":"ags = []\nfor i in ['female', 'male']:\n    gender_ags = []\n    for j in range(1, 4):\n        gender_ags.append(train_data.loc[(train_data['Sex'] == i) & (train_data['Pclass'] == j)]['Age'].mean())\n    ags.extend([gender_ags])","metadata":{"papermill":{"duration":0.036363,"end_time":"2022-08-11T18:04:39.161047","exception":false,"start_time":"2022-08-11T18:04:39.124684","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:11.162643Z","iopub.execute_input":"2022-08-11T18:24:11.163460Z","iopub.status.idle":"2022-08-11T18:24:11.182714Z","shell.execute_reply.started":"2022-08-11T18:24:11.163426Z","shell.execute_reply":"2022-08-11T18:24:11.181612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, gender in enumerate(['female', 'male']):\n    for j, pclass in enumerate(range(1, 4)):\n        train_data.loc[(train_data['Sex'] == gender) & (train_data['Pclass'] == pclass) & (train_data['Age'].isna()), 'Age'] = ags[i][j]","metadata":{"papermill":{"duration":0.035422,"end_time":"2022-08-11T18:04:39.214519","exception":false,"start_time":"2022-08-11T18:04:39.179097","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:11.183656Z","iopub.execute_input":"2022-08-11T18:24:11.184207Z","iopub.status.idle":"2022-08-11T18:24:11.200130Z","shell.execute_reply.started":"2022-08-11T18:24:11.184183Z","shell.execute_reply":"2022-08-11T18:24:11.199285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, gender in enumerate(['female', 'male']):\n    for j, pclass in enumerate(range(1, 4)):\n        test_data.loc[(test_data['Sex'] == gender) & (test_data['Pclass'] == pclass) & (test_data['Age'].isna()), 'Age'] = ags[i][j]","metadata":{"papermill":{"duration":0.034434,"end_time":"2022-08-11T18:04:39.266949","exception":false,"start_time":"2022-08-11T18:04:39.232515","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:11.201336Z","iopub.execute_input":"2022-08-11T18:24:11.201602Z","iopub.status.idle":"2022-08-11T18:24:11.212737Z","shell.execute_reply.started":"2022-08-11T18:24:11.201572Z","shell.execute_reply":"2022-08-11T18:24:11.212096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Embarked","metadata":{"papermill":{"duration":0.017563,"end_time":"2022-08-11T18:04:39.302479","exception":false,"start_time":"2022-08-11T18:04:39.284916","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train_data.groupby(['Embarked']).size()","metadata":{"papermill":{"duration":0.030028,"end_time":"2022-08-11T18:04:39.350361","exception":false,"start_time":"2022-08-11T18:04:39.320333","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:11.213690Z","iopub.execute_input":"2022-08-11T18:24:11.214118Z","iopub.status.idle":"2022-08-11T18:24:11.226399Z","shell.execute_reply.started":"2022-08-11T18:24:11.214083Z","shell.execute_reply":"2022-08-11T18:24:11.225176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['Embarked'].fillna('S', inplace=True)","metadata":{"papermill":{"duration":0.02772,"end_time":"2022-08-11T18:04:39.395924","exception":false,"start_time":"2022-08-11T18:04:39.368204","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:11.227599Z","iopub.execute_input":"2022-08-11T18:24:11.228214Z","iopub.status.idle":"2022-08-11T18:24:11.236752Z","shell.execute_reply.started":"2022-08-11T18:24:11.228190Z","shell.execute_reply":"2022-08-11T18:24:11.235971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Fare","metadata":{"papermill":{"duration":0.017563,"end_time":"2022-08-11T18:04:39.431362","exception":false,"start_time":"2022-08-11T18:04:39.413799","status":"completed"},"tags":[]}},{"cell_type":"code","source":"test_data.Fare = test_data.Fare.fillna( train_data.Fare.mean() )","metadata":{"papermill":{"duration":0.027691,"end_time":"2022-08-11T18:04:39.476797","exception":false,"start_time":"2022-08-11T18:04:39.449106","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:11.242196Z","iopub.execute_input":"2022-08-11T18:24:11.242741Z","iopub.status.idle":"2022-08-11T18:24:11.251739Z","shell.execute_reply.started":"2022-08-11T18:24:11.242713Z","shell.execute_reply":"2022-08-11T18:24:11.250977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Cabin","metadata":{"papermill":{"duration":0.018257,"end_time":"2022-08-11T18:04:39.513328","exception":false,"start_time":"2022-08-11T18:04:39.495071","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train_data['Cabin']=train_data['Cabin'].fillna(0)\ntest_data['Cabin']=test_data['Cabin'].fillna(0)","metadata":{"papermill":{"duration":0.028916,"end_time":"2022-08-11T18:04:39.560294","exception":false,"start_time":"2022-08-11T18:04:39.531378","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:11.253204Z","iopub.execute_input":"2022-08-11T18:24:11.254169Z","iopub.status.idle":"2022-08-11T18:24:11.265699Z","shell.execute_reply.started":"2022-08-11T18:24:11.254142Z","shell.execute_reply":"2022-08-11T18:24:11.264982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature Engineering","metadata":{"papermill":{"duration":0.017898,"end_time":"2022-08-11T18:04:39.596849","exception":false,"start_time":"2022-08-11T18:04:39.578951","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# def get_title(name):\n#     title_search = re.search(' ([A-Za-z]+)\\.', name)\n#     if title_search:\n#         return title_search.group(1)\n#     return \"\"\n\n# train_data['title']=train_data['Name'].apply(get_title)\n# test_data['title']=test_data['Name'].apply(get_title)\n\n# title_lev1=list(train_data['title'].value_counts().reset_index()['index'])\n# title_lev2=list(test_data['title'].value_counts().reset_index()['index'])\n\n# title_lev=list(set().union(title_lev1, title_lev2))\n# print(title_lev)\n\n# train_data['title']=pd.Categorical(train_data['title'], categories=title_lev)\n# test_data['title']=pd.Categorical(test_data['title'], categories=title_lev)","metadata":{"papermill":{"duration":0.028364,"end_time":"2022-08-11T18:04:39.650893","exception":false,"start_time":"2022-08-11T18:04:39.622529","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:11.266868Z","iopub.execute_input":"2022-08-11T18:24:11.268390Z","iopub.status.idle":"2022-08-11T18:24:11.280264Z","shell.execute_reply.started":"2022-08-11T18:24:11.268338Z","shell.execute_reply":"2022-08-11T18:24:11.279081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Get dummies","metadata":{"papermill":{"duration":0.022922,"end_time":"2022-08-11T18:04:39.696032","exception":false,"start_time":"2022-08-11T18:04:39.673110","status":"completed"},"tags":[]}},{"cell_type":"code","source":"training=pd.get_dummies(train_data, columns=[\"Pclass\",\"Embarked\",\"Sex\"])\ntraining.drop(['Sex_female', 'PassengerId', 'Name', 'Cabin', 'Ticket'], axis=1, inplace=True)\n\ntraining.head()","metadata":{"papermill":{"duration":0.045693,"end_time":"2022-08-11T18:04:39.764081","exception":false,"start_time":"2022-08-11T18:04:39.718388","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:11.281879Z","iopub.execute_input":"2022-08-11T18:24:11.282443Z","iopub.status.idle":"2022-08-11T18:24:11.309752Z","shell.execute_reply.started":"2022-08-11T18:24:11.282412Z","shell.execute_reply":"2022-08-11T18:24:11.308449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testing=pd.get_dummies(test_data, columns=[\"Pclass\",\"Embarked\",\"Sex\"])\ntesting.drop(['Sex_female', 'PassengerId', 'Name', 'Cabin', 'Ticket'], axis=1, inplace=True)\n\ntesting.head()","metadata":{"papermill":{"duration":0.044144,"end_time":"2022-08-11T18:04:39.826403","exception":false,"start_time":"2022-08-11T18:04:39.782259","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:11.311400Z","iopub.execute_input":"2022-08-11T18:24:11.311740Z","iopub.status.idle":"2022-08-11T18:24:11.336120Z","shell.execute_reply.started":"2022-08-11T18:24:11.311707Z","shell.execute_reply":"2022-08-11T18:24:11.335143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = training.drop(['Survived'], axis=1)\ny = training['Survived']","metadata":{"papermill":{"duration":0.02719,"end_time":"2022-08-11T18:04:39.871901","exception":false,"start_time":"2022-08-11T18:04:39.844711","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:11.339854Z","iopub.execute_input":"2022-08-11T18:24:11.340227Z","iopub.status.idle":"2022-08-11T18:24:11.347002Z","shell.execute_reply.started":"2022-08-11T18:24:11.340195Z","shell.execute_reply":"2022-08-11T18:24:11.345999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"papermill":{"duration":0.029897,"end_time":"2022-08-11T18:04:39.920910","exception":false,"start_time":"2022-08-11T18:04:39.891013","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:11.348360Z","iopub.execute_input":"2022-08-11T18:24:11.348696Z","iopub.status.idle":"2022-08-11T18:24:11.362431Z","shell.execute_reply.started":"2022-08-11T18:24:11.348663Z","shell.execute_reply":"2022-08-11T18:24:11.361488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predict","metadata":{"papermill":{"duration":0.017885,"end_time":"2022-08-11T18:04:39.957191","exception":false,"start_time":"2022-08-11T18:04:39.939306","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## Logistic Regression","metadata":{"papermill":{"duration":0.017961,"end_time":"2022-08-11T18:04:39.993333","exception":false,"start_time":"2022-08-11T18:04:39.975372","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\n\nlr_clf = LogisticRegression(solver='liblinear')\nlr_clf.fit(X_train, y_train)\n\ny_pred = lr_clf.predict(X_test)\naccuracy_score(y_pred, y_test)","metadata":{"papermill":{"duration":0.036801,"end_time":"2022-08-11T18:04:40.048813","exception":false,"start_time":"2022-08-11T18:04:40.012012","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:11.363970Z","iopub.execute_input":"2022-08-11T18:24:11.364349Z","iopub.status.idle":"2022-08-11T18:24:11.382431Z","shell.execute_reply.started":"2022-08-11T18:24:11.364315Z","shell.execute_reply":"2022-08-11T18:24:11.381306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Random Forest","metadata":{"papermill":{"duration":0.018212,"end_time":"2022-08-11T18:04:40.085561","exception":false,"start_time":"2022-08-11T18:04:40.067349","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\n\nrf = RandomForestClassifier(random_state=42)\nrf.fit(X_train, y_train)\n\ny_pred = rf.predict(X_test)\naccuracy_score(y_pred, y_test)","metadata":{"papermill":{"duration":0.360915,"end_time":"2022-08-11T18:04:40.464809","exception":false,"start_time":"2022-08-11T18:04:40.103894","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:11.383525Z","iopub.execute_input":"2022-08-11T18:24:11.384070Z","iopub.status.idle":"2022-08-11T18:24:11.733572Z","shell.execute_reply.started":"2022-08-11T18:24:11.384045Z","shell.execute_reply":"2022-08-11T18:24:11.732544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Tuning","metadata":{"papermill":{"duration":0.01886,"end_time":"2022-08-11T18:04:40.503333","exception":false,"start_time":"2022-08-11T18:04:40.484473","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# rfc=RandomForestClassifier(random_state=42)\n\n# param_grid = { \n#     'n_estimators': [10, 30,50, 100,150, 200, 250],\n#     'max_features': ['auto', 'sqrt', 'log2'],\n#     'max_depth' : [4,5,6,7,8,9],\n#     'criterion' :['gini', 'entropy']\n# }\n\n# CV_rfc = GridSearchCV(estimator=rfc, param_grid=param_grid, cv= 5)\n# CV_rfc.fit(X_train, y_train)","metadata":{"papermill":{"duration":0.026718,"end_time":"2022-08-11T18:04:40.549377","exception":false,"start_time":"2022-08-11T18:04:40.522659","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:11.734692Z","iopub.execute_input":"2022-08-11T18:24:11.734978Z","iopub.status.idle":"2022-08-11T18:24:11.739311Z","shell.execute_reply.started":"2022-08-11T18:24:11.734948Z","shell.execute_reply":"2022-08-11T18:24:11.738104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# CV_rfc.best_params_","metadata":{"papermill":{"duration":0.026446,"end_time":"2022-08-11T18:04:40.594846","exception":false,"start_time":"2022-08-11T18:04:40.568400","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:11.740626Z","iopub.execute_input":"2022-08-11T18:24:11.740899Z","iopub.status.idle":"2022-08-11T18:24:11.756004Z","shell.execute_reply.started":"2022-08-11T18:24:11.740876Z","shell.execute_reply":"2022-08-11T18:24:11.755257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# rfc1=RandomForestClassifier(random_state=42, max_features='auto', n_estimators=30, max_depth=6, criterion='entropy')\n# rfc1.fit(X_train, y_train)\n\n# y_pred=rfc1.predict(X_test)\n# accuracy_score(y_test, y_pred)","metadata":{"papermill":{"duration":0.026199,"end_time":"2022-08-11T18:04:40.639911","exception":false,"start_time":"2022-08-11T18:04:40.613712","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:11.757277Z","iopub.execute_input":"2022-08-11T18:24:11.757721Z","iopub.status.idle":"2022-08-11T18:24:11.769210Z","shell.execute_reply.started":"2022-08-11T18:24:11.757695Z","shell.execute_reply":"2022-08-11T18:24:11.767986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Pedict test data use best model","metadata":{"papermill":{"duration":0.018141,"end_time":"2022-08-11T18:04:40.676615","exception":false,"start_time":"2022-08-11T18:04:40.658474","status":"completed"},"tags":[]}},{"cell_type":"code","source":"Y_pred = rf.predict(testing)","metadata":{"papermill":{"duration":0.04692,"end_time":"2022-08-11T18:04:40.741923","exception":false,"start_time":"2022-08-11T18:04:40.695003","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:11.771078Z","iopub.execute_input":"2022-08-11T18:24:11.771566Z","iopub.status.idle":"2022-08-11T18:24:11.800686Z","shell.execute_reply.started":"2022-08-11T18:24:11.771531Z","shell.execute_reply":"2022-08-11T18:24:11.799712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\n        \"PassengerId\": test_data[\"PassengerId\"],\n        \"Survived\": Y_pred\n    })","metadata":{"papermill":{"duration":0.027096,"end_time":"2022-08-11T18:04:40.787774","exception":false,"start_time":"2022-08-11T18:04:40.760678","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:11.802117Z","iopub.execute_input":"2022-08-11T18:24:11.802407Z","iopub.status.idle":"2022-08-11T18:24:11.808169Z","shell.execute_reply.started":"2022-08-11T18:24:11.802376Z","shell.execute_reply":"2022-08-11T18:24:11.806845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('./submission.csv', index=False)","metadata":{"papermill":{"duration":0.031348,"end_time":"2022-08-11T18:04:40.837955","exception":false,"start_time":"2022-08-11T18:04:40.806607","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-11T18:24:11.809324Z","iopub.execute_input":"2022-08-11T18:24:11.809906Z","iopub.status.idle":"2022-08-11T18:24:11.825543Z","shell.execute_reply.started":"2022-08-11T18:24:11.809871Z","shell.execute_reply":"2022-08-11T18:24:11.824828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.0181,"end_time":"2022-08-11T18:04:40.874518","exception":false,"start_time":"2022-08-11T18:04:40.856418","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}