{"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 numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:14:24.411455Z","iopub.execute_input":"2022-07-26T08:14:24.411874Z","iopub.status.idle":"2022-07-26T08:14:24.420707Z","shell.execute_reply.started":"2022-07-26T08:14:24.411832Z","shell.execute_reply":"2022-07-26T08:14:24.419276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#importing the data and overview \n\ntrain_df= pd.read_csv('../input/titanic/train.csv')\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:14:24.586586Z","iopub.execute_input":"2022-07-26T08:14:24.587019Z","iopub.status.idle":"2022-07-26T08:14:24.612918Z","shell.execute_reply.started":"2022-07-26T08:14:24.586988Z","shell.execute_reply":"2022-07-26T08:14:24.611612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:14:24.734140Z","iopub.execute_input":"2022-07-26T08:14:24.734607Z","iopub.status.idle":"2022-07-26T08:14:24.750745Z","shell.execute_reply.started":"2022-07-26T08:14:24.734573Z","shell.execute_reply":"2022-07-26T08:14:24.749867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(18, 9))\nsns.heatmap(yticklabels=False, data=train_df.isnull(), cbar=False, cmap=\"vlag\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:14:24.845023Z","iopub.execute_input":"2022-07-26T08:14:24.845709Z","iopub.status.idle":"2022-07-26T08:14:25.062933Z","shell.execute_reply.started":"2022-07-26T08:14:24.845670Z","shell.execute_reply":"2022-07-26T08:14:25.061341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"survived = train_df[[\"Sex\", \"Survived\"]].groupby(['Sex'], as_index=False).mean().sort_values(by='Survived', ascending=False)\nsurvived.set_index('Sex')\nplt.figure(figsize=(12,8))\nplt.pie(x=survived.Survived, labels=survived.Sex, autopct='%.0f%%', colors=sns.color_palette('muted'))\nplt.title('Survived rate based on Sex')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:14:25.065414Z","iopub.execute_input":"2022-07-26T08:14:25.066530Z","iopub.status.idle":"2022-07-26T08:14:25.197189Z","shell.execute_reply.started":"2022-07-26T08:14:25.066482Z","shell.execute_reply":"2022-07-26T08:14:25.195840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data=train_df, x='Survived')\n\n# survived = 1\n# didn't survive = 0","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:14:25.210319Z","iopub.execute_input":"2022-07-26T08:14:25.210887Z","iopub.status.idle":"2022-07-26T08:14:25.331884Z","shell.execute_reply.started":"2022-07-26T08:14:25.210829Z","shell.execute_reply":"2022-07-26T08:14:25.330567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(data=train_df, x='Survived', y='Age')","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:14:25.334047Z","iopub.execute_input":"2022-07-26T08:14:25.335427Z","iopub.status.idle":"2022-07-26T08:14:25.483196Z","shell.execute_reply.started":"2022-07-26T08:14:25.335377Z","shell.execute_reply":"2022-07-26T08:14:25.482051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,8))\nsns.heatmap(train_df.corr(), annot=True, cmap='rocket')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:14:26.634766Z","iopub.execute_input":"2022-07-26T08:14:26.635444Z","iopub.status.idle":"2022-07-26T08:14:27.108415Z","shell.execute_reply.started":"2022-07-26T08:14:26.635387Z","shell.execute_reply":"2022-07-26T08:14:27.107285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corrSurvived = train_df.corr()['Survived'].sort_values(ascending = False)\nindexes = corrSurvived.index\nlabels = []\ncorr = []\n\nfor i in range(1, len(corrSurvived)):\n    labels.append(indexes[i])\n    corr.append(corrSurvived[i])\nplt.figure(figsize=(12,8))\nsns.barplot(x=labels, y=corr, palette='rocket')\nplt.title('Correlation coefficient between different features and Survived')","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:14:28.302246Z","iopub.execute_input":"2022-07-26T08:14:28.303221Z","iopub.status.idle":"2022-07-26T08:14:28.509583Z","shell.execute_reply.started":"2022-07-26T08:14:28.303184Z","shell.execute_reply":"2022-07-26T08:14:28.508012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preparation","metadata":{}},{"cell_type":"code","source":"train_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:14:30.222566Z","iopub.execute_input":"2022-07-26T08:14:30.222963Z","iopub.status.idle":"2022-07-26T08:14:30.235928Z","shell.execute_reply.started":"2022-07-26T08:14:30.222932Z","shell.execute_reply":"2022-07-26T08:14:30.234274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"Age\"].fillna(train_df[\"Age\"].mean(), inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:14:31.113695Z","iopub.execute_input":"2022-07-26T08:14:31.114258Z","iopub.status.idle":"2022-07-26T08:14:31.121568Z","shell.execute_reply.started":"2022-07-26T08:14:31.114218Z","shell.execute_reply":"2022-07-26T08:14:31.120429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['Sex'] = train_df['Sex'].replace('male', 0)\ntrain_df['Sex'] = train_df['Sex'].replace('female', 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:14:33.616413Z","iopub.execute_input":"2022-07-26T08:14:33.617015Z","iopub.status.idle":"2022-07-26T08:14:33.624473Z","shell.execute_reply.started":"2022-07-26T08:14:33.616981Z","shell.execute_reply":"2022-07-26T08:14:33.622995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:14:34.928244Z","iopub.execute_input":"2022-07-26T08:14:34.928766Z","iopub.status.idle":"2022-07-26T08:14:34.948576Z","shell.execute_reply.started":"2022-07-26T08:14:34.928729Z","shell.execute_reply":"2022-07-26T08:14:34.947549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.drop(['Name', 'PassengerId', 'Ticket', 'Embarked', 'Cabin'], axis = 1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:14:35.939672Z","iopub.execute_input":"2022-07-26T08:14:35.940451Z","iopub.status.idle":"2022-07-26T08:14:35.949158Z","shell.execute_reply.started":"2022-07-26T08:14:35.940397Z","shell.execute_reply":"2022-07-26T08:14:35.947753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:14:39.906063Z","iopub.execute_input":"2022-07-26T08:14:39.906490Z","iopub.status.idle":"2022-07-26T08:14:39.917731Z","shell.execute_reply.started":"2022-07-26T08:14:39.906456Z","shell.execute_reply":"2022-07-26T08:14:39.916805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:14:42.158462Z","iopub.execute_input":"2022-07-26T08:14:42.158901Z","iopub.status.idle":"2022-07-26T08:14:42.174256Z","shell.execute_reply.started":"2022-07-26T08:14:42.158824Z","shell.execute_reply":"2022-07-26T08:14:42.172785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Encoding","metadata":{}},{"cell_type":"code","source":"train_df = pd.get_dummies(train_df, columns=['Pclass'])","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:14:47.214339Z","iopub.execute_input":"2022-07-26T08:14:47.214750Z","iopub.status.idle":"2022-07-26T08:14:47.225965Z","shell.execute_reply.started":"2022-07-26T08:14:47.214718Z","shell.execute_reply":"2022-07-26T08:14:47.224630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Splitting Data","metadata":{}},{"cell_type":"code","source":"X = train_df.drop('Survived', axis=1)\ny = train_df['Survived']","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:14:48.193782Z","iopub.execute_input":"2022-07-26T08:14:48.194451Z","iopub.status.idle":"2022-07-26T08:14:48.203896Z","shell.execute_reply.started":"2022-07-26T08:14:48.194410Z","shell.execute_reply":"2022-07-26T08:14:48.202119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Scalling","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\n\nscaler = StandardScaler()\nscaler.fit(X)\nscaled_X = scaler.transform(X)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:14:50.397120Z","iopub.execute_input":"2022-07-26T08:14:50.397785Z","iopub.status.idle":"2022-07-26T08:14:50.408400Z","shell.execute_reply.started":"2022-07-26T08:14:50.397746Z","shell.execute_reply":"2022-07-26T08:14:50.407248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training the model","metadata":{}},{"cell_type":"code","source":"from sklearn.svm import SVC\n\nSVC_model = SVC()\nSVC_model.fit(scaled_X, y)\nSVC_model.score(scaled_X, y)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:14:52.430775Z","iopub.execute_input":"2022-07-26T08:14:52.432170Z","iopub.status.idle":"2022-07-26T08:14:52.488445Z","shell.execute_reply.started":"2022-07-26T08:14:52.432123Z","shell.execute_reply":"2022-07-26T08:14:52.487199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"#importing test data \n\ntest_df = pd.read_csv('../input/titanic/test.csv')\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:15:42.192115Z","iopub.execute_input":"2022-07-26T08:15:42.192577Z","iopub.status.idle":"2022-07-26T08:15:42.218449Z","shell.execute_reply.started":"2022-07-26T08:15:42.192542Z","shell.execute_reply":"2022-07-26T08:15:42.217046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:15:43.577752Z","iopub.execute_input":"2022-07-26T08:15:43.578167Z","iopub.status.idle":"2022-07-26T08:15:43.589470Z","shell.execute_reply.started":"2022-07-26T08:15:43.578132Z","shell.execute_reply":"2022-07-26T08:15:43.588321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df[\"Age\"].fillna(test_df[\"Age\"].mean(), inplace = True)\ntest_df[\"Fare\"].fillna(test_df[\"Fare\"].mean(), inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:15:45.509952Z","iopub.execute_input":"2022-07-26T08:15:45.510423Z","iopub.status.idle":"2022-07-26T08:15:45.519314Z","shell.execute_reply.started":"2022-07-26T08:15:45.510387Z","shell.execute_reply":"2022-07-26T08:15:45.517781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['Sex'] = test_df['Sex'].replace('male', 0)\ntest_df['Sex'] = test_df['Sex'].replace('female', 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:15:47.165590Z","iopub.execute_input":"2022-07-26T08:15:47.166871Z","iopub.status.idle":"2022-07-26T08:15:47.175235Z","shell.execute_reply.started":"2022-07-26T08:15:47.166825Z","shell.execute_reply":"2022-07-26T08:15:47.174076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df = pd.DataFrame({'PassengerId' : test_df['PassengerId']})\nfinal_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:16:00.292879Z","iopub.execute_input":"2022-07-26T08:16:00.293288Z","iopub.status.idle":"2022-07-26T08:16:00.305306Z","shell.execute_reply.started":"2022-07-26T08:16:00.293256Z","shell.execute_reply":"2022-07-26T08:16:00.303503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.drop(['Name', 'PassengerId', 'Ticket', 'Embarked', 'Cabin'], axis = 1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:16:05.256373Z","iopub.execute_input":"2022-07-26T08:16:05.256854Z","iopub.status.idle":"2022-07-26T08:16:05.266705Z","shell.execute_reply.started":"2022-07-26T08:16:05.256817Z","shell.execute_reply":"2022-07-26T08:16:05.264955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:16:06.979507Z","iopub.execute_input":"2022-07-26T08:16:06.980655Z","iopub.status.idle":"2022-07-26T08:16:06.991532Z","shell.execute_reply.started":"2022-07-26T08:16:06.980591Z","shell.execute_reply":"2022-07-26T08:16:06.990064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:16:08.812852Z","iopub.execute_input":"2022-07-26T08:16:08.813491Z","iopub.status.idle":"2022-07-26T08:16:08.826875Z","shell.execute_reply.started":"2022-07-26T08:16:08.813454Z","shell.execute_reply":"2022-07-26T08:16:08.825228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.get_dummies(test_df, columns=['Pclass'])","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:17:17.428311Z","iopub.execute_input":"2022-07-26T08:17:17.428744Z","iopub.status.idle":"2022-07-26T08:17:17.439234Z","shell.execute_reply.started":"2022-07-26T08:17:17.428695Z","shell.execute_reply":"2022-07-26T08:17:17.437755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler.fit(test_df)\nscaled_test = scaler.transform(test_df)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:17:21.598940Z","iopub.execute_input":"2022-07-26T08:17:21.599993Z","iopub.status.idle":"2022-07-26T08:17:21.610127Z","shell.execute_reply.started":"2022-07-26T08:17:21.599952Z","shell.execute_reply":"2022-07-26T08:17:21.609222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"survived = SVC_model.predict(scaled_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:17:24.097731Z","iopub.execute_input":"2022-07-26T08:17:24.098167Z","iopub.status.idle":"2022-07-26T08:17:24.114076Z","shell.execute_reply.started":"2022-07-26T08:17:24.098121Z","shell.execute_reply":"2022-07-26T08:17:24.113201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df['Survived'] = survived","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:17:25.937534Z","iopub.execute_input":"2022-07-26T08:17:25.938820Z","iopub.status.idle":"2022-07-26T08:17:25.944804Z","shell.execute_reply.started":"2022-07-26T08:17:25.938761Z","shell.execute_reply":"2022-07-26T08:17:25.943616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:17:27.943272Z","iopub.execute_input":"2022-07-26T08:17:27.943670Z","iopub.status.idle":"2022-07-26T08:17:27.957361Z","shell.execute_reply.started":"2022-07-26T08:17:27.943631Z","shell.execute_reply":"2022-07-26T08:17:27.956137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df.to_csv('titanic_submission1.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T08:17:31.949417Z","iopub.execute_input":"2022-07-26T08:17:31.949986Z","iopub.status.idle":"2022-07-26T08:17:31.958738Z","shell.execute_reply.started":"2022-07-26T08:17:31.949949Z","shell.execute_reply":"2022-07-26T08:17:31.957774Z"},"trusted":true},"execution_count":null,"outputs":[]}]}