{"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":"markdown","source":"# Importing libraries","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nimport pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport math\nfrom sklearn import preprocessing\nimport matplotlib.pyplot as plt \nplt.rc(\"font\", size=14)\nimport seaborn as sns\nsns.set(style=\"white\") #white background style for seaborn plots\nsns.set(style=\"whitegrid\", color_codes=True)\n\nimport warnings\nwarnings.simplefilter(action='ignore')","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:37.208767Z","iopub.execute_input":"2022-08-10T12:15:37.212587Z","iopub.status.idle":"2022-08-10T12:15:37.227835Z","shell.execute_reply.started":"2022-08-10T12:15:37.212493Z","shell.execute_reply":"2022-08-10T12:15:37.226493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Importing the data","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv(\"../input/titanic/test.csv\")\ntrain = pd.read_csv(\"../input/titanic/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:37.234285Z","iopub.execute_input":"2022-08-10T12:15:37.237587Z","iopub.status.idle":"2022-08-10T12:15:37.277469Z","shell.execute_reply.started":"2022-08-10T12:15:37.237532Z","shell.execute_reply":"2022-08-10T12:15:37.276067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Checking the basic shape of data","metadata":{}},{"cell_type":"code","source":"print(\"Train Shape:\",train.shape)\nprint(\"Test Shape:\",test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:37.280441Z","iopub.execute_input":"2022-08-10T12:15:37.280996Z","iopub.status.idle":"2022-08-10T12:15:37.296391Z","shell.execute_reply.started":"2022-08-10T12:15:37.280946Z","shell.execute_reply":"2022-08-10T12:15:37.294418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:37.298838Z","iopub.execute_input":"2022-08-10T12:15:37.299954Z","iopub.status.idle":"2022-08-10T12:15:37.321289Z","shell.execute_reply.started":"2022-08-10T12:15:37.299901Z","shell.execute_reply":"2022-08-10T12:15:37.319838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:37.322875Z","iopub.execute_input":"2022-08-10T12:15:37.323640Z","iopub.status.idle":"2022-08-10T12:15:37.344151Z","shell.execute_reply.started":"2022-08-10T12:15:37.323578Z","shell.execute_reply":"2022-08-10T12:15:37.342712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:37.346102Z","iopub.execute_input":"2022-08-10T12:15:37.346979Z","iopub.status.idle":"2022-08-10T12:15:37.370561Z","shell.execute_reply.started":"2022-08-10T12:15:37.346929Z","shell.execute_reply":"2022-08-10T12:15:37.368980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:37.372341Z","iopub.execute_input":"2022-08-10T12:15:37.373139Z","iopub.status.idle":"2022-08-10T12:15:37.384416Z","shell.execute_reply.started":"2022-08-10T12:15:37.373099Z","shell.execute_reply":"2022-08-10T12:15:37.383128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualization","metadata":{}},{"cell_type":"code","source":"sns.countplot(x=\"Survived\", data=train)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:37.385805Z","iopub.execute_input":"2022-08-10T12:15:37.386922Z","iopub.status.idle":"2022-08-10T12:15:37.574669Z","shell.execute_reply.started":"2022-08-10T12:15:37.386879Z","shell.execute_reply":"2022-08-10T12:15:37.573345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x=\"Survived\",hue=\"Sex\", data=train)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:37.575928Z","iopub.execute_input":"2022-08-10T12:15:37.577006Z","iopub.status.idle":"2022-08-10T12:15:37.776446Z","shell.execute_reply.started":"2022-08-10T12:15:37.576961Z","shell.execute_reply":"2022-08-10T12:15:37.774919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x=\"Survived\",hue=\"Pclass\", data=train)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:37.778261Z","iopub.execute_input":"2022-08-10T12:15:37.778727Z","iopub.status.idle":"2022-08-10T12:15:37.999697Z","shell.execute_reply.started":"2022-08-10T12:15:37.778635Z","shell.execute_reply":"2022-08-10T12:15:37.998302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"Age\"].plot.hist()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:38.002565Z","iopub.execute_input":"2022-08-10T12:15:38.003453Z","iopub.status.idle":"2022-08-10T12:15:38.284663Z","shell.execute_reply.started":"2022-08-10T12:15:38.003403Z","shell.execute_reply":"2022-08-10T12:15:38.283413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"Fare\"].plot.hist(bins=20, figsize=(10,5))","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:38.286187Z","iopub.execute_input":"2022-08-10T12:15:38.286997Z","iopub.status.idle":"2022-08-10T12:15:38.571177Z","shell.execute_reply.started":"2022-08-10T12:15:38.286945Z","shell.execute_reply":"2022-08-10T12:15:38.570095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x=\"SibSp\", data=train)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:38.572733Z","iopub.execute_input":"2022-08-10T12:15:38.573639Z","iopub.status.idle":"2022-08-10T12:15:38.794811Z","shell.execute_reply.started":"2022-08-10T12:15:38.573572Z","shell.execute_reply":"2022-08-10T12:15:38.793386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(train.isnull(), cmap=\"viridis\")","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:38.796237Z","iopub.execute_input":"2022-08-10T12:15:38.796735Z","iopub.status.idle":"2022-08-10T12:15:39.214523Z","shell.execute_reply.started":"2022-08-10T12:15:38.796698Z","shell.execute_reply":"2022-08-10T12:15:39.213114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Percent of missing \"Age\" records is %.2f%%' %((train['Age'].isnull().sum()/train.shape[0])*100))\nprint('Percent of missing \"Cabin\" records is %.2f%%' %((train['Cabin'].isnull().sum()/train.shape[0])*100))\nprint('Percent of missing \"Embarked\" records is %.2f%%' %((train['Embarked'].isnull().sum()/train.shape[0])*100))","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.216020Z","iopub.execute_input":"2022-08-10T12:15:39.216684Z","iopub.status.idle":"2022-08-10T12:15:39.226025Z","shell.execute_reply.started":"2022-08-10T12:15:39.216639Z","shell.execute_reply":"2022-08-10T12:15:39.224840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ax = train[\"Age\"].hist(bins=15, density=True, stacked=True, color='teal', alpha=0.6)\ntrain[\"Age\"].plot(kind='density', color='teal')\nax.set(xlabel='Age')\nplt.xlim(-10,85)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.227508Z","iopub.execute_input":"2022-08-10T12:15:39.227872Z","iopub.status.idle":"2022-08-10T12:15:39.519018Z","shell.execute_reply.started":"2022-08-10T12:15:39.227841Z","shell.execute_reply":"2022-08-10T12:15:39.517858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# # Feature Engineering","metadata":{}},{"cell_type":"markdown","source":"# Title column by name","metadata":{}},{"cell_type":"code","source":"for Name in train:\n    train['Title'] = train.Name.str.extract(' ([A-Za-z]+)\\.', expand=False)\n\npd.crosstab(train['Title'], train['Sex'])","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.520630Z","iopub.execute_input":"2022-08-10T12:15:39.520976Z","iopub.status.idle":"2022-08-10T12:15:39.562630Z","shell.execute_reply.started":"2022-08-10T12:15:39.520944Z","shell.execute_reply":"2022-08-10T12:15:39.561724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for Name in train:\n    train['Title'] = train['Title'].replace(['Lady', 'Countess','Capt', 'Col',\\\n \t'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')\n\n    train['Title'] = train['Title'].replace('Mlle', 'Miss')\n    train['Title'] = train['Title'].replace('Ms', 'Miss')\n    train['Title'] = train['Title'].replace('Mme', 'Mrs')\n    \ntrain[['Title', 'Survived']].groupby(['Title'], as_index=False).mean()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.563826Z","iopub.execute_input":"2022-08-10T12:15:39.564416Z","iopub.status.idle":"2022-08-10T12:15:39.612069Z","shell.execute_reply.started":"2022-08-10T12:15:39.564380Z","shell.execute_reply":"2022-08-10T12:15:39.610851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Title'] = train['Title'].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.613933Z","iopub.execute_input":"2022-08-10T12:15:39.614357Z","iopub.status.idle":"2022-08-10T12:15:39.621359Z","shell.execute_reply.started":"2022-08-10T12:15:39.614321Z","shell.execute_reply":"2022-08-10T12:15:39.620105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.replace({'Title':{'Mr': 1, 'Miss': 2, 'Mrs': 3, 'Master': 4, 'Rare': 5}}, inplace= True)\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.622785Z","iopub.execute_input":"2022-08-10T12:15:39.623819Z","iopub.status.idle":"2022-08-10T12:15:39.660947Z","shell.execute_reply.started":"2022-08-10T12:15:39.623776Z","shell.execute_reply":"2022-08-10T12:15:39.660033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"for test data","metadata":{}},{"cell_type":"code","source":"for Name in test:\n    test['Title'] = test.Name.str.extract(' ([A-Za-z]+)\\.', expand=False)\n\npd.crosstab(test['Title'], test['Sex'])","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.662263Z","iopub.execute_input":"2022-08-10T12:15:39.663101Z","iopub.status.idle":"2022-08-10T12:15:39.702039Z","shell.execute_reply.started":"2022-08-10T12:15:39.663053Z","shell.execute_reply":"2022-08-10T12:15:39.700585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for Name in test:\n    test['Title'] = test['Title'].replace(['Lady', 'Countess','Capt', 'Col',\\\n \t'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')\n\n    test['Title'] = test['Title'].replace('Mlle', 'Miss')\n    test['Title'] = test['Title'].replace('Ms', 'Miss')\n    test['Title'] = test['Title'].replace('Mme', 'Mrs')","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.703713Z","iopub.execute_input":"2022-08-10T12:15:39.704096Z","iopub.status.idle":"2022-08-10T12:15:39.740217Z","shell.execute_reply.started":"2022-08-10T12:15:39.704064Z","shell.execute_reply":"2022-08-10T12:15:39.738853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Title'] = test['Title'].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.741690Z","iopub.execute_input":"2022-08-10T12:15:39.742250Z","iopub.status.idle":"2022-08-10T12:15:39.748185Z","shell.execute_reply.started":"2022-08-10T12:15:39.742215Z","shell.execute_reply":"2022-08-10T12:15:39.746951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.replace({'Title':{'Mr': 1, 'Miss': 2, 'Mrs': 3, 'Master': 4, 'Rare': 5}}, inplace= True)\ntest","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.749945Z","iopub.execute_input":"2022-08-10T12:15:39.750300Z","iopub.status.idle":"2022-08-10T12:15:39.785947Z","shell.execute_reply.started":"2022-08-10T12:15:39.750269Z","shell.execute_reply":"2022-08-10T12:15:39.784671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Ticket column ","metadata":{}},{"cell_type":"code","source":"def cleanTicket(ticket):\n    ticket = ticket.replace('.', '')\n    ticket = ticket.replace('/', '')\n    ticket = ticket.split()\n    ticket = map(lambda t : t.strip(), ticket)\n    ticket = list(filter(lambda t : not t.isdigit(), ticket))\n    if len(ticket) > 0:\n        return ticket[0]\n    else: \n        return 'XXX'\n\ntickets = set()\nfor t in train['Ticket']:\n    tickets.add(cleanTicket(t))\n\nprint(len(tickets))","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.787563Z","iopub.execute_input":"2022-08-10T12:15:39.788838Z","iopub.status.idle":"2022-08-10T12:15:39.800872Z","shell.execute_reply.started":"2022-08-10T12:15:39.788793Z","shell.execute_reply":"2022-08-10T12:15:39.799521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_ticket():\n    \n    global combined\n    \n    # a function that extracts each prefix of the ticket, returns 'XXX' if no prefix (i.e the ticket is a digit)\n    def cleanTicket(ticket):\n        ticket = ticket.replace('.','')\n        ticket = ticket.replace('/','')\n        ticket = ticket.split()\n        ticket = map(lambda t : t.strip(), ticket)\n        ticket = filter(lambda t : not t.isdigit(), ticket)\n        if len(ticket) > 0:\n            return ticket[0]\n        else: \n            return 'XXX'","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.803020Z","iopub.execute_input":"2022-08-10T12:15:39.803909Z","iopub.status.idle":"2022-08-10T12:15:39.813271Z","shell.execute_reply.started":"2022-08-10T12:15:39.803860Z","shell.execute_reply":"2022-08-10T12:15:39.812167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Ticket'] = train['Ticket'].map(cleanTicket)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.815934Z","iopub.execute_input":"2022-08-10T12:15:39.817277Z","iopub.status.idle":"2022-08-10T12:15:39.833288Z","shell.execute_reply.started":"2022-08-10T12:15:39.817230Z","shell.execute_reply":"2022-08-10T12:15:39.831978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cleanTicket(ticket):\n    ticket = ticket.replace('.', '')\n    ticket = ticket.replace('/', '')\n    ticket = ticket.split()\n    ticket = map(lambda t : t.strip(), ticket)\n    ticket = list(filter(lambda t : not t.isdigit(), ticket))\n    if len(ticket) > 0:\n        return ticket[0]\n    else: \n        return 'XXX'\n\ntickets = set()\nfor t in test['Ticket']:\n    tickets.add(cleanTicket(t))\n\nprint(len(tickets))","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.834657Z","iopub.execute_input":"2022-08-10T12:15:39.835546Z","iopub.status.idle":"2022-08-10T12:15:39.845354Z","shell.execute_reply.started":"2022-08-10T12:15:39.835508Z","shell.execute_reply":"2022-08-10T12:15:39.844233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Ticket'] = test['Ticket'].map(cleanTicket)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.847162Z","iopub.execute_input":"2022-08-10T12:15:39.847992Z","iopub.status.idle":"2022-08-10T12:15:39.857405Z","shell.execute_reply.started":"2022-08-10T12:15:39.847948Z","shell.execute_reply":"2022-08-10T12:15:39.856143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Ticket'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.859140Z","iopub.execute_input":"2022-08-10T12:15:39.860024Z","iopub.status.idle":"2022-08-10T12:15:39.871757Z","shell.execute_reply.started":"2022-08-10T12:15:39.859988Z","shell.execute_reply":"2022-08-10T12:15:39.870759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Ticket'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.874653Z","iopub.execute_input":"2022-08-10T12:15:39.876002Z","iopub.status.idle":"2022-08-10T12:15:39.889656Z","shell.execute_reply.started":"2022-08-10T12:15:39.875951Z","shell.execute_reply":"2022-08-10T12:15:39.888666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.replace({'Ticket':{'XXX': 0, 'PC': 1, 'CA': 2, 'A5': 3, 'SOTONOQ': 4, 'STONO': 5,'WC':6, 'SCPARIS': 7, 'A4': 8, 'STONO2':9, 'SOC':10, 'FCC':11, 'C': 12, 'SCParis': 13, 'LINE':14, 'WEP':15, 'SCAH':16, 'SOPP':17, 'PP':18, 'SOTONO2':19, 'SWPP':20, 'PPP':21, 'FC':22, 'AS':23, 'SCOW':24, 'SC':25, 'SP':26, 'Fa':27, 'SOP':28, 'SCA4':29, 'CASOTON':30}}, inplace= True)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.891489Z","iopub.execute_input":"2022-08-10T12:15:39.892386Z","iopub.status.idle":"2022-08-10T12:15:39.908205Z","shell.execute_reply.started":"2022-08-10T12:15:39.892346Z","shell.execute_reply":"2022-08-10T12:15:39.906926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.replace({'Ticket':{'XXX': 0, 'PC': 1, 'CA': 2, 'A5': 3, 'SOTONOQ': 4, 'STONO': 5,'WC':6, 'SCPARIS': 7, 'A4': 8, 'STONO2':9, 'SOC':10, 'FCC':11, 'C': 12, 'SCParis': 13, 'LINE':14, 'WEP':15, 'SCAH':16, 'SOPP':17, 'PP':18, 'SOTONO2':19, 'SWPP':20, 'PPP':21, 'FC':22, 'AS':23, 'SCOW':24, 'SC':25, 'SP':26, 'Fa':27, 'SOP':28, 'SCA4':29, 'CASOTON':30, 'LP':31, 'A':32, 'AQ4':33, 'STONOQ':34, 'SCA3': 35, 'AQ3':36 }}, inplace= True)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.910137Z","iopub.execute_input":"2022-08-10T12:15:39.910820Z","iopub.status.idle":"2022-08-10T12:15:39.924904Z","shell.execute_reply.started":"2022-08-10T12:15:39.910785Z","shell.execute_reply":"2022-08-10T12:15:39.923759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Cabin column","metadata":{}},{"cell_type":"code","source":"CabinTrain=train.Cabin.str.replace('[0-9]', '')","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.926423Z","iopub.execute_input":"2022-08-10T12:15:39.926944Z","iopub.status.idle":"2022-08-10T12:15:39.933775Z","shell.execute_reply.started":"2022-08-10T12:15:39.926904Z","shell.execute_reply":"2022-08-10T12:15:39.932797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop([\"Cabin\"], axis=1, inplace= True)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.935307Z","iopub.execute_input":"2022-08-10T12:15:39.935955Z","iopub.status.idle":"2022-08-10T12:15:39.962833Z","shell.execute_reply.started":"2022-08-10T12:15:39.935918Z","shell.execute_reply":"2022-08-10T12:15:39.961727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.concat([train, CabinTrain],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.964416Z","iopub.execute_input":"2022-08-10T12:15:39.964807Z","iopub.status.idle":"2022-08-10T12:15:39.973033Z","shell.execute_reply.started":"2022-08-10T12:15:39.964774Z","shell.execute_reply":"2022-08-10T12:15:39.971848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:39.974855Z","iopub.execute_input":"2022-08-10T12:15:39.975226Z","iopub.status.idle":"2022-08-10T12:15:40.005924Z","shell.execute_reply.started":"2022-08-10T12:15:39.975194Z","shell.execute_reply":"2022-08-10T12:15:40.004626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in train.columns:\n    print(col)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.007088Z","iopub.execute_input":"2022-08-10T12:15:40.007794Z","iopub.status.idle":"2022-08-10T12:15:40.014009Z","shell.execute_reply.started":"2022-08-10T12:15:40.007756Z","shell.execute_reply":"2022-08-10T12:15:40.012804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Cabin'].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.016111Z","iopub.execute_input":"2022-08-10T12:15:40.017142Z","iopub.status.idle":"2022-08-10T12:15:40.028868Z","shell.execute_reply.started":"2022-08-10T12:15:40.017095Z","shell.execute_reply":"2022-08-10T12:15:40.027755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"Cabin\"].value_counts(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.031197Z","iopub.execute_input":"2022-08-10T12:15:40.031752Z","iopub.status.idle":"2022-08-10T12:15:40.043254Z","shell.execute_reply.started":"2022-08-10T12:15:40.031704Z","shell.execute_reply":"2022-08-10T12:15:40.042114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.replace({'Cabin':{'B B': 'B', 'C C C': 'C', 'C C': 'C', 'F G': 'F', 'B B B B':'B', 'B B B': 'B', 'D D':'D', 'F E': 'E' }}, inplace= True)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.045390Z","iopub.execute_input":"2022-08-10T12:15:40.046312Z","iopub.status.idle":"2022-08-10T12:15:40.056192Z","shell.execute_reply.started":"2022-08-10T12:15:40.046265Z","shell.execute_reply":"2022-08-10T12:15:40.055133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"Cabin\"].value_counts(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.058576Z","iopub.execute_input":"2022-08-10T12:15:40.059412Z","iopub.status.idle":"2022-08-10T12:15:40.076347Z","shell.execute_reply.started":"2022-08-10T12:15:40.059372Z","shell.execute_reply":"2022-08-10T12:15:40.075085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Pclass :1 \\n\",train[train['Pclass']==1]['Cabin'].value_counts())\nprint(\"Pclass:2 \\n\",train[train['Pclass']==2]['Cabin'].value_counts())\nprint(\"Pclass :3 \\n\",train[train['Pclass']==3]['Cabin'].value_counts())","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.078702Z","iopub.execute_input":"2022-08-10T12:15:40.079820Z","iopub.status.idle":"2022-08-10T12:15:40.095318Z","shell.execute_reply.started":"2022-08-10T12:15:40.079764Z","shell.execute_reply":"2022-08-10T12:15:40.093859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def impute_cabin_train(cols):\n    Cabin=cols[0]\n    Pclass=cols[1]\n    \n    if(pd.isnull(Cabin)):\n        if(Pclass==1):\n            return 'C'\n        elif(Pclass==2):\n            return 'F'\n        else:\n            return 'G'\n    else:\n        return Cabin","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.098522Z","iopub.execute_input":"2022-08-10T12:15:40.099739Z","iopub.status.idle":"2022-08-10T12:15:40.108742Z","shell.execute_reply.started":"2022-08-10T12:15:40.099696Z","shell.execute_reply":"2022-08-10T12:15:40.107418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"Cabin\"]= train[[\"Cabin\", \"Pclass\"]].apply(impute_cabin_train, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.110570Z","iopub.execute_input":"2022-08-10T12:15:40.111419Z","iopub.status.idle":"2022-08-10T12:15:40.134847Z","shell.execute_reply.started":"2022-08-10T12:15:40.111371Z","shell.execute_reply":"2022-08-10T12:15:40.133741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.136069Z","iopub.execute_input":"2022-08-10T12:15:40.136783Z","iopub.status.idle":"2022-08-10T12:15:40.165642Z","shell.execute_reply.started":"2022-08-10T12:15:40.136746Z","shell.execute_reply":"2022-08-10T12:15:40.164234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"Cabin\"].fillna(\"NA\", inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.167536Z","iopub.execute_input":"2022-08-10T12:15:40.167930Z","iopub.status.idle":"2022-08-10T12:15:40.174968Z","shell.execute_reply.started":"2022-08-10T12:15:40.167896Z","shell.execute_reply":"2022-08-10T12:15:40.173458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"Cabin\"].value_counts(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.177217Z","iopub.execute_input":"2022-08-10T12:15:40.178286Z","iopub.status.idle":"2022-08-10T12:15:40.196110Z","shell.execute_reply.started":"2022-08-10T12:15:40.178227Z","shell.execute_reply":"2022-08-10T12:15:40.194637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(x=\"Cabin\", y= \"Fare\", data=train)\nplt.grid(True)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.198118Z","iopub.execute_input":"2022-08-10T12:15:40.199724Z","iopub.status.idle":"2022-08-10T12:15:40.514387Z","shell.execute_reply.started":"2022-08-10T12:15:40.199678Z","shell.execute_reply":"2022-08-10T12:15:40.513238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Cabin'].replace({'NA':0,'C':1,'E':2,\"G\":3,'D':4,'A':5,'B':6,'F':7,'T':7},inplace=True)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.516134Z","iopub.execute_input":"2022-08-10T12:15:40.516515Z","iopub.status.idle":"2022-08-10T12:15:40.539860Z","shell.execute_reply.started":"2022-08-10T12:15:40.516481Z","shell.execute_reply":"2022-08-10T12:15:40.538854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Age Column","metadata":{}},{"cell_type":"code","source":"def impute_age_train(cols):\n    Age=cols[0]\n    Pclass=cols[1]\n    \n    if(pd.isnull(Age)):\n        if(Pclass==1):\n            return 37\n        elif(Pclass==2):\n            return 29\n        else:\n            return 24\n    else:\n        return Age","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.541633Z","iopub.execute_input":"2022-08-10T12:15:40.542013Z","iopub.status.idle":"2022-08-10T12:15:40.549578Z","shell.execute_reply.started":"2022-08-10T12:15:40.541981Z","shell.execute_reply":"2022-08-10T12:15:40.548200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"Age\"]= train[[\"Age\", \"Pclass\"]].apply(impute_age_train, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.551497Z","iopub.execute_input":"2022-08-10T12:15:40.552572Z","iopub.status.idle":"2022-08-10T12:15:40.580948Z","shell.execute_reply.started":"2022-08-10T12:15:40.552509Z","shell.execute_reply":"2022-08-10T12:15:40.579210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def impute_age_test(cols):\n    Age=cols[0]\n    Pclass=cols[1]\n    \n    if(pd.isnull(Age)):\n        if(Pclass==1):\n            return 42\n        elif(Pclass==2):\n            return 26\n        else:\n            return 25\n    else:\n        return Age","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.583740Z","iopub.execute_input":"2022-08-10T12:15:40.584506Z","iopub.status.idle":"2022-08-10T12:15:40.591771Z","shell.execute_reply.started":"2022-08-10T12:15:40.584454Z","shell.execute_reply":"2022-08-10T12:15:40.590505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"Age\"]= test[[\"Age\", \"Pclass\"]].apply(impute_age_test, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.593551Z","iopub.execute_input":"2022-08-10T12:15:40.594747Z","iopub.status.idle":"2022-08-10T12:15:40.614592Z","shell.execute_reply.started":"2022-08-10T12:15:40.594700Z","shell.execute_reply":"2022-08-10T12:15:40.612970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fare Column","metadata":{}},{"cell_type":"code","source":"mean_fare=train['Fare'].mean()\ntrain['Fare'].fillna(mean_fare,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.616551Z","iopub.execute_input":"2022-08-10T12:15:40.617374Z","iopub.status.idle":"2022-08-10T12:15:40.633544Z","shell.execute_reply.started":"2022-08-10T12:15:40.617330Z","shell.execute_reply":"2022-08-10T12:15:40.632278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"binsF = [-1, 7.9104, 14.4542, 31, 512.330]\nlabelsF = ['low', 'medium-low', 'medium', 'high']\ntrain['Fare'] = pd.cut(train[\"Fare\"], bins = binsF, labels = labelsF)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.635099Z","iopub.execute_input":"2022-08-10T12:15:40.635820Z","iopub.status.idle":"2022-08-10T12:15:40.654877Z","shell.execute_reply.started":"2022-08-10T12:15:40.635781Z","shell.execute_reply":"2022-08-10T12:15:40.653463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_fare_t=test['Fare'].mean()\ntest['Fare'].fillna(mean_fare_t,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.656706Z","iopub.execute_input":"2022-08-10T12:15:40.657770Z","iopub.status.idle":"2022-08-10T12:15:40.664850Z","shell.execute_reply.started":"2022-08-10T12:15:40.657732Z","shell.execute_reply":"2022-08-10T12:15:40.663886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"binsFT = [-1, 7.9104, 14.4542, 31, 512.330]\nlabelsFT = ['low', 'medium-low', 'medium', 'high']\ntest['Fare'] = pd.cut(test[\"Fare\"], bins = binsFT, labels = labelsFT)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.666526Z","iopub.execute_input":"2022-08-10T12:15:40.667167Z","iopub.status.idle":"2022-08-10T12:15:40.680716Z","shell.execute_reply.started":"2022-08-10T12:15:40.667133Z","shell.execute_reply":"2022-08-10T12:15:40.679550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Fare'].replace({'low':0,'medium-low':1,'medium':2,'high':3},inplace=True)\ntest['Fare'].replace({'low':0,'medium-low':1,'medium':2,'high':3},inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.682633Z","iopub.execute_input":"2022-08-10T12:15:40.683315Z","iopub.status.idle":"2022-08-10T12:15:40.696686Z","shell.execute_reply.started":"2022-08-10T12:15:40.683278Z","shell.execute_reply":"2022-08-10T12:15:40.695417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.fillna({'Fare' : train['Fare'].mode()[0]})\ntest.fillna({'Fare' : test['Fare'].mode()[0]})","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.698354Z","iopub.execute_input":"2022-08-10T12:15:40.699147Z","iopub.status.idle":"2022-08-10T12:15:40.726919Z","shell.execute_reply.started":"2022-08-10T12:15:40.699110Z","shell.execute_reply":"2022-08-10T12:15:40.725685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Embarked column ","metadata":{}},{"cell_type":"code","source":"train['Embarked'] = train['Embarked'].fillna(train['Embarked'].mode()[0])","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.728337Z","iopub.execute_input":"2022-08-10T12:15:40.729161Z","iopub.status.idle":"2022-08-10T12:15:40.735552Z","shell.execute_reply.started":"2022-08-10T12:15:40.729117Z","shell.execute_reply":"2022-08-10T12:15:40.734665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.736766Z","iopub.execute_input":"2022-08-10T12:15:40.737680Z","iopub.status.idle":"2022-08-10T12:15:40.758517Z","shell.execute_reply.started":"2022-08-10T12:15:40.737644Z","shell.execute_reply":"2022-08-10T12:15:40.757442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sex = pd.get_dummies(train[\"Sex\"],drop_first=True)\nsex.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.759973Z","iopub.execute_input":"2022-08-10T12:15:40.760798Z","iopub.status.idle":"2022-08-10T12:15:40.780718Z","shell.execute_reply.started":"2022-08-10T12:15:40.760762Z","shell.execute_reply":"2022-08-10T12:15:40.779707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embark = pd.get_dummies(train[\"Embarked\"],drop_first=True)\nembark.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.782141Z","iopub.execute_input":"2022-08-10T12:15:40.782505Z","iopub.status.idle":"2022-08-10T12:15:40.795554Z","shell.execute_reply.started":"2022-08-10T12:15:40.782474Z","shell.execute_reply":"2022-08-10T12:15:40.794677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Pclass = pd.get_dummies(train[\"Pclass\"],drop_first=True)\nPclass.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.796880Z","iopub.execute_input":"2022-08-10T12:15:40.797701Z","iopub.status.idle":"2022-08-10T12:15:40.814988Z","shell.execute_reply.started":"2022-08-10T12:15:40.797660Z","shell.execute_reply":"2022-08-10T12:15:40.813695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Age column","metadata":{}},{"cell_type":"code","source":"bins = [0, 5, 17, 25, 50, 80]\nlabels = ['Infant', 'Kid', 'Young', 'Adult', 'Old']\ntrain['Age'] = pd.cut(train['Age'], bins = bins, labels=labels)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.816749Z","iopub.execute_input":"2022-08-10T12:15:40.818169Z","iopub.status.idle":"2022-08-10T12:15:40.826093Z","shell.execute_reply.started":"2022-08-10T12:15:40.818134Z","shell.execute_reply":"2022-08-10T12:15:40.825067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bins = [0, 5, 17, 25, 50, 80]\nlabels = ['Infant', 'Kid', 'Young', 'Adult', 'Old']\ntest['Age'] = pd.cut(test['Age'], bins = bins, labels=labels)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.827700Z","iopub.execute_input":"2022-08-10T12:15:40.828858Z","iopub.status.idle":"2022-08-10T12:15:40.840279Z","shell.execute_reply.started":"2022-08-10T12:15:40.828811Z","shell.execute_reply":"2022-08-10T12:15:40.839169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(train['Age'].value_counts())","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.843256Z","iopub.execute_input":"2022-08-10T12:15:40.844327Z","iopub.status.idle":"2022-08-10T12:15:40.857309Z","shell.execute_reply.started":"2022-08-10T12:15:40.844287Z","shell.execute_reply":"2022-08-10T12:15:40.856241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Age'].mode()[0]","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.858517Z","iopub.execute_input":"2022-08-10T12:15:40.858927Z","iopub.status.idle":"2022-08-10T12:15:40.873760Z","shell.execute_reply.started":"2022-08-10T12:15:40.858895Z","shell.execute_reply":"2022-08-10T12:15:40.872464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.fillna({'Age' : train['Age'].mode()[0]})\ntest.fillna({'Age' : test['Age'].mode()[0]})","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.875132Z","iopub.execute_input":"2022-08-10T12:15:40.875514Z","iopub.status.idle":"2022-08-10T12:15:40.909104Z","shell.execute_reply.started":"2022-08-10T12:15:40.875482Z","shell.execute_reply":"2022-08-10T12:15:40.907806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Age'].replace({'Infant':0,'Kid':1,'Young':2,'Adult':3,'Old':4},inplace=True)\ntest['Age'].replace({'Infant':0,'Kid':1,'Young':2,'Adult':3,'Old':4},inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.911032Z","iopub.execute_input":"2022-08-10T12:15:40.911829Z","iopub.status.idle":"2022-08-10T12:15:40.925582Z","shell.execute_reply.started":"2022-08-10T12:15:40.911780Z","shell.execute_reply":"2022-08-10T12:15:40.924242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Cabin Test ","metadata":{}},{"cell_type":"code","source":"CabinTest=test.Cabin.str.replace('[0-9]', '')","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.927445Z","iopub.execute_input":"2022-08-10T12:15:40.928286Z","iopub.status.idle":"2022-08-10T12:15:40.935813Z","shell.execute_reply.started":"2022-08-10T12:15:40.928237Z","shell.execute_reply":"2022-08-10T12:15:40.934658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.drop([\"Cabin\"], axis=1, inplace= True)\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.937457Z","iopub.execute_input":"2022-08-10T12:15:40.937905Z","iopub.status.idle":"2022-08-10T12:15:40.963882Z","shell.execute_reply.started":"2022-08-10T12:15:40.937872Z","shell.execute_reply":"2022-08-10T12:15:40.962506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.concat([test, CabinTest],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.965641Z","iopub.execute_input":"2022-08-10T12:15:40.966958Z","iopub.status.idle":"2022-08-10T12:15:40.975760Z","shell.execute_reply.started":"2022-08-10T12:15:40.966905Z","shell.execute_reply":"2022-08-10T12:15:40.974654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in test.columns:\n    print(col)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.977717Z","iopub.execute_input":"2022-08-10T12:15:40.978455Z","iopub.status.idle":"2022-08-10T12:15:40.988970Z","shell.execute_reply.started":"2022-08-10T12:15:40.978408Z","shell.execute_reply":"2022-08-10T12:15:40.987675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Cabin'].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:40.990363Z","iopub.execute_input":"2022-08-10T12:15:40.990836Z","iopub.status.idle":"2022-08-10T12:15:41.003222Z","shell.execute_reply.started":"2022-08-10T12:15:40.990789Z","shell.execute_reply":"2022-08-10T12:15:41.002157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"Cabin\"].value_counts(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.005985Z","iopub.execute_input":"2022-08-10T12:15:41.006787Z","iopub.status.idle":"2022-08-10T12:15:41.017539Z","shell.execute_reply.started":"2022-08-10T12:15:41.006747Z","shell.execute_reply":"2022-08-10T12:15:41.016544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.replace({'Cabin':{'B B': 'B', 'C C C': 'C', 'C C': 'C', 'F G': 'F', 'B B B B':'B', 'B B B': 'B', 'D D':'D', 'F E': 'E', 'E E': 'E' }}, inplace= True)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.019361Z","iopub.execute_input":"2022-08-10T12:15:41.019857Z","iopub.status.idle":"2022-08-10T12:15:41.036780Z","shell.execute_reply.started":"2022-08-10T12:15:41.019807Z","shell.execute_reply":"2022-08-10T12:15:41.035457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"Cabin\"].value_counts(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.056008Z","iopub.execute_input":"2022-08-10T12:15:41.056486Z","iopub.status.idle":"2022-08-10T12:15:41.066918Z","shell.execute_reply.started":"2022-08-10T12:15:41.056448Z","shell.execute_reply":"2022-08-10T12:15:41.065742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Pclass :1 \\n\",test[test['Pclass']==1]['Cabin'].value_counts())\nprint(\"Pclass:2 \\n\",test[test['Pclass']==2]['Cabin'].value_counts())\nprint(\"Pclass :3 \\n\",test[test['Pclass']==3]['Cabin'].value_counts())","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.068405Z","iopub.execute_input":"2022-08-10T12:15:41.069329Z","iopub.status.idle":"2022-08-10T12:15:41.088189Z","shell.execute_reply.started":"2022-08-10T12:15:41.069293Z","shell.execute_reply":"2022-08-10T12:15:41.086929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def impute_cabin_test(cols):\n    Cabin=cols[0]\n    Pclass=cols[1]\n    \n    if(pd.isnull(Cabin)):\n        if(Pclass==1):\n            return 'C'\n        elif(Pclass==2):\n            return 'F'\n        else:\n            return 'E'\n    else:\n        return Cabin","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.089949Z","iopub.execute_input":"2022-08-10T12:15:41.090514Z","iopub.status.idle":"2022-08-10T12:15:41.097738Z","shell.execute_reply.started":"2022-08-10T12:15:41.090476Z","shell.execute_reply":"2022-08-10T12:15:41.096524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"Cabin\"]=test[[\"Cabin\", \"Pclass\"]].apply(impute_cabin_test, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.099193Z","iopub.execute_input":"2022-08-10T12:15:41.099579Z","iopub.status.idle":"2022-08-10T12:15:41.120724Z","shell.execute_reply.started":"2022-08-10T12:15:41.099544Z","shell.execute_reply":"2022-08-10T12:15:41.119439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"Cabin\"].fillna(\"NA\", inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.123810Z","iopub.execute_input":"2022-08-10T12:15:41.124982Z","iopub.status.idle":"2022-08-10T12:15:41.136542Z","shell.execute_reply.started":"2022-08-10T12:15:41.124924Z","shell.execute_reply":"2022-08-10T12:15:41.135395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"Cabin\"].value_counts(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.137883Z","iopub.execute_input":"2022-08-10T12:15:41.138828Z","iopub.status.idle":"2022-08-10T12:15:41.153122Z","shell.execute_reply.started":"2022-08-10T12:15:41.138793Z","shell.execute_reply":"2022-08-10T12:15:41.152062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Cabin'].replace({'NA':0,'C':1,'E':2,\"G\":3,'D':4,'A':5,'B':6,'F':7},inplace=True)\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.154647Z","iopub.execute_input":"2022-08-10T12:15:41.155343Z","iopub.status.idle":"2022-08-10T12:15:41.178638Z","shell.execute_reply.started":"2022-08-10T12:15:41.155309Z","shell.execute_reply":"2022-08-10T12:15:41.177657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sex_t = pd.get_dummies(test[\"Sex\"],drop_first=True)\nsex_t.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.180218Z","iopub.execute_input":"2022-08-10T12:15:41.180683Z","iopub.status.idle":"2022-08-10T12:15:41.192667Z","shell.execute_reply.started":"2022-08-10T12:15:41.180649Z","shell.execute_reply":"2022-08-10T12:15:41.191554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embark_t = pd.get_dummies(test[\"Embarked\"],drop_first=True)\nembark_t.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.194474Z","iopub.execute_input":"2022-08-10T12:15:41.194861Z","iopub.status.idle":"2022-08-10T12:15:41.207237Z","shell.execute_reply.started":"2022-08-10T12:15:41.194829Z","shell.execute_reply":"2022-08-10T12:15:41.205976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Pclass_t = pd.get_dummies(test[\"Pclass\"],drop_first=True)\nPclass_t.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.208971Z","iopub.execute_input":"2022-08-10T12:15:41.210131Z","iopub.status.idle":"2022-08-10T12:15:41.226145Z","shell.execute_reply.started":"2022-08-10T12:15:41.210083Z","shell.execute_reply":"2022-08-10T12:15:41.224832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Travel alove column","metadata":{}},{"cell_type":"code","source":"train['TravelAlone']=np.where((train[\"SibSp\"]+train[\"Parch\"])>0, 0, 1)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.227541Z","iopub.execute_input":"2022-08-10T12:15:41.228158Z","iopub.status.idle":"2022-08-10T12:15:41.235172Z","shell.execute_reply.started":"2022-08-10T12:15:41.228107Z","shell.execute_reply":"2022-08-10T12:15:41.234088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['TravelAlone']=np.where((test[\"SibSp\"]+test[\"Parch\"])>0, 0, 1)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.236733Z","iopub.execute_input":"2022-08-10T12:15:41.237755Z","iopub.status.idle":"2022-08-10T12:15:41.251378Z","shell.execute_reply.started":"2022-08-10T12:15:41.237715Z","shell.execute_reply":"2022-08-10T12:15:41.250370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating Variable","metadata":{}},{"cell_type":"code","source":"df= pd.concat([train, sex,embark,Pclass],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.253059Z","iopub.execute_input":"2022-08-10T12:15:41.253426Z","iopub.status.idle":"2022-08-10T12:15:41.265330Z","shell.execute_reply.started":"2022-08-10T12:15:41.253395Z","shell.execute_reply":"2022-08-10T12:15:41.264403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2= pd.concat([test, sex_t ,embark_t ,Pclass_t],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.266965Z","iopub.execute_input":"2022-08-10T12:15:41.267705Z","iopub.status.idle":"2022-08-10T12:15:41.279929Z","shell.execute_reply.started":"2022-08-10T12:15:41.267659Z","shell.execute_reply":"2022-08-10T12:15:41.278963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop([\"Pclass\",\"PassengerId\",\"Sex\",\"Name\",\"Embarked\"], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.281437Z","iopub.execute_input":"2022-08-10T12:15:41.281902Z","iopub.status.idle":"2022-08-10T12:15:41.295913Z","shell.execute_reply.started":"2022-08-10T12:15:41.281859Z","shell.execute_reply":"2022-08-10T12:15:41.294618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.drop([\"Pclass\",\"PassengerId\",\"Sex\",\"Name\",\"Embarked\"], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.297412Z","iopub.execute_input":"2022-08-10T12:15:41.297828Z","iopub.status.idle":"2022-08-10T12:15:41.310168Z","shell.execute_reply.started":"2022-08-10T12:15:41.297793Z","shell.execute_reply":"2022-08-10T12:15:41.309215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X= df.drop(\"Survived\",axis=1)\ny= df[\"Survived\"]\nX.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.311352Z","iopub.execute_input":"2022-08-10T12:15:41.312256Z","iopub.status.idle":"2022-08-10T12:15:41.333454Z","shell.execute_reply.started":"2022-08-10T12:15:41.312220Z","shell.execute_reply":"2022-08-10T12:15:41.332199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Checking Accuracy","metadata":{}},{"cell_type":"markdown","source":"# Regression","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import train_test_split\n\nx_train, x_val, y_train, y_val = train_test_split (X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.335002Z","iopub.execute_input":"2022-08-10T12:15:41.335411Z","iopub.status.idle":"2022-08-10T12:15:41.343448Z","shell.execute_reply.started":"2022-08-10T12:15:41.335377Z","shell.execute_reply":"2022-08-10T12:15:41.342558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = LogisticRegression(random_state=0, max_iter=1000).fit(x_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.344576Z","iopub.execute_input":"2022-08-10T12:15:41.345321Z","iopub.status.idle":"2022-08-10T12:15:41.392165Z","shell.execute_reply.started":"2022-08-10T12:15:41.345289Z","shell.execute_reply":"2022-08-10T12:15:41.390809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions= clf.predict(x_val)\nfrom sklearn.metrics import accuracy_score\naccuracy_score(y_val, predictions)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.394233Z","iopub.execute_input":"2022-08-10T12:15:41.395046Z","iopub.status.idle":"2022-08-10T12:15:41.408024Z","shell.execute_reply.started":"2022-08-10T12:15:41.394998Z","shell.execute_reply":"2022-08-10T12:15:41.406792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.410076Z","iopub.execute_input":"2022-08-10T12:15:41.410835Z","iopub.status.idle":"2022-08-10T12:15:41.437455Z","shell.execute_reply.started":"2022-08-10T12:15:41.410789Z","shell.execute_reply":"2022-08-10T12:15:41.436135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_preds = clf.predict(df2)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.439453Z","iopub.execute_input":"2022-08-10T12:15:41.440286Z","iopub.status.idle":"2022-08-10T12:15:41.450737Z","shell.execute_reply.started":"2022-08-10T12:15:41.440239Z","shell.execute_reply":"2022-08-10T12:15:41.449485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ids = test[\"PassengerId\"]","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.452543Z","iopub.execute_input":"2022-08-10T12:15:41.453256Z","iopub.status.idle":"2022-08-10T12:15:41.463220Z","shell.execute_reply.started":"2022-08-10T12:15:41.453211Z","shell.execute_reply":"2022-08-10T12:15:41.462016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sol = pd.DataFrame({\"PassengerId\":test_ids.values,\n                    \"Survived\": submission_preds,\n                   })","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.465067Z","iopub.execute_input":"2022-08-10T12:15:41.465838Z","iopub.status.idle":"2022-08-10T12:15:41.477096Z","shell.execute_reply.started":"2022-08-10T12:15:41.465792Z","shell.execute_reply":"2022-08-10T12:15:41.475814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sol","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.478953Z","iopub.execute_input":"2022-08-10T12:15:41.479757Z","iopub.status.idle":"2022-08-10T12:15:41.502791Z","shell.execute_reply.started":"2022-08-10T12:15:41.479710Z","shell.execute_reply":"2022-08-10T12:15:41.501471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sol.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.504671Z","iopub.execute_input":"2022-08-10T12:15:41.505434Z","iopub.status.idle":"2022-08-10T12:15:41.511526Z","shell.execute_reply.started":"2022-08-10T12:15:41.505388Z","shell.execute_reply":"2022-08-10T12:15:41.510591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Randome Forest","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nfrom sklearn.datasets import make_classification\nmodel = RandomForestClassifier( criterion= 'gini',\n                              n_estimators=1750,\n                              max_depth=7,\n                              min_samples_split=6,\n                              min_samples_leaf=6,\n                              max_features= 'auto',\n                              oob_score=True,\n                              random_state=42,\n                              n_jobs=-1,\n                              verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.512821Z","iopub.execute_input":"2022-08-10T12:15:41.513536Z","iopub.status.idle":"2022-08-10T12:15:41.526696Z","shell.execute_reply.started":"2022-08-10T12:15:41.513503Z","shell.execute_reply":"2022-08-10T12:15:41.525707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:41.528369Z","iopub.execute_input":"2022-08-10T12:15:41.529183Z","iopub.status.idle":"2022-08-10T12:15:46.367940Z","shell.execute_reply.started":"2022-08-10T12:15:41.529137Z","shell.execute_reply":"2022-08-10T12:15:46.366413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(x_val)\nprint(model.score(x_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:46.369142Z","iopub.execute_input":"2022-08-10T12:15:46.370083Z","iopub.status.idle":"2022-08-10T12:15:47.290271Z","shell.execute_reply.started":"2022-08-10T12:15:46.370044Z","shell.execute_reply":"2022-08-10T12:15:47.288839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_preds_randomeforest = model.predict(df2)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:47.291864Z","iopub.execute_input":"2022-08-10T12:15:47.292354Z","iopub.status.idle":"2022-08-10T12:15:47.906321Z","shell.execute_reply.started":"2022-08-10T12:15:47.292320Z","shell.execute_reply":"2022-08-10T12:15:47.904978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"solRF = pd.DataFrame({\"PassengerId\":test_ids.values,\n                    \"Survived\": submission_preds_randomeforest,\n                   })","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:47.907919Z","iopub.execute_input":"2022-08-10T12:15:47.908345Z","iopub.status.idle":"2022-08-10T12:15:47.915328Z","shell.execute_reply.started":"2022-08-10T12:15:47.908309Z","shell.execute_reply":"2022-08-10T12:15:47.914067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"solRF","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:47.916690Z","iopub.execute_input":"2022-08-10T12:15:47.917352Z","iopub.status.idle":"2022-08-10T12:15:47.943277Z","shell.execute_reply.started":"2022-08-10T12:15:47.917317Z","shell.execute_reply":"2022-08-10T12:15:47.942011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"solRF.to_csv(\"submissionRF.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:47.945198Z","iopub.execute_input":"2022-08-10T12:15:47.945704Z","iopub.status.idle":"2022-08-10T12:15:47.958469Z","shell.execute_reply.started":"2022-08-10T12:15:47.945657Z","shell.execute_reply":"2022-08-10T12:15:47.957073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Wrapper method using decsion tree","metadata":{}},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score,confusion_matrix\nfrom sklearn import metrics\nfrom sklearn.metrics import classification_report\nfrom sklearn.feature_selection import SequentialFeatureSelector\nfrom sklearn.neighbors import KNeighborsClassifier\nknn= KNeighborsClassifier()\nsfs=SequentialFeatureSelector(knn,n_features_to_select=4,direction='forward')\nsfs.fit(X,y)\nsfs.get_support()\ncol=X.columns[sfs.get_support()]\nprint(col)\nx2= pd.DataFrame(df, columns=col)\ny2=df[\"Survived\"]\nx_train, x_test, y_train, y_test = train_test_split(x2, y2, test_size=0.3, random_state=3)\nclf = DecisionTreeClassifier(min_samples_split=4)\nclf = clf.fit(x_train,y_train)\ny_pred = clf.predict(x_test)\n\nprint(\"Accuracy:\",metrics.accuracy_score(y_test, y_pred))\nprint(classification_report(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:47.959885Z","iopub.execute_input":"2022-08-10T12:15:47.960887Z","iopub.status.idle":"2022-08-10T12:15:50.216693Z","shell.execute_reply.started":"2022-08-10T12:15:47.960851Z","shell.execute_reply":"2022-08-10T12:15:50.215368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Begging method","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import BaggingClassifier\nfrom sklearn.model_selection import GridSearchCV, StratifiedShuffleSplit\nn_estimators = [10,30,50,70,80,150,160, 170,175,180,185];\ncv = StratifiedShuffleSplit(n_splits=10, test_size=.30, random_state=15)\n\nparameters = {'n_estimators':n_estimators,\n              \n        }\ngrid = GridSearchCV(BaggingClassifier(base_estimator= None, ## If None, then the base estimator is a decision tree.\n                                      bootstrap_features=False),\n                                 param_grid=parameters,\n                                 cv=cv,\n                                 n_jobs = -1)\ngrid.fit(X,y)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:15:50.218061Z","iopub.execute_input":"2022-08-10T12:15:50.218523Z","iopub.status.idle":"2022-08-10T12:16:02.589555Z","shell.execute_reply.started":"2022-08-10T12:15:50.218490Z","shell.execute_reply":"2022-08-10T12:16:02.588210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print (grid.best_score_)\nprint (grid.best_params_)\nprint (grid.best_estimator_)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:16:02.591117Z","iopub.execute_input":"2022-08-10T12:16:02.591556Z","iopub.status.idle":"2022-08-10T12:16:02.598539Z","shell.execute_reply.started":"2022-08-10T12:16:02.591520Z","shell.execute_reply":"2022-08-10T12:16:02.597290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bagging_grid = grid.best_estimator_\nbagging_grid.score(X,y)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:16:02.600160Z","iopub.execute_input":"2022-08-10T12:16:02.600661Z","iopub.status.idle":"2022-08-10T12:16:02.628787Z","shell.execute_reply.started":"2022-08-10T12:16:02.600598Z","shell.execute_reply":"2022-08-10T12:16:02.627570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# K-Fold Cross Validation\nfrom sklearn.model_selection import cross_val_score,KFold\nfrom sklearn.linear_model import LogisticRegression\nlogmodel = LogisticRegression()\nkf=KFold(n_splits=9)\nscore=cross_val_score(logmodel,x_train,y_train,cv=kf)\nprint(\"Cross Validation Scores are {}\".format(score))\nprint(\"Average Cross Validation score :{}\".format(score.mean()))","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:16:02.630194Z","iopub.execute_input":"2022-08-10T12:16:02.630623Z","iopub.status.idle":"2022-08-10T12:16:02.726149Z","shell.execute_reply.started":"2022-08-10T12:16:02.630567Z","shell.execute_reply":"2022-08-10T12:16:02.724810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# General models accuracies","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.svm import SVC\nfrom sklearn.naive_bayes import GaussianNB\nfrom sklearn.discriminant_analysis import LinearDiscriminantAnalysis","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:16:02.728535Z","iopub.execute_input":"2022-08-10T12:16:02.729019Z","iopub.status.idle":"2022-08-10T12:16:02.736006Z","shell.execute_reply.started":"2022-08-10T12:16:02.728982Z","shell.execute_reply":"2022-08-10T12:16:02.734512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"estimators = {\n    'Logisitic_Regression': LogisticRegression(),\n    'KNeighborsClassifier': KNeighborsClassifier(),\n    'DecisionTreeClassifier': DecisionTreeClassifier(),\n    'RandomForestClassifier': RandomForestClassifier(),\n    'SVC': SVC(gamma = 'auto'),\n    'GaussianNB': GaussianNB(),\n    'discriminant_analysis': LinearDiscriminantAnalysis()\n}","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:16:02.737496Z","iopub.execute_input":"2022-08-10T12:16:02.738491Z","iopub.status.idle":"2022-08-10T12:16:02.750273Z","shell.execute_reply.started":"2022-08-10T12:16:02.738446Z","shell.execute_reply":"2022-08-10T12:16:02.748680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#using Kfold and cross_val_score\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.metrics import classification_report","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:16:02.752043Z","iopub.execute_input":"2022-08-10T12:16:02.753131Z","iopub.status.idle":"2022-08-10T12:16:02.762022Z","shell.execute_reply.started":"2022-08-10T12:16:02.753086Z","shell.execute_reply":"2022-08-10T12:16:02.761064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for name, code in estimators.items():\n    kfold = StratifiedKFold(n_splits=10, random_state=11 , shuffle=True)\n    cv_result = cross_val_score(code, X , y ,\n                            cv = kfold, scoring= 'accuracy')\n    print(f'{name:>20}: ' + \n          f'mean accuracy={cv_result.mean():.2%}; ' +\n          f'standard deviation={cv_result.std():.2%}')","metadata":{"execution":{"iopub.status.busy":"2022-08-10T12:16:02.763259Z","iopub.execute_input":"2022-08-10T12:16:02.763985Z","iopub.status.idle":"2022-08-10T12:16:05.901611Z","shell.execute_reply.started":"2022-08-10T12:16:02.763951Z","shell.execute_reply":"2022-08-10T12:16:05.900171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}