{"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 pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-01T16:32:33.884488Z","iopub.execute_input":"2022-08-01T16:32:33.885062Z","iopub.status.idle":"2022-08-01T16:32:33.895460Z","shell.execute_reply.started":"2022-08-01T16:32:33.885019Z","shell.execute_reply":"2022-08-01T16:32:33.893701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('../input/titanic/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:33.909610Z","iopub.execute_input":"2022-08-01T16:32:33.911166Z","iopub.status.idle":"2022-08-01T16:32:33.926109Z","shell.execute_reply.started":"2022-08-01T16:32:33.911101Z","shell.execute_reply":"2022-08-01T16:32:33.924748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.read_csv('../input/titanic/test.csv') ","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:33.928960Z","iopub.execute_input":"2022-08-01T16:32:33.930482Z","iopub.status.idle":"2022-08-01T16:32:33.942443Z","shell.execute_reply.started":"2022-08-01T16:32:33.930423Z","shell.execute_reply":"2022-08-01T16:32:33.941051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:33.945595Z","iopub.execute_input":"2022-08-01T16:32:33.946632Z","iopub.status.idle":"2022-08-01T16:32:33.972083Z","shell.execute_reply.started":"2022-08-01T16:32:33.946573Z","shell.execute_reply":"2022-08-01T16:32:33.970939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum() \n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:33.974509Z","iopub.execute_input":"2022-08-01T16:32:33.974944Z","iopub.status.idle":"2022-08-01T16:32:33.991520Z","shell.execute_reply.started":"2022-08-01T16:32:33.974906Z","shell.execute_reply":"2022-08-01T16:32:33.990547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='Sex', data=train)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:33.993039Z","iopub.execute_input":"2022-08-01T16:32:33.993689Z","iopub.status.idle":"2022-08-01T16:32:34.205271Z","shell.execute_reply.started":"2022-08-01T16:32:33.993648Z","shell.execute_reply":"2022-08-01T16:32:34.203859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='Pclass', data=train)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:34.208242Z","iopub.execute_input":"2022-08-01T16:32:34.208698Z","iopub.status.idle":"2022-08-01T16:32:34.404610Z","shell.execute_reply.started":"2022-08-01T16:32:34.208659Z","shell.execute_reply":"2022-08-01T16:32:34.403053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='Embarked', data=train)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:34.406653Z","iopub.execute_input":"2022-08-01T16:32:34.407138Z","iopub.status.idle":"2022-08-01T16:32:34.615370Z","shell.execute_reply.started":"2022-08-01T16:32:34.407096Z","shell.execute_reply":"2022-08-01T16:32:34.614207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='SibSp', data=train)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:34.617073Z","iopub.execute_input":"2022-08-01T16:32:34.617773Z","iopub.status.idle":"2022-08-01T16:32:34.847908Z","shell.execute_reply.started":"2022-08-01T16:32:34.617732Z","shell.execute_reply":"2022-08-01T16:32:34.846502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Died'] = 1 - train['Survived']\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:34.849598Z","iopub.execute_input":"2022-08-01T16:32:34.850053Z","iopub.status.idle":"2022-08-01T16:32:34.857921Z","shell.execute_reply.started":"2022-08-01T16:32:34.850013Z","shell.execute_reply":"2022-08-01T16:32:34.856801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.groupby('Sex')[['Survived', 'Died']].sum().plot(kind='bar',\n                                                           figsize=(10, 5),\n                                                           stacked=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:34.859320Z","iopub.execute_input":"2022-08-01T16:32:34.860466Z","iopub.status.idle":"2022-08-01T16:32:35.122184Z","shell.execute_reply.started":"2022-08-01T16:32:34.860413Z","shell.execute_reply":"2022-08-01T16:32:35.121113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Name']","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.129025Z","iopub.execute_input":"2022-08-01T16:32:35.129493Z","iopub.status.idle":"2022-08-01T16:32:35.141412Z","shell.execute_reply.started":"2022-08-01T16:32:35.129457Z","shell.execute_reply":"2022-08-01T16:32:35.139719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Name'][1].split(',')[1].split('.')[0]","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.143451Z","iopub.execute_input":"2022-08-01T16:32:35.143996Z","iopub.status.idle":"2022-08-01T16:32:35.156658Z","shell.execute_reply.started":"2022-08-01T16:32:35.143946Z","shell.execute_reply":"2022-08-01T16:32:35.155537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Title']=train['Name'].apply(lambda x : x.split(',')[1].split('.')[0])","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.158539Z","iopub.execute_input":"2022-08-01T16:32:35.159248Z","iopub.status.idle":"2022-08-01T16:32:35.175254Z","shell.execute_reply.started":"2022-08-01T16:32:35.159199Z","shell.execute_reply":"2022-08-01T16:32:35.173804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.176906Z","iopub.execute_input":"2022-08-01T16:32:35.178429Z","iopub.status.idle":"2022-08-01T16:32:35.208532Z","shell.execute_reply.started":"2022-08-01T16:32:35.178369Z","shell.execute_reply":"2022-08-01T16:32:35.206956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Title'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.210472Z","iopub.execute_input":"2022-08-01T16:32:35.211093Z","iopub.status.idle":"2022-08-01T16:32:35.223868Z","shell.execute_reply.started":"2022-08-01T16:32:35.211033Z","shell.execute_reply":"2022-08-01T16:32:35.222528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Title_Dictionary = {\" Capt\": \"Officer\",\" Col\": \"Officer\",\" Major\": \"Officer\",\" Jonkheer\": \"Royalty\",\n                    \" Don\": \"Royalty\",\" Sir\" : \"Royalty\",\n                    \" Dr\": \"Officer\",\" Rev\": \"Officer\",\" the Countess\":\"Royalty\",\" Mme\": \"Mrs\",\" Mlle\": \"Miss\",\n                    \" Ms\": \"Mrs\",\" Mr\" : \"Mr\",\" Mrs\" : \"Mrs\",\" Miss\" : \"Miss\",\" Master\" : \"Master\",\" Lady\" : \"Royalty\"}","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.225670Z","iopub.execute_input":"2022-08-01T16:32:35.226334Z","iopub.status.idle":"2022-08-01T16:32:35.235136Z","shell.execute_reply.started":"2022-08-01T16:32:35.226289Z","shell.execute_reply":"2022-08-01T16:32:35.234078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Title']=train['Title'].map(Title_Dictionary)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.236615Z","iopub.execute_input":"2022-08-01T16:32:35.237615Z","iopub.status.idle":"2022-08-01T16:32:35.251110Z","shell.execute_reply.started":"2022-08-01T16:32:35.237572Z","shell.execute_reply":"2022-08-01T16:32:35.249751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Title'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.253131Z","iopub.execute_input":"2022-08-01T16:32:35.254260Z","iopub.status.idle":"2022-08-01T16:32:35.267297Z","shell.execute_reply.started":"2022-08-01T16:32:35.254211Z","shell.execute_reply":"2022-08-01T16:32:35.265955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1=train.drop(['Name','Ticket','Cabin','PassengerId','Died'], axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.269118Z","iopub.execute_input":"2022-08-01T16:32:35.270094Z","iopub.status.idle":"2022-08-01T16:32:35.279155Z","shell.execute_reply.started":"2022-08-01T16:32:35.270035Z","shell.execute_reply":"2022-08-01T16:32:35.277439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.281330Z","iopub.execute_input":"2022-08-01T16:32:35.282084Z","iopub.status.idle":"2022-08-01T16:32:35.304348Z","shell.execute_reply.started":"2022-08-01T16:32:35.282038Z","shell.execute_reply":"2022-08-01T16:32:35.303029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dummies = pd.get_dummies(df1[['Sex','Title']], drop_first= True)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.306228Z","iopub.execute_input":"2022-08-01T16:32:35.306979Z","iopub.status.idle":"2022-08-01T16:32:35.324474Z","shell.execute_reply.started":"2022-08-01T16:32:35.306929Z","shell.execute_reply":"2022-08-01T16:32:35.323078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = pd.concat([df1, dummies], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.326516Z","iopub.execute_input":"2022-08-01T16:32:35.327438Z","iopub.status.idle":"2022-08-01T16:32:35.338079Z","shell.execute_reply.started":"2022-08-01T16:32:35.327382Z","shell.execute_reply":"2022-08-01T16:32:35.336692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.339645Z","iopub.execute_input":"2022-08-01T16:32:35.340103Z","iopub.status.idle":"2022-08-01T16:32:35.368927Z","shell.execute_reply.started":"2022-08-01T16:32:35.340063Z","shell.execute_reply":"2022-08-01T16:32:35.367892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = df1.drop(['Title',  'Sex'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.370425Z","iopub.execute_input":"2022-08-01T16:32:35.371474Z","iopub.status.idle":"2022-08-01T16:32:35.379328Z","shell.execute_reply.started":"2022-08-01T16:32:35.371431Z","shell.execute_reply":"2022-08-01T16:32:35.378225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.381130Z","iopub.execute_input":"2022-08-01T16:32:35.381637Z","iopub.status.idle":"2022-08-01T16:32:35.409469Z","shell.execute_reply.started":"2022-08-01T16:32:35.381591Z","shell.execute_reply":"2022-08-01T16:32:35.408357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"median_age_men=df1[df1['Sex_male']==1]['Age'].median()\nmedian_age_women=df1[df1['Sex_male']==0]['Age'].median()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.419045Z","iopub.execute_input":"2022-08-01T16:32:35.419823Z","iopub.status.idle":"2022-08-01T16:32:35.430292Z","shell.execute_reply.started":"2022-08-01T16:32:35.419781Z","shell.execute_reply":"2022-08-01T16:32:35.428912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def function(age,sex):\n    if np.isnan(age):\n        if sex == 1:\n            return median_age_men\n        else:\n            return median_age_women\n    else:\n        return age","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.432263Z","iopub.execute_input":"2022-08-01T16:32:35.432763Z","iopub.status.idle":"2022-08-01T16:32:35.441546Z","shell.execute_reply.started":"2022-08-01T16:32:35.432725Z","shell.execute_reply":"2022-08-01T16:32:35.439808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1['Age']= df1.apply(lambda x : function(x['Age'], x['Sex_male']),axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.443464Z","iopub.execute_input":"2022-08-01T16:32:35.444774Z","iopub.status.idle":"2022-08-01T16:32:35.483260Z","shell.execute_reply.started":"2022-08-01T16:32:35.444712Z","shell.execute_reply":"2022-08-01T16:32:35.481921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.485222Z","iopub.execute_input":"2022-08-01T16:32:35.485780Z","iopub.status.idle":"2022-08-01T16:32:35.501706Z","shell.execute_reply.started":"2022-08-01T16:32:35.485728Z","shell.execute_reply":"2022-08-01T16:32:35.500108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.dropna(inplace=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.504369Z","iopub.execute_input":"2022-08-01T16:32:35.505264Z","iopub.status.idle":"2022-08-01T16:32:35.516600Z","shell.execute_reply.started":"2022-08-01T16:32:35.505208Z","shell.execute_reply":"2022-08-01T16:32:35.515054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.519290Z","iopub.execute_input":"2022-08-01T16:32:35.520461Z","iopub.status.idle":"2022-08-01T16:32:35.537819Z","shell.execute_reply.started":"2022-08-01T16:32:35.520401Z","shell.execute_reply":"2022-08-01T16:32:35.535966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dummy_emb = pd.get_dummies(df1['Embarked'], drop_first= True)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.540567Z","iopub.execute_input":"2022-08-01T16:32:35.541544Z","iopub.status.idle":"2022-08-01T16:32:35.550658Z","shell.execute_reply.started":"2022-08-01T16:32:35.541480Z","shell.execute_reply":"2022-08-01T16:32:35.548752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = pd.concat([df1, dummy_emb], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.553049Z","iopub.execute_input":"2022-08-01T16:32:35.554020Z","iopub.status.idle":"2022-08-01T16:32:35.570521Z","shell.execute_reply.started":"2022-08-01T16:32:35.553960Z","shell.execute_reply":"2022-08-01T16:32:35.568924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = df1.drop(['Embarked'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.572616Z","iopub.execute_input":"2022-08-01T16:32:35.574052Z","iopub.status.idle":"2022-08-01T16:32:35.586006Z","shell.execute_reply.started":"2022-08-01T16:32:35.573990Z","shell.execute_reply":"2022-08-01T16:32:35.583890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.588390Z","iopub.execute_input":"2022-08-01T16:32:35.589459Z","iopub.status.idle":"2022-08-01T16:32:35.617928Z","shell.execute_reply.started":"2022-08-01T16:32:35.589398Z","shell.execute_reply":"2022-08-01T16:32:35.616645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.620083Z","iopub.execute_input":"2022-08-01T16:32:35.620928Z","iopub.status.idle":"2022-08-01T16:32:35.639011Z","shell.execute_reply.started":"2022-08-01T16:32:35.620865Z","shell.execute_reply":"2022-08-01T16:32:35.637564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.641181Z","iopub.execute_input":"2022-08-01T16:32:35.642055Z","iopub.status.idle":"2022-08-01T16:32:35.714651Z","shell.execute_reply.started":"2022-08-01T16:32:35.642000Z","shell.execute_reply":"2022-08-01T16:32:35.713397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.Age = (df1.Age-min(df1.Age))/(max(df1.Age)-min(df1.Age))","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.716747Z","iopub.execute_input":"2022-08-01T16:32:35.717172Z","iopub.status.idle":"2022-08-01T16:32:35.725570Z","shell.execute_reply.started":"2022-08-01T16:32:35.717136Z","shell.execute_reply":"2022-08-01T16:32:35.724269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.Fare = (df1.Fare-min(df1.Fare))/(max(df1.Fare)-min(df1.Fare))\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.727961Z","iopub.execute_input":"2022-08-01T16:32:35.729035Z","iopub.status.idle":"2022-08-01T16:32:35.743282Z","shell.execute_reply.started":"2022-08-01T16:32:35.728972Z","shell.execute_reply":"2022-08-01T16:32:35.741683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.745013Z","iopub.execute_input":"2022-08-01T16:32:35.746069Z","iopub.status.idle":"2022-08-01T16:32:35.813851Z","shell.execute_reply.started":"2022-08-01T16:32:35.746021Z","shell.execute_reply":"2022-08-01T16:32:35.812912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.815354Z","iopub.execute_input":"2022-08-01T16:32:35.816399Z","iopub.status.idle":"2022-08-01T16:32:35.821123Z","shell.execute_reply.started":"2022-08-01T16:32:35.816359Z","shell.execute_reply":"2022-08-01T16:32:35.820070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df1.drop('Survived', axis=1)\ny = df1['Survived']","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.822925Z","iopub.execute_input":"2022-08-01T16:32:35.823646Z","iopub.status.idle":"2022-08-01T16:32:35.839639Z","shell.execute_reply.started":"2022-08-01T16:32:35.823607Z","shell.execute_reply":"2022-08-01T16:32:35.838309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,stratify=df1['Survived'],random_state=101)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.841966Z","iopub.execute_input":"2022-08-01T16:32:35.842509Z","iopub.status.idle":"2022-08-01T16:32:35.860415Z","shell.execute_reply.started":"2022-08-01T16:32:35.842458Z","shell.execute_reply":"2022-08-01T16:32:35.859080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.862346Z","iopub.execute_input":"2022-08-01T16:32:35.863695Z","iopub.status.idle":"2022-08-01T16:32:35.871529Z","shell.execute_reply.started":"2022-08-01T16:32:35.863644Z","shell.execute_reply":"2022-08-01T16:32:35.869876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = LogisticRegression()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.873414Z","iopub.execute_input":"2022-08-01T16:32:35.874537Z","iopub.status.idle":"2022-08-01T16:32:35.886663Z","shell.execute_reply.started":"2022-08-01T16:32:35.874479Z","shell.execute_reply":"2022-08-01T16:32:35.885277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.888863Z","iopub.execute_input":"2022-08-01T16:32:35.889718Z","iopub.status.idle":"2022-08-01T16:32:35.939341Z","shell.execute_reply.started":"2022-08-01T16:32:35.889663Z","shell.execute_reply":"2022-08-01T16:32:35.938044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, classification_report, confusion_matrix\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.941450Z","iopub.execute_input":"2022-08-01T16:32:35.942289Z","iopub.status.idle":"2022-08-01T16:32:35.951620Z","shell.execute_reply.started":"2022-08-01T16:32:35.942236Z","shell.execute_reply":"2022-08-01T16:32:35.949917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.954557Z","iopub.execute_input":"2022-08-01T16:32:35.956736Z","iopub.status.idle":"2022-08-01T16:32:35.976337Z","shell.execute_reply.started":"2022-08-01T16:32:35.956675Z","shell.execute_reply":"2022-08-01T16:32:35.974118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test, predictions)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.978935Z","iopub.execute_input":"2022-08-01T16:32:35.979774Z","iopub.status.idle":"2022-08-01T16:32:35.990276Z","shell.execute_reply.started":"2022-08-01T16:32:35.979721Z","shell.execute_reply":"2022-08-01T16:32:35.988627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test, predictions))","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:35.992686Z","iopub.execute_input":"2022-08-01T16:32:35.993596Z","iopub.status.idle":"2022-08-01T16:32:36.020901Z","shell.execute_reply.started":"2022-08-01T16:32:35.993540Z","shell.execute_reply":"2022-08-01T16:32:36.019417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(confusion_matrix(y_test, predictions))","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:36.023133Z","iopub.execute_input":"2022-08-01T16:32:36.024088Z","iopub.status.idle":"2022-08-01T16:32:36.039188Z","shell.execute_reply.started":"2022-08-01T16:32:36.024029Z","shell.execute_reply":"2022-08-01T16:32:36.036815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test ","metadata":{}},{"cell_type":"code","source":"test.head()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:36.042946Z","iopub.execute_input":"2022-08-01T16:32:36.045303Z","iopub.status.idle":"2022-08-01T16:32:36.073943Z","shell.execute_reply.started":"2022-08-01T16:32:36.045229Z","shell.execute_reply":"2022-08-01T16:32:36.072414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:36.076275Z","iopub.execute_input":"2022-08-01T16:32:36.076739Z","iopub.status.idle":"2022-08-01T16:32:36.092221Z","shell.execute_reply.started":"2022-08-01T16:32:36.076701Z","shell.execute_reply":"2022-08-01T16:32:36.090678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Title']=test['Name'].apply(lambda x : x.split(',')[1].split('.')[0])","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:36.094402Z","iopub.execute_input":"2022-08-01T16:32:36.094877Z","iopub.status.idle":"2022-08-01T16:32:36.103167Z","shell.execute_reply.started":"2022-08-01T16:32:36.094816Z","shell.execute_reply":"2022-08-01T16:32:36.102110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Title'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:36.104951Z","iopub.execute_input":"2022-08-01T16:32:36.105585Z","iopub.status.idle":"2022-08-01T16:32:36.123773Z","shell.execute_reply.started":"2022-08-01T16:32:36.105545Z","shell.execute_reply":"2022-08-01T16:32:36.122077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Title'][414]","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:36.125981Z","iopub.execute_input":"2022-08-01T16:32:36.126679Z","iopub.status.idle":"2022-08-01T16:32:36.140142Z","shell.execute_reply.started":"2022-08-01T16:32:36.126627Z","shell.execute_reply":"2022-08-01T16:32:36.138731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Title_Dictionary2 = {\" Capt\": \"Officer\",\" Col\": \"Officer\",\" Major\": \"Officer\",\" Jonkheer\": \"Royalty\",\n                    \" Don\": \"Royalty\",\" Sir\" : \"Royalty\",\" Dona\": \"Royalty\",\n                    \" Dr\": \"Officer\",\" Rev\": \"Officer\",\" the Countess\":\"Royalty\",\" Mme\": \"Mrs\",\" Mlle\": \"Miss\",\n                    \" Ms\": \"Mrs\",\" Mr\" : \"Mr\",\" Mrs\" : \"Mrs\",\" Miss\" : \"Miss\",\" Master\" : \"Master\",\" Lady\" : \"Royalty\"}","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:44.319096Z","iopub.execute_input":"2022-08-01T16:32:44.320050Z","iopub.status.idle":"2022-08-01T16:32:44.328490Z","shell.execute_reply.started":"2022-08-01T16:32:44.319989Z","shell.execute_reply":"2022-08-01T16:32:44.326953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Title']=test['Title'].map(Title_Dictionary2)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:48.376320Z","iopub.execute_input":"2022-08-01T16:32:48.376831Z","iopub.status.idle":"2022-08-01T16:32:48.384916Z","shell.execute_reply.started":"2022-08-01T16:32:48.376791Z","shell.execute_reply":"2022-08-01T16:32:48.383820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Title'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:51.857627Z","iopub.execute_input":"2022-08-01T16:32:51.858159Z","iopub.status.idle":"2022-08-01T16:32:51.869331Z","shell.execute_reply.started":"2022-08-01T16:32:51.858119Z","shell.execute_reply":"2022-08-01T16:32:51.867886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2=test.drop(['Name','Ticket','Cabin','PassengerId'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:54.914813Z","iopub.execute_input":"2022-08-01T16:32:54.917138Z","iopub.status.idle":"2022-08-01T16:32:54.926890Z","shell.execute_reply.started":"2022-08-01T16:32:54.917069Z","shell.execute_reply":"2022-08-01T16:32:54.925332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:58.608610Z","iopub.execute_input":"2022-08-01T16:32:58.609700Z","iopub.status.idle":"2022-08-01T16:32:58.627182Z","shell.execute_reply.started":"2022-08-01T16:32:58.609645Z","shell.execute_reply":"2022-08-01T16:32:58.625667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dummies_test = pd.get_dummies(df2[['Sex','Title']], drop_first= True)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:33:02.561356Z","iopub.execute_input":"2022-08-01T16:33:02.561929Z","iopub.status.idle":"2022-08-01T16:33:02.579439Z","shell.execute_reply.started":"2022-08-01T16:33:02.561885Z","shell.execute_reply":"2022-08-01T16:33:02.577935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2 = pd.concat([df2, dummies_test], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:33:05.627220Z","iopub.execute_input":"2022-08-01T16:33:05.628216Z","iopub.status.idle":"2022-08-01T16:33:05.638193Z","shell.execute_reply.started":"2022-08-01T16:33:05.628155Z","shell.execute_reply":"2022-08-01T16:33:05.636964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2 = df2.drop(['Title',  'Sex'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:33:08.755201Z","iopub.execute_input":"2022-08-01T16:33:08.755983Z","iopub.status.idle":"2022-08-01T16:33:08.769319Z","shell.execute_reply.started":"2022-08-01T16:33:08.755707Z","shell.execute_reply":"2022-08-01T16:33:08.767803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2['Age']= df2.apply(lambda x : function(x['Age'], x['Sex_male']),axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:33:11.851945Z","iopub.execute_input":"2022-08-01T16:33:11.852523Z","iopub.status.idle":"2022-08-01T16:33:11.876990Z","shell.execute_reply.started":"2022-08-01T16:33:11.852474Z","shell.execute_reply":"2022-08-01T16:33:11.874905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:33:16.428390Z","iopub.execute_input":"2022-08-01T16:33:16.429588Z","iopub.status.idle":"2022-08-01T16:33:16.444333Z","shell.execute_reply.started":"2022-08-01T16:33:16.429528Z","shell.execute_reply":"2022-08-01T16:33:16.442026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2['Fare']=df2['Fare'].fillna(df2['Fare'].median())","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:33:20.711543Z","iopub.execute_input":"2022-08-01T16:33:20.713387Z","iopub.status.idle":"2022-08-01T16:33:20.721626Z","shell.execute_reply.started":"2022-08-01T16:33:20.713324Z","shell.execute_reply":"2022-08-01T16:33:20.720330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:33:24.754413Z","iopub.execute_input":"2022-08-01T16:33:24.754956Z","iopub.status.idle":"2022-08-01T16:33:24.771983Z","shell.execute_reply.started":"2022-08-01T16:33:24.754914Z","shell.execute_reply":"2022-08-01T16:33:24.770106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:33:28.992785Z","iopub.execute_input":"2022-08-01T16:33:28.993556Z","iopub.status.idle":"2022-08-01T16:33:29.018413Z","shell.execute_reply.started":"2022-08-01T16:33:28.993506Z","shell.execute_reply":"2022-08-01T16:33:29.016674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.select_dtypes(['object']).columns","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:33:33.417448Z","iopub.execute_input":"2022-08-01T16:33:33.418898Z","iopub.status.idle":"2022-08-01T16:33:33.433776Z","shell.execute_reply.started":"2022-08-01T16:33:33.418669Z","shell.execute_reply":"2022-08-01T16:33:33.432354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dummy_emb_test = pd.get_dummies(df2['Embarked'], drop_first= True)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:33:36.531047Z","iopub.execute_input":"2022-08-01T16:33:36.531585Z","iopub.status.idle":"2022-08-01T16:33:36.541711Z","shell.execute_reply.started":"2022-08-01T16:33:36.531544Z","shell.execute_reply":"2022-08-01T16:33:36.539801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2 = pd.concat([df2, dummy_emb_test], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:33:40.104627Z","iopub.execute_input":"2022-08-01T16:33:40.105182Z","iopub.status.idle":"2022-08-01T16:33:40.115186Z","shell.execute_reply.started":"2022-08-01T16:33:40.105136Z","shell.execute_reply":"2022-08-01T16:33:40.113296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2 = df2.drop(['Embarked'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:33:43.454625Z","iopub.execute_input":"2022-08-01T16:33:43.455224Z","iopub.status.idle":"2022-08-01T16:33:43.465455Z","shell.execute_reply.started":"2022-08-01T16:33:43.455179Z","shell.execute_reply":"2022-08-01T16:33:43.463877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.select_dtypes(['object']).columns","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:33:49.016238Z","iopub.execute_input":"2022-08-01T16:33:49.017805Z","iopub.status.idle":"2022-08-01T16:33:49.032950Z","shell.execute_reply.started":"2022-08-01T16:33:49.017730Z","shell.execute_reply":"2022-08-01T16:33:49.031233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.Age = (df2.Age-min(df2.Age))/(max(df2.Age)-min(df2.Age))\ndf2.Fare = (df2.Fare-min(df2.Fare))/(max(df2.Fare)-min(df2.Fare))","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:33:54.133737Z","iopub.execute_input":"2022-08-01T16:33:54.134300Z","iopub.status.idle":"2022-08-01T16:33:54.145681Z","shell.execute_reply.started":"2022-08-01T16:33:54.134257Z","shell.execute_reply":"2022-08-01T16:33:54.143912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:36.393137Z","iopub.execute_input":"2022-08-01T16:32:36.393972Z","iopub.status.idle":"2022-08-01T16:32:36.420921Z","shell.execute_reply.started":"2022-08-01T16:32:36.393921Z","shell.execute_reply":"2022-08-01T16:32:36.419665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:32:36.422905Z","iopub.execute_input":"2022-08-01T16:32:36.423746Z","iopub.status.idle":"2022-08-01T16:32:36.480529Z","shell.execute_reply.started":"2022-08-01T16:32:36.423702Z","shell.execute_reply":"2022-08-01T16:32:36.479297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(df2)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:34:13.530130Z","iopub.execute_input":"2022-08-01T16:34:13.530666Z","iopub.status.idle":"2022-08-01T16:34:13.541355Z","shell.execute_reply.started":"2022-08-01T16:34:13.530628Z","shell.execute_reply":"2022-08-01T16:34:13.539544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:34:17.308275Z","iopub.execute_input":"2022-08-01T16:34:17.309797Z","iopub.status.idle":"2022-08-01T16:34:17.321351Z","shell.execute_reply.started":"2022-08-01T16:34:17.309741Z","shell.execute_reply":"2022-08-01T16:34:17.319801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\n        \"PassengerId\": test[\"PassengerId\"],\n        \"Survived\": predictions\n    })\nsubmission.to_csv('titanic4.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:35:20.252240Z","iopub.execute_input":"2022-08-01T16:35:20.252961Z","iopub.status.idle":"2022-08-01T16:35:20.269500Z","shell.execute_reply.started":"2022-08-01T16:35:20.252906Z","shell.execute_reply":"2022-08-01T16:35:20.267111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}