{"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":"","metadata":{},"execution_count":null,"outputs":[]},{"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":"2022-08-03T16:16:43.028581Z","iopub.execute_input":"2022-08-03T16:16:43.029815Z","iopub.status.idle":"2022-08-03T16:16:43.063035Z","shell.execute_reply.started":"2022-08-03T16:16:43.029680Z","shell.execute_reply":"2022-08-03T16:16:43.061919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:16:43.064675Z","iopub.execute_input":"2022-08-03T16:16:43.065192Z","iopub.status.idle":"2022-08-03T16:16:44.644196Z","shell.execute_reply.started":"2022-08-03T16:16:43.065160Z","shell.execute_reply":"2022-08-03T16:16:44.642853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/titanic/train.csv')\ndf_test = pd.read_csv('/kaggle/input/titanic/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:16:44.646230Z","iopub.execute_input":"2022-08-03T16:16:44.646764Z","iopub.status.idle":"2022-08-03T16:16:44.678939Z","shell.execute_reply.started":"2022-08-03T16:16:44.646714Z","shell.execute_reply":"2022-08-03T16:16:44.677607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:16:44.682842Z","iopub.execute_input":"2022-08-03T16:16:44.683861Z","iopub.status.idle":"2022-08-03T16:16:44.723659Z","shell.execute_reply.started":"2022-08-03T16:16:44.683798Z","shell.execute_reply":"2022-08-03T16:16:44.720252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:16:44.726795Z","iopub.execute_input":"2022-08-03T16:16:44.727664Z","iopub.status.idle":"2022-08-03T16:16:44.761910Z","shell.execute_reply.started":"2022-08-03T16:16:44.727607Z","shell.execute_reply":"2022-08-03T16:16:44.760790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:16:44.763842Z","iopub.execute_input":"2022-08-03T16:16:44.764235Z","iopub.status.idle":"2022-08-03T16:16:44.813257Z","shell.execute_reply.started":"2022-08-03T16:16:44.764195Z","shell.execute_reply":"2022-08-03T16:16:44.812119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_feats = [feats for feats in df_train if df_train[feats].dtypes != 'O']\nnum_feats","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:16:44.815197Z","iopub.execute_input":"2022-08-03T16:16:44.816032Z","iopub.status.idle":"2022-08-03T16:16:44.824450Z","shell.execute_reply.started":"2022-08-03T16:16:44.815987Z","shell.execute_reply":"2022-08-03T16:16:44.823350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for feature in num_feats:\n    df_train.groupby(feature)[\"Survived\"].mean().plot.bar()\n    plt.show()\n    ","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:16:44.826371Z","iopub.execute_input":"2022-08-03T16:16:44.827286Z","iopub.status.idle":"2022-08-03T16:16:58.658943Z","shell.execute_reply.started":"2022-08-03T16:16:44.827240Z","shell.execute_reply":"2022-08-03T16:16:58.657768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_feats = [feat for feat in df_train if feat not in num_feats]\ncat_feats","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:16:58.660428Z","iopub.execute_input":"2022-08-03T16:16:58.660786Z","iopub.status.idle":"2022-08-03T16:16:58.668962Z","shell.execute_reply.started":"2022-08-03T16:16:58.660754Z","shell.execute_reply":"2022-08-03T16:16:58.667741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for feature in cat_feats:\n    df_train.groupby(feature)[\"Survived\"].mean().plot.bar()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:16:58.673997Z","iopub.execute_input":"2022-08-03T16:16:58.674378Z","iopub.status.idle":"2022-08-03T16:17:24.041008Z","shell.execute_reply.started":"2022-08-03T16:16:58.674345Z","shell.execute_reply":"2022-08-03T16:17:24.039905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(df_train.corr(),cmap=\"YlGnBu\",annot=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:24.042482Z","iopub.execute_input":"2022-08-03T16:17:24.042856Z","iopub.status.idle":"2022-08-03T16:17:24.526771Z","shell.execute_reply.started":"2022-08-03T16:17:24.042824Z","shell.execute_reply":"2022-08-03T16:17:24.525615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"Cabin\"].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:24.528376Z","iopub.execute_input":"2022-08-03T16:17:24.528696Z","iopub.status.idle":"2022-08-03T16:17:24.535728Z","shell.execute_reply.started":"2022-08-03T16:17:24.528668Z","shell.execute_reply":"2022-08-03T16:17:24.534549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:24.537317Z","iopub.execute_input":"2022-08-03T16:17:24.537677Z","iopub.status.idle":"2022-08-03T16:17:24.557415Z","shell.execute_reply.started":"2022-08-03T16:17:24.537623Z","shell.execute_reply":"2022-08-03T16:17:24.556536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.drop(['Cabin'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:24.558835Z","iopub.execute_input":"2022-08-03T16:17:24.559930Z","iopub.status.idle":"2022-08-03T16:17:24.567141Z","shell.execute_reply.started":"2022-08-03T16:17:24.559896Z","shell.execute_reply":"2022-08-03T16:17:24.566273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.isnull().sum()\n# Age has highest no. of missing values ,for now we'll fill NA with 500\n# We will estimate the NA values only after calculating the MI scores\n# NA values of 'Embarked' column can be filled it's median","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:24.568546Z","iopub.execute_input":"2022-08-03T16:17:24.569122Z","iopub.status.idle":"2022-08-03T16:17:24.581970Z","shell.execute_reply.started":"2022-08-03T16:17:24.569086Z","shell.execute_reply":"2022-08-03T16:17:24.581105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Age'].fillna(500,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:24.583360Z","iopub.execute_input":"2022-08-03T16:17:24.583939Z","iopub.status.idle":"2022-08-03T16:17:24.589484Z","shell.execute_reply.started":"2022-08-03T16:17:24.583906Z","shell.execute_reply":"2022-08-03T16:17:24.588468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.Age.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:24.591024Z","iopub.execute_input":"2022-08-03T16:17:24.591382Z","iopub.status.idle":"2022-08-03T16:17:24.601718Z","shell.execute_reply.started":"2022-08-03T16:17:24.591352Z","shell.execute_reply":"2022-08-03T16:17:24.600618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_cat_feats = [feat for feat in df_train if feat not in num_feats]\nnew_cat_feats","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:24.603032Z","iopub.execute_input":"2022-08-03T16:17:24.603373Z","iopub.status.idle":"2022-08-03T16:17:24.615814Z","shell.execute_reply.started":"2022-08-03T16:17:24.603343Z","shell.execute_reply":"2022-08-03T16:17:24.614946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df_train.copy()\nX.drop(new_cat_feats,axis=1,inplace=True)\ny = X.pop(\"Survived\")","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:24.617360Z","iopub.execute_input":"2022-08-03T16:17:24.618005Z","iopub.status.idle":"2022-08-03T16:17:24.626884Z","shell.execute_reply.started":"2022-08-03T16:17:24.617971Z","shell.execute_reply":"2022-08-03T16:17:24.626007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:24.628512Z","iopub.execute_input":"2022-08-03T16:17:24.629307Z","iopub.status.idle":"2022-08-03T16:17:24.642711Z","shell.execute_reply.started":"2022-08-03T16:17:24.629264Z","shell.execute_reply":"2022-08-03T16:17:24.641611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = X.columns\nfor col in cols:\n    X[col] = X[col].astype(float)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:24.644107Z","iopub.execute_input":"2022-08-03T16:17:24.644695Z","iopub.status.idle":"2022-08-03T16:17:24.651644Z","shell.execute_reply.started":"2022-08-03T16:17:24.644664Z","shell.execute_reply":"2022-08-03T16:17:24.650748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:24.652829Z","iopub.execute_input":"2022-08-03T16:17:24.653233Z","iopub.status.idle":"2022-08-03T16:17:24.670816Z","shell.execute_reply.started":"2022-08-03T16:17:24.653203Z","shell.execute_reply":"2022-08-03T16:17:24.669030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.feature_selection import mutual_info_regression\nmi_scores = mutual_info_regression(X, y)\nmi_scores = pd.Series(mi_scores, name=\"MI Scores\", index=X.columns)\nmi_scores = mi_scores.sort_values(ascending=False)\nmi_scores","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:24.672199Z","iopub.execute_input":"2022-08-03T16:17:24.672800Z","iopub.status.idle":"2022-08-03T16:17:24.983400Z","shell.execute_reply.started":"2022-08-03T16:17:24.672768Z","shell.execute_reply":"2022-08-03T16:17:24.982344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# First we focus on the features with top 3 scores, namely : 'Fare','Pclass','Age'\n#   1.For age we'll first impute NA values from mean of age based on\n#     its the column it is having maximum correlation with.\n#   2.Convert age feature into discrete bands(0 - 10, 11 - 20 ,...71-80)\n# We'll create a new feature from Parch and SibSp : Family_size\n# Extract title from name feature","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:24.984732Z","iopub.execute_input":"2022-08-03T16:17:24.985054Z","iopub.status.idle":"2022-08-03T16:17:24.990584Z","shell.execute_reply.started":"2022-08-03T16:17:24.985024Z","shell.execute_reply":"2022-08-03T16:17:24.989365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_mi_scores(scores):\n    scores = scores.sort_values(ascending=True)\n    width = np.arange(len(scores))\n    ticks = list(scores.index)\n    plt.barh(width, scores)\n    plt.yticks(width, ticks)\n    plt.title(\"Mutual Information Scores\")\n\n\nplt.figure(dpi=100, figsize=(8, 5))\nplot_mi_scores(mi_scores)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:24.992614Z","iopub.execute_input":"2022-08-03T16:17:24.993140Z","iopub.status.idle":"2022-08-03T16:17:25.179448Z","shell.execute_reply.started":"2022-08-03T16:17:24.993094Z","shell.execute_reply":"2022-08-03T16:17:25.178627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(data=df_train,x=\"Fare\",hue=\"Survived\",binwidth=50,kde=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:25.180665Z","iopub.execute_input":"2022-08-03T16:17:25.181146Z","iopub.status.idle":"2022-08-03T16:17:25.428936Z","shell.execute_reply.started":"2022-08-03T16:17:25.181116Z","shell.execute_reply":"2022-08-03T16:17:25.427849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(data=df_train,x=\"Age\",hue=\"Survived\",kde=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:25.430319Z","iopub.execute_input":"2022-08-03T16:17:25.430672Z","iopub.status.idle":"2022-08-03T16:17:25.935331Z","shell.execute_reply.started":"2022-08-03T16:17:25.430634Z","shell.execute_reply":"2022-08-03T16:17:25.933320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.corr()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:25.943992Z","iopub.execute_input":"2022-08-03T16:17:25.944364Z","iopub.status.idle":"2022-08-03T16:17:25.961059Z","shell.execute_reply.started":"2022-08-03T16:17:25.944332Z","shell.execute_reply":"2022-08-03T16:17:25.959961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"na_age = df_train['Age'] == 500\ndf_train.loc[na_age,'Age'] = 0","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:25.962666Z","iopub.execute_input":"2022-08-03T16:17:25.963574Z","iopub.status.idle":"2022-08-03T16:17:25.971283Z","shell.execute_reply.started":"2022-08-03T16:17:25.963518Z","shell.execute_reply":"2022-08-03T16:17:25.970287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.groupby('Pclass')['Age'].mean()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:25.972579Z","iopub.execute_input":"2022-08-03T16:17:25.973159Z","iopub.status.idle":"2022-08-03T16:17:25.986688Z","shell.execute_reply.started":"2022-08-03T16:17:25.973126Z","shell.execute_reply":"2022-08-03T16:17:25.985467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Filling NA for 'Age' column:\ndf_train.groupby('Pclass')['Age'].mean().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:25.989685Z","iopub.execute_input":"2022-08-03T16:17:25.991879Z","iopub.status.idle":"2022-08-03T16:17:26.110688Z","shell.execute_reply.started":"2022-08-03T16:17:25.991832Z","shell.execute_reply":"2022-08-03T16:17:26.109072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_train.loc[(df_train['Age'].isna()) & (df_train['Pclass'] == 1),'Age'] = 33\n# df_train.loc[(df_train['Age'].isna()) & (df_train['Pclass'] == 2),'Age'] = 28\n# df_train.loc[(df_train['Age'].isna()) & (df_train['Pclass'] == 3),'Age'] = 18","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.113089Z","iopub.execute_input":"2022-08-03T16:17:26.114641Z","iopub.status.idle":"2022-08-03T16:17:26.121100Z","shell.execute_reply.started":"2022-08-03T16:17:26.114552Z","shell.execute_reply":"2022-08-03T16:17:26.119654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.loc[(df_train['Age'] == 0) & (df_train['Pclass'] == 1),'Age'] = 33\ndf_train.loc[(df_train['Age'] == 0) & (df_train['Pclass'] == 2),'Age'] = 28\ndf_train.loc[(df_train['Age'] == 0) & (df_train['Pclass'] == 3),'Age'] = 18","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.123381Z","iopub.execute_input":"2022-08-03T16:17:26.125074Z","iopub.status.idle":"2022-08-03T16:17:26.142131Z","shell.execute_reply.started":"2022-08-03T16:17:26.124994Z","shell.execute_reply":"2022-08-03T16:17:26.140574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(data=df_train,x = \"Age\",hue = \"Survived\",kde=True,binwidth=5)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.144107Z","iopub.execute_input":"2022-08-03T16:17:26.144768Z","iopub.status.idle":"2022-08-03T16:17:26.481807Z","shell.execute_reply.started":"2022-08-03T16:17:26.144732Z","shell.execute_reply":"2022-08-03T16:17:26.481001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"Age_bands\"] = pd.cut(df_train[\"Age\"],5)\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.483172Z","iopub.execute_input":"2022-08-03T16:17:26.483480Z","iopub.status.idle":"2022-08-03T16:17:26.511080Z","shell.execute_reply.started":"2022-08-03T16:17:26.483453Z","shell.execute_reply":"2022-08-03T16:17:26.510033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"Age_bands\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.512680Z","iopub.execute_input":"2022-08-03T16:17:26.513354Z","iopub.status.idle":"2022-08-03T16:17:26.527123Z","shell.execute_reply.started":"2022-08-03T16:17:26.513309Z","shell.execute_reply":"2022-08-03T16:17:26.525916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.loc[df_train[\"Age\"] <= 16,'Age'] = 0\ndf_train.loc[(df_train[\"Age\"] > 16) & (df_train[\"Age\"] <= 32) ,'Age'] = 0\ndf_train.loc[(df_train[\"Age\"] > 32) & (df_train[\"Age\"] <= 48) ,'Age'] = 2\ndf_train.loc[(df_train[\"Age\"] > 48) & (df_train[\"Age\"] <= 64) ,'Age'] = 3\ndf_train.loc[(df_train[\"Age\"] > 64) ,'Age'] = 4","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.528417Z","iopub.execute_input":"2022-08-03T16:17:26.529377Z","iopub.status.idle":"2022-08-03T16:17:26.541591Z","shell.execute_reply.started":"2022-08-03T16:17:26.529344Z","shell.execute_reply":"2022-08-03T16:17:26.540608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pd.set_option('display.max_rows', 500)\n# pd.crosstab(df_train['Age'],df_train['Pclass'])","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.543067Z","iopub.execute_input":"2022-08-03T16:17:26.543723Z","iopub.status.idle":"2022-08-03T16:17:26.551797Z","shell.execute_reply.started":"2022-08-03T16:17:26.543689Z","shell.execute_reply":"2022-08-03T16:17:26.550862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"relations = [\"SibSp\",\"Parch\"]\ndf_train[\"Family_size\"] = df_train[relations].sum(axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.553273Z","iopub.execute_input":"2022-08-03T16:17:26.553626Z","iopub.status.idle":"2022-08-03T16:17:26.565012Z","shell.execute_reply.started":"2022-08-03T16:17:26.553592Z","shell.execute_reply":"2022-08-03T16:17:26.563790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head(25)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.566250Z","iopub.execute_input":"2022-08-03T16:17:26.567029Z","iopub.status.idle":"2022-08-03T16:17:26.596895Z","shell.execute_reply.started":"2022-08-03T16:17:26.566996Z","shell.execute_reply":"2022-08-03T16:17:26.595583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.corr()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.598185Z","iopub.execute_input":"2022-08-03T16:17:26.599301Z","iopub.status.idle":"2022-08-03T16:17:26.618514Z","shell.execute_reply.started":"2022-08-03T16:17:26.599264Z","shell.execute_reply":"2022-08-03T16:17:26.617444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.619955Z","iopub.execute_input":"2022-08-03T16:17:26.620281Z","iopub.status.idle":"2022-08-03T16:17:26.628266Z","shell.execute_reply.started":"2022-08-03T16:17:26.620252Z","shell.execute_reply":"2022-08-03T16:17:26.627496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.preprocessing import OneHotEncoder\n# enc = OneHotEncoder()\n# enc.fit(df_train[\"Embarked\"])  ","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.629179Z","iopub.execute_input":"2022-08-03T16:17:26.629477Z","iopub.status.idle":"2022-08-03T16:17:26.637168Z","shell.execute_reply.started":"2022-08-03T16:17:26.629447Z","shell.execute_reply":"2022-08-03T16:17:26.636093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import OrdinalEncoder\nord_encoder = OrdinalEncoder()\nembarked_col = df_train[[\"Embarked\"]]\nencoded_embarked = ord_encoder.fit_transform(embarked_col)\ndf_train[\"Embarked_num\"] = encoded_embarked","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.638518Z","iopub.execute_input":"2022-08-03T16:17:26.638876Z","iopub.status.idle":"2022-08-03T16:17:26.651481Z","shell.execute_reply.started":"2022-08-03T16:17:26.638845Z","shell.execute_reply":"2022-08-03T16:17:26.650366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.653139Z","iopub.execute_input":"2022-08-03T16:17:26.653848Z","iopub.status.idle":"2022-08-03T16:17:26.675250Z","shell.execute_reply.started":"2022-08-03T16:17:26.653805Z","shell.execute_reply":"2022-08-03T16:17:26.673951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"Embarked_num\"].unique()\n# 0 = C,1 = Q,S = 2 ","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.676834Z","iopub.execute_input":"2022-08-03T16:17:26.677279Z","iopub.status.idle":"2022-08-03T16:17:26.685473Z","shell.execute_reply.started":"2022-08-03T16:17:26.677237Z","shell.execute_reply":"2022-08-03T16:17:26.684361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embarked_mode = df_train[\"Embarked\"].mode()\nembarked_mode.astype(str)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.686780Z","iopub.execute_input":"2022-08-03T16:17:26.687203Z","iopub.status.idle":"2022-08-03T16:17:26.698606Z","shell.execute_reply.started":"2022-08-03T16:17:26.687163Z","shell.execute_reply":"2022-08-03T16:17:26.697682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"Embarked_num\"].fillna(df_train[\"Embarked_num\"].median(),inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.700112Z","iopub.execute_input":"2022-08-03T16:17:26.700682Z","iopub.status.idle":"2022-08-03T16:17:26.709521Z","shell.execute_reply.started":"2022-08-03T16:17:26.700649Z","shell.execute_reply":"2022-08-03T16:17:26.708631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.711380Z","iopub.execute_input":"2022-08-03T16:17:26.712389Z","iopub.status.idle":"2022-08-03T16:17:26.725116Z","shell.execute_reply.started":"2022-08-03T16:17:26.712346Z","shell.execute_reply":"2022-08-03T16:17:26.723575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.drop(['Embarked'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.726343Z","iopub.execute_input":"2022-08-03T16:17:26.727170Z","iopub.status.idle":"2022-08-03T16:17:26.733341Z","shell.execute_reply.started":"2022-08-03T16:17:26.727129Z","shell.execute_reply":"2022-08-03T16:17:26.732362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_cat_feats.remove(\"Embarked\")","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.735792Z","iopub.execute_input":"2022-08-03T16:17:26.737024Z","iopub.status.idle":"2022-08-03T16:17:26.743397Z","shell.execute_reply.started":"2022-08-03T16:17:26.736977Z","shell.execute_reply":"2022-08-03T16:17:26.742606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"Title\"] = df_train[\"Name\"].str.extract(' ([A-Za-z]+)\\.', expand=False)\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.745417Z","iopub.execute_input":"2022-08-03T16:17:26.746390Z","iopub.status.idle":"2022-08-03T16:17:26.771720Z","shell.execute_reply.started":"2022-08-03T16:17:26.746347Z","shell.execute_reply":"2022-08-03T16:17:26.770807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"Title\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.772949Z","iopub.execute_input":"2022-08-03T16:17:26.773806Z","iopub.status.idle":"2022-08-03T16:17:26.782290Z","shell.execute_reply.started":"2022-08-03T16:17:26.773773Z","shell.execute_reply":"2022-08-03T16:17:26.781131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Title'] = df_train['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')\ndf_train['Title'] = df_train['Title'].replace(['Mlle','Ms'], 'Miss')\ndf_train['Title'] = df_train['Title'].replace('Mlle', 'Miss')\ndf_train['Title'] = df_train['Title'].replace('Mnme', 'Miss')","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.783409Z","iopub.execute_input":"2022-08-03T16:17:26.784326Z","iopub.status.idle":"2022-08-03T16:17:26.797714Z","shell.execute_reply.started":"2022-08-03T16:17:26.784286Z","shell.execute_reply":"2022-08-03T16:17:26.796454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.groupby('Title')[\"Survived\"].mean().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.799246Z","iopub.execute_input":"2022-08-03T16:17:26.800411Z","iopub.status.idle":"2022-08-03T16:17:26.957483Z","shell.execute_reply.started":"2022-08-03T16:17:26.800363Z","shell.execute_reply":"2022-08-03T16:17:26.956636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.drop(\"Age_bands\",axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.958809Z","iopub.execute_input":"2022-08-03T16:17:26.959942Z","iopub.status.idle":"2022-08-03T16:17:26.966053Z","shell.execute_reply.started":"2022-08-03T16:17:26.959904Z","shell.execute_reply":"2022-08-03T16:17:26.964955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ord_encoder = OrdinalEncoder()\ntitle_col = df_train[[\"Title\"]]\nencoded_title = ord_encoder.fit_transform(title_col)\ndf_train[\"Title_num\"] = encoded_title","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.967701Z","iopub.execute_input":"2022-08-03T16:17:26.968189Z","iopub.status.idle":"2022-08-03T16:17:26.979420Z","shell.execute_reply.started":"2022-08-03T16:17:26.968059Z","shell.execute_reply":"2022-08-03T16:17:26.978403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df_train.copy()\nX.drop(new_cat_feats,axis=1,inplace=True)\nX.drop(\"Title\",axis=1,inplace=True)\ny = X.pop(\"Survived\")","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.981244Z","iopub.execute_input":"2022-08-03T16:17:26.981714Z","iopub.status.idle":"2022-08-03T16:17:26.992117Z","shell.execute_reply.started":"2022-08-03T16:17:26.981670Z","shell.execute_reply":"2022-08-03T16:17:26.990981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:26.993527Z","iopub.execute_input":"2022-08-03T16:17:26.994169Z","iopub.status.idle":"2022-08-03T16:17:27.009032Z","shell.execute_reply.started":"2022-08-03T16:17:26.994136Z","shell.execute_reply":"2022-08-03T16:17:27.007668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mi_scores2= mutual_info_regression(X, y)\nmi_scores2 = pd.Series(mi_scores2, name=\"MI Scores\", index=X.columns)\nmi_scores2= mi_scores2.sort_values(ascending=False)\nmi_scores2","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:27.010577Z","iopub.execute_input":"2022-08-03T16:17:27.011215Z","iopub.status.idle":"2022-08-03T16:17:27.076055Z","shell.execute_reply.started":"2022-08-03T16:17:27.011177Z","shell.execute_reply":"2022-08-03T16:17:27.075273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:27.077739Z","iopub.execute_input":"2022-08-03T16:17:27.078473Z","iopub.status.idle":"2022-08-03T16:17:27.098636Z","shell.execute_reply.started":"2022-08-03T16:17:27.078427Z","shell.execute_reply":"2022-08-03T16:17:27.097540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df_train.copy()\ny = X.pop(\"Survived\")\nx_feats = [\"Title_num\",\"Fare\",\"Family_size\",\"Pclass\"]\nX = df_train.loc[:,x_feats]\n","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:27.100393Z","iopub.execute_input":"2022-08-03T16:17:27.101166Z","iopub.status.idle":"2022-08-03T16:17:27.110924Z","shell.execute_reply.started":"2022-08-03T16:17:27.101118Z","shell.execute_reply":"2022-08-03T16:17:27.109890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# K-Means\nfrom sklearn.cluster import KMeans\nX_scaled = (X - X.mean(axis=0))/X.std(axis=0)\nkmeans = KMeans(n_clusters=10, random_state=0)\nX[\"Cluster\"] = kmeans.fit_predict(X_scaled)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:27.112392Z","iopub.execute_input":"2022-08-03T16:17:27.112962Z","iopub.status.idle":"2022-08-03T16:17:27.283494Z","shell.execute_reply.started":"2022-08-03T16:17:27.112929Z","shell.execute_reply":"2022-08-03T16:17:27.282617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:27.284964Z","iopub.execute_input":"2022-08-03T16:17:27.285531Z","iopub.status.idle":"2022-08-03T16:17:27.298639Z","shell.execute_reply.started":"2022-08-03T16:17:27.285495Z","shell.execute_reply":"2022-08-03T16:17:27.297592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in x_feats:\n    sns.histplot(data = X,x = col,hue = \"Cluster\",kde=True)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:17:27.300124Z","iopub.execute_input":"2022-08-03T16:17:27.300711Z","iopub.status.idle":"2022-08-03T16:17:32.591235Z","shell.execute_reply.started":"2022-08-03T16:17:27.300676Z","shell.execute_reply":"2022-08-03T16:17:32.590133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"Clust1\"] = X[\"Cluster\"]\n# CHANGE : Use the K means distances as features instead of the clusters as features","metadata":{"execution":{"iopub.status.busy":"2022-08-03T16:35:25.381687Z","iopub.execute_input":"2022-08-03T16:35:25.382144Z","iopub.status.idle":"2022-08-03T16:35:25.389825Z","shell.execute_reply.started":"2022-08-03T16:35:25.382108Z","shell.execute_reply":"2022-08-03T16:35:25.388570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# PCA\nX = df_train.copy()\ny = X.pop(\"Survived\")\nX= X.loc[:,x_feats]\nX_scaled = (X - X.mean(axis=0))/X.std(axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:04:18.164130Z","iopub.execute_input":"2022-08-03T17:04:18.164713Z","iopub.status.idle":"2022-08-03T17:04:18.184625Z","shell.execute_reply.started":"2022-08-03T17:04:18.164669Z","shell.execute_reply":"2022-08-03T17:04:18.182641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\n\npca = PCA()\nX_pca = pca.fit_transform(X_scaled)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:04:18.733612Z","iopub.execute_input":"2022-08-03T17:04:18.734044Z","iopub.status.idle":"2022-08-03T17:04:18.744807Z","shell.execute_reply.started":"2022-08-03T17:04:18.734006Z","shell.execute_reply":"2022-08-03T17:04:18.743518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_pca = pd.DataFrame(X_pca,columns=[\"PC1\",\"PC2\",\"PC3\",\"PC4\"])","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:04:19.641773Z","iopub.execute_input":"2022-08-03T17:04:19.642268Z","iopub.status.idle":"2022-08-03T17:04:19.649482Z","shell.execute_reply.started":"2022-08-03T17:04:19.642229Z","shell.execute_reply":"2022-08-03T17:04:19.648271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_pca.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:04:27.369925Z","iopub.execute_input":"2022-08-03T17:04:27.370301Z","iopub.status.idle":"2022-08-03T17:04:27.385552Z","shell.execute_reply.started":"2022-08-03T17:04:27.370269Z","shell.execute_reply":"2022-08-03T17:04:27.383845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evr = pca.explained_variance_ratio_\nplt.bar([1,2,3,4],evr)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:11:30.981268Z","iopub.execute_input":"2022-08-03T17:11:30.981886Z","iopub.status.idle":"2022-08-03T17:11:31.200122Z","shell.execute_reply.started":"2022-08-03T17:11:30.981843Z","shell.execute_reply":"2022-08-03T17:11:31.198606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mi_scores3= mutual_info_regression(X_pca, y)\nmi_scores3 = pd.Series(mi_scores3, name=\"MI Scores\", index=X.columns)\nmi_scores3= mi_scores3.sort_values(ascending=False)\nmi_scores3","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:18:01.668196Z","iopub.execute_input":"2022-08-03T17:18:01.668706Z","iopub.status.idle":"2022-08-03T17:18:01.713018Z","shell.execute_reply.started":"2022-08-03T17:18:01.668665Z","shell.execute_reply":"2022-08-03T17:18:01.711765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}