{"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":"import matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nfrom sklearn.preprocessing import MinMaxScaler\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-15T21:18:35.081385Z","iopub.execute_input":"2022-07-15T21:18:35.081859Z","iopub.status.idle":"2022-07-15T21:18:36.595714Z","shell.execute_reply.started":"2022-07-15T21:18:35.081765Z","shell.execute_reply":"2022-07-15T21:18:36.594371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# The dataset","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/titanic/train.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:36.598578Z","iopub.execute_input":"2022-07-15T21:18:36.599105Z","iopub.status.idle":"2022-07-15T21:18:36.657458Z","shell.execute_reply.started":"2022-07-15T21:18:36.599054Z","shell.execute_reply":"2022-07-15T21:18:36.656113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('../input/titanic/test.csv')\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:36.659197Z","iopub.execute_input":"2022-07-15T21:18:36.660510Z","iopub.status.idle":"2022-07-15T21:18:36.691017Z","shell.execute_reply.started":"2022-07-15T21:18:36.660447Z","shell.execute_reply":"2022-07-15T21:18:36.689832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gender_submission = pd.read_csv('../input/titanic/gender_submission.csv')\ngender_submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:36.693493Z","iopub.execute_input":"2022-07-15T21:18:36.694011Z","iopub.status.idle":"2022-07-15T21:18:36.709033Z","shell.execute_reply.started":"2022-07-15T21:18:36.693976Z","shell.execute_reply":"2022-07-15T21:18:36.707948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data info and visualization","metadata":{}},{"cell_type":"markdown","source":"### Train dataset","metadata":{}},{"cell_type":"markdown","source":"### Descriptive statistics","metadata":{}},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:36.710617Z","iopub.execute_input":"2022-07-15T21:18:36.710966Z","iopub.status.idle":"2022-07-15T21:18:36.769721Z","shell.execute_reply.started":"2022-07-15T21:18:36.710936Z","shell.execute_reply":"2022-07-15T21:18:36.768726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### General info","metadata":{}},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:36.771297Z","iopub.execute_input":"2022-07-15T21:18:36.772142Z","iopub.status.idle":"2022-07-15T21:18:36.792608Z","shell.execute_reply.started":"2022-07-15T21:18:36.772093Z","shell.execute_reply":"2022-07-15T21:18:36.791319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Missing data count","metadata":{}},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:36.794750Z","iopub.execute_input":"2022-07-15T21:18:36.795214Z","iopub.status.idle":"2022-07-15T21:18:36.807051Z","shell.execute_reply.started":"2022-07-15T21:18:36.795169Z","shell.execute_reply":"2022-07-15T21:18:36.806169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Histogram","metadata":{}},{"cell_type":"code","source":"train.hist(figsize=(15, 10));","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:36.808292Z","iopub.execute_input":"2022-07-15T21:18:36.808847Z","iopub.status.idle":"2022-07-15T21:18:37.941889Z","shell.execute_reply.started":"2022-07-15T21:18:36.808814Z","shell.execute_reply":"2022-07-15T21:18:37.940470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Correlation heatmap","metadata":{}},{"cell_type":"code","source":"sns.heatmap(train.corr(), annot=True);","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:37.943570Z","iopub.execute_input":"2022-07-15T21:18:37.944062Z","iopub.status.idle":"2022-07-15T21:18:38.455322Z","shell.execute_reply.started":"2022-07-15T21:18:37.944026Z","shell.execute_reply":"2022-07-15T21:18:38.454088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Survived passengers count","metadata":{}},{"cell_type":"code","source":"sns.countplot(train['Survived']);","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:38.459223Z","iopub.execute_input":"2022-07-15T21:18:38.459604Z","iopub.status.idle":"2022-07-15T21:18:38.620032Z","shell.execute_reply.started":"2022-07-15T21:18:38.459573Z","shell.execute_reply":"2022-07-15T21:18:38.618507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Passengers sex count","metadata":{}},{"cell_type":"code","source":"sns.countplot(train['Sex']);","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:38.622838Z","iopub.execute_input":"2022-07-15T21:18:38.625042Z","iopub.status.idle":"2022-07-15T21:18:38.748499Z","shell.execute_reply.started":"2022-07-15T21:18:38.624981Z","shell.execute_reply":"2022-07-15T21:18:38.746993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Boxplot","metadata":{}},{"cell_type":"code","source":"ax = sns.boxplot(data=train.dropna());\nax.figure.set_size_inches(10, 8)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:38.750349Z","iopub.execute_input":"2022-07-15T21:18:38.751555Z","iopub.status.idle":"2022-07-15T21:18:38.971105Z","shell.execute_reply.started":"2022-07-15T21:18:38.751488Z","shell.execute_reply":"2022-07-15T21:18:38.969764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Test dataset","metadata":{}},{"cell_type":"markdown","source":"### Descriptive statistic","metadata":{}},{"cell_type":"code","source":"test.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:38.972988Z","iopub.execute_input":"2022-07-15T21:18:38.973723Z","iopub.status.idle":"2022-07-15T21:18:39.007944Z","shell.execute_reply.started":"2022-07-15T21:18:38.973655Z","shell.execute_reply":"2022-07-15T21:18:39.006770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### General info","metadata":{}},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:39.010568Z","iopub.execute_input":"2022-07-15T21:18:39.011473Z","iopub.status.idle":"2022-07-15T21:18:39.025898Z","shell.execute_reply.started":"2022-07-15T21:18:39.011432Z","shell.execute_reply":"2022-07-15T21:18:39.024557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Missing values count","metadata":{}},{"cell_type":"code","source":"test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:39.027293Z","iopub.execute_input":"2022-07-15T21:18:39.027691Z","iopub.status.idle":"2022-07-15T21:18:39.041806Z","shell.execute_reply.started":"2022-07-15T21:18:39.027638Z","shell.execute_reply":"2022-07-15T21:18:39.040968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Correlation heat map","metadata":{}},{"cell_type":"code","source":"sns.heatmap(test.corr(), annot=True);","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:39.043014Z","iopub.execute_input":"2022-07-15T21:18:39.043691Z","iopub.status.idle":"2022-07-15T21:18:39.559640Z","shell.execute_reply.started":"2022-07-15T21:18:39.043646Z","shell.execute_reply":"2022-07-15T21:18:39.558455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Boxplot","metadata":{}},{"cell_type":"code","source":"ax = sns.boxplot(data=test.dropna())\nax.figure.set_size_inches(10, 8)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:39.561231Z","iopub.execute_input":"2022-07-15T21:18:39.562257Z","iopub.status.idle":"2022-07-15T21:18:39.766660Z","shell.execute_reply.started":"2022-07-15T21:18:39.562201Z","shell.execute_reply":"2022-07-15T21:18:39.765761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data pre-processing","metadata":{}},{"cell_type":"markdown","source":"## Adding 'Survived' column in test dataset","metadata":{}},{"cell_type":"code","source":"test = test.assign(survived=gender_submission['Survived'])\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:39.767989Z","iopub.execute_input":"2022-07-15T21:18:39.768811Z","iopub.status.idle":"2022-07-15T21:18:39.789423Z","shell.execute_reply.started":"2022-07-15T21:18:39.768769Z","shell.execute_reply":"2022-07-15T21:18:39.788093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Removing unnecessary columns in both datasets","metadata":{}},{"cell_type":"code","source":"test = test.drop(['Name', 'Ticket', 'Fare', 'Cabin'], axis='columns')\ntrain = train.drop(['Name', 'Ticket', 'Fare', 'Cabin'], axis='columns')\ntest.shape, train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:39.791300Z","iopub.execute_input":"2022-07-15T21:18:39.791973Z","iopub.status.idle":"2022-07-15T21:18:39.804058Z","shell.execute_reply.started":"2022-07-15T21:18:39.791934Z","shell.execute_reply":"2022-07-15T21:18:39.802927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:39.805393Z","iopub.execute_input":"2022-07-15T21:18:39.806565Z","iopub.status.idle":"2022-07-15T21:18:39.823119Z","shell.execute_reply.started":"2022-07-15T21:18:39.806521Z","shell.execute_reply":"2022-07-15T21:18:39.822244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:39.824378Z","iopub.execute_input":"2022-07-15T21:18:39.825161Z","iopub.status.idle":"2022-07-15T21:18:39.841612Z","shell.execute_reply.started":"2022-07-15T21:18:39.825121Z","shell.execute_reply":"2022-07-15T21:18:39.840391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Arranging the order of training dataset columns","metadata":{}},{"cell_type":"code","source":"train_survived_column = train['Survived']\ntrain = train.drop('Survived', axis='columns')","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:39.843203Z","iopub.execute_input":"2022-07-15T21:18:39.843561Z","iopub.status.idle":"2022-07-15T21:18:39.850996Z","shell.execute_reply.started":"2022-07-15T21:18:39.843529Z","shell.execute_reply":"2022-07-15T21:18:39.849786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.assign(Survived=train_survived_column)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:39.852907Z","iopub.execute_input":"2022-07-15T21:18:39.853283Z","iopub.status.idle":"2022-07-15T21:18:39.875230Z","shell.execute_reply.started":"2022-07-15T21:18:39.853249Z","shell.execute_reply":"2022-07-15T21:18:39.874078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Turning the 'Sex' column into numeric categorical data","metadata":{}},{"cell_type":"code","source":"train['Sex'] = train['Sex'].map({\n    'male': 1,\n    'female': 2\n})\n\ntest['Sex'] = test['Sex'].map({\n    'male': 1,\n    'female': 2\n})","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:39.876669Z","iopub.execute_input":"2022-07-15T21:18:39.877323Z","iopub.status.idle":"2022-07-15T21:18:39.887294Z","shell.execute_reply.started":"2022-07-15T21:18:39.877285Z","shell.execute_reply":"2022-07-15T21:18:39.885771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:39.889049Z","iopub.execute_input":"2022-07-15T21:18:39.889628Z","iopub.status.idle":"2022-07-15T21:18:39.909174Z","shell.execute_reply.started":"2022-07-15T21:18:39.889580Z","shell.execute_reply":"2022-07-15T21:18:39.908122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:39.910883Z","iopub.execute_input":"2022-07-15T21:18:39.911533Z","iopub.status.idle":"2022-07-15T21:18:39.927445Z","shell.execute_reply.started":"2022-07-15T21:18:39.911497Z","shell.execute_reply":"2022-07-15T21:18:39.926641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Turning the 'Embraked' column into numeric categorical data","metadata":{}},{"cell_type":"code","source":"train['Embarked'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:39.928944Z","iopub.execute_input":"2022-07-15T21:18:39.929528Z","iopub.status.idle":"2022-07-15T21:18:39.938270Z","shell.execute_reply.started":"2022-07-15T21:18:39.929493Z","shell.execute_reply":"2022-07-15T21:18:39.937371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Embarked'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:39.944733Z","iopub.execute_input":"2022-07-15T21:18:39.945388Z","iopub.status.idle":"2022-07-15T21:18:39.954287Z","shell.execute_reply.started":"2022-07-15T21:18:39.945350Z","shell.execute_reply":"2022-07-15T21:18:39.953084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Embarked'] = train['Embarked'].map({\n    'S': 1,\n    'C': 2,\n    'Q': 3\n})\n\ntest['Embarked'] = test['Embarked'].map({\n    'S': 1,\n    'C': 2,\n    'Q': 3\n})","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:39.956496Z","iopub.execute_input":"2022-07-15T21:18:39.957316Z","iopub.status.idle":"2022-07-15T21:18:39.967746Z","shell.execute_reply.started":"2022-07-15T21:18:39.957269Z","shell.execute_reply":"2022-07-15T21:18:39.966654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:39.969236Z","iopub.execute_input":"2022-07-15T21:18:39.969707Z","iopub.status.idle":"2022-07-15T21:18:39.987750Z","shell.execute_reply.started":"2022-07-15T21:18:39.969639Z","shell.execute_reply":"2022-07-15T21:18:39.986659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:39.989333Z","iopub.execute_input":"2022-07-15T21:18:39.989820Z","iopub.status.idle":"2022-07-15T21:18:40.007269Z","shell.execute_reply.started":"2022-07-15T21:18:39.989755Z","shell.execute_reply":"2022-07-15T21:18:40.006445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Separating target and features","metadata":{}},{"cell_type":"code","source":"y_train = train['Survived']\ntrain = train.drop('Survived', axis='columns')","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:40.008403Z","iopub.execute_input":"2022-07-15T21:18:40.009342Z","iopub.status.idle":"2022-07-15T21:18:40.021742Z","shell.execute_reply.started":"2022-07-15T21:18:40.009306Z","shell.execute_reply":"2022-07-15T21:18:40.020721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = test['survived']\ntest = test.drop('survived', axis='columns')","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:40.023368Z","iopub.execute_input":"2022-07-15T21:18:40.023889Z","iopub.status.idle":"2022-07-15T21:18:40.034758Z","shell.execute_reply.started":"2022-07-15T21:18:40.023855Z","shell.execute_reply":"2022-07-15T21:18:40.033407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape, y_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:40.036130Z","iopub.execute_input":"2022-07-15T21:18:40.036840Z","iopub.status.idle":"2022-07-15T21:18:40.052394Z","shell.execute_reply.started":"2022-07-15T21:18:40.036795Z","shell.execute_reply":"2022-07-15T21:18:40.051440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data normalization","metadata":{}},{"cell_type":"code","source":"scaler = MinMaxScaler()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:40.054175Z","iopub.execute_input":"2022-07-15T21:18:40.054566Z","iopub.status.idle":"2022-07-15T21:18:40.064101Z","shell.execute_reply.started":"2022-07-15T21:18:40.054529Z","shell.execute_reply":"2022-07-15T21:18:40.062973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.DataFrame(scaler.fit_transform(train), columns=train.columns)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:40.065761Z","iopub.execute_input":"2022-07-15T21:18:40.066342Z","iopub.status.idle":"2022-07-15T21:18:40.093863Z","shell.execute_reply.started":"2022-07-15T21:18:40.066306Z","shell.execute_reply":"2022-07-15T21:18:40.092618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.DataFrame(scaler.fit_transform(test), columns=test.columns)\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T21:18:40.095824Z","iopub.execute_input":"2022-07-15T21:18:40.096945Z","iopub.status.idle":"2022-07-15T21:18:40.120512Z","shell.execute_reply.started":"2022-07-15T21:18:40.096887Z","shell.execute_reply":"2022-07-15T21:18:40.118965Z"},"trusted":true},"execution_count":null,"outputs":[]}]}