{"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 numpy as np \nimport pandas as pd \nimport sklearn\nimport seaborn as sns\nnp.random.seed(42)\n\n# To plot pretty figures\n%matplotlib inline\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\n\n#train test split\nfrom sklearn.model_selection import train_test_split\n\n#mdels\nfrom sklearn.linear_model import SGDClassifier\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\n\n#data scaler\nfrom sklearn.preprocessing import StandardScaler\n\n#object data processor\nfrom sklearn.preprocessing import OneHotEncoder\n\n#grid search\nfrom sklearn.model_selection import GridSearchCV\n\n\n#evaluator\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, precision_recall_curve, roc_curve\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-31T12:52:27.777358Z","iopub.execute_input":"2022-07-31T12:52:27.777806Z","iopub.status.idle":"2022-07-31T12:52:28.859932Z","shell.execute_reply.started":"2022-07-31T12:52:27.777722Z","shell.execute_reply":"2022-07-31T12:52:28.858582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/titanic/train.csv')\ntest = pd.read_csv('../input/titanic/test.csv')\nsubmission = pd.read_csv('../input/titanic/gender_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:28.862518Z","iopub.execute_input":"2022-07-31T12:52:28.864293Z","iopub.status.idle":"2022-07-31T12:52:28.900652Z","shell.execute_reply.started":"2022-07-31T12:52:28.864243Z","shell.execute_reply":"2022-07-31T12:52:28.899660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:28.901829Z","iopub.execute_input":"2022-07-31T12:52:28.902718Z","iopub.status.idle":"2022-07-31T12:52:28.934028Z","shell.execute_reply.started":"2022-07-31T12:52:28.902681Z","shell.execute_reply":"2022-07-31T12:52:28.933156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:28.936224Z","iopub.execute_input":"2022-07-31T12:52:28.937045Z","iopub.status.idle":"2022-07-31T12:52:28.960796Z","shell.execute_reply.started":"2022-07-31T12:52:28.937012Z","shell.execute_reply":"2022-07-31T12:52:28.959367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.drop(columns='Survived')\ny = train.Survived","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:28.962413Z","iopub.execute_input":"2022-07-31T12:52:28.962759Z","iopub.status.idle":"2022-07-31T12:52:28.971283Z","shell.execute_reply.started":"2022-07-31T12:52:28.962727Z","shell.execute_reply":"2022-07-31T12:52:28.969997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.index = X.PassengerId\nX = X.drop(columns='PassengerId')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:28.972870Z","iopub.execute_input":"2022-07-31T12:52:28.973539Z","iopub.status.idle":"2022-07-31T12:52:28.988142Z","shell.execute_reply.started":"2022-07-31T12:52:28.973497Z","shell.execute_reply":"2022-07-31T12:52:28.986994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:28.989616Z","iopub.execute_input":"2022-07-31T12:52:28.990175Z","iopub.status.idle":"2022-07-31T12:52:29.012477Z","shell.execute_reply.started":"2022-07-31T12:52:28.990144Z","shell.execute_reply":"2022-07-31T12:52:29.011227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:29.013698Z","iopub.execute_input":"2022-07-31T12:52:29.014225Z","iopub.status.idle":"2022-07-31T12:52:29.033198Z","shell.execute_reply.started":"2022-07-31T12:52:29.014192Z","shell.execute_reply":"2022-07-31T12:52:29.032077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Pclass EDA\n* non-null\n* 1,2,3\n* 3 is most frequent","metadata":{}},{"cell_type":"code","source":"(X.Pclass.isnull()).sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:29.034628Z","iopub.execute_input":"2022-07-31T12:52:29.034980Z","iopub.status.idle":"2022-07-31T12:52:29.045068Z","shell.execute_reply.started":"2022-07-31T12:52:29.034939Z","shell.execute_reply":"2022-07-31T12:52:29.043665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(X.Pclass)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:29.049197Z","iopub.execute_input":"2022-07-31T12:52:29.050313Z","iopub.status.idle":"2022-07-31T12:52:29.206796Z","shell.execute_reply.started":"2022-07-31T12:52:29.050266Z","shell.execute_reply":"2022-07-31T12:52:29.205570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Name EDA\n* non-null\n* people name\n* may not be useful","metadata":{}},{"cell_type":"code","source":"X.Name.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:29.208133Z","iopub.execute_input":"2022-07-31T12:52:29.208470Z","iopub.status.idle":"2022-07-31T12:52:29.218743Z","shell.execute_reply.started":"2022-07-31T12:52:29.208439Z","shell.execute_reply":"2022-07-31T12:52:29.217538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Sex EDA\n* object\n* male is dominant","metadata":{}},{"cell_type":"code","source":"sns.countplot(X.Sex)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:29.220399Z","iopub.execute_input":"2022-07-31T12:52:29.220985Z","iopub.status.idle":"2022-07-31T12:52:29.386536Z","shell.execute_reply.started":"2022-07-31T12:52:29.220941Z","shell.execute_reply":"2022-07-31T12:52:29.385641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Age EDA\n* 177 null\n* mean = 29.699\n* median =28","metadata":{}},{"cell_type":"code","source":"X.Age.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:29.387711Z","iopub.execute_input":"2022-07-31T12:52:29.388193Z","iopub.status.idle":"2022-07-31T12:52:29.395700Z","shell.execute_reply.started":"2022-07-31T12:52:29.388157Z","shell.execute_reply":"2022-07-31T12:52:29.394499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(X.Age)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:29.397547Z","iopub.execute_input":"2022-07-31T12:52:29.398042Z","iopub.status.idle":"2022-07-31T12:52:29.648699Z","shell.execute_reply.started":"2022-07-31T12:52:29.398000Z","shell.execute_reply":"2022-07-31T12:52:29.647738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.Age.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:29.649957Z","iopub.execute_input":"2022-07-31T12:52:29.650496Z","iopub.status.idle":"2022-07-31T12:52:29.661050Z","shell.execute_reply.started":"2022-07-31T12:52:29.650464Z","shell.execute_reply":"2022-07-31T12:52:29.659788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Sibsp EDA\n* number of siblings or spouse\n* non null\n* 0 or 1 are dominant","metadata":{}},{"cell_type":"code","source":"sns.countplot(X.SibSp)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:29.662762Z","iopub.execute_input":"2022-07-31T12:52:29.663298Z","iopub.status.idle":"2022-07-31T12:52:29.856899Z","shell.execute_reply.started":"2022-07-31T12:52:29.663262Z","shell.execute_reply":"2022-07-31T12:52:29.855430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"parch EDA\n* number of parents / children\n* non-null\n* 0, 1, 2 are dominant","metadata":{}},{"cell_type":"code","source":"sns.countplot(X.Parch)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:29.858605Z","iopub.execute_input":"2022-07-31T12:52:29.859277Z","iopub.status.idle":"2022-07-31T12:52:30.059357Z","shell.execute_reply.started":"2022-07-31T12:52:29.859242Z","shell.execute_reply":"2022-07-31T12:52:30.058144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Ticker EDA\n* oject\n* many unique\n* may be deleted","metadata":{}},{"cell_type":"code","source":"X.Ticket.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:30.060794Z","iopub.execute_input":"2022-07-31T12:52:30.061179Z","iopub.status.idle":"2022-07-31T12:52:30.072601Z","shell.execute_reply.started":"2022-07-31T12:52:30.061142Z","shell.execute_reply":"2022-07-31T12:52:30.071468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.Ticket.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:30.074134Z","iopub.execute_input":"2022-07-31T12:52:30.074952Z","iopub.status.idle":"2022-07-31T12:52:30.087195Z","shell.execute_reply.started":"2022-07-31T12:52:30.074907Z","shell.execute_reply":"2022-07-31T12:52:30.086003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Fare EDA\n* Passenger fare\n* non-null\n* mean =32\n* std is wide","metadata":{}},{"cell_type":"code","source":"sns.histplot(X.Fare)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:30.088569Z","iopub.execute_input":"2022-07-31T12:52:30.089133Z","iopub.status.idle":"2022-07-31T12:52:30.481164Z","shell.execute_reply.started":"2022-07-31T12:52:30.089094Z","shell.execute_reply":"2022-07-31T12:52:30.479879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.Fare.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:30.482572Z","iopub.execute_input":"2022-07-31T12:52:30.482901Z","iopub.status.idle":"2022-07-31T12:52:30.493646Z","shell.execute_reply.started":"2022-07-31T12:52:30.482871Z","shell.execute_reply":"2022-07-31T12:52:30.492866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Cabin EDA\n* too many null\n* not necessary","metadata":{}},{"cell_type":"markdown","source":"Embarked EDA\n* Embarked place\n* object\n* 2 null\n* 3 nunique\n* S is most frequent","metadata":{}},{"cell_type":"code","source":"X.Embarked.nunique()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:30.494995Z","iopub.execute_input":"2022-07-31T12:52:30.495509Z","iopub.status.idle":"2022-07-31T12:52:30.506352Z","shell.execute_reply.started":"2022-07-31T12:52:30.495478Z","shell.execute_reply":"2022-07-31T12:52:30.505364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(X.Embarked\n             )","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:30.507770Z","iopub.execute_input":"2022-07-31T12:52:30.508397Z","iopub.status.idle":"2022-07-31T12:52:30.685690Z","shell.execute_reply.started":"2022-07-31T12:52:30.508361Z","shell.execute_reply":"2022-07-31T12:52:30.684483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop_columns = ['Name', 'Ticket', 'Cabin']\nnum_columns = ['Pclass', 'Age', 'SibSp', 'Parch', 'Fare']\nobj_columns = ['Sex', 'Embarked']","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:30.689451Z","iopub.execute_input":"2022-07-31T12:52:30.689822Z","iopub.status.idle":"2022-07-31T12:52:30.695737Z","shell.execute_reply.started":"2022-07-31T12:52:30.689789Z","shell.execute_reply":"2022-07-31T12:52:30.694330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"First, delete columns not necessary","metadata":{}},{"cell_type":"code","source":"X = X.drop(columns=drop_columns)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:30.697475Z","iopub.execute_input":"2022-07-31T12:52:30.698239Z","iopub.status.idle":"2022-07-31T12:52:30.710364Z","shell.execute_reply.started":"2022-07-31T12:52:30.698192Z","shell.execute_reply":"2022-07-31T12:52:30.709319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('../input/titanic/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:30.711570Z","iopub.execute_input":"2022-07-31T12:52:30.712114Z","iopub.status.idle":"2022-07-31T12:52:30.728645Z","shell.execute_reply.started":"2022-07-31T12:52:30.712083Z","shell.execute_reply":"2022-07-31T12:52:30.727463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:30.730244Z","iopub.execute_input":"2022-07-31T12:52:30.730777Z","iopub.status.idle":"2022-07-31T12:52:30.751651Z","shell.execute_reply.started":"2022-07-31T12:52:30.730745Z","shell.execute_reply":"2022-07-31T12:52:30.750361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.drop(columns=drop_columns)\ntest.set_index('PassengerId', inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:30.757105Z","iopub.execute_input":"2022-07-31T12:52:30.757447Z","iopub.status.idle":"2022-07-31T12:52:30.764991Z","shell.execute_reply.started":"2022-07-31T12:52:30.757417Z","shell.execute_reply":"2022-07-31T12:52:30.763784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:30.766091Z","iopub.execute_input":"2022-07-31T12:52:30.767010Z","iopub.status.idle":"2022-07-31T12:52:30.787261Z","shell.execute_reply.started":"2022-07-31T12:52:30.766976Z","shell.execute_reply":"2022-07-31T12:52:30.786199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Second, scaling num_columns with StandardScaler","metadata":{}},{"cell_type":"code","source":"X.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:30.788561Z","iopub.execute_input":"2022-07-31T12:52:30.789305Z","iopub.status.idle":"2022-07-31T12:52:30.803487Z","shell.execute_reply.started":"2022-07-31T12:52:30.789268Z","shell.execute_reply":"2022-07-31T12:52:30.802449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Replace Age missing value with mean because data is left skewed\nX.Age = X.Age.fillna(X.Age.mean())","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:30.804869Z","iopub.execute_input":"2022-07-31T12:52:30.805872Z","iopub.status.idle":"2022-07-31T12:52:30.817890Z","shell.execute_reply.started":"2022-07-31T12:52:30.805827Z","shell.execute_reply":"2022-07-31T12:52:30.816960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()\nX[num_columns] = scaler.fit_transform(X[num_columns].astype(np.float64))","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:30.819398Z","iopub.execute_input":"2022-07-31T12:52:30.820571Z","iopub.status.idle":"2022-07-31T12:52:30.841689Z","shell.execute_reply.started":"2022-07-31T12:52:30.820528Z","shell.execute_reply":"2022-07-31T12:52:30.840780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Third, replace object columns numbers with onehotencoder","metadata":{}},{"cell_type":"code","source":"X.Embarked = X.Embarked.fillna('S')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:30.842738Z","iopub.execute_input":"2022-07-31T12:52:30.843333Z","iopub.status.idle":"2022-07-31T12:52:30.850726Z","shell.execute_reply.started":"2022-07-31T12:52:30.843297Z","shell.execute_reply":"2022-07-31T12:52:30.849892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.Embarked.unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:30.851898Z","iopub.execute_input":"2022-07-31T12:52:30.852630Z","iopub.status.idle":"2022-07-31T12:52:30.868263Z","shell.execute_reply.started":"2022-07-31T12:52:30.852599Z","shell.execute_reply":"2022-07-31T12:52:30.867019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_obj_onehot = pd.get_dummies(X[obj_columns])","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:55:06.258708Z","iopub.execute_input":"2022-07-31T12:55:06.259084Z","iopub.status.idle":"2022-07-31T12:55:06.269832Z","shell.execute_reply.started":"2022-07-31T12:55:06.259053Z","shell.execute_reply":"2022-07-31T12:55:06.268738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_obj_onehot","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:55:17.520502Z","iopub.execute_input":"2022-07-31T12:55:17.520888Z","iopub.status.idle":"2022-07-31T12:55:17.540400Z","shell.execute_reply.started":"2022-07-31T12:55:17.520855Z","shell.execute_reply":"2022-07-31T12:55:17.539385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = pd.merge(X, X_obj_onehot, left_on=X.index, right_on=X_obj_onehot.index)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:02:18.173272Z","iopub.execute_input":"2022-07-31T13:02:18.173695Z","iopub.status.idle":"2022-07-31T13:02:18.185488Z","shell.execute_reply.started":"2022-07-31T13:02:18.173603Z","shell.execute_reply":"2022-07-31T13:02:18.184545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X.rename(columns={'key_0':'PassengerId'})","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:09:04.321468Z","iopub.execute_input":"2022-07-31T13:09:04.321894Z","iopub.status.idle":"2022-07-31T13:09:04.328106Z","shell.execute_reply.started":"2022-07-31T13:09:04.321861Z","shell.execute_reply":"2022-07-31T13:09:04.326968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.set_index('PassengerId', inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:09:07.999411Z","iopub.execute_input":"2022-07-31T13:09:07.999828Z","iopub.status.idle":"2022-07-31T13:09:08.006323Z","shell.execute_reply.started":"2022-07-31T13:09:07.999793Z","shell.execute_reply":"2022-07-31T13:09:08.005152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X.drop(obj_columns, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:13:19.014276Z","iopub.execute_input":"2022-07-31T13:13:19.014653Z","iopub.status.idle":"2022-07-31T13:13:19.021113Z","shell.execute_reply.started":"2022-07-31T13:13:19.014622Z","shell.execute_reply":"2022-07-31T13:13:19.019932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:13:23.040796Z","iopub.execute_input":"2022-07-31T13:13:23.041205Z","iopub.status.idle":"2022-07-31T13:13:23.057568Z","shell.execute_reply.started":"2022-07-31T13:13:23.041170Z","shell.execute_reply":"2022-07-31T13:13:23.056218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Below code is using OneHotEncoder\n# from sklearn.preprocessing import OneHotEncoder\n\n# obj_encoder = OneHotEncoder(sparse=False)\n# X_obj_onehot = obj_encoder.fit_transform(X[obj_columns])","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:52:30.884046Z","iopub.execute_input":"2022-07-31T12:52:30.884541Z","iopub.status.idle":"2022-07-31T12:52:30.900667Z","shell.execute_reply.started":"2022-07-31T12:52:30.884510Z","shell.execute_reply":"2022-07-31T12:52:30.899362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA and data processing is done. Next, determine the best model to use","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\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-07-31T13:21:39.465885Z","iopub.execute_input":"2022-07-31T13:21:39.466260Z","iopub.status.idle":"2022-07-31T13:21:39.474664Z","shell.execute_reply.started":"2022-07-31T13:21:39.466230Z","shell.execute_reply":"2022-07-31T13:21:39.473722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:21:53.221093Z","iopub.execute_input":"2022-07-31T13:21:53.221611Z","iopub.status.idle":"2022-07-31T13:21:53.229800Z","shell.execute_reply.started":"2022-07-31T13:21:53.221566Z","shell.execute_reply":"2022-07-31T13:21:53.228685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:22:05.304040Z","iopub.execute_input":"2022-07-31T13:22:05.304588Z","iopub.status.idle":"2022-07-31T13:22:05.313703Z","shell.execute_reply.started":"2022-07-31T13:22:05.304537Z","shell.execute_reply":"2022-07-31T13:22:05.312121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:22:31.021772Z","iopub.execute_input":"2022-07-31T13:22:31.022168Z","iopub.status.idle":"2022-07-31T13:22:31.028830Z","shell.execute_reply.started":"2022-07-31T13:22:31.022137Z","shell.execute_reply":"2022-07-31T13:22:31.028005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:22:39.294775Z","iopub.execute_input":"2022-07-31T13:22:39.295209Z","iopub.status.idle":"2022-07-31T13:22:39.302852Z","shell.execute_reply.started":"2022-07-31T13:22:39.295174Z","shell.execute_reply":"2022-07-31T13:22:39.301512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgd = SGDClassifier(max_iter=1000, tol=1e-3, random_state=42)\nrfc = RandomForestClassifier(n_estimators=100, random_state=42)\nknn = KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:25:30.527783Z","iopub.execute_input":"2022-07-31T13:25:30.528227Z","iopub.status.idle":"2022-07-31T13:25:30.534635Z","shell.execute_reply.started":"2022-07-31T13:25:30.528192Z","shell.execute_reply":"2022-07-31T13:25:30.533459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgd.fit(X_train, y_train)\ncross_val_score(sgd, X_train, y_train, cv=3, scoring=\"accuracy\").mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:26:34.254731Z","iopub.execute_input":"2022-07-31T13:26:34.255137Z","iopub.status.idle":"2022-07-31T13:26:34.294047Z","shell.execute_reply.started":"2022-07-31T13:26:34.255101Z","shell.execute_reply":"2022-07-31T13:26:34.293029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rfc.fit(X_train, y_train)\ncross_val_score(rfc, X_train, y_train, cv=3, scoring=\"accuracy\").mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:27:18.144206Z","iopub.execute_input":"2022-07-31T13:27:18.144613Z","iopub.status.idle":"2022-07-31T13:27:19.016789Z","shell.execute_reply.started":"2022-07-31T13:27:18.144581Z","shell.execute_reply":"2022-07-31T13:27:19.015964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train, y_train)\ncross_val_score(knn, X_train, y_train, cv=3, scoring=\"accuracy\").mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:27:23.260208Z","iopub.execute_input":"2022-07-31T13:27:23.260831Z","iopub.status.idle":"2022-07-31T13:27:23.318272Z","shell.execute_reply.started":"2022-07-31T13:27:23.260785Z","shell.execute_reply":"2022-07-31T13:27:23.317410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From the above result, knn is the best model.\nNext, search the best paramater.","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV\n\nparam_grid = [\n        {'weights': ['distance', 'uniform']},\n        {'n_neighbors': [10, 20, 30, 40]}\n    ]\n\ngrid_search = GridSearchCV(knn, param_grid, cv=3, scoring='accuracy', verbose=2)\ngrid_search.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:31:19.141332Z","iopub.execute_input":"2022-07-31T13:31:19.141741Z","iopub.status.idle":"2022-07-31T13:31:19.418435Z","shell.execute_reply.started":"2022-07-31T13:31:19.141708Z","shell.execute_reply":"2022-07-31T13:31:19.417144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_search.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:31:23.741726Z","iopub.execute_input":"2022-07-31T13:31:23.742116Z","iopub.status.idle":"2022-07-31T13:31:23.749345Z","shell.execute_reply.started":"2022-07-31T13:31:23.742084Z","shell.execute_reply":"2022-07-31T13:31:23.748204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_search.best_score_","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:31:39.316706Z","iopub.execute_input":"2022-07-31T13:31:39.317129Z","iopub.status.idle":"2022-07-31T13:31:39.324398Z","shell.execute_reply.started":"2022-07-31T13:31:39.317093Z","shell.execute_reply":"2022-07-31T13:31:39.323270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\ny_pred = grid_search.predict(X_test)\naccuracy_score(y_test, y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:34:24.701215Z","iopub.execute_input":"2022-07-31T13:34:24.702070Z","iopub.status.idle":"2022-07-31T13:34:24.722335Z","shell.execute_reply.started":"2022-07-31T13:34:24.702031Z","shell.execute_reply":"2022-07-31T13:34:24.720975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Above result shows that this model is not overfitting. So, this model is useful.","metadata":{}},{"cell_type":"code","source":"test_obj_onehot = pd.get_dummies(test[obj_columns])\ntest = pd.merge(test, test_obj_onehot, left_on=test.index, right_on=test_obj_onehot.index)\ntest = test.rename(columns={'key_0':'PassengerId'})\ntest.set_index('PassengerId', inplace=True)\ntest = test.drop(obj_columns, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:44:11.867461Z","iopub.execute_input":"2022-07-31T13:44:11.867844Z","iopub.status.idle":"2022-07-31T13:44:11.888896Z","shell.execute_reply.started":"2022-07-31T13:44:11.867810Z","shell.execute_reply":"2022-07-31T13:44:11.887336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:44:27.632738Z","iopub.execute_input":"2022-07-31T13:44:27.633617Z","iopub.status.idle":"2022-07-31T13:44:27.655166Z","shell.execute_reply.started":"2022-07-31T13:44:27.633558Z","shell.execute_reply":"2022-07-31T13:44:27.653268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier(n_neighbors = 20)\nknn.fit(X, y)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:47:44.340833Z","iopub.execute_input":"2022-07-31T13:47:44.341265Z","iopub.status.idle":"2022-07-31T13:47:44.354985Z","shell.execute_reply.started":"2022-07-31T13:47:44.341227Z","shell.execute_reply":"2022-07-31T13:47:44.353842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:54:15.576482Z","iopub.execute_input":"2022-07-31T13:54:15.576880Z","iopub.status.idle":"2022-07-31T13:54:15.595611Z","shell.execute_reply.started":"2022-07-31T13:54:15.576845Z","shell.execute_reply":"2022-07-31T13:54:15.593878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.Age = test.Age.fillna(test.Age.mean())\ntest.Fare = test.Fare.fillna(test.Fare.mode().iloc[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:54:10.414701Z","iopub.execute_input":"2022-07-31T13:54:10.415103Z","iopub.status.idle":"2022-07-31T13:54:10.424525Z","shell.execute_reply.started":"2022-07-31T13:54:10.415068Z","shell.execute_reply":"2022-07-31T13:54:10.423354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = knn.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:54:27.311826Z","iopub.execute_input":"2022-07-31T13:54:27.312251Z","iopub.status.idle":"2022-07-31T13:54:27.341266Z","shell.execute_reply.started":"2022-07-31T13:54:27.312215Z","shell.execute_reply":"2022-07-31T13:54:27.340015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:55:23.736103Z","iopub.execute_input":"2022-07-31T13:55:23.736516Z","iopub.status.idle":"2022-07-31T13:55:23.752619Z","shell.execute_reply.started":"2022-07-31T13:55:23.736484Z","shell.execute_reply":"2022-07-31T13:55:23.751420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['Prediction'] = pred","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:24.408526Z","iopub.execute_input":"2022-07-31T14:00:24.408957Z","iopub.status.idle":"2022-07-31T14:00:24.414909Z","shell.execute_reply.started":"2022-07-31T14:00:24.408907Z","shell.execute_reply":"2022-07-31T14:00:24.413692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = submission.drop('Survived', axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:38.890367Z","iopub.execute_input":"2022-07-31T14:00:38.891280Z","iopub.status.idle":"2022-07-31T14:00:38.896839Z","shell.execute_reply.started":"2022-07-31T14:00:38.891243Z","shell.execute_reply":"2022-07-31T14:00:38.895864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = submission.rename(columns={'Prediction': 'Survived'})","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:42.653277Z","iopub.execute_input":"2022-07-31T14:00:42.653698Z","iopub.status.idle":"2022-07-31T14:00:42.660258Z","shell.execute_reply.started":"2022-07-31T14:00:42.653661Z","shell.execute_reply":"2022-07-31T14:00:42.659006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.set_index('PassengerId', inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:01:49.485344Z","iopub.execute_input":"2022-07-31T14:01:49.485874Z","iopub.status.idle":"2022-07-31T14:01:49.492541Z","shell.execute_reply.started":"2022-07-31T14:01:49.485835Z","shell.execute_reply":"2022-07-31T14:01:49.490997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:01:51.359249Z","iopub.execute_input":"2022-07-31T14:01:51.359643Z","iopub.status.idle":"2022-07-31T14:01:51.368651Z","shell.execute_reply.started":"2022-07-31T14:01:51.359604Z","shell.execute_reply":"2022-07-31T14:01:51.367818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:02:54.727573Z","iopub.execute_input":"2022-07-31T14:02:54.728095Z","iopub.status.idle":"2022-07-31T14:02:54.738353Z","shell.execute_reply.started":"2022-07-31T14:02:54.728051Z","shell.execute_reply":"2022-07-31T14:02:54.736844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}