{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport seaborn as sns\nimport matplotlib.pyplot as plt\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-01T17:22:51.261758Z","iopub.execute_input":"2022-08-01T17:22:51.262642Z","iopub.status.idle":"2022-08-01T17:22:52.728199Z","shell.execute_reply.started":"2022-08-01T17:22:51.262523Z","shell.execute_reply":"2022-08-01T17:22:52.726656Z"},"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\")\ndf_gender_sub = pd.read_csv(\"/kaggle/input/titanic/gender_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-01T17:23:01.497480Z","iopub.execute_input":"2022-08-01T17:23:01.498050Z","iopub.status.idle":"2022-08-01T17:23:01.543069Z","shell.execute_reply.started":"2022-08-01T17:23:01.498005Z","shell.execute_reply":"2022-08-01T17:23:01.541606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.describe()\ndf_train.info()\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T17:23:08.834506Z","iopub.execute_input":"2022-08-01T17:23:08.834930Z","iopub.status.idle":"2022-08-01T17:23:08.907211Z","shell.execute_reply.started":"2022-08-01T17:23:08.834899Z","shell.execute_reply":"2022-08-01T17:23:08.906042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for cols in ['Survived','Pclass','Sex','SibSp','Parch','Embarked']:\n    print(np.unique(df_train[df_train[cols].notnull()][cols]))","metadata":{"execution":{"iopub.status.busy":"2022-08-01T17:37:30.579774Z","iopub.execute_input":"2022-08-01T17:37:30.580543Z","iopub.status.idle":"2022-08-01T17:37:30.597112Z","shell.execute_reply.started":"2022-08-01T17:37:30.580495Z","shell.execute_reply":"2022-08-01T17:37:30.595866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# PassengerId, Name , Ticket and Cabin are ignored\n# Age, Pclass , Fare , SibSp , Parch are numerics\n# Sex , Embarked are objects\n# Age and Fare are continous. rest all are categorical","metadata":{}},{"cell_type":"code","source":"fig , ax = plt.subplots(2)\ni=0\nfor cols in ['Age','Fare']:\n    sns.boxplot(data=df_train, x=cols , ax=ax[i])\n    i+=1\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T17:37:54.485021Z","iopub.execute_input":"2022-08-01T17:37:54.485448Z","iopub.status.idle":"2022-08-01T17:37:54.774712Z","shell.execute_reply.started":"2022-08-01T17:37:54.485404Z","shell.execute_reply":"2022-08-01T17:37:54.773354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Removing the outliers","metadata":{}},{"cell_type":"code","source":"df_train = df_train[df_train['Fare'] < 300]","metadata":{"execution":{"iopub.status.busy":"2022-08-01T17:38:03.133783Z","iopub.execute_input":"2022-08-01T17:38:03.134172Z","iopub.status.idle":"2022-08-01T17:38:03.141307Z","shell.execute_reply.started":"2022-08-01T17:38:03.134140Z","shell.execute_reply":"2022-08-01T17:38:03.140415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig , ax = plt.subplots(3,5,figsize=(20,15))\nj=0\nfor cols in ['Pclass','Sex','SibSp','Parch','Embarked']:\n    sns.countplot(data=df_train , x=cols , ax = ax[0,j])\n    sns.barplot(data=df_train, x=cols, y='Survived', ax=ax[1,j])\n    sns.histplot(data=df_train, x = cols , hue = 'Survived', multiple = 'stack' ,  ax=ax[2,j])\n    j += 1\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T17:38:14.453202Z","iopub.execute_input":"2022-08-01T17:38:14.453605Z","iopub.status.idle":"2022-08-01T17:38:17.570631Z","shell.execute_reply.started":"2022-08-01T17:38:14.453557Z","shell.execute_reply":"2022-08-01T17:38:17.569031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig , ax = plt.subplots(2,2,figsize=(20,15))\nj=0\nfor cols in ['Age','Fare']:\n    sns.countplot(data=df_train,x= cols, hue='Survived', ax = ax[0,j] , dodge=False)\n    sns.histplot(data=df_train, x = cols, hue='Survived', bins=10, multiple = 'stack', kde='True' ,ax=ax[1,j])\n    j+=1\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T17:38:26.815881Z","iopub.execute_input":"2022-08-01T17:38:26.816261Z","iopub.status.idle":"2022-08-01T17:38:32.906818Z","shell.execute_reply.started":"2022-08-01T17:38:26.816231Z","shell.execute_reply":"2022-08-01T17:38:32.905655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(df_train.corr())","metadata":{"execution":{"iopub.status.busy":"2022-08-01T17:38:39.308960Z","iopub.execute_input":"2022-08-01T17:38:39.309369Z","iopub.status.idle":"2022-08-01T17:38:39.644422Z","shell.execute_reply.started":"2022-08-01T17:38:39.309338Z","shell.execute_reply":"2022-08-01T17:38:39.643059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig , ax = plt.subplots(2 , figsize=(12,15))\ndf_train.plot.scatter(x='Age',y='Fare', ax = ax[0])\nsns.boxplot(data=df_train , x = 'Survived' , y = 'Fare' , ax = ax[1])","metadata":{"execution":{"iopub.status.busy":"2022-08-01T17:38:46.478103Z","iopub.execute_input":"2022-08-01T17:38:46.478536Z","iopub.status.idle":"2022-08-01T17:38:46.855777Z","shell.execute_reply.started":"2022-08-01T17:38:46.478498Z","shell.execute_reply":"2022-08-01T17:38:46.854611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T17:38:55.068761Z","iopub.execute_input":"2022-08-01T17:38:55.069225Z","iopub.status.idle":"2022-08-01T17:38:55.081837Z","shell.execute_reply.started":"2022-08-01T17:38:55.069187Z","shell.execute_reply":"2022-08-01T17:38:55.080861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.Age = df_train.Age.fillna(df_train.Age.mean())\ndf_train.Embarked = df_train.Embarked.fillna(method='ffill')","metadata":{"execution":{"iopub.status.busy":"2022-08-01T17:39:02.719754Z","iopub.execute_input":"2022-08-01T17:39:02.720155Z","iopub.status.idle":"2022-08-01T17:39:02.731526Z","shell.execute_reply.started":"2022-08-01T17:39:02.720124Z","shell.execute_reply":"2022-08-01T17:39:02.730614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.compose import make_column_transformer\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.linear_model import LinearRegression\n\n\ncol_trans = make_column_transformer(\n    (OneHotEncoder(handle_unknown='ignore'),['Sex','Embarked']),\n    remainder='passthrough')\n\npip = make_pipeline(col_trans,LinearRegression())","metadata":{"execution":{"iopub.status.busy":"2022-08-01T17:39:40.385052Z","iopub.execute_input":"2022-08-01T17:39:40.385438Z","iopub.status.idle":"2022-08-01T17:39:40.625161Z","shell.execute_reply.started":"2022-08-01T17:39:40.385408Z","shell.execute_reply":"2022-08-01T17:39:40.623981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X, y = df_train[['Pclass','Sex','Age','SibSp','Parch','Embarked','Fare']],df_train['Survived']\nmodel = pip.fit(X,y)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T17:39:45.299514Z","iopub.execute_input":"2022-08-01T17:39:45.299999Z","iopub.status.idle":"2022-08-01T17:39:45.329259Z","shell.execute_reply.started":"2022-08-01T17:39:45.299963Z","shell.execute_reply":"2022-08-01T17:39:45.328281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T17:39:51.395186Z","iopub.execute_input":"2022-08-01T17:39:51.395630Z","iopub.status.idle":"2022-08-01T17:39:51.414686Z","shell.execute_reply.started":"2022-08-01T17:39:51.395587Z","shell.execute_reply":"2022-08-01T17:39:51.413529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.Age = pd.to_numeric(df_test.Age , errors = 'coerce')\ndf_test.Age = df_test.Age.fillna(df_test.Age.mean())","metadata":{"execution":{"iopub.status.busy":"2022-08-01T17:40:13.812001Z","iopub.execute_input":"2022-08-01T17:40:13.812407Z","iopub.status.idle":"2022-08-01T17:40:13.818962Z","shell.execute_reply.started":"2022-08-01T17:40:13.812374Z","shell.execute_reply":"2022-08-01T17:40:13.817957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T17:40:17.550990Z","iopub.execute_input":"2022-08-01T17:40:17.552085Z","iopub.status.idle":"2022-08-01T17:40:17.562328Z","shell.execute_reply.started":"2022-08-01T17:40:17.552037Z","shell.execute_reply":"2022-08-01T17:40:17.561006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.Fare = df_test.Fare.fillna(df_test.Fare.mean())","metadata":{"execution":{"iopub.status.busy":"2022-08-01T17:40:25.137970Z","iopub.execute_input":"2022-08-01T17:40:25.138392Z","iopub.status.idle":"2022-08-01T17:40:25.145553Z","shell.execute_reply.started":"2022-08-01T17:40:25.138357Z","shell.execute_reply":"2022-08-01T17:40:25.144320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns = ['Pclass','Sex','Age','SibSp','Parch','Embarked','Fare']\n\npreds = model.predict(df_test[columns])\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T17:40:31.197741Z","iopub.execute_input":"2022-08-01T17:40:31.198135Z","iopub.status.idle":"2022-08-01T17:40:31.211530Z","shell.execute_reply.started":"2022-08-01T17:40:31.198103Z","shell.execute_reply":"2022-08-01T17:40:31.210250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count = 0\nfor i in range(0,len(preds)):\n    if(int(preds[i]) == df_gender_sub.iloc[i,1]):\n        count += 1\nprint(count)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T17:40:56.182438Z","iopub.execute_input":"2022-08-01T17:40:56.182852Z","iopub.status.idle":"2022-08-01T17:40:56.210620Z","shell.execute_reply.started":"2022-08-01T17:40:56.182819Z","shell.execute_reply":"2022-08-01T17:40:56.209508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test_results = df_test[['PassengerId']]\npreds = preds.astype(int)\ndf_test_results['Survived'] = preds","metadata":{"execution":{"iopub.status.busy":"2022-08-01T17:41:06.931852Z","iopub.execute_input":"2022-08-01T17:41:06.932238Z","iopub.status.idle":"2022-08-01T17:41:06.942199Z","shell.execute_reply.started":"2022-08-01T17:41:06.932207Z","shell.execute_reply":"2022-08-01T17:41:06.939951Z"},"trusted":true},"execution_count":null,"outputs":[]}]}