{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-14T05:37:09.341724Z","iopub.execute_input":"2022-08-14T05:37:09.342752Z","iopub.status.idle":"2022-08-14T05:37:09.377821Z","shell.execute_reply.started":"2022-08-14T05:37:09.342602Z","shell.execute_reply":"2022-08-14T05:37:09.376599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **1. Importing Necessary Libraries**","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn import tree,svm\nfrom sklearn.metrics import accuracy_score\n","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:37:58.291227Z","iopub.execute_input":"2022-08-14T05:37:58.291685Z","iopub.status.idle":"2022-08-14T05:37:59.245318Z","shell.execute_reply.started":"2022-08-14T05:37:58.291650Z","shell.execute_reply":"2022-08-14T05:37:59.244282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset=pd.read_csv('/kaggle/input/titanic/train.csv')\ndataset.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:05.742043Z","iopub.execute_input":"2022-08-14T05:38:05.742422Z","iopub.status.idle":"2022-08-14T05:38:05.788419Z","shell.execute_reply.started":"2022-08-14T05:38:05.742391Z","shell.execute_reply":"2022-08-14T05:38:05.787216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(dataset.shape)\nprint('The shape of our training set : %s passengers and %s features'%(dataset.shape[0],dataset.shape[1]))","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:14.684955Z","iopub.execute_input":"2022-08-14T05:38:14.685387Z","iopub.status.idle":"2022-08-14T05:38:14.692750Z","shell.execute_reply.started":"2022-08-14T05:38:14.685352Z","shell.execute_reply":"2022-08-14T05:38:14.691389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:16.308764Z","iopub.execute_input":"2022-08-14T05:38:16.309570Z","iopub.status.idle":"2022-08-14T05:38:16.336939Z","shell.execute_reply.started":"2022-08-14T05:38:16.309529Z","shell.execute_reply":"2022-08-14T05:38:16.335638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Check for null values\ndataset.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:17.807225Z","iopub.execute_input":"2022-08-14T05:38:17.807994Z","iopub.status.idle":"2022-08-14T05:38:17.819082Z","shell.execute_reply.started":"2022-08-14T05:38:17.807944Z","shell.execute_reply":"2022-08-14T05:38:17.817770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"heatmap=sns.heatmap(dataset[['Survived','SibSp','Parch','Age','Fare']].corr(),annot=True)\nsns.set(rc={'figure.figsize':(12,10)})","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:19.425410Z","iopub.execute_input":"2022-08-14T05:38:19.425863Z","iopub.status.idle":"2022-08-14T05:38:19.784289Z","shell.execute_reply.started":"2022-08-14T05:38:19.425825Z","shell.execute_reply":"2022-08-14T05:38:19.783046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Finding unique values\ndataset['SibSp'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:21.821001Z","iopub.execute_input":"2022-08-14T05:38:21.821508Z","iopub.status.idle":"2022-08-14T05:38:21.829666Z","shell.execute_reply.started":"2022-08-14T05:38:21.821466Z","shell.execute_reply":"2022-08-14T05:38:21.828421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bargraph_sibsp=sns.barplot(x='SibSp',y='Survived',data=dataset)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:23.143569Z","iopub.execute_input":"2022-08-14T05:38:23.144625Z","iopub.status.idle":"2022-08-14T05:38:23.577813Z","shell.execute_reply.started":"2022-08-14T05:38:23.144585Z","shell.execute_reply":"2022-08-14T05:38:23.576782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ageplot=sns.FacetGrid(dataset,col='Survived',height=8)\nageplot=ageplot.map(sns.distplot,'Age')\nageplot=ageplot.set_ylabels(\"Survival Probability\")","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:25.022684Z","iopub.execute_input":"2022-08-14T05:38:25.023589Z","iopub.status.idle":"2022-08-14T05:38:25.761646Z","shell.execute_reply.started":"2022-08-14T05:38:25.023550Z","shell.execute_reply":"2022-08-14T05:38:25.760391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"genderplot=sns.barplot(x='Sex',y='Survived',data=dataset)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:26.618752Z","iopub.execute_input":"2022-08-14T05:38:26.619155Z","iopub.status.idle":"2022-08-14T05:38:26.894886Z","shell.execute_reply.started":"2022-08-14T05:38:26.619124Z","shell.execute_reply":"2022-08-14T05:38:26.893788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pclassplot=sns.catplot(x='Pclass',y='Survived',data=dataset,kind='bar',height=8)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:27.524869Z","iopub.execute_input":"2022-08-14T05:38:27.525261Z","iopub.status.idle":"2022-08-14T05:38:27.941593Z","shell.execute_reply.started":"2022-08-14T05:38:27.525229Z","shell.execute_reply":"2022-08-14T05:38:27.940480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bargraph_embark=sns.barplot(x='Embarked',y='Survived',data=dataset)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:30.468958Z","iopub.execute_input":"2022-08-14T05:38:30.469411Z","iopub.status.idle":"2022-08-14T05:38:30.769652Z","shell.execute_reply.started":"2022-08-14T05:38:30.469373Z","shell.execute_reply":"2022-08-14T05:38:30.768517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## DATA PREPOCESSING","metadata":{}},{"cell_type":"code","source":"mean=dataset['Age'].mean()\nstd=dataset['Age'].std()\nrand_age=np.random.randint(mean-std,mean+std,size=177)\nage=dataset['Age'].copy()\nage[np.isnan(age)]=rand_age\ndataset['Age']=age\n","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:33.282302Z","iopub.execute_input":"2022-08-14T05:38:33.283149Z","iopub.status.idle":"2022-08-14T05:38:33.293421Z","shell.execute_reply.started":"2022-08-14T05:38:33.283099Z","shell.execute_reply":"2022-08-14T05:38:33.292221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.Embarked.fillna(dataset.Embarked.mode()[0], inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:34.473260Z","iopub.execute_input":"2022-08-14T05:38:34.473631Z","iopub.status.idle":"2022-08-14T05:38:34.480393Z","shell.execute_reply.started":"2022-08-14T05:38:34.473601Z","shell.execute_reply":"2022-08-14T05:38:34.479080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:35.339864Z","iopub.execute_input":"2022-08-14T05:38:35.340728Z","iopub.status.idle":"2022-08-14T05:38:35.350298Z","shell.execute_reply.started":"2022-08-14T05:38:35.340680Z","shell.execute_reply":"2022-08-14T05:38:35.349095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop=['PassengerId','Name','Cabin','Ticket']\ndataset.drop(drop,axis=1,inplace=True)\ndataset.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:37.335522Z","iopub.execute_input":"2022-08-14T05:38:37.335925Z","iopub.status.idle":"2022-08-14T05:38:37.354638Z","shell.execute_reply.started":"2022-08-14T05:38:37.335891Z","shell.execute_reply":"2022-08-14T05:38:37.353453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"genders={'male':0,'female':1}\ndataset['Sex']=dataset['Sex'].map(genders)\npots={'S':0,'C':1,'Q':2}\ndataset['Embarked']=dataset['Embarked'].map(pots)\ndataset.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:38.704466Z","iopub.execute_input":"2022-08-14T05:38:38.704894Z","iopub.status.idle":"2022-08-14T05:38:38.723817Z","shell.execute_reply.started":"2022-08-14T05:38:38.704856Z","shell.execute_reply":"2022-08-14T05:38:38.722966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Splitting the dataset into the Training set and Test set","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX=dataset.iloc[:,1:].values\ny=dataset.iloc[:,0].values\nx_train,x_test,y_train,y_test= train_test_split(X,y,test_size=0.2,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:40.675568Z","iopub.execute_input":"2022-08-14T05:38:40.676012Z","iopub.status.idle":"2022-08-14T05:38:40.684882Z","shell.execute_reply.started":"2022-08-14T05:38:40.675975Z","shell.execute_reply":"2022-08-14T05:38:40.683411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:41.641662Z","iopub.execute_input":"2022-08-14T05:38:41.642314Z","iopub.status.idle":"2022-08-14T05:38:41.648498Z","shell.execute_reply.started":"2022-08-14T05:38:41.642281Z","shell.execute_reply":"2022-08-14T05:38:41.647486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:42.708742Z","iopub.execute_input":"2022-08-14T05:38:42.709512Z","iopub.status.idle":"2022-08-14T05:38:42.716801Z","shell.execute_reply.started":"2022-08-14T05:38:42.709473Z","shell.execute_reply":"2022-08-14T05:38:42.715907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:43.507568Z","iopub.execute_input":"2022-08-14T05:38:43.508036Z","iopub.status.idle":"2022-08-14T05:38:43.514613Z","shell.execute_reply.started":"2022-08-14T05:38:43.507999Z","shell.execute_reply":"2022-08-14T05:38:43.513594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:44.508714Z","iopub.execute_input":"2022-08-14T05:38:44.509522Z","iopub.status.idle":"2022-08-14T05:38:44.515366Z","shell.execute_reply.started":"2022-08-14T05:38:44.509481Z","shell.execute_reply":"2022-08-14T05:38:44.514146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Feature Scaling","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nsc = StandardScaler()\nx_train = sc.fit_transform(x_train)\nx_test = sc.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:46.147999Z","iopub.execute_input":"2022-08-14T05:38:46.148368Z","iopub.status.idle":"2022-08-14T05:38:46.157075Z","shell.execute_reply.started":"2022-08-14T05:38:46.148337Z","shell.execute_reply":"2022-08-14T05:38:46.155804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Random Forest","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nclassifier=RandomForestClassifier(random_state=42)\nclassifier.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:47.924151Z","iopub.execute_input":"2022-08-14T05:38:47.924957Z","iopub.status.idle":"2022-08-14T05:38:48.125117Z","shell.execute_reply.started":"2022-08-14T05:38:47.924913Z","shell.execute_reply":"2022-08-14T05:38:48.124252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = classifier.predict(x_test)\nprint(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:48.866401Z","iopub.execute_input":"2022-08-14T05:38:48.866815Z","iopub.status.idle":"2022-08-14T05:38:48.895625Z","shell.execute_reply.started":"2022-08-14T05:38:48.866782Z","shell.execute_reply":"2022-08-14T05:38:48.894391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, accuracy_score\ncm = confusion_matrix(y_test, y_pred)\nprint(cm)\nrf_acc=accuracy_score(y_test, y_pred)*100\nprint(rf_acc)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:49.580230Z","iopub.execute_input":"2022-08-14T05:38:49.580977Z","iopub.status.idle":"2022-08-14T05:38:49.591918Z","shell.execute_reply.started":"2022-08-14T05:38:49.580923Z","shell.execute_reply":"2022-08-14T05:38:49.590561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Logistics Regression","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nclassifier = LogisticRegression(random_state = 42)\nclassifier.fit(x_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:51.053191Z","iopub.execute_input":"2022-08-14T05:38:51.054006Z","iopub.status.idle":"2022-08-14T05:38:51.069952Z","shell.execute_reply.started":"2022-08-14T05:38:51.053950Z","shell.execute_reply":"2022-08-14T05:38:51.068580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = classifier.predict(x_test)\nprint(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:51.914510Z","iopub.execute_input":"2022-08-14T05:38:51.914919Z","iopub.status.idle":"2022-08-14T05:38:51.924784Z","shell.execute_reply.started":"2022-08-14T05:38:51.914886Z","shell.execute_reply":"2022-08-14T05:38:51.922292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, accuracy_score\ncm = confusion_matrix(y_test, y_pred)\nprint(cm)\nlr_acc=accuracy_score(y_test, y_pred)*100\nprint(lr_acc)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:52.634647Z","iopub.execute_input":"2022-08-14T05:38:52.635066Z","iopub.status.idle":"2022-08-14T05:38:52.643654Z","shell.execute_reply.started":"2022-08-14T05:38:52.635033Z","shell.execute_reply":"2022-08-14T05:38:52.642324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## K-Neighbour Classifier","metadata":{}},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nclassifier=KNeighborsClassifier(n_neighbors=5, p=2, metric='minkowski')\nclassifier.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:54.334106Z","iopub.execute_input":"2022-08-14T05:38:54.334725Z","iopub.status.idle":"2022-08-14T05:38:54.343182Z","shell.execute_reply.started":"2022-08-14T05:38:54.334671Z","shell.execute_reply":"2022-08-14T05:38:54.342022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = classifier.predict(x_test)\nprint(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:55.617737Z","iopub.execute_input":"2022-08-14T05:38:55.618141Z","iopub.status.idle":"2022-08-14T05:38:55.635785Z","shell.execute_reply.started":"2022-08-14T05:38:55.618109Z","shell.execute_reply":"2022-08-14T05:38:55.634733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_set_confusin_matrix\nfrom sklearn.metrics import confusion_matrix, accuracy_score\ncm = confusion_matrix(y_test, y_pred)\nprint(cm)\nkn_acc=accuracy_score(y_test, y_pred)*100\nprint(kn_acc)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:56.484644Z","iopub.execute_input":"2022-08-14T05:38:56.485135Z","iopub.status.idle":"2022-08-14T05:38:56.494112Z","shell.execute_reply.started":"2022-08-14T05:38:56.485096Z","shell.execute_reply":"2022-08-14T05:38:56.492736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Decision Tree","metadata":{}},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier\nclassifier=DecisionTreeClassifier(criterion='entropy',random_state=42)\nclassifier.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:58.132550Z","iopub.execute_input":"2022-08-14T05:38:58.133316Z","iopub.status.idle":"2022-08-14T05:38:58.144522Z","shell.execute_reply.started":"2022-08-14T05:38:58.133271Z","shell.execute_reply":"2022-08-14T05:38:58.143189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = classifier.predict(x_test)\nprint(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:59.041994Z","iopub.execute_input":"2022-08-14T05:38:59.042589Z","iopub.status.idle":"2022-08-14T05:38:59.050683Z","shell.execute_reply.started":"2022-08-14T05:38:59.042557Z","shell.execute_reply":"2022-08-14T05:38:59.049766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, accuracy_score\ncm = confusion_matrix(y_test, y_pred)\nprint(cm)\ndt_acc=accuracy_score(y_test, y_pred)*100\nprint(dt_acc)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:38:59.946883Z","iopub.execute_input":"2022-08-14T05:38:59.947867Z","iopub.status.idle":"2022-08-14T05:38:59.956662Z","shell.execute_reply.started":"2022-08-14T05:38:59.947812Z","shell.execute_reply":"2022-08-14T05:38:59.955398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Support Vector Machine","metadata":{}},{"cell_type":"code","source":"from sklearn.svm import SVC\nclassifier=SVC(kernel='linear',random_state=42)\nclassifier.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:39:01.423492Z","iopub.execute_input":"2022-08-14T05:39:01.423928Z","iopub.status.idle":"2022-08-14T05:39:01.451155Z","shell.execute_reply.started":"2022-08-14T05:39:01.423892Z","shell.execute_reply":"2022-08-14T05:39:01.450258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = classifier.predict(x_test)\nprint(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:39:02.164446Z","iopub.execute_input":"2022-08-14T05:39:02.165432Z","iopub.status.idle":"2022-08-14T05:39:02.175711Z","shell.execute_reply.started":"2022-08-14T05:39:02.165396Z","shell.execute_reply":"2022-08-14T05:39:02.174434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, accuracy_score\ncm = confusion_matrix(y_test, y_pred)\nprint(cm)\nsvm_acc=accuracy_score(y_test, y_pred)*100\nprint(svm_acc)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:39:03.039095Z","iopub.execute_input":"2022-08-14T05:39:03.039671Z","iopub.status.idle":"2022-08-14T05:39:03.048684Z","shell.execute_reply.started":"2022-08-14T05:39:03.039636Z","shell.execute_reply":"2022-08-14T05:39:03.047489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Accuracy of Random Forest Classifier =\",rf_acc)\nprint(\"Accuracy of Logistic Regressor =\",lr_acc)\nprint(\"Accuracy of K-Neighbor Classifier =\",kn_acc)\nprint(\"Accuracy of Decision Tree Classifier = \",dt_acc)\nprint(\"Accuracy of Support Vector Machine Classifier = \",svm_acc)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:39:03.911593Z","iopub.execute_input":"2022-08-14T05:39:03.912262Z","iopub.status.idle":"2022-08-14T05:39:03.918488Z","shell.execute_reply.started":"2022-08-14T05:39:03.912190Z","shell.execute_reply":"2022-08-14T05:39:03.917516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Final Prediction","metadata":{}},{"cell_type":"code","source":"test_data = pd.read_csv('/kaggle/input/titanic/test.csv')\ntest_data.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:39:09.041653Z","iopub.execute_input":"2022-08-14T05:39:09.042356Z","iopub.status.idle":"2022-08-14T05:39:09.071405Z","shell.execute_reply.started":"2022-08-14T05:39:09.042317Z","shell.execute_reply":"2022-08-14T05:39:09.070408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:39:09.993447Z","iopub.execute_input":"2022-08-14T05:39:09.994244Z","iopub.status.idle":"2022-08-14T05:39:10.009365Z","shell.execute_reply.started":"2022-08-14T05:39:09.994184Z","shell.execute_reply":"2022-08-14T05:39:10.007846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:39:11.315488Z","iopub.execute_input":"2022-08-14T05:39:11.316622Z","iopub.status.idle":"2022-08-14T05:39:11.325348Z","shell.execute_reply.started":"2022-08-14T05:39:11.316580Z","shell.execute_reply":"2022-08-14T05:39:11.324438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean=test_data['Age'].mean()\nstd=test_data['Age'].std()\nrand_age=np.random.randint(mean-std,mean+std,size=86)\nage=test_data['Age'].copy()\nage[np.isnan(age)]=rand_age\ntest_data['Age']=age\ntest_data['Fare'].fillna(test_data['Fare'].mean(), inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:39:12.283662Z","iopub.execute_input":"2022-08-14T05:39:12.284109Z","iopub.status.idle":"2022-08-14T05:39:12.297582Z","shell.execute_reply.started":"2022-08-14T05:39:12.284073Z","shell.execute_reply":"2022-08-14T05:39:12.296388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col_to_drop = [\"PassengerId\", \"Ticket\", \"Cabin\", \"Name\"]\ntest_data.drop(col_to_drop, axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:39:13.043080Z","iopub.execute_input":"2022-08-14T05:39:13.043480Z","iopub.status.idle":"2022-08-14T05:39:13.049827Z","shell.execute_reply.started":"2022-08-14T05:39:13.043447Z","shell.execute_reply":"2022-08-14T05:39:13.048534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:39:13.762155Z","iopub.execute_input":"2022-08-14T05:39:13.762755Z","iopub.status.idle":"2022-08-14T05:39:13.771756Z","shell.execute_reply.started":"2022-08-14T05:39:13.762720Z","shell.execute_reply":"2022-08-14T05:39:13.770790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:39:14.586808Z","iopub.execute_input":"2022-08-14T05:39:14.587525Z","iopub.status.idle":"2022-08-14T05:39:14.603965Z","shell.execute_reply.started":"2022-08-14T05:39:14.587487Z","shell.execute_reply":"2022-08-14T05:39:14.602721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"genders={'male':0,'female':1}\ntest_data['Sex']=test_data['Sex'].map(genders)\npots={'S':0,'C':1,'Q':2}\ntest_data['Embarked']=test_data['Embarked'].map(pots)\ntest_data.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:39:15.595903Z","iopub.execute_input":"2022-08-14T05:39:15.596868Z","iopub.status.idle":"2022-08-14T05:39:15.616526Z","shell.execute_reply.started":"2022-08-14T05:39:15.596821Z","shell.execute_reply":"2022-08-14T05:39:15.615142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = test_data\ny_pred = classifier.predict(X_test)\noriginaltest_data = pd.read_csv('/kaggle/input/titanic/test.csv')\nsubmission = pd.DataFrame({\n        \"PassengerId\": originaltest_data[\"PassengerId\"],\n        \"Survived\": y_pred\n    })\nsubmission.head(20)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:39:16.474068Z","iopub.execute_input":"2022-08-14T05:39:16.475453Z","iopub.status.idle":"2022-08-14T05:39:16.503713Z","shell.execute_reply.started":"2022-08-14T05:39:16.475410Z","shell.execute_reply":"2022-08-14T05:39:16.502527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:39:17.312597Z","iopub.execute_input":"2022-08-14T05:39:17.313543Z","iopub.status.idle":"2022-08-14T05:39:17.322101Z","shell.execute_reply.started":"2022-08-14T05:39:17.313502Z","shell.execute_reply":"2022-08-14T05:39:17.321078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}