{"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 sklearn\nimport pandas as pd\nimport numpy as np\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-14T12:32:00.036079Z","iopub.execute_input":"2022-08-14T12:32:00.039403Z","iopub.status.idle":"2022-08-14T12:32:00.048555Z","shell.execute_reply.started":"2022-08-14T12:32:00.039343Z","shell.execute_reply":"2022-08-14T12:32:00.046903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv(os.path.join('../input/titanic','train.csv'),index_col='PassengerId')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:32:23.942477Z","iopub.execute_input":"2022-08-14T12:32:23.942866Z","iopub.status.idle":"2022-08-14T12:32:23.993620Z","shell.execute_reply.started":"2022-08-14T12:32:23.942837Z","shell.execute_reply":"2022-08-14T12:32:23.992174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:32:33.708239Z","iopub.execute_input":"2022-08-14T12:32:33.709458Z","iopub.status.idle":"2022-08-14T12:32:33.754567Z","shell.execute_reply.started":"2022-08-14T12:32:33.709397Z","shell.execute_reply":"2022-08-14T12:32:33.753256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:32:40.145447Z","iopub.execute_input":"2022-08-14T12:32:40.145851Z","iopub.status.idle":"2022-08-14T12:32:40.159661Z","shell.execute_reply.started":"2022-08-14T12:32:40.145822Z","shell.execute_reply":"2022-08-14T12:32:40.158053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop(columns=['Name','Ticket','Cabin'],inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:32:46.230937Z","iopub.execute_input":"2022-08-14T12:32:46.231321Z","iopub.status.idle":"2022-08-14T12:32:46.240016Z","shell.execute_reply.started":"2022-08-14T12:32:46.231289Z","shell.execute_reply":"2022-08-14T12:32:46.238405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df.shape)\nprint(df.columns)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:32:52.663413Z","iopub.execute_input":"2022-08-14T12:32:52.663866Z","iopub.status.idle":"2022-08-14T12:32:52.672563Z","shell.execute_reply.started":"2022-08-14T12:32:52.663837Z","shell.execute_reply":"2022-08-14T12:32:52.670885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:33:01.066370Z","iopub.execute_input":"2022-08-14T12:33:01.067045Z","iopub.status.idle":"2022-08-14T12:33:01.083556Z","shell.execute_reply.started":"2022-08-14T12:33:01.067003Z","shell.execute_reply":"2022-08-14T12:33:01.081382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.impute import SimpleImputer","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:33:06.582286Z","iopub.execute_input":"2022-08-14T12:33:06.582942Z","iopub.status.idle":"2022-08-14T12:33:06.847745Z","shell.execute_reply.started":"2022-08-14T12:33:06.582897Z","shell.execute_reply":"2022-08-14T12:33:06.846204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numerical_imputer=SimpleImputer(missing_values=np.NaN,strategy='mean')\nnumerical_imputer.fit(df[['Age']])","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:33:11.396057Z","iopub.execute_input":"2022-08-14T12:33:11.396464Z","iopub.status.idle":"2022-08-14T12:33:11.415427Z","shell.execute_reply.started":"2022-08-14T12:33:11.396435Z","shell.execute_reply":"2022-08-14T12:33:11.414210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[['Age']]=numerical_imputer.fit_transform(df[['Age']])\ndf[['Age']].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:33:16.785467Z","iopub.execute_input":"2022-08-14T12:33:16.785839Z","iopub.status.idle":"2022-08-14T12:33:16.805633Z","shell.execute_reply.started":"2022-08-14T12:33:16.785809Z","shell.execute_reply":"2022-08-14T12:33:16.804367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical_imputer=SimpleImputer(missing_values=np.NaN,strategy='most_frequent')\ncategorical_imputer.fit(df[['Embarked']])","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:33:21.757911Z","iopub.execute_input":"2022-08-14T12:33:21.758367Z","iopub.status.idle":"2022-08-14T12:33:21.774404Z","shell.execute_reply.started":"2022-08-14T12:33:21.758308Z","shell.execute_reply":"2022-08-14T12:33:21.772657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[['Embarked']]=categorical_imputer.fit_transform(df[['Embarked']])\ndf[['Embarked']].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:33:25.995685Z","iopub.execute_input":"2022-08-14T12:33:25.996308Z","iopub.status.idle":"2022-08-14T12:33:26.023425Z","shell.execute_reply.started":"2022-08-14T12:33:25.996265Z","shell.execute_reply":"2022-08-14T12:33:26.022184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=df.drop(columns=['Survived'],axis=1)\ny=pd.DataFrame(df['Survived'])","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:33:30.135780Z","iopub.execute_input":"2022-08-14T12:33:30.136138Z","iopub.status.idle":"2022-08-14T12:33:30.146746Z","shell.execute_reply.started":"2022-08-14T12:33:30.136109Z","shell.execute_reply":"2022-08-14T12:33:30.145520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:33:34.775299Z","iopub.execute_input":"2022-08-14T12:33:34.775731Z","iopub.status.idle":"2022-08-14T12:33:34.790606Z","shell.execute_reply.started":"2022-08-14T12:33:34.775701Z","shell.execute_reply":"2022-08-14T12:33:34.788800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_labels=['Died','Survived']","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:33:39.340839Z","iopub.execute_input":"2022-08-14T12:33:39.341283Z","iopub.status.idle":"2022-08-14T12:33:39.348224Z","shell.execute_reply.started":"2022-08-14T12:33:39.341205Z","shell.execute_reply":"2022-08-14T12:33:39.346380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:33:45.072700Z","iopub.execute_input":"2022-08-14T12:33:45.073272Z","iopub.status.idle":"2022-08-14T12:33:45.080568Z","shell.execute_reply.started":"2022-08-14T12:33:45.073201Z","shell.execute_reply":"2022-08-14T12:33:45.079185Z"},"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=.2,shuffle=True)\nx_train.shape,y_train.shape,x_test.shape,y_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:33:50.206171Z","iopub.execute_input":"2022-08-14T12:33:50.206744Z","iopub.status.idle":"2022-08-14T12:33:50.228131Z","shell.execute_reply.started":"2022-08-14T12:33:50.206700Z","shell.execute_reply":"2022-08-14T12:33:50.226885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train=np.squeeze(y_train.to_numpy())\ny_test=np.squeeze(y_test.to_numpy())","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:33:54.966359Z","iopub.execute_input":"2022-08-14T12:33:54.966892Z","iopub.status.idle":"2022-08-14T12:33:54.978749Z","shell.execute_reply.started":"2022-08-14T12:33:54.966851Z","shell.execute_reply":"2022-08-14T12:33:54.977077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.pipeline import make_pipeline\nfrom sklearn.preprocessing import StandardScaler,OneHotEncoder\nfrom sklearn.compose import ColumnTransformer","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:34:00.037492Z","iopub.execute_input":"2022-08-14T12:34:00.037890Z","iopub.status.idle":"2022-08-14T12:34:00.051527Z","shell.execute_reply.started":"2022-08-14T12:34:00.037861Z","shell.execute_reply":"2022-08-14T12:34:00.050094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numerical_features=x_train.select_dtypes(exclude='object').columns\nnumerical_pipeline=make_pipeline(StandardScaler())\nnumerical_pipeline","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:34:04.386029Z","iopub.execute_input":"2022-08-14T12:34:04.386449Z","iopub.status.idle":"2022-08-14T12:34:04.400483Z","shell.execute_reply.started":"2022-08-14T12:34:04.386418Z","shell.execute_reply":"2022-08-14T12:34:04.398733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical_features=x_train.select_dtypes('object').columns\ncategorical_pipeline=make_pipeline(OneHotEncoder())\ncategorical_pipeline","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:34:09.840857Z","iopub.execute_input":"2022-08-14T12:34:09.841450Z","iopub.status.idle":"2022-08-14T12:34:09.856068Z","shell.execute_reply.started":"2022-08-14T12:34:09.841398Z","shell.execute_reply":"2022-08-14T12:34:09.854375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transformer=ColumnTransformer([\n    ('numerical_pipeline',numerical_pipeline,numerical_features),\n    ('categorical_pipeline',categorical_pipeline,categorical_features)\n])\ntransformer","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:34:14.910646Z","iopub.execute_input":"2022-08-14T12:34:14.911127Z","iopub.status.idle":"2022-08-14T12:34:14.942887Z","shell.execute_reply.started":"2022-08-14T12:34:14.911058Z","shell.execute_reply":"2022-08-14T12:34:14.941771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.naive_bayes import GaussianNB\nfrom sklearn.ensemble import RandomForestClassifier,AdaBoostClassifier","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:34:20.613268Z","iopub.execute_input":"2022-08-14T12:34:20.613783Z","iopub.status.idle":"2022-08-14T12:34:20.713544Z","shell.execute_reply.started":"2022-08-14T12:34:20.613753Z","shell.execute_reply":"2022-08-14T12:34:20.712287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LRpipeline=make_pipeline(transformer,LogisticRegression())\nKNCpipeline=make_pipeline(transformer,KNeighborsClassifier())\nGNBpipeline=make_pipeline(transformer,GaussianNB())\nRFCpipeline=make_pipeline(transformer,RandomForestClassifier())\nABCpipeline=make_pipeline(transformer,AdaBoostClassifier())","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:34:25.503757Z","iopub.execute_input":"2022-08-14T12:34:25.504121Z","iopub.status.idle":"2022-08-14T12:34:25.513660Z","shell.execute_reply.started":"2022-08-14T12:34:25.504093Z","shell.execute_reply":"2022-08-14T12:34:25.510359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LRpipeline.fit(x_train,y_train)\nKNCpipeline.fit(x_train,y_train)\nGNBpipeline.fit(x_train,y_train)\nRFCpipeline.fit(x_train,y_train)\nABCpipeline.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:34:30.248958Z","iopub.execute_input":"2022-08-14T12:34:30.249488Z","iopub.status.idle":"2022-08-14T12:34:30.641840Z","shell.execute_reply.started":"2022-08-14T12:34:30.249456Z","shell.execute_reply":"2022-08-14T12:34:30.640412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'LogisticRegression : {LRpipeline.score(x_test,y_test)}')\nprint(f'KNeighborsClassifier: {KNCpipeline.score(x_test,y_test)}')\nprint(f'GaussianNB : {GNBpipeline.score(x_test,y_test)}')\nprint(f'RandomForestClassifier : {RFCpipeline.score(x_test,y_test)}')\nprint(f'AdaBoostClassifier : {ABCpipeline.score(x_test,y_test)}')","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:34:36.173211Z","iopub.execute_input":"2022-08-14T12:34:36.174598Z","iopub.status.idle":"2022-08-14T12:34:36.255918Z","shell.execute_reply.started":"2022-08-14T12:34:36.174557Z","shell.execute_reply":"2022-08-14T12:34:36.254380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\nimport random","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:34:43.913924Z","iopub.execute_input":"2022-08-14T12:34:43.914635Z","iopub.status.idle":"2022-08-14T12:34:43.925313Z","shell.execute_reply.started":"2022-08-14T12:34:43.914590Z","shell.execute_reply":"2022-08-14T12:34:43.923690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=random.randint(0,169)\ny_preds=ABCpipeline.predict(x_test)\nprint('Actual','Predicted')\nfor i in range(x,x+10):\n    print(f'{class_labels[y_test[i]]}   {class_labels[y_preds[i]]}')","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:36:58.833133Z","iopub.execute_input":"2022-08-14T12:36:58.834402Z","iopub.status.idle":"2022-08-14T12:36:58.864159Z","shell.execute_reply.started":"2022-08-14T12:36:58.834370Z","shell.execute_reply":"2022-08-14T12:36:58.862396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import ConfusionMatrixDisplay\nfrom matplotlib import pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:37:09.983398Z","iopub.execute_input":"2022-08-14T12:37:09.983965Z","iopub.status.idle":"2022-08-14T12:37:09.991846Z","shell.execute_reply.started":"2022-08-14T12:37:09.983902Z","shell.execute_reply":"2022-08-14T12:37:09.990243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(15, 8))\nConfusionMatrixDisplay.from_predictions(y_test, y_preds, ax=ax,cmap=plt.cm.plasma)\nax.xaxis.set_ticklabels(class_labels)\nax.yaxis.set_ticklabels(class_labels)\nax.set_title(\"Confusion Matrix for RandomForestClassifier\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:37:14.027240Z","iopub.execute_input":"2022-08-14T12:37:14.027758Z","iopub.status.idle":"2022-08-14T12:37:14.372288Z","shell.execute_reply.started":"2022-08-14T12:37:14.027686Z","shell.execute_reply":"2022-08-14T12:37:14.370130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df=pd.read_csv(os.path.join('../input/titanic','test.csv'),index_col='PassengerId')\ntest_df.drop(columns=['Name','Ticket','Cabin'],inplace=True)\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:37:32.027677Z","iopub.execute_input":"2022-08-14T12:37:32.028054Z","iopub.status.idle":"2022-08-14T12:37:32.057958Z","shell.execute_reply.started":"2022-08-14T12:37:32.028024Z","shell.execute_reply":"2022-08-14T12:37:32.056583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:37:39.024668Z","iopub.execute_input":"2022-08-14T12:37:39.025272Z","iopub.status.idle":"2022-08-14T12:37:39.037773Z","shell.execute_reply.started":"2022-08-14T12:37:39.025228Z","shell.execute_reply":"2022-08-14T12:37:39.036382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:37:43.749092Z","iopub.execute_input":"2022-08-14T12:37:43.750784Z","iopub.status.idle":"2022-08-14T12:37:43.768092Z","shell.execute_reply.started":"2022-08-14T12:37:43.750743Z","shell.execute_reply":"2022-08-14T12:37:43.766215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numerical_imputer=SimpleImputer()\nnumerical_imputer.fit(test_df[['Age','Fare']])","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:37:50.968834Z","iopub.execute_input":"2022-08-14T12:37:50.969196Z","iopub.status.idle":"2022-08-14T12:37:50.985188Z","shell.execute_reply.started":"2022-08-14T12:37:50.969166Z","shell.execute_reply":"2022-08-14T12:37:50.983384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df[['Age','Fare']]=numerical_imputer.fit_transform(test_df[['Age','Fare']])\ntest_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:37:55.250300Z","iopub.execute_input":"2022-08-14T12:37:55.251097Z","iopub.status.idle":"2022-08-14T12:37:55.271151Z","shell.execute_reply.started":"2022-08-14T12:37:55.251066Z","shell.execute_reply":"2022-08-14T12:37:55.269674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.sort_index(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:38:00.553965Z","iopub.execute_input":"2022-08-14T12:38:00.554462Z","iopub.status.idle":"2022-08-14T12:38:00.564841Z","shell.execute_reply.started":"2022-08-14T12:38:00.554430Z","shell.execute_reply":"2022-08-14T12:38:00.563070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds=ABCpipeline.predict(test_df)\nsubmission=pd.DataFrame(np.squeeze(pd.DataFrame(test_df.index).to_numpy()),columns=['PassengerId'])\nsubmission['Survived']=test_preds\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:38:20.460071Z","iopub.execute_input":"2022-08-14T12:38:20.460486Z","iopub.status.idle":"2022-08-14T12:38:20.498678Z","shell.execute_reply.started":"2022-08-14T12:38:20.460456Z","shell.execute_reply":"2022-08-14T12:38:20.497362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:38:46.387691Z","iopub.execute_input":"2022-08-14T12:38:46.388095Z","iopub.status.idle":"2022-08-14T12:38:46.399683Z","shell.execute_reply.started":"2022-08-14T12:38:46.388065Z","shell.execute_reply":"2022-08-14T12:38:46.398216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}