{"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":"# Importing Basic Libaries and Dataset\n","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:50.258346Z","iopub.execute_input":"2022-08-12T05:15:50.258708Z","iopub.status.idle":"2022-08-12T05:15:50.263607Z","shell.execute_reply.started":"2022-08-12T05:15:50.258680Z","shell.execute_reply":"2022-08-12T05:15:50.262600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/titanic/train.csv')\ntest_df = pd.read_csv('../input/titanic/test.csv')\nval_df = pd.read_csv('../input/titanic/gender_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:50.265464Z","iopub.execute_input":"2022-08-12T05:15:50.265895Z","iopub.status.idle":"2022-08-12T05:15:50.283236Z","shell.execute_reply.started":"2022-08-12T05:15:50.265871Z","shell.execute_reply":"2022-08-12T05:15:50.282614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:50.284390Z","iopub.execute_input":"2022-08-12T05:15:50.284921Z","iopub.status.idle":"2022-08-12T05:15:50.299759Z","shell.execute_reply.started":"2022-08-12T05:15:50.284889Z","shell.execute_reply":"2022-08-12T05:15:50.298844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:50.300759Z","iopub.execute_input":"2022-08-12T05:15:50.301128Z","iopub.status.idle":"2022-08-12T05:15:50.305962Z","shell.execute_reply.started":"2022-08-12T05:15:50.301103Z","shell.execute_reply":"2022-08-12T05:15:50.304997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:50.308297Z","iopub.execute_input":"2022-08-12T05:15:50.308776Z","iopub.status.idle":"2022-08-12T05:15:50.326527Z","shell.execute_reply.started":"2022-08-12T05:15:50.308745Z","shell.execute_reply":"2022-08-12T05:15:50.325721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:50.327638Z","iopub.execute_input":"2022-08-12T05:15:50.328048Z","iopub.status.idle":"2022-08-12T05:15:50.332536Z","shell.execute_reply.started":"2022-08-12T05:15:50.328024Z","shell.execute_reply":"2022-08-12T05:15:50.331561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data PreProcessing","metadata":{}},{"cell_type":"code","source":"test_df['Survived'] = val_df['Survived'] #Taking survived column from validation dataset as it is absent in test dataset","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:50.542441Z","iopub.execute_input":"2022-08-12T05:15:50.543353Z","iopub.status.idle":"2022-08-12T05:15:50.547911Z","shell.execute_reply.started":"2022-08-12T05:15:50.543318Z","shell.execute_reply":"2022-08-12T05:15:50.547233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:50.549182Z","iopub.execute_input":"2022-08-12T05:15:50.549938Z","iopub.status.idle":"2022-08-12T05:15:50.581915Z","shell.execute_reply.started":"2022-08-12T05:15:50.549911Z","shell.execute_reply":"2022-08-12T05:15:50.581211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:50.583201Z","iopub.execute_input":"2022-08-12T05:15:50.583718Z","iopub.status.idle":"2022-08-12T05:15:50.597670Z","shell.execute_reply.started":"2022-08-12T05:15:50.583683Z","shell.execute_reply":"2022-08-12T05:15:50.596349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['Age'] = train_df['Age'].fillna(train_df['Age'].median())","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:50.599395Z","iopub.execute_input":"2022-08-12T05:15:50.600084Z","iopub.status.idle":"2022-08-12T05:15:50.608357Z","shell.execute_reply.started":"2022-08-12T05:15:50.600052Z","shell.execute_reply":"2022-08-12T05:15:50.607240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['Embarked'] = train_df['Embarked'].fillna(method='ffill')","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:50.610498Z","iopub.execute_input":"2022-08-12T05:15:50.611012Z","iopub.status.idle":"2022-08-12T05:15:50.618519Z","shell.execute_reply.started":"2022-08-12T05:15:50.610983Z","shell.execute_reply":"2022-08-12T05:15:50.617738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:50.619625Z","iopub.execute_input":"2022-08-12T05:15:50.620220Z","iopub.status.idle":"2022-08-12T05:15:50.629403Z","shell.execute_reply.started":"2022-08-12T05:15:50.620188Z","shell.execute_reply":"2022-08-12T05:15:50.628458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.drop(['Name', 'Ticket'], axis=1, inplace = True) #Dropping name and ticket as they are useless","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:50.731495Z","iopub.execute_input":"2022-08-12T05:15:50.732071Z","iopub.status.idle":"2022-08-12T05:15:50.741313Z","shell.execute_reply.started":"2022-08-12T05:15:50.732041Z","shell.execute_reply":"2022-08-12T05:15:50.740543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Applying same changes in test dataset","metadata":{}},{"cell_type":"code","source":"test_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:50.743211Z","iopub.execute_input":"2022-08-12T05:15:50.743499Z","iopub.status.idle":"2022-08-12T05:15:50.759235Z","shell.execute_reply.started":"2022-08-12T05:15:50.743475Z","shell.execute_reply":"2022-08-12T05:15:50.758552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['Age'] = test_df['Age'].fillna(test_df['Age'].median())","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:52.514397Z","iopub.execute_input":"2022-08-12T05:15:52.515033Z","iopub.status.idle":"2022-08-12T05:15:52.520547Z","shell.execute_reply.started":"2022-08-12T05:15:52.514995Z","shell.execute_reply":"2022-08-12T05:15:52.519650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['Fare'] = test_df['Fare'].fillna(test_df['Fare'].median())","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:52.522233Z","iopub.execute_input":"2022-08-12T05:15:52.522700Z","iopub.status.idle":"2022-08-12T05:15:52.535282Z","shell.execute_reply.started":"2022-08-12T05:15:52.522672Z","shell.execute_reply":"2022-08-12T05:15:52.534538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.drop(['Name', 'Ticket'], axis=1, inplace = True) #Dropping name and ticket as they are useless","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:52.536479Z","iopub.execute_input":"2022-08-12T05:15:52.536869Z","iopub.status.idle":"2022-08-12T05:15:52.546347Z","shell.execute_reply.started":"2022-08-12T05:15:52.536844Z","shell.execute_reply":"2022-08-12T05:15:52.545395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature Engineering Cabin Feature with KNN","metadata":{}},{"cell_type":"code","source":"train_df_mask = pd.notnull(train_df['Cabin'])\nnew_train_df = train_df[train_df_mask]\nnew_train_df","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:53.162834Z","iopub.execute_input":"2022-08-12T05:15:53.163231Z","iopub.status.idle":"2022-08-12T05:15:53.185997Z","shell.execute_reply.started":"2022-08-12T05:15:53.163196Z","shell.execute_reply":"2022-08-12T05:15:53.184932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df_mask = pd.notnull(test_df['Cabin'])\nnew_test_df = test_df[test_df_mask]\nnew_test_df","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:53.187833Z","iopub.execute_input":"2022-08-12T05:15:53.188649Z","iopub.status.idle":"2022-08-12T05:15:53.209770Z","shell.execute_reply.started":"2022-08-12T05:15:53.188612Z","shell.execute_reply":"2022-08-12T05:15:53.208648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cabin_df = pd.concat([new_train_df, new_test_df])\ncabin_df","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:53.211116Z","iopub.execute_input":"2022-08-12T05:15:53.211429Z","iopub.status.idle":"2022-08-12T05:15:53.233216Z","shell.execute_reply.started":"2022-08-12T05:15:53.211381Z","shell.execute_reply":"2022-08-12T05:15:53.231961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_distances(series1, df1):\n    features = ['Pclass', 'Embarked', 'Fare']\n    series1['Embarked'] = ord(series1['Embarked'])\n    df1['Embarked'] = df1['Embarked'].apply(lambda x:ord(x))\n    distances = []\n    for i in range(len(df1)):\n        cur_distance = 0\n        for j in features:\n            cur_distance += np.power((series1[j] - df1.iloc[i][j]), 2)\n        distances.append(np.sqrt(cur_distance))\n    return distances","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:53.234552Z","iopub.execute_input":"2022-08-12T05:15:53.235602Z","iopub.status.idle":"2022-08-12T05:15:53.242169Z","shell.execute_reply.started":"2022-08-12T05:15:53.235561Z","shell.execute_reply":"2022-08-12T05:15:53.241463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_cabin_list = []\nfor i in range(len(train_df)):\n    if(train_df_mask[i] == False):\n        cabin_dict = dict.fromkeys(list(cabin_df['Cabin'].unique()), 0)\n        distances = get_distances(train_df.iloc[i], cabin_df.copy(deep=True))\n        indexes = cabin_df['PassengerId'].to_list()\n        distances_index = zip(distances, indexes)\n        distances_index = sorted(distances_index, key = lambda x:x[0])[:6]\n        for j in range(6):\n            cur_index = distances_index[j][1]\n            cur_cabin = cabin_df[cabin_df['PassengerId'] == cur_index]['Cabin'].iloc[0]\n            cabin_dict[cur_cabin] = cabin_dict[cur_cabin] + 1\n        cabin_dict = sorted(cabin_dict.items(), key = lambda x:x[1], reverse = True)\n        final_cabin = list(cabin_dict)[0]\n        final_cabin_list.append(final_cabin[0])\n    else:\n        final_cabin_list.append(train_df.iloc[i]['Cabin'])","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:15:54.638574Z","iopub.execute_input":"2022-08-12T05:15:54.639365Z","iopub.status.idle":"2022-08-12T05:17:21.375066Z","shell.execute_reply.started":"2022-08-12T05:15:54.639333Z","shell.execute_reply":"2022-08-12T05:17:21.373832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.sum(final_cabin_list == train_df['Cabin'])","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:17:21.377155Z","iopub.execute_input":"2022-08-12T05:17:21.377477Z","iopub.status.idle":"2022-08-12T05:17:21.384760Z","shell.execute_reply.started":"2022-08-12T05:17:21.377449Z","shell.execute_reply":"2022-08-12T05:17:21.383331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['Cabin'] = final_cabin_list","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:17:21.385963Z","iopub.execute_input":"2022-08-12T05:17:21.386298Z","iopub.status.idle":"2022-08-12T05:17:21.396937Z","shell.execute_reply.started":"2022-08-12T05:17:21.386268Z","shell.execute_reply":"2022-08-12T05:17:21.395861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_cabin_list = []\nfor i in range(len(test_df)):\n    if(test_df_mask[i] == False):\n        cabin_dict = dict.fromkeys(list(cabin_df['Cabin'].unique()), 0)\n        distances = get_distances(test_df.iloc[i], cabin_df.copy(deep=True))\n        indexes = cabin_df['PassengerId'].to_list()\n        distances_index = zip(distances, indexes)\n        distances_index = sorted(distances_index, key = lambda x:x[0])[:6]\n        for j in range(6):\n            cur_index = distances_index[j][1]\n            cur_cabin = cabin_df[cabin_df['PassengerId'] == cur_index]['Cabin'].iloc[0]\n            cabin_dict[cur_cabin] = cabin_dict[cur_cabin] + 1\n        cabin_dict = sorted(cabin_dict.items(), key = lambda x:x[1], reverse = True)\n        final_cabin = list(cabin_dict)[0]\n        final_cabin_list.append(final_cabin[0])\n    else:\n        final_cabin_list.append(test_df.iloc[i]['Cabin'])","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:17:21.398788Z","iopub.execute_input":"2022-08-12T05:17:21.399195Z","iopub.status.idle":"2022-08-12T05:18:02.794293Z","shell.execute_reply.started":"2022-08-12T05:17:21.399162Z","shell.execute_reply":"2022-08-12T05:18:02.793373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.sum(final_cabin_list == test_df['Cabin'])","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:02.796394Z","iopub.execute_input":"2022-08-12T05:18:02.796802Z","iopub.status.idle":"2022-08-12T05:18:02.802665Z","shell.execute_reply.started":"2022-08-12T05:18:02.796777Z","shell.execute_reply":"2022-08-12T05:18:02.801748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['Cabin'] = final_cabin_list","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:02.803949Z","iopub.execute_input":"2022-08-12T05:18:02.804345Z","iopub.status.idle":"2022-08-12T05:18:02.814754Z","shell.execute_reply.started":"2022-08-12T05:18:02.804311Z","shell.execute_reply":"2022-08-12T05:18:02.813568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Visualizations","metadata":{}},{"cell_type":"code","source":"train_df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:02.816206Z","iopub.execute_input":"2022-08-12T05:18:02.816677Z","iopub.status.idle":"2022-08-12T05:18:02.852098Z","shell.execute_reply.started":"2022-08-12T05:18:02.816647Z","shell.execute_reply":"2022-08-12T05:18:02.851258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"survived_by_p_class = train_df.groupby(['Survived', 'Pclass'])","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:02.854127Z","iopub.execute_input":"2022-08-12T05:18:02.854568Z","iopub.status.idle":"2022-08-12T05:18:02.858454Z","shell.execute_reply.started":"2022-08-12T05:18:02.854541Z","shell.execute_reply":"2022-08-12T05:18:02.857699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"survived_by_p_class.describe() #Getting an overview of passengers survived by class","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:02.859511Z","iopub.execute_input":"2022-08-12T05:18:02.859741Z","iopub.status.idle":"2022-08-12T05:18:02.961977Z","shell.execute_reply.started":"2022-08-12T05:18:02.859719Z","shell.execute_reply":"2022-08-12T05:18:02.961282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.barplot(x = 'Pclass', y='Survived', data = train_df)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:02.963133Z","iopub.execute_input":"2022-08-12T05:18:02.964008Z","iopub.status.idle":"2022-08-12T05:18:03.185307Z","shell.execute_reply.started":"2022-08-12T05:18:02.963980Z","shell.execute_reply":"2022-08-12T05:18:03.184471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.barplot(x = 'Sex', y='Survived', data = train_df) # Passengers survived by gender","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.186443Z","iopub.execute_input":"2022-08-12T05:18:03.187625Z","iopub.status.idle":"2022-08-12T05:18:03.335805Z","shell.execute_reply.started":"2022-08-12T05:18:03.187586Z","shell.execute_reply":"2022-08-12T05:18:03.334925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(data = train_df, x='Fare', hue='Survived', bins = 10, element = 'step') #Passengers survived by fare","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.336984Z","iopub.execute_input":"2022-08-12T05:18:03.338094Z","iopub.status.idle":"2022-08-12T05:18:03.485064Z","shell.execute_reply.started":"2022-08-12T05:18:03.338053Z","shell.execute_reply":"2022-08-12T05:18:03.484294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"survived_by_gender = train_df.groupby(['Sex', 'Pclass']).agg(Survived_Count = ('Survived', 'sum'))\nsurvived_by_gender.plot.bar() #Passengers survived by gender and fare","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.487679Z","iopub.execute_input":"2022-08-12T05:18:03.489721Z","iopub.status.idle":"2022-08-12T05:18:03.636142Z","shell.execute_reply.started":"2022-08-12T05:18:03.489675Z","shell.execute_reply":"2022-08-12T05:18:03.635296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modifying features to train a regression model","metadata":{}},{"cell_type":"code","source":"train_df['Survived'] = train_df['Survived'].apply(str)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.641058Z","iopub.execute_input":"2022-08-12T05:18:03.641830Z","iopub.status.idle":"2022-08-12T05:18:03.649007Z","shell.execute_reply.started":"2022-08-12T05:18:03.641721Z","shell.execute_reply":"2022-08-12T05:18:03.648043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['Pclass'] = train_df['Pclass'].apply(str)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.650783Z","iopub.execute_input":"2022-08-12T05:18:03.651142Z","iopub.status.idle":"2022-08-12T05:18:03.660757Z","shell.execute_reply.started":"2022-08-12T05:18:03.651106Z","shell.execute_reply":"2022-08-12T05:18:03.659878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_train_df = pd.get_dummies(train_df,columns=[\"Sex\"])\nfinal_train_df.drop('Sex_female', axis = 1, inplace=True)\nfinal_train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.663809Z","iopub.execute_input":"2022-08-12T05:18:03.664878Z","iopub.status.idle":"2022-08-12T05:18:03.686612Z","shell.execute_reply.started":"2022-08-12T05:18:03.664678Z","shell.execute_reply":"2022-08-12T05:18:03.685819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_train_df = pd.get_dummies(final_train_df,columns=[\"Pclass\"])\nfinal_train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.687538Z","iopub.execute_input":"2022-08-12T05:18:03.688114Z","iopub.status.idle":"2022-08-12T05:18:03.704815Z","shell.execute_reply.started":"2022-08-12T05:18:03.688089Z","shell.execute_reply":"2022-08-12T05:18:03.703921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_train_df = pd.get_dummies(final_train_df,columns=[\"Embarked\"])\nfinal_train_df.drop('Embarked_C', axis = 1, inplace=True)\nfinal_train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.706094Z","iopub.execute_input":"2022-08-12T05:18:03.706397Z","iopub.status.idle":"2022-08-12T05:18:03.726450Z","shell.execute_reply.started":"2022-08-12T05:18:03.706372Z","shell.execute_reply":"2022-08-12T05:18:03.725726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_train_df = pd.get_dummies(final_train_df,columns=[\"Cabin\"])\nfinal_train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.727477Z","iopub.execute_input":"2022-08-12T05:18:03.728087Z","iopub.status.idle":"2022-08-12T05:18:03.753614Z","shell.execute_reply.started":"2022-08-12T05:18:03.728056Z","shell.execute_reply":"2022-08-12T05:18:03.752596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Applying same changes in test dataset","metadata":{}},{"cell_type":"code","source":"test_df['Survived'] = test_df['Survived'].apply(str)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.754845Z","iopub.execute_input":"2022-08-12T05:18:03.755141Z","iopub.status.idle":"2022-08-12T05:18:03.760097Z","shell.execute_reply.started":"2022-08-12T05:18:03.755116Z","shell.execute_reply":"2022-08-12T05:18:03.759437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['Pclass'] = test_df['Pclass'].apply(str)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.761310Z","iopub.execute_input":"2022-08-12T05:18:03.762111Z","iopub.status.idle":"2022-08-12T05:18:03.773864Z","shell.execute_reply.started":"2022-08-12T05:18:03.762082Z","shell.execute_reply":"2022-08-12T05:18:03.773011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_test_df = pd.get_dummies(test_df,columns=[\"Sex\"])\nfinal_test_df.drop('Sex_female', axis = 1, inplace=True)\nfinal_test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.775045Z","iopub.execute_input":"2022-08-12T05:18:03.775479Z","iopub.status.idle":"2022-08-12T05:18:03.797876Z","shell.execute_reply.started":"2022-08-12T05:18:03.775447Z","shell.execute_reply":"2022-08-12T05:18:03.796983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_test_df = pd.get_dummies(final_test_df,columns=[\"Pclass\"])\nfinal_test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.798984Z","iopub.execute_input":"2022-08-12T05:18:03.799256Z","iopub.status.idle":"2022-08-12T05:18:03.816523Z","shell.execute_reply.started":"2022-08-12T05:18:03.799232Z","shell.execute_reply":"2022-08-12T05:18:03.815239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_test_df = pd.get_dummies(final_test_df,columns=[\"Embarked\"])\nfinal_test_df.drop('Embarked_C', axis = 1, inplace=True)\nfinal_test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.817871Z","iopub.execute_input":"2022-08-12T05:18:03.818201Z","iopub.status.idle":"2022-08-12T05:18:03.837494Z","shell.execute_reply.started":"2022-08-12T05:18:03.818173Z","shell.execute_reply":"2022-08-12T05:18:03.836634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_test_df = pd.get_dummies(final_test_df,columns=[\"Cabin\"])\nfinal_test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.838674Z","iopub.execute_input":"2022-08-12T05:18:03.838952Z","iopub.status.idle":"2022-08-12T05:18:03.859676Z","shell.execute_reply.started":"2022-08-12T05:18:03.838929Z","shell.execute_reply":"2022-08-12T05:18:03.858940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Adjusting Cabins for train and test dfs","metadata":{}},{"cell_type":"code","source":"for i in final_train_df.columns:\n    if i not in final_test_df.columns:\n        cur_list = np.zeros((final_test_df.shape[0]))\n        final_test_df[i] = cur_list ","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.860759Z","iopub.execute_input":"2022-08-12T05:18:03.861268Z","iopub.status.idle":"2022-08-12T05:18:03.899166Z","shell.execute_reply.started":"2022-08-12T05:18:03.861238Z","shell.execute_reply":"2022-08-12T05:18:03.898165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.900398Z","iopub.execute_input":"2022-08-12T05:18:03.900818Z","iopub.status.idle":"2022-08-12T05:18:03.926960Z","shell.execute_reply.started":"2022-08-12T05:18:03.900782Z","shell.execute_reply":"2022-08-12T05:18:03.926025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in final_test_df.columns:\n    if i not in final_train_df.columns:\n        cur_list = np.zeros((final_train_df.shape[0]))\n        final_train_df[i] = cur_list ","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.928118Z","iopub.execute_input":"2022-08-12T05:18:03.928635Z","iopub.status.idle":"2022-08-12T05:18:03.948811Z","shell.execute_reply.started":"2022-08-12T05:18:03.928599Z","shell.execute_reply":"2022-08-12T05:18:03.947875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.950401Z","iopub.execute_input":"2022-08-12T05:18:03.950909Z","iopub.status.idle":"2022-08-12T05:18:03.976932Z","shell.execute_reply.started":"2022-08-12T05:18:03.950871Z","shell.execute_reply":"2022-08-12T05:18:03.976242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = final_test_df.pop(\"Survived\") # As we need y_test for validating our model","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.978083Z","iopub.execute_input":"2022-08-12T05:18:03.978544Z","iopub.status.idle":"2022-08-12T05:18:03.983389Z","shell.execute_reply.started":"2022-08-12T05:18:03.978517Z","shell.execute_reply":"2022-08-12T05:18:03.982538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.984485Z","iopub.execute_input":"2022-08-12T05:18:03.985174Z","iopub.status.idle":"2022-08-12T05:18:03.996915Z","shell.execute_reply.started":"2022-08-12T05:18:03.985147Z","shell.execute_reply":"2022-08-12T05:18:03.995960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = final_train_df.pop(\"Survived\") # As we need y_train for training our model","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:03.998104Z","iopub.execute_input":"2022-08-12T05:18:03.998816Z","iopub.status.idle":"2022-08-12T05:18:04.010399Z","shell.execute_reply.started":"2022-08-12T05:18:03.998782Z","shell.execute_reply":"2022-08-12T05:18:04.008707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:04.011995Z","iopub.execute_input":"2022-08-12T05:18:04.012489Z","iopub.status.idle":"2022-08-12T05:18:04.022781Z","shell.execute_reply.started":"2022-08-12T05:18:04.012451Z","shell.execute_reply":"2022-08-12T05:18:04.021695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Scaling the Datasets","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, confusion_matrix, classification_report, plot_confusion_matrix, precision_score, recall_score\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.linear_model import LogisticRegression","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:04.023965Z","iopub.execute_input":"2022-08-12T05:18:04.024647Z","iopub.status.idle":"2022-08-12T05:18:04.437981Z","shell.execute_reply.started":"2022-08-12T05:18:04.024615Z","shell.execute_reply":"2022-08-12T05:18:04.436937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler= StandardScaler()\n\nscaler.fit(final_train_df)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:04.439233Z","iopub.execute_input":"2022-08-12T05:18:04.439558Z","iopub.status.idle":"2022-08-12T05:18:04.453974Z","shell.execute_reply.started":"2022-08-12T05:18:04.439531Z","shell.execute_reply":"2022-08-12T05:18:04.453288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Classification with scikit-learn","metadata":{}},{"cell_type":"code","source":"Logistic_model = LogisticRegression(max_iter=9000)\n\nLogistic_model.fit(final_train_df,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:04.454914Z","iopub.execute_input":"2022-08-12T05:18:04.455572Z","iopub.status.idle":"2022-08-12T05:18:04.848063Z","shell.execute_reply.started":"2022-08-12T05:18:04.455543Z","shell.execute_reply":"2022-08-12T05:18:04.847346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred =  Logistic_model.predict(final_test_df)  #predicting the test","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:04.849210Z","iopub.execute_input":"2022-08-12T05:18:04.849693Z","iopub.status.idle":"2022-08-12T05:18:04.867099Z","shell.execute_reply.started":"2022-08-12T05:18:04.849665Z","shell.execute_reply":"2022-08-12T05:18:04.866300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test, y_pred) # Testing the accuracy of our model by comparing y test and y predicted","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:04.870735Z","iopub.execute_input":"2022-08-12T05:18:04.872689Z","iopub.status.idle":"2022-08-12T05:18:04.882896Z","shell.execute_reply.started":"2022-08-12T05:18:04.872653Z","shell.execute_reply":"2022-08-12T05:18:04.881911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame() #Saving the result\nsubmission['PassengerId'] = test_df['PassengerId']\nsubmission['Survived'] = y_pred\nsubmission.to_csv('Logistic_Reg_submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:04.886403Z","iopub.execute_input":"2022-08-12T05:18:04.888365Z","iopub.status.idle":"2022-08-12T05:18:04.899226Z","shell.execute_reply.started":"2022-08-12T05:18:04.888334Z","shell.execute_reply":"2022-08-12T05:18:04.898369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_confusion_matrix(Logistic_model, final_test_df, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T05:18:04.903220Z","iopub.execute_input":"2022-08-12T05:18:04.905073Z","iopub.status.idle":"2022-08-12T05:18:05.061342Z","shell.execute_reply.started":"2022-08-12T05:18:04.905041Z","shell.execute_reply":"2022-08-12T05:18:05.060460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}