{"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)\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-07-09T11:00:29.641971Z","iopub.execute_input":"2022-07-09T11:00:29.642455Z","iopub.status.idle":"2022-07-09T11:00:29.673857Z","shell.execute_reply.started":"2022-07-09T11:00:29.642364Z","shell.execute_reply":"2022-07-09T11:00:29.672595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport sklearn\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nimport math\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:01:09.004023Z","iopub.execute_input":"2022-07-09T11:01:09.004510Z","iopub.status.idle":"2022-07-09T11:01:09.658658Z","shell.execute_reply.started":"2022-07-09T11:01:09.004473Z","shell.execute_reply":"2022-07-09T11:01:09.657507Z"},"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')","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:01:24.307391Z","iopub.execute_input":"2022-07-09T11:01:24.308477Z","iopub.status.idle":"2022-07-09T11:01:24.339666Z","shell.execute_reply.started":"2022-07-09T11:01:24.308431Z","shell.execute_reply":"2022-07-09T11:01:24.338844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Shape of the Dataset:', train_df.shape)\nprint('Size of the Dataset:', train_df.size)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:01:34.737080Z","iopub.execute_input":"2022-07-09T11:01:34.737463Z","iopub.status.idle":"2022-07-09T11:01:34.743050Z","shell.execute_reply.started":"2022-07-09T11:01:34.737433Z","shell.execute_reply":"2022-07-09T11:01:34.741876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Shape of the test dataset:', test_df.shape)\nprint('Size of the test dataset:', test_df.size)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:01:40.617608Z","iopub.execute_input":"2022-07-09T11:01:40.618051Z","iopub.status.idle":"2022-07-09T11:01:40.623324Z","shell.execute_reply.started":"2022-07-09T11:01:40.618016Z","shell.execute_reply":"2022-07-09T11:01:40.622510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Merging both the datasets","metadata":{}},{"cell_type":"code","source":"train_df['source'] = 'train'\ntest_df['source'] = 'test'","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:01:58.112440Z","iopub.execute_input":"2022-07-09T11:01:58.112859Z","iopub.status.idle":"2022-07-09T11:01:58.124439Z","shell.execute_reply.started":"2022-07-09T11:01:58.112824Z","shell.execute_reply":"2022-07-09T11:01:58.123541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.concat([train_df, test_df], ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:02:04.782374Z","iopub.execute_input":"2022-07-09T11:02:04.782744Z","iopub.status.idle":"2022-07-09T11:02:04.797742Z","shell.execute_reply.started":"2022-07-09T11:02:04.782715Z","shell.execute_reply":"2022-07-09T11:02:04.796588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:02:10.292179Z","iopub.execute_input":"2022-07-09T11:02:10.292596Z","iopub.status.idle":"2022-07-09T11:02:10.327624Z","shell.execute_reply.started":"2022-07-09T11:02:10.292561Z","shell.execute_reply":"2022-07-09T11:02:10.326519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:02:21.392588Z","iopub.execute_input":"2022-07-09T11:02:21.393702Z","iopub.status.idle":"2022-07-09T11:02:21.432813Z","shell.execute_reply.started":"2022-07-09T11:02:21.393645Z","shell.execute_reply":"2022-07-09T11:02:21.431557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:02:29.222774Z","iopub.execute_input":"2022-07-09T11:02:29.225158Z","iopub.status.idle":"2022-07-09T11:02:29.236014Z","shell.execute_reply.started":"2022-07-09T11:02:29.225114Z","shell.execute_reply":"2022-07-09T11:02:29.235010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Exploratory Data Analysis (EDA)**","metadata":{}},{"cell_type":"code","source":"df.drop(['Cabin','Name','Ticket','Embarked'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:02:58.982952Z","iopub.execute_input":"2022-07-09T11:02:58.983366Z","iopub.status.idle":"2022-07-09T11:02:58.990331Z","shell.execute_reply.started":"2022-07-09T11:02:58.983334Z","shell.execute_reply":"2022-07-09T11:02:58.989532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:03:04.612481Z","iopub.execute_input":"2022-07-09T11:03:04.613306Z","iopub.status.idle":"2022-07-09T11:03:04.627368Z","shell.execute_reply.started":"2022-07-09T11:03:04.613267Z","shell.execute_reply":"2022-07-09T11:03:04.626408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:03:11.682801Z","iopub.execute_input":"2022-07-09T11:03:11.683566Z","iopub.status.idle":"2022-07-09T11:03:11.702412Z","shell.execute_reply.started":"2022-07-09T11:03:11.683528Z","shell.execute_reply":"2022-07-09T11:03:11.701406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Age'].hist(bins=20)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:03:18.812821Z","iopub.execute_input":"2022-07-09T11:03:18.813216Z","iopub.status.idle":"2022-07-09T11:03:19.044712Z","shell.execute_reply.started":"2022-07-09T11:03:18.813183Z","shell.execute_reply":"2022-07-09T11:03:19.043937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Age'] = df['Age'].fillna(df['Age'].mean())\ndf['Fare'] = df['Fare'].fillna(df['Fare'].mean())","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:03:26.307811Z","iopub.execute_input":"2022-07-09T11:03:26.308204Z","iopub.status.idle":"2022-07-09T11:03:26.315422Z","shell.execute_reply.started":"2022-07-09T11:03:26.308174Z","shell.execute_reply":"2022-07-09T11:03:26.314437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"warnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:03:41.712278Z","iopub.execute_input":"2022-07-09T11:03:41.712707Z","iopub.status.idle":"2022-07-09T11:03:41.717515Z","shell.execute_reply.started":"2022-07-09T11:03:41.712659Z","shell.execute_reply":"2022-07-09T11:03:41.716763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:03:51.107806Z","iopub.execute_input":"2022-07-09T11:03:51.109078Z","iopub.status.idle":"2022-07-09T11:03:51.142522Z","shell.execute_reply.started":"2022-07-09T11:03:51.109025Z","shell.execute_reply":"2022-07-09T11:03:51.141697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Sex'] = le.fit_transform(df['Sex'])","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:03:56.712447Z","iopub.execute_input":"2022-07-09T11:03:56.712848Z","iopub.status.idle":"2022-07-09T11:03:56.719110Z","shell.execute_reply.started":"2022-07-09T11:03:56.712817Z","shell.execute_reply":"2022-07-09T11:03:56.717848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.get_dummies(df, columns=['Sex'])","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:04:02.722002Z","iopub.execute_input":"2022-07-09T11:04:02.723025Z","iopub.status.idle":"2022-07-09T11:04:02.733341Z","shell.execute_reply.started":"2022-07-09T11:04:02.722972Z","shell.execute_reply":"2022-07-09T11:04:02.732301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:04:10.123169Z","iopub.execute_input":"2022-07-09T11:04:10.123516Z","iopub.status.idle":"2022-07-09T11:04:10.132594Z","shell.execute_reply.started":"2022-07-09T11:04:10.123488Z","shell.execute_reply":"2022-07-09T11:04:10.131431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = df.loc[df['source'] == 'train']\ntest = df.loc[df['source'] == 'test']","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:04:18.042944Z","iopub.execute_input":"2022-07-09T11:04:18.044040Z","iopub.status.idle":"2022-07-09T11:04:18.054182Z","shell.execute_reply.started":"2022-07-09T11:04:18.043980Z","shell.execute_reply":"2022-07-09T11:04:18.052748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.drop(['Survived', 'source'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:04:26.558927Z","iopub.execute_input":"2022-07-09T11:04:26.559347Z","iopub.status.idle":"2022-07-09T11:04:26.565877Z","shell.execute_reply.started":"2022-07-09T11:04:26.559317Z","shell.execute_reply":"2022-07-09T11:04:26.564856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(['source'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:04:34.676889Z","iopub.execute_input":"2022-07-09T11:04:34.677307Z","iopub.status.idle":"2022-07-09T11:04:34.684391Z","shell.execute_reply.started":"2022-07-09T11:04:34.677265Z","shell.execute_reply":"2022-07-09T11:04:34.683217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.to_csv('train_modified.csv', index=False)\ntest.to_csv('test_modified.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:04:41.103212Z","iopub.execute_input":"2022-07-09T11:04:41.103598Z","iopub.status.idle":"2022-07-09T11:04:41.120212Z","shell.execute_reply.started":"2022-07-09T11:04:41.103567Z","shell.execute_reply":"2022-07-09T11:04:41.119189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('./train_modified.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:04:47.332804Z","iopub.execute_input":"2022-07-09T11:04:47.333313Z","iopub.status.idle":"2022-07-09T11:04:47.343452Z","shell.execute_reply.started":"2022-07-09T11:04:47.333265Z","shell.execute_reply":"2022-07-09T11:04:47.342428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df  = pd.read_csv('./test_modified.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:04:51.982061Z","iopub.execute_input":"2022-07-09T11:04:51.982717Z","iopub.status.idle":"2022-07-09T11:04:51.990153Z","shell.execute_reply.started":"2022-07-09T11:04:51.982677Z","shell.execute_reply":"2022-07-09T11:04:51.989353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = train_df.drop(['Survived'], axis=1)\ny_train = train_df.Survived","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:04:56.957092Z","iopub.execute_input":"2022-07-09T11:04:56.957641Z","iopub.status.idle":"2022-07-09T11:04:56.964751Z","shell.execute_reply.started":"2022-07-09T11:04:56.957603Z","shell.execute_reply":"2022-07-09T11:04:56.963407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:05:02.767179Z","iopub.execute_input":"2022-07-09T11:05:02.767696Z","iopub.status.idle":"2022-07-09T11:05:02.790821Z","shell.execute_reply.started":"2022-07-09T11:05:02.767639Z","shell.execute_reply":"2022-07-09T11:05:02.789575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:05:08.712965Z","iopub.execute_input":"2022-07-09T11:05:08.713482Z","iopub.status.idle":"2022-07-09T11:05:08.723822Z","shell.execute_reply.started":"2022-07-09T11:05:08.713435Z","shell.execute_reply":"2022-07-09T11:05:08.722649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = test_df","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:05:15.343263Z","iopub.execute_input":"2022-07-09T11:05:15.343656Z","iopub.status.idle":"2022-07-09T11:05:15.348687Z","shell.execute_reply.started":"2022-07-09T11:05:15.343624Z","shell.execute_reply":"2022-07-09T11:05:15.347369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:05:22.567747Z","iopub.execute_input":"2022-07-09T11:05:22.568702Z","iopub.status.idle":"2022-07-09T11:05:22.586256Z","shell.execute_reply.started":"2022-07-09T11:05:22.568658Z","shell.execute_reply":"2022-07-09T11:05:22.585115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:05:31.257337Z","iopub.execute_input":"2022-07-09T11:05:31.257717Z","iopub.status.idle":"2022-07-09T11:05:31.268494Z","shell.execute_reply.started":"2022-07-09T11:05:31.257685Z","shell.execute_reply":"2022-07-09T11:05:31.267466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Modelling**","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nxtrain, xtest, ytrain, ytest = train_test_split(x_train, y_train, test_size=0.3, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:06:04.422265Z","iopub.execute_input":"2022-07-09T11:06:04.422643Z","iopub.status.idle":"2022-07-09T11:06:04.483129Z","shell.execute_reply.started":"2022-07-09T11:06:04.422612Z","shell.execute_reply":"2022-07-09T11:06:04.482164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nlr = LogisticRegression()\nlr.fit(xtrain, ytrain)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:06:12.622721Z","iopub.execute_input":"2022-07-09T11:06:12.623437Z","iopub.status.idle":"2022-07-09T11:06:12.739921Z","shell.execute_reply.started":"2022-07-09T11:06:12.623397Z","shell.execute_reply":"2022-07-09T11:06:12.739161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Coefficient:\\n', lr.coef_)\nprint('\\nIntercept:', lr.intercept_)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:06:19.153083Z","iopub.execute_input":"2022-07-09T11:06:19.153507Z","iopub.status.idle":"2022-07-09T11:06:19.160296Z","shell.execute_reply.started":"2022-07-09T11:06:19.153474Z","shell.execute_reply":"2022-07-09T11:06:19.159198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = lr.predict(xtest)\npredictions","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:06:25.352721Z","iopub.execute_input":"2022-07-09T11:06:25.353488Z","iopub.status.idle":"2022-07-09T11:06:25.365721Z","shell.execute_reply.started":"2022-07-09T11:06:25.353449Z","shell.execute_reply":"2022-07-09T11:06:25.364388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import mean_squared_error\nprint('RMSE:', math.sqrt(mean_squared_error(ytest, predictions)))","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:06:34.592796Z","iopub.execute_input":"2022-07-09T11:06:34.593315Z","iopub.status.idle":"2022-07-09T11:06:34.600928Z","shell.execute_reply.started":"2022-07-09T11:06:34.593273Z","shell.execute_reply":"2022-07-09T11:06:34.600043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = lr.predict(x_test)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:06:41.597839Z","iopub.execute_input":"2022-07-09T11:06:41.598206Z","iopub.status.idle":"2022-07-09T11:06:41.610824Z","shell.execute_reply.started":"2022-07-09T11:06:41.598176Z","shell.execute_reply":"2022-07-09T11:06:41.609969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, confusion_matrix, classification_report","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:06:50.193424Z","iopub.execute_input":"2022-07-09T11:06:50.193858Z","iopub.status.idle":"2022-07-09T11:06:50.199931Z","shell.execute_reply.started":"2022-07-09T11:06:50.193820Z","shell.execute_reply":"2022-07-09T11:06:50.198636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Accuracy Score:\\n', accuracy_score(ytest, predictions)*100, '%')\nprint('\\nClassification Report:\\n', classification_report(ytest, predictions))\nprint('Confusion Matrix:\\n', confusion_matrix(ytest, predictions))","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:06:56.122436Z","iopub.execute_input":"2022-07-09T11:06:56.122832Z","iopub.status.idle":"2022-07-09T11:06:56.141174Z","shell.execute_reply.started":"2022-07-09T11:06:56.122799Z","shell.execute_reply":"2022-07-09T11:06:56.139936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'PassengerId': test_df['PassengerId'],\n                                 'Survived': y_pred}, columns=['PassengerId', 'Survived'])","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:07:12.652937Z","iopub.execute_input":"2022-07-09T11:07:12.653349Z","iopub.status.idle":"2022-07-09T11:07:12.661053Z","shell.execute_reply.started":"2022-07-09T11:07:12.653315Z","shell.execute_reply":"2022-07-09T11:07:12.659742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:07:24.492714Z","iopub.execute_input":"2022-07-09T11:07:24.493136Z","iopub.status.idle":"2022-07-09T11:07:24.506522Z","shell.execute_reply.started":"2022-07-09T11:07:24.493103Z","shell.execute_reply":"2022-07-09T11:07:24.505266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T11:07:37.112485Z","iopub.execute_input":"2022-07-09T11:07:37.112886Z","iopub.status.idle":"2022-07-09T11:07:37.120304Z","shell.execute_reply.started":"2022-07-09T11:07:37.112840Z","shell.execute_reply":"2022-07-09T11:07:37.119310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}