{"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":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-07-09T09:39:00.295227Z","iopub.execute_input":"2022-07-09T09:39:00.295703Z","iopub.status.idle":"2022-07-09T09:39:00.301803Z","shell.execute_reply.started":"2022-07-09T09:39:00.295655Z","shell.execute_reply":"2022-07-09T09:39:00.300917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_train1=pd.read_csv(\"../input/titanic/train.csv\")\ntitanic_test=pd.read_csv(\"../input/titanic/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-09T09:39:00.323031Z","iopub.execute_input":"2022-07-09T09:39:00.323749Z","iopub.status.idle":"2022-07-09T09:39:00.345612Z","shell.execute_reply.started":"2022-07-09T09:39:00.323681Z","shell.execute_reply":"2022-07-09T09:39:00.343859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_train1.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T09:39:00.348414Z","iopub.execute_input":"2022-07-09T09:39:00.348821Z","iopub.status.idle":"2022-07-09T09:39:00.368283Z","shell.execute_reply.started":"2022-07-09T09:39:00.348775Z","shell.execute_reply":"2022-07-09T09:39:00.367153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_train=titanic_train1.drop([\"PassengerId\",\"Name\",\"SibSp\",\"Parch\",\"Ticket\",\"Cabin\",\"Embarked\"],axis=\"columns\")\ntitanic_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T09:39:00.369698Z","iopub.execute_input":"2022-07-09T09:39:00.370694Z","iopub.status.idle":"2022-07-09T09:39:00.391491Z","shell.execute_reply.started":"2022-07-09T09:39:00.370644Z","shell.execute_reply":"2022-07-09T09:39:00.390632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2 = pd.get_dummies(titanic_train,columns=['Sex'])\ndf2.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T09:39:00.393839Z","iopub.execute_input":"2022-07-09T09:39:00.394555Z","iopub.status.idle":"2022-07-09T09:39:00.416956Z","shell.execute_reply.started":"2022-07-09T09:39:00.394512Z","shell.execute_reply":"2022-07-09T09:39:00.415795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corrMatrix = df2.corr()\nsns.set(rc = {'figure.figsize':(15,8)})\nsns.heatmap(corrMatrix, annot=True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T09:39:00.418468Z","iopub.execute_input":"2022-07-09T09:39:00.418834Z","iopub.status.idle":"2022-07-09T09:39:00.946338Z","shell.execute_reply.started":"2022-07-09T09:39:00.418802Z","shell.execute_reply":"2022-07-09T09:39:00.944993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3=df2[['Fare','Sex_female','Survived']]\n","metadata":{"execution":{"iopub.status.busy":"2022-07-09T09:39:00.948077Z","iopub.execute_input":"2022-07-09T09:39:00.948439Z","iopub.status.idle":"2022-07-09T09:39:00.959420Z","shell.execute_reply.started":"2022-07-09T09:39:00.948394Z","shell.execute_reply":"2022-07-09T09:39:00.956265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3['Survived'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T09:39:00.960902Z","iopub.execute_input":"2022-07-09T09:39:00.961401Z","iopub.status.idle":"2022-07-09T09:39:00.971657Z","shell.execute_reply.started":"2022-07-09T09:39:00.961361Z","shell.execute_reply":"2022-07-09T09:39:00.970743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(5,5))\nplt.bar(list(df2['Survived'].value_counts().keys()),list(df2['Survived'].value_counts()),color=['r','g'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T09:39:00.973473Z","iopub.execute_input":"2022-07-09T09:39:00.974433Z","iopub.status.idle":"2022-07-09T09:39:01.247473Z","shell.execute_reply.started":"2022-07-09T09:39:00.974390Z","shell.execute_reply":"2022-07-09T09:39:01.246308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PART 1\n# import random undersampling and other necessary libraries \nfrom collections import Counter\nfrom imblearn.under_sampling import RandomUnderSampler\nfrom sklearn.model_selection import train_test_split\nimport pandas as pd\nimport numpy as np\nimport warnings\nwarnings.simplefilter(action='ignore', category=FutureWarning)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T09:39:01.249313Z","iopub.execute_input":"2022-07-09T09:39:01.249669Z","iopub.status.idle":"2022-07-09T09:39:01.256010Z","shell.execute_reply.started":"2022-07-09T09:39:01.249623Z","shell.execute_reply":"2022-07-09T09:39:01.255055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df3.iloc[:,:-1]\ny = df3.iloc[:,-1]\n\n\n#Split train-test data\nX_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.30)\n\n# summarize class distribution\nprint(\"Before undersampling: \", Counter(y_train))\n\n# define undersampling strategy\nundersample = RandomUnderSampler(sampling_strategy='majority')\n\n# fit and apply the transform\nX_train_under, y_train_under = undersample.fit_resample(X_train, y_train)\n\n# summarize class distribution\nprint(\"After undersampling: \", Counter(y_train_under))\n\n#PART 2\n# import SVM libraries \nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import classification_report, roc_auc_score\n\nmodel=RandomForestClassifier()\nclf_under = model.fit(X_train_under, y_train_under)\npred_under = clf_under.predict(X_test)\n\nprint(\"ROC AUC score for undersampled data: \", roc_auc_score(y_test, pred_under))\n","metadata":{"execution":{"iopub.status.busy":"2022-07-09T09:39:01.257985Z","iopub.execute_input":"2022-07-09T09:39:01.258783Z","iopub.status.idle":"2022-07-09T09:39:01.503845Z","shell.execute_reply.started":"2022-07-09T09:39:01.258718Z","shell.execute_reply":"2022-07-09T09:39:01.503019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import metrics\nfrom sklearn.metrics import recall_score\nmodel_score = clf_under.score(X_test, y_test)\nprint(model_score)\nprint(metrics.confusion_matrix(y_test, pred_under))\nprint(metrics.classification_report(y_test, pred_under))","metadata":{"execution":{"iopub.status.busy":"2022-07-09T09:39:01.505188Z","iopub.execute_input":"2022-07-09T09:39:01.506919Z","iopub.status.idle":"2022-07-09T09:39:01.537007Z","shell.execute_reply.started":"2022-07-09T09:39:01.506871Z","shell.execute_reply":"2022-07-09T09:39:01.536180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy.stats import randint\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.model_selection import RandomizedSearchCV\n  \n# Creating the hyperparameter grid \nparam_dist = {\"max_depth\": [3, None],\n              \"max_features\": randint(1, 9),\n              \"min_samples_leaf\": randint(1, 9),\n              \"criterion\": [\"gini\", \"entropy\"]}\n  \n# Instantiating Decision Tree classifier\ntree = DecisionTreeClassifier()\n  \n# Instantiating RandomizedSearchCV object\ntree_cv = RandomizedSearchCV(tree, param_dist, cv = 5)\n  \ntree_cv.fit(X_test,y_test)\n  \n# Print the tuned parameters and score\nprint(\"Tuned Decision Tree Parameters: {}\".format(tree_cv.best_params_))\nprint(\"Best score is {}\".format(tree_cv.best_score_))","metadata":{"execution":{"iopub.status.busy":"2022-07-09T09:39:01.538751Z","iopub.execute_input":"2022-07-09T09:39:01.539324Z","iopub.status.idle":"2022-07-09T09:39:01.737872Z","shell.execute_reply.started":"2022-07-09T09:39:01.539281Z","shell.execute_reply":"2022-07-09T09:39:01.734789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}