{"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":"# data analysis and wrangling\nimport pandas as pd\nimport numpy as np\n\n# data visualization\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nplt.style.use('seaborn')\nsns.set(font_scale=2.5)\n\n# find NULL Data\nimport missingno as msno\n\n# ignore warnings\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-05T05:24:30.289530Z","iopub.execute_input":"2022-07-05T05:24:30.290320Z","iopub.status.idle":"2022-07-05T05:24:30.300862Z","shell.execute_reply.started":"2022-07-05T05:24:30.290280Z","shell.execute_reply":"2022-07-05T05:24:30.299867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dataset\ndf_train = pd.read_csv('../input/titanic/train.csv')\ndf_test = pd.read_csv('../input/titanic/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:30.339227Z","iopub.execute_input":"2022-07-05T05:24:30.339529Z","iopub.status.idle":"2022-07-05T05:24:30.356648Z","shell.execute_reply.started":"2022-07-05T05:24:30.339495Z","shell.execute_reply":"2022-07-05T05:24:30.355983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:30.415568Z","iopub.execute_input":"2022-07-05T05:24:30.415866Z","iopub.status.idle":"2022-07-05T05:24:30.433269Z","shell.execute_reply.started":"2022-07-05T05:24:30.415835Z","shell.execute_reply":"2022-07-05T05:24:30.432275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:30.467456Z","iopub.execute_input":"2022-07-05T05:24:30.467762Z","iopub.status.idle":"2022-07-05T05:24:30.502109Z","shell.execute_reply.started":"2022-07-05T05:24:30.467731Z","shell.execute_reply":"2022-07-05T05:24:30.501197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:30.515365Z","iopub.execute_input":"2022-07-05T05:24:30.515836Z","iopub.status.idle":"2022-07-05T05:24:30.543529Z","shell.execute_reply.started":"2022-07-05T05:24:30.515787Z","shell.execute_reply":"2022-07-05T05:24:30.542550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# NaN data\nfor col in df_train.columns:\n    msg = 'columns: {:>10}\\t Percent of NaN value: {:.2f}%'.format(col, 100 * (df_train[col].isnull().sum() / df_train[col].shape[0]))\n    print(msg)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:30.561613Z","iopub.execute_input":"2022-07-05T05:24:30.561917Z","iopub.status.idle":"2022-07-05T05:24:30.573984Z","shell.execute_reply.started":"2022-07-05T05:24:30.561887Z","shell.execute_reply":"2022-07-05T05:24:30.572928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# NaN data\nfor col in df_test.columns:\n    msg = 'columns: {:>10}\\t Percent of NaN value: {:.2f}%'.format(col, 100 * (df_test[col].isnull().sum() / df_test[col].shape[0]))\n    print(msg)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:30.605406Z","iopub.execute_input":"2022-07-05T05:24:30.605687Z","iopub.status.idle":"2022-07-05T05:24:30.617569Z","shell.execute_reply.started":"2022-07-05T05:24:30.605658Z","shell.execute_reply":"2022-07-05T05:24:30.617034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"msno.matrix(df=df_train.iloc[:, :], figsize=(8, 8), color=(0.8, 0.5, 0.2))","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:30.652378Z","iopub.execute_input":"2022-07-05T05:24:30.652688Z","iopub.status.idle":"2022-07-05T05:24:31.092011Z","shell.execute_reply.started":"2022-07-05T05:24:30.652654Z","shell.execute_reply":"2022-07-05T05:24:31.091034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"msno.bar(df=df_train.iloc[:, :], figsize=(8, 8), color=(0.8, 0.5, 0.2))","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:31.094392Z","iopub.execute_input":"2022-07-05T05:24:31.094734Z","iopub.status.idle":"2022-07-05T05:24:32.170043Z","shell.execute_reply.started":"2022-07-05T05:24:31.094691Z","shell.execute_reply":"2022-07-05T05:24:32.168994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(1,2,figsize=(18,8))\n\ndf_train['Survived'].value_counts().plot.pie(explode=[0,0.1], autopct='%1.1f%%',ax=ax[0], shadow=True)\nax[0].set_title('Pie plot - Survivrd')\nax[0].set_ylabel('')\nsns.countplot('Survived', data=df_train, ax=ax[1])\nax[1].set_title('Count plot - Survived')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:32.171978Z","iopub.execute_input":"2022-07-05T05:24:32.172594Z","iopub.status.idle":"2022-07-05T05:24:32.495899Z","shell.execute_reply.started":"2022-07-05T05:24:32.172541Z","shell.execute_reply":"2022-07-05T05:24:32.495202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### EDA란?\n수집한 데이터가 들어왔을 때, 이를 다양한 각도에서 관찰하고 이해하는 과정이다. 한마디로 데이터를 분석하기 전에 그래프나 통계적인 방법으로 자료를 직관적으로 바라보는 과정이다.","metadata":{}},{"cell_type":"markdown","source":"### Pclass\n","metadata":{}},{"cell_type":"code","source":"df_train[['Pclass', 'Survived']].groupby(['Pclass'], as_index=True).count()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:32.498007Z","iopub.execute_input":"2022-07-05T05:24:32.498374Z","iopub.status.idle":"2022-07-05T05:24:32.509029Z","shell.execute_reply.started":"2022-07-05T05:24:32.498345Z","shell.execute_reply":"2022-07-05T05:24:32.508321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.crosstab(df_train['Pclass'], df_train['Survived'], margins=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:32.510066Z","iopub.execute_input":"2022-07-05T05:24:32.510393Z","iopub.status.idle":"2022-07-05T05:24:32.562591Z","shell.execute_reply.started":"2022-07-05T05:24:32.510363Z","shell.execute_reply":"2022-07-05T05:24:32.561669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[['Pclass', 'Survived']].groupby(['Pclass'], as_index=True).mean().sort_values(by='Survived', ascending=False).plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:32.563767Z","iopub.execute_input":"2022-07-05T05:24:32.564033Z","iopub.status.idle":"2022-07-05T05:24:32.802204Z","shell.execute_reply.started":"2022-07-05T05:24:32.564003Z","shell.execute_reply":"2022-07-05T05:24:32.801262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_position = 1.04\nf, ax = plt.subplots(1, 2, figsize=(18, 8))\ndf_train['Pclass'].value_counts().plot.bar(color=['#CD7F32', '#FFDF00', '#D3D3D3'], ax=ax[0])\nax[0].set_title('Number of passengers By Pclass', y=y_position)\nax[0].set_ylabel('Count')\nsns.countplot('Pclass', hue='Survived', data=df_train, ax=ax[1])\nax[1].set_title('Pclass: Survived vs Dead', y=y_position)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:32.803422Z","iopub.execute_input":"2022-07-05T05:24:32.803732Z","iopub.status.idle":"2022-07-05T05:24:33.105226Z","shell.execute_reply.started":"2022-07-05T05:24:32.803699Z","shell.execute_reply":"2022-07-05T05:24:33.103952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Sex","metadata":{}},{"cell_type":"code","source":"f, ax = plt.subplots(1, 2, figsize=(18, 8))\ndf_train[['Sex', 'Survived']].groupby(['Sex'], as_index=True).mean().plot.bar(ax=ax[0])\nax[0].set_title('Survived vs Sex')\nsns.countplot('Sex', hue='Survived', data=df_train, ax=ax[1])\nax[1].set_title('Sex: Survivied vs Dead')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:33.107110Z","iopub.execute_input":"2022-07-05T05:24:33.107435Z","iopub.status.idle":"2022-07-05T05:24:33.480975Z","shell.execute_reply.started":"2022-07-05T05:24:33.107387Z","shell.execute_reply":"2022-07-05T05:24:33.480110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.crosstab(df_train['Sex'], df_train['Survived'], margins=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:33.482468Z","iopub.execute_input":"2022-07-05T05:24:33.482715Z","iopub.status.idle":"2022-07-05T05:24:33.526759Z","shell.execute_reply.started":"2022-07-05T05:24:33.482687Z","shell.execute_reply":"2022-07-05T05:24:33.525856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Sex and Pclass","metadata":{}},{"cell_type":"code","source":"sns.factorplot('Pclass', 'Survived', hue='Sex', data=df_train, size=6, aspect=1.5)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:33.531150Z","iopub.execute_input":"2022-07-05T05:24:33.531443Z","iopub.status.idle":"2022-07-05T05:24:34.282741Z","shell.execute_reply.started":"2022-07-05T05:24:33.531411Z","shell.execute_reply":"2022-07-05T05:24:34.281715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.factorplot('Sex', 'Survived', hue='Pclass',data=df_train, saturation=5, size=6, aspect=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:34.284124Z","iopub.execute_input":"2022-07-05T05:24:34.284356Z","iopub.status.idle":"2022-07-05T05:24:34.978556Z","shell.execute_reply.started":"2022-07-05T05:24:34.284327Z","shell.execute_reply":"2022-07-05T05:24:34.977857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Age\n","metadata":{}},{"cell_type":"code","source":"print('제일 나이 많은 탑승객 : {:.1f} years'.format(df_train['Age'].max()))\nprint('제일 나이 적은 탑승객 : {:.1f} years'.format(df_train['Age'].min()))\nprint('탑승객 평균 나이 : {:.1f} years'.format(df_train['Age'].mean()))","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:34.979612Z","iopub.execute_input":"2022-07-05T05:24:34.980238Z","iopub.status.idle":"2022-07-05T05:24:34.987407Z","shell.execute_reply.started":"2022-07-05T05:24:34.980199Z","shell.execute_reply":"2022-07-05T05:24:34.986303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 분포 그래프\nfig, ax = plt.subplots(1, 1, figsize=(9, 5))\nsns.kdeplot(df_train[df_train['Survived'] == 1]['Age'], ax=ax)\nsns.kdeplot(df_train[df_train['Survived'] == 0]['Age'], ax=ax)\nplt.legend(['Survived == 1', 'Survived == 0'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:34.989476Z","iopub.execute_input":"2022-07-05T05:24:34.989826Z","iopub.status.idle":"2022-07-05T05:24:35.247701Z","shell.execute_reply.started":"2022-07-05T05:24:34.989762Z","shell.execute_reply":"2022-07-05T05:24:35.246913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 분포 그래프\nfig, ax = plt.subplots(1, 1, figsize=(9, 5))\nsns.kdeplot(df_train[df_train['Survived'] == 1]['Pclass'], ax=ax)\nsns.kdeplot(df_train[df_train['Survived'] == 0]['Pclass'], ax=ax)\nplt.legend(['Survived == 1', 'Survived == 0'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:35.249255Z","iopub.execute_input":"2022-07-05T05:24:35.249740Z","iopub.status.idle":"2022-07-05T05:24:35.470218Z","shell.execute_reply.started":"2022-07-05T05:24:35.249695Z","shell.execute_reply":"2022-07-05T05:24:35.469367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 분포 그래프\nfig, ax = plt.subplots(1, 1, figsize=(9, 5))\nsns.kdeplot(df_train[(df_train['Survived'] == 1) & (df_train['Pclass'] == 1)]['Age'], ax=ax)\nsns.kdeplot(df_train[(df_train['Survived'] == 0) & (df_train['Pclass'] == 1)]['Age'], ax=ax)\nplt.legend(['Survived == 1', 'Survived == 0'])\nplt.title('1st class')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:35.471697Z","iopub.execute_input":"2022-07-05T05:24:35.472189Z","iopub.status.idle":"2022-07-05T05:24:35.723588Z","shell.execute_reply.started":"2022-07-05T05:24:35.472141Z","shell.execute_reply":"2022-07-05T05:24:35.722745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 분포 그래프\nfig, ax = plt.subplots(1, 1, figsize=(9, 5))\nsns.kdeplot(df_train[(df_train['Survived'] == 1) & (df_train['Pclass'] == 2)]['Age'], ax=ax)\nsns.kdeplot(df_train[(df_train['Survived'] == 0) & (df_train['Pclass'] == 2)]['Age'], ax=ax)\nplt.legend(['Survived == 1', 'Survived == 0'])\nplt.title('2nd class')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:35.725220Z","iopub.execute_input":"2022-07-05T05:24:35.725668Z","iopub.status.idle":"2022-07-05T05:24:35.991723Z","shell.execute_reply.started":"2022-07-05T05:24:35.725625Z","shell.execute_reply":"2022-07-05T05:24:35.990799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 분포 그래프\nfig, ax = plt.subplots(1, 1, figsize=(9, 5))\nsns.kdeplot(df_train[(df_train['Survived'] == 1) & (df_train['Pclass'] == 3)]['Age'], ax=ax)\nsns.kdeplot(df_train[(df_train['Survived'] == 0) & (df_train['Pclass'] == 3)]['Age'], ax=ax)\nplt.legend(['Survived == 1', 'Survived == 0'])\nplt.title('3rd class')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:35.993014Z","iopub.execute_input":"2022-07-05T05:24:35.993259Z","iopub.status.idle":"2022-07-05T05:24:36.410972Z","shell.execute_reply.started":"2022-07-05T05:24:35.993228Z","shell.execute_reply":"2022-07-05T05:24:36.405683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"change_age_range_survival_ratio = []\n\nfor i in range(1, 80):\n    change_age_range_survival_ratio.append(df_train[df_train['Age'] < i]['Survived'].sum() / len(df_train[df_train['Age'] < i]['Survived']))\n    \nplt.figure(figsize=(7,7))\nplt.plot(change_age_range_survival_ratio)\nplt.title('Survival rate change depending on range of Age', y=1.02)\nplt.ylabel('Survived rate')\nplt.xlabel('Range of Age')\nplt.show ","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.411973Z","iopub.status.idle":"2022-07-05T05:24:36.412293Z","shell.execute_reply.started":"2022-07-05T05:24:36.412127Z","shell.execute_reply":"2022-07-05T05:24:36.412145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Pclass, Sex, Age","metadata":{}},{"cell_type":"code","source":"f, ax = plt.subplots(1, 2, figsize=(18, 8))\nsns.violinplot('Pclass', 'Age', hue='Survived', data=df_train, scale='count', split=True, ax=ax[0])\nax[0].set_title('Pclass and Age vs Survived')\nax[0].set_yticks(range(0, 110, 10))\n\nsns.violinplot('Sex', 'Age', hue='Survived', data=df_train, scale='count', split=True, ax=ax[1])\nax[1].set_title('Pclass and Age vs Survived')\nax[1].set_yticks(range(0, 110, 10))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.413588Z","iopub.status.idle":"2022-07-05T05:24:36.413955Z","shell.execute_reply.started":"2022-07-05T05:24:36.413741Z","shell.execute_reply":"2022-07-05T05:24:36.413765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Embarked","metadata":{}},{"cell_type":"code","source":"f, ax = plt.subplots(1, 1, figsize=(7, 7))\ndf_train[['Embarked', 'Survived']].groupby(['Embarked'], as_index=True).mean().sort_values(by='Survived', ascending=False).plot.bar(ax=ax)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.414813Z","iopub.status.idle":"2022-07-05T05:24:36.415144Z","shell.execute_reply.started":"2022-07-05T05:24:36.414961Z","shell.execute_reply":"2022-07-05T05:24:36.414984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(2, 2, figsize=(20, 15))\nsns.countplot('Embarked', data=df_train, ax=ax[0, 0])\nax[0, 0].set_title('(1) No. Of Passengers Boared')\n\nsns.countplot('Embarked', hue='Sex', data=df_train, ax=ax[0, 1])\nax[0, 1].set_title('(2) Male-Female split for embarked')\n\nsns.countplot('Embarked', hue='Survived', data=df_train, ax=ax[1, 0])\nax[1, 0].set_title('(3) Embarked vs Survived')\n\nsns.countplot('Embarked', hue='Pclass', data=df_train, ax=ax[1, 1])\nax[1, 1].set_title('(4) Embarked vs Pclass')\n\nplt.subplots_adjust(wspace=0.2, hspace=0.5)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.416370Z","iopub.status.idle":"2022-07-05T05:24:36.416675Z","shell.execute_reply.started":"2022-07-05T05:24:36.416512Z","shell.execute_reply":"2022-07-05T05:24:36.416529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Family - SibSp + Parch","metadata":{}},{"cell_type":"code","source":"df_train['FamilySize'] = df_train['SibSp'] + df_train['Parch'] + 1\ndf_test['FamilySize'] = df_test['SibSp'] + df_test['Parch'] + 1","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.417944Z","iopub.status.idle":"2022-07-05T05:24:36.418252Z","shell.execute_reply.started":"2022-07-05T05:24:36.418095Z","shell.execute_reply":"2022-07-05T05:24:36.418111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Maxium size of Family: ', df_train['FamilySize'].max())\nprint('Minium size of Family: ', df_train['FamilySize'].min())","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.419264Z","iopub.status.idle":"2022-07-05T05:24:36.419599Z","shell.execute_reply.started":"2022-07-05T05:24:36.419433Z","shell.execute_reply":"2022-07-05T05:24:36.419451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(1, 3, figsize=(40, 10))\nsns.countplot('FamilySize', data=df_train, ax=ax[0])\nax[0].set_title('(1) No. Of Passenger Boarded', y=1.02)\n\nsns.countplot('FamilySize', hue='Survived', data=df_train, ax=ax[1])\nax[1].set_title('(2) Survived countplot depending on FamilySize', y=1.02)\n\ndf_train[['FamilySize', 'Survived']].groupby(['FamilySize'], as_index=True).mean().sort_values(by='Survived', ascending=False).plot.bar(ax=ax[2])\nax[2].set_title('(3) Survived rate depenfing on FamilySize', y=1.02)\n\nplt.subplots_adjust(wspace=0.2, hspace=0.5)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.420622Z","iopub.status.idle":"2022-07-05T05:24:36.420980Z","shell.execute_reply.started":"2022-07-05T05:24:36.420761Z","shell.execute_reply":"2022-07-05T05:24:36.420792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Fare","metadata":{}},{"cell_type":"code","source":"f, ax = plt.subplots(1, 1, figsize=(8, 8))\ng = sns.distplot(df_train['Fare'], color='b', label='Skewness: {:2f}'.format(df_train['Fare'].skew()), ax=ax)\ng.legend(loc='best')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.421745Z","iopub.status.idle":"2022-07-05T05:24:36.422079Z","shell.execute_reply.started":"2022-07-05T05:24:36.421922Z","shell.execute_reply":"2022-07-05T05:24:36.421938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Fare'] = df_train['Fare'].map(lambda i: np.log(i) if i>0 else 0)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.422938Z","iopub.status.idle":"2022-07-05T05:24:36.423239Z","shell.execute_reply.started":"2022-07-05T05:24:36.423079Z","shell.execute_reply":"2022-07-05T05:24:36.423095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(1, 1, figsize=(8, 8))\ng = sns.distplot(df_train['Fare'], color='b', label='Skewness: {:2f}'.format(df_train['Fare'].skew()), ax=ax)\ng = g.legend(loc='best')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.424513Z","iopub.status.idle":"2022-07-05T05:24:36.424861Z","shell.execute_reply.started":"2022-07-05T05:24:36.424658Z","shell.execute_reply":"2022-07-05T05:24:36.424674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Ticket'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.425832Z","iopub.status.idle":"2022-07-05T05:24:36.426133Z","shell.execute_reply.started":"2022-07-05T05:24:36.425971Z","shell.execute_reply":"2022-07-05T05:24:36.425987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Feature engineering\nThe process of selecting, manipulating, and transforming raw data into features. <br>\nCreate and select columns to machine learning.","metadata":{"execution":{"iopub.status.busy":"2022-06-27T11:15:51.487663Z","iopub.execute_input":"2022-06-27T11:15:51.488039Z","iopub.status.idle":"2022-06-27T11:15:51.495735Z","shell.execute_reply.started":"2022-06-27T11:15:51.487999Z","shell.execute_reply":"2022-06-27T11:15:51.494387Z"}}},{"cell_type":"code","source":"df_train['Inital'] = df_train['Name'].str.extract('([A-Za-z]+)\\.')\ndf_test['Inital'] = df_test['Name'].str.extract('([A-Za-z]+)\\.')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.428805Z","iopub.status.idle":"2022-07-05T05:24:36.429505Z","shell.execute_reply.started":"2022-07-05T05:24:36.429311Z","shell.execute_reply":"2022-07-05T05:24:36.429332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.430886Z","iopub.status.idle":"2022-07-05T05:24:36.431369Z","shell.execute_reply.started":"2022-07-05T05:24:36.431116Z","shell.execute_reply":"2022-07-05T05:24:36.431142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.crosstab(df_train['Inital'], df_train['Sex']).T.style.background_gradient(cmap='summer_r')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.432753Z","iopub.status.idle":"2022-07-05T05:24:36.433256Z","shell.execute_reply.started":"2022-07-05T05:24:36.432995Z","shell.execute_reply":"2022-07-05T05:24:36.433029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Inital'].replace(['Capt', 'Col', 'Countess', 'Don', 'Dr', 'Jonkheer', 'Lady', 'Major', 'Master', 'Miss', 'Mlle', 'Mme', 'Mr', 'Mrs', 'Ms', 'Rev', 'Sir'],\n                          ['Mr', 'Other', 'Mrs', 'Mr', 'Mr', 'Other', 'Mrs', 'Mr', 'Mr', 'Miss', 'Miss', 'Miss', 'Mr', 'Mrs', 'Miss', 'Other', 'Mr'], inplace=True)\n\ndf_test['Inital'].replace(['Capt', 'Col', 'Countess', 'Don', 'Dr', 'Jonkheer', 'Lady', 'Major', 'Master', 'Miss', 'Mlle', 'Mme', 'Mr', 'Mrs', 'Ms', 'Rev', 'Sir'],\n                          ['Mr', 'Other', 'Mrs', 'Mr', 'Mr', 'Other', 'Mrs', 'Mr', 'Mr', 'Miss', 'Miss', 'Miss', 'Mr', 'Mrs', 'Miss', 'Other', 'Mr'], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.434429Z","iopub.status.idle":"2022-07-05T05:24:36.435078Z","shell.execute_reply.started":"2022-07-05T05:24:36.434752Z","shell.execute_reply":"2022-07-05T05:24:36.434798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.groupby('Inital').mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.436501Z","iopub.status.idle":"2022-07-05T05:24:36.438176Z","shell.execute_reply.started":"2022-07-05T05:24:36.437843Z","shell.execute_reply":"2022-07-05T05:24:36.437883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.groupby('Inital')['Survived'].mean().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.439975Z","iopub.status.idle":"2022-07-05T05:24:36.440306Z","shell.execute_reply.started":"2022-07-05T05:24:36.440140Z","shell.execute_reply":"2022-07-05T05:24:36.440158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all = pd.concat([df_train, df_test])\ndf_all","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.441613Z","iopub.status.idle":"2022-07-05T05:24:36.442146Z","shell.execute_reply.started":"2022-07-05T05:24:36.441922Z","shell.execute_reply":"2022-07-05T05:24:36.441949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.443171Z","iopub.status.idle":"2022-07-05T05:24:36.443483Z","shell.execute_reply.started":"2022-07-05T05:24:36.443323Z","shell.execute_reply":"2022-07-05T05:24:36.443340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all.groupby('Inital').mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.444904Z","iopub.status.idle":"2022-07-05T05:24:36.445227Z","shell.execute_reply.started":"2022-07-05T05:24:36.445063Z","shell.execute_reply":"2022-07-05T05:24:36.445079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.loc[(df_train['Age'].isnull()) & (df_train['Inital'] == 'Mr'), 'Age'] = 30\ndf_train.loc[(df_train['Age'].isnull()) & (df_train['Inital'] == 'Mrs'), 'Age'] = 37\ndf_train.loc[(df_train['Age'].isnull()) & (df_train['Inital'] == 'Miss'), 'Age'] = 22\ndf_train.loc[(df_train['Age'].isnull()) & (df_train['Inital'] == 'Other'), 'Age'] = 45\n\ndf_test.loc[(df_test['Age'].isnull()) & (df_test['Inital'] == 'Mr'), 'Age'] = 30\ndf_test.loc[(df_test['Age'].isnull()) & (df_test['Inital'] == 'Mrs'), 'Age'] = 37\ndf_test.loc[(df_test['Age'].isnull()) & (df_test['Inital'] == 'Miss'), 'Age'] = 22\ndf_test.loc[(df_test['Age'].isnull()) & (df_test['Inital'] == 'Other'), 'Age'] = 45","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.446712Z","iopub.status.idle":"2022-07-05T05:24:36.447088Z","shell.execute_reply.started":"2022-07-05T05:24:36.446910Z","shell.execute_reply":"2022-07-05T05:24:36.446928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Age'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.448226Z","iopub.status.idle":"2022-07-05T05:24:36.448539Z","shell.execute_reply.started":"2022-07-05T05:24:36.448379Z","shell.execute_reply":"2022-07-05T05:24:36.448395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['Age'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.449373Z","iopub.status.idle":"2022-07-05T05:24:36.449694Z","shell.execute_reply.started":"2022-07-05T05:24:36.449515Z","shell.execute_reply":"2022-07-05T05:24:36.449532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Embarked'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.450607Z","iopub.status.idle":"2022-07-05T05:24:36.450965Z","shell.execute_reply.started":"2022-07-05T05:24:36.450761Z","shell.execute_reply":"2022-07-05T05:24:36.450796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.451954Z","iopub.status.idle":"2022-07-05T05:24:36.452260Z","shell.execute_reply.started":"2022-07-05T05:24:36.452100Z","shell.execute_reply":"2022-07-05T05:24:36.452116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Embarked'].fillna('S', inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.453006Z","iopub.status.idle":"2022-07-05T05:24:36.453298Z","shell.execute_reply.started":"2022-07-05T05:24:36.453142Z","shell.execute_reply":"2022-07-05T05:24:36.453158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Embarked'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.454151Z","iopub.status.idle":"2022-07-05T05:24:36.454452Z","shell.execute_reply.started":"2022-07-05T05:24:36.454292Z","shell.execute_reply":"2022-07-05T05:24:36.454308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Age_cat'] = 0","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.455915Z","iopub.status.idle":"2022-07-05T05:24:36.456229Z","shell.execute_reply.started":"2022-07-05T05:24:36.456069Z","shell.execute_reply":"2022-07-05T05:24:36.456087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.457603Z","iopub.status.idle":"2022-07-05T05:24:36.457966Z","shell.execute_reply.started":"2022-07-05T05:24:36.457754Z","shell.execute_reply":"2022-07-05T05:24:36.457783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.loc[df_train['Age'] < 10, 'Age_cat'] = 0\ndf_train.loc[(10 <= df_train['Age']) & (df_train['Age'] < 20), 'Age_cat'] = 1\ndf_train.loc[(20 <= df_train['Age']) & (df_train['Age'] < 30), 'Age_cat'] = 2\ndf_train.loc[(30 <= df_train['Age']) & (df_train['Age'] < 40), 'Age_cat'] = 3\ndf_train.loc[(40 <= df_train['Age']) & (df_train['Age'] < 50), 'Age_cat'] = 4\ndf_train.loc[(50 <= df_train['Age']) & (df_train['Age'] < 60), 'Age_cat'] = 5\ndf_train.loc[(60 <= df_train['Age']) & (df_train['Age'] < 70), 'Age_cat'] = 6\ndf_train.loc[70 <= df_train['Age'], 'Age_cat'] = 7","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.458972Z","iopub.status.idle":"2022-07-05T05:24:36.459272Z","shell.execute_reply.started":"2022-07-05T05:24:36.459113Z","shell.execute_reply":"2022-07-05T05:24:36.459129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.loc[df_test['Age'] < 10, 'Age_cat'] = 0\ndf_test.loc[(10 <= df_test['Age']) & (df_test['Age'] < 20), 'Age_cat'] = 1\ndf_test.loc[(20 <= df_test['Age']) & (df_test['Age'] < 30), 'Age_cat'] = 2\ndf_test.loc[(30 <= df_test['Age']) & (df_test['Age'] < 40), 'Age_cat'] = 3\ndf_test.loc[(40 <= df_test['Age']) & (df_test['Age'] < 50), 'Age_cat'] = 4\ndf_test.loc[(50 <= df_test['Age']) & (df_test['Age'] < 60), 'Age_cat'] = 5\ndf_test.loc[(60 <= df_test['Age']) & (df_test['Age'] < 70), 'Age_cat'] = 6\ndf_test.loc[70 <= df_test['Age'], 'Age_cat'] = 7","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.460264Z","iopub.status.idle":"2022-07-05T05:24:36.460857Z","shell.execute_reply.started":"2022-07-05T05:24:36.460635Z","shell.execute_reply":"2022-07-05T05:24:36.460656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.461615Z","iopub.status.idle":"2022-07-05T05:24:36.461923Z","shell.execute_reply.started":"2022-07-05T05:24:36.461750Z","shell.execute_reply":"2022-07-05T05:24:36.461766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def category_age(x):\n    if x < 10:\n        return 0\n    elif x < 20:\n        return 1\n    elif x < 30:\n        return 2\n    elif x < 40:\n        return 3\n    elif x < 50:\n        return 4\n    elif x < 60:\n        return 5\n    elif x < 70:\n        return 6\n    else:\n        return 7","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.462880Z","iopub.status.idle":"2022-07-05T05:24:36.463170Z","shell.execute_reply.started":"2022-07-05T05:24:36.463017Z","shell.execute_reply":"2022-07-05T05:24:36.463032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Age_cat2'] = df_train['Age'].apply(category_age)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.464234Z","iopub.status.idle":"2022-07-05T05:24:36.464548Z","shell.execute_reply.started":"2022-07-05T05:24:36.464386Z","shell.execute_reply":"2022-07-05T05:24:36.464403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(df_train['Age_cat'] == df_train['Age_cat2']).all()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.466970Z","iopub.status.idle":"2022-07-05T05:24:36.467288Z","shell.execute_reply.started":"2022-07-05T05:24:36.467124Z","shell.execute_reply":"2022-07-05T05:24:36.467141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.drop(['Age', 'Age_cat2'], axis=1, inplace=True)\ndf_test.drop(['Age'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.468734Z","iopub.status.idle":"2022-07-05T05:24:36.469126Z","shell.execute_reply.started":"2022-07-05T05:24:36.468935Z","shell.execute_reply":"2022-07-05T05:24:36.468959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Inital'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.470262Z","iopub.status.idle":"2022-07-05T05:24:36.470758Z","shell.execute_reply.started":"2022-07-05T05:24:36.470494Z","shell.execute_reply":"2022-07-05T05:24:36.470524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Inital'] = df_train['Inital'].map({'Mr':0, 'Mrs':1, 'Miss':2, 'Other':3})\ndf_test['Inital'] = df_test['Inital'].map({'Mr':0, 'Mrs':1, 'Miss':2, 'Other':3})","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.474508Z","iopub.status.idle":"2022-07-05T05:24:36.475084Z","shell.execute_reply.started":"2022-07-05T05:24:36.474798Z","shell.execute_reply":"2022-07-05T05:24:36.474827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Embarked'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.476874Z","iopub.status.idle":"2022-07-05T05:24:36.477375Z","shell.execute_reply.started":"2022-07-05T05:24:36.477110Z","shell.execute_reply":"2022-07-05T05:24:36.477137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Embarked'] = df_train['Embarked'].map({'C':0, 'Q':1, 'S':2})\ndf_test['Embarked'] = df_test['Embarked'].map({'C':0, 'Q':1, 'S':2})","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.479517Z","iopub.status.idle":"2022-07-05T05:24:36.479902Z","shell.execute_reply.started":"2022-07-05T05:24:36.479701Z","shell.execute_reply":"2022-07-05T05:24:36.479718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.481135Z","iopub.status.idle":"2022-07-05T05:24:36.481466Z","shell.execute_reply.started":"2022-07-05T05:24:36.481295Z","shell.execute_reply":"2022-07-05T05:24:36.481312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Embarked'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.482548Z","iopub.status.idle":"2022-07-05T05:24:36.482904Z","shell.execute_reply.started":"2022-07-05T05:24:36.482696Z","shell.execute_reply":"2022-07-05T05:24:36.482712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Sex'] = df_train['Sex'].map({'female':0, 'male':1})\ndf_test['Sex'] = df_test['Sex'].map({'female':0, 'male':1})","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.483790Z","iopub.status.idle":"2022-07-05T05:24:36.484094Z","shell.execute_reply.started":"2022-07-05T05:24:36.483935Z","shell.execute_reply":"2022-07-05T05:24:36.483951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.485273Z","iopub.status.idle":"2022-07-05T05:24:36.485574Z","shell.execute_reply.started":"2022-07-05T05:24:36.485415Z","shell.execute_reply":"2022-07-05T05:24:36.485431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"heatmap_data = df_train[['Survived', 'Pclass', 'Sex', 'Fare', 'Embarked', 'FamilySize', 'Inital', 'Age_cat']]","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.486854Z","iopub.status.idle":"2022-07-05T05:24:36.487166Z","shell.execute_reply.started":"2022-07-05T05:24:36.487005Z","shell.execute_reply":"2022-07-05T05:24:36.487021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colormap = plt.cm.Blues\nplt.figure(figsize=(14, 10))\nplt.title('Pearson Correalation of Features', y=1.05, size=15)\nsns.heatmap(heatmap_data.astype(float).corr(), linewidths=0.1, vmax=1.0,\n           square=True, cmap=colormap, linecolor='white', annot=True, annot_kws={'size':16}, fmt='.2f')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.488229Z","iopub.status.idle":"2022-07-05T05:24:36.488587Z","shell.execute_reply.started":"2022-07-05T05:24:36.488384Z","shell.execute_reply":"2022-07-05T05:24:36.488401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### One-hot Encoding","metadata":{}},{"cell_type":"code","source":"df_train = pd.get_dummies(df_train, columns=['Inital'], prefix='Inital')\ndf_test = pd.get_dummies(df_test, columns=['Inital'], prefix='Inital')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.489912Z","iopub.status.idle":"2022-07-05T05:24:36.490236Z","shell.execute_reply.started":"2022-07-05T05:24:36.490070Z","shell.execute_reply":"2022-07-05T05:24:36.490086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.get_dummies(df_train, columns=['Embarked'], prefix='Embarked')\ndf_test = pd.get_dummies(df_test, columns=['Embarked'], prefix='Embarked')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.491407Z","iopub.status.idle":"2022-07-05T05:24:36.491740Z","shell.execute_reply.started":"2022-07-05T05:24:36.491566Z","shell.execute_reply":"2022-07-05T05:24:36.491584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Delete needless Data","metadata":{}},{"cell_type":"code","source":"df_train.drop(['PassengerId', 'Name', 'SibSp', 'Parch', 'Ticket', 'Cabin'], axis=1, inplace=True)\ndf_test.drop(['PassengerId', 'Name', 'SibSp', 'Parch', 'Ticket', 'Cabin'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.492573Z","iopub.status.idle":"2022-07-05T05:24:36.492921Z","shell.execute_reply.started":"2022-07-05T05:24:36.492713Z","shell.execute_reply":"2022-07-05T05:24:36.492728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.493736Z","iopub.status.idle":"2022-07-05T05:24:36.494053Z","shell.execute_reply.started":"2022-07-05T05:24:36.493890Z","shell.execute_reply":"2022-07-05T05:24:36.493906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.495022Z","iopub.status.idle":"2022-07-05T05:24:36.495315Z","shell.execute_reply.started":"2022-07-05T05:24:36.495162Z","shell.execute_reply":"2022-07-05T05:24:36.495177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.drop(['Inital_0', 'Inital_1', 'Inital_2', 'Inital_3'], axis=1, inplace=True)\ndf_test.drop(['Inital_0.0', 'Inital_1.0', 'Inital_2.0', 'Inital_3.0'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.496197Z","iopub.status.idle":"2022-07-05T05:24:36.496489Z","shell.execute_reply.started":"2022-07-05T05:24:36.496331Z","shell.execute_reply":"2022-07-05T05:24:36.496347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Model Development","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nfrom sklearn import metrics\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.497414Z","iopub.status.idle":"2022-07-05T05:24:36.497707Z","shell.execute_reply.started":"2022-07-05T05:24:36.497551Z","shell.execute_reply":"2022-07-05T05:24:36.497566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['Fare'].fillna(round(df_test['Fare'].sum()/len(df_test), 4), inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.499177Z","iopub.status.idle":"2022-07-05T05:24:36.499894Z","shell.execute_reply.started":"2022-07-05T05:24:36.499681Z","shell.execute_reply":"2022-07-05T05:24:36.499702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = df_train.drop('Survived', axis=1).values\ntarget_label = df_train['Survived'].values\nX_test = df_test.values","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.501024Z","iopub.status.idle":"2022-07-05T05:24:36.501319Z","shell.execute_reply.started":"2022-07-05T05:24:36.501166Z","shell.execute_reply":"2022-07-05T05:24:36.501182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_tr, X_vid, y_tr, y_vid = train_test_split(X_train, target_label, test_size=0.3, random_state=2018)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.502094Z","iopub.status.idle":"2022-07-05T05:24:36.502391Z","shell.execute_reply.started":"2022-07-05T05:24:36.502233Z","shell.execute_reply":"2022-07-05T05:24:36.502249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = RandomForestClassifier()\nmodel.fit(X_tr, y_tr)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.503293Z","iopub.status.idle":"2022-07-05T05:24:36.503595Z","shell.execute_reply.started":"2022-07-05T05:24:36.503437Z","shell.execute_reply":"2022-07-05T05:24:36.503453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(X_vid)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.504792Z","iopub.status.idle":"2022-07-05T05:24:36.505123Z","shell.execute_reply.started":"2022-07-05T05:24:36.504951Z","shell.execute_reply":"2022-07-05T05:24:36.504973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('총 {}명 중 {:.2f}% 정확도로 생존 맞춤'.format(y_vid.shape[0], 100*metrics.accuracy_score(prediction, y_vid)))","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.506345Z","iopub.status.idle":"2022-07-05T05:24:36.506683Z","shell.execute_reply.started":"2022-07-05T05:24:36.506505Z","shell.execute_reply":"2022-07-05T05:24:36.506529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pandas import Series","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.507616Z","iopub.status.idle":"2022-07-05T05:24:36.507959Z","shell.execute_reply.started":"2022-07-05T05:24:36.507763Z","shell.execute_reply":"2022-07-05T05:24:36.507803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.figure(figsize=(8, 8))\n# Series_feat_imp.sort_values(ascending=True).plot.barh()\n# plt.xlabel('Feature importance')\n# plt.ylabel('Feature')\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.509550Z","iopub.status.idle":"2022-07-05T05:24:36.510115Z","shell.execute_reply.started":"2022-07-05T05:24:36.509912Z","shell.execute_reply":"2022-07-05T05:24:36.509935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/titanic/gender_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.511083Z","iopub.status.idle":"2022-07-05T05:24:36.511416Z","shell.execute_reply.started":"2022-07-05T05:24:36.511231Z","shell.execute_reply":"2022-07-05T05:24:36.511255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.512551Z","iopub.status.idle":"2022-07-05T05:24:36.512982Z","shell.execute_reply.started":"2022-07-05T05:24:36.512738Z","shell.execute_reply":"2022-07-05T05:24:36.512764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.518970Z","iopub.status.idle":"2022-07-05T05:24:36.519367Z","shell.execute_reply.started":"2022-07-05T05:24:36.519183Z","shell.execute_reply":"2022-07-05T05:24:36.519204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['Survived'] = prediction","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.520527Z","iopub.status.idle":"2022-07-05T05:24:36.520909Z","shell.execute_reply.started":"2022-07-05T05:24:36.520690Z","shell.execute_reply":"2022-07-05T05:24:36.520714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('./my_second_submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T05:24:36.521896Z","iopub.status.idle":"2022-07-05T05:24:36.522233Z","shell.execute_reply.started":"2022-07-05T05:24:36.522059Z","shell.execute_reply":"2022-07-05T05:24:36.522081Z"},"trusted":true},"execution_count":null,"outputs":[]}]}