{"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 seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn import preprocessing, svm\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.metrics import classification_report\nfrom sklearn.linear_model import LogisticRegression\nimport matplotlib.pyplot as plt\n","metadata":{"id":"qE8ats40gceX","execution":{"iopub.status.busy":"2022-07-22T16:40:58.097792Z","iopub.execute_input":"2022-07-22T16:40:58.098254Z","iopub.status.idle":"2022-07-22T16:40:58.107395Z","shell.execute_reply.started":"2022-07-22T16:40:58.098167Z","shell.execute_reply":"2022-07-22T16:40:58.106101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '../input/spaceship-titanic/train.csv'\ntest_path = '../input/spaceship-titanic/test.csv'\n\ntrain_data = pd.read_csv(train_path)\ntest_data = pd.read_csv(test_path)\n\n# train_data = train_data.apply(LabelEncoder().fit_transform)\n\ntrain_data \n","metadata":{"id":"evPsouY0g0Z_","outputId":"54c60e36-81c1-4e99-9556-30410f2deab3","execution":{"iopub.status.busy":"2022-07-22T16:40:58.686326Z","iopub.execute_input":"2022-07-22T16:40:58.686950Z","iopub.status.idle":"2022-07-22T16:40:58.797333Z","shell.execute_reply.started":"2022-07-22T16:40:58.686906Z","shell.execute_reply":"2022-07-22T16:40:58.795696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data Preparation\n# New feature - Group\ntrain_data['Group'] = [x.split('_')[0] for x in list(train_data['PassengerId'])]\ntrain_data['Group'] = train_data['Group'].astype(int)\ntest_data['Group'] = [x.split('_')[0] for x in list(test_data['PassengerId'])]\ntest_data['Group'] = test_data['Group'].astype(int)\n\n# New feature - Group size\ntrain_data['GroupSize']=train_data['Group'].map(lambda x: train_data['Group'].value_counts()[x])\ntrain_data = train_data.drop('Group', axis=1)\ntest_data['GroupSize']=test_data['Group'].map(lambda x: test_data['Group'].value_counts()[x])\ntest_data = test_data.drop('Group', axis=1)\n\n# New Feature - Deck Num and Side\ntrain_data[['Deck', 'Num', 'Side']] = train_data['Cabin'].str.split('/', n=2, expand=True)\ntrain_data = train_data.drop('Cabin', axis=1)\ntrain_data = train_data[['PassengerId', 'HomePlanet', 'CryoSleep', 'Destination', 'Age', 'VIP', 'RoomService', 'FoodCourt', 'ShoppingMall', 'Spa', 'VRDeck', 'Deck', 'Num', 'Side','GroupSize', 'Name', 'Transported']]\ntest_data[['Deck', 'Num', 'Side']] = test_data['Cabin'].str.split('/', n=2, expand=True)\ntest_data = test_data.drop('Cabin', axis=1)\ntest_data = test_data[['PassengerId', 'HomePlanet', 'CryoSleep', 'Destination', 'Age', 'VIP', 'RoomService', 'FoodCourt', 'ShoppingMall', 'Spa', 'VRDeck', 'Deck', 'Num', 'Side', 'GroupSize', 'Name']]\n\n# fill null\ntrain_data[['HomePlanet']] = train_data[['HomePlanet']].fillna(\"Earth\")\ntrain_data[['CryoSleep', 'VIP']] = train_data[['CryoSleep', 'VIP']].fillna(False)\ntrain_data[['Destination']] = train_data[['Destination']].fillna(\"TRAPPIST-1e\")\ntrain_data[['RoomService']] = train_data[['RoomService']].fillna(train_data['RoomService'].mean())\ntrain_data[['FoodCourt']] = train_data[['FoodCourt']].fillna(train_data['FoodCourt'].mean())\ntrain_data[['ShoppingMall']] = train_data[['ShoppingMall']].fillna(train_data['ShoppingMall'].mean())\ntrain_data[['Spa']] = train_data[['Spa']].fillna(train_data['Spa'].mean())\ntrain_data[['VRDeck']] = train_data[['VRDeck']].fillna(train_data['VRDeck'].mean())\ntrain_data[['Deck']] = train_data[['Deck']].fillna(\"F\")\ntrain_data[['Num']] = train_data[['Num']].fillna(\"82\")\ntrain_data[['Side']] = train_data[['Side']].fillna(\"S\")\n\ntest_data[['HomePlanet']] = test_data[['HomePlanet']].fillna(\"Earth\")\ntest_data[['CryoSleep', 'VIP']] = test_data[['CryoSleep', 'VIP']].fillna(False)\ntest_data[['Destination']] = test_data[['Destination']].fillna(\"TRAPPIST-1e\")\ntest_data[['RoomService']] = test_data[['RoomService']].fillna(test_data['RoomService'].mean())\ntest_data[['FoodCourt']] = test_data[['FoodCourt']].fillna(test_data['FoodCourt'].mean())\ntest_data[['ShoppingMall']] = test_data[['ShoppingMall']].fillna(test_data['ShoppingMall'].mean())\ntest_data[['Spa']] = test_data[['Spa']].fillna(test_data['Spa'].mean())\ntest_data[['VRDeck']] = test_data[['VRDeck']].fillna(test_data['VRDeck'].mean())\ntest_data[['Deck']] = test_data[['Deck']].fillna(\"F\")\ntest_data[['Num']] = test_data[['Num']].fillna(\"82\")\ntest_data[['Side']] = test_data[['Side']].fillna(\"S\")","metadata":{"execution":{"iopub.status.busy":"2022-07-22T16:40:59.196169Z","iopub.execute_input":"2022-07-22T16:40:59.196502Z","iopub.status.idle":"2022-07-22T16:41:10.716358Z","shell.execute_reply.started":"2022-07-22T16:40:59.196472Z","shell.execute_reply":"2022-07-22T16:41:10.715127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['Side'].value_counts()\n\n\n# deck F\n#NUM 82\n#side S","metadata":{"execution":{"iopub.status.busy":"2022-07-22T16:41:10.718633Z","iopub.execute_input":"2022-07-22T16:41:10.719512Z","iopub.status.idle":"2022-07-22T16:41:10.731962Z","shell.execute_reply.started":"2022-07-22T16:41:10.719468Z","shell.execute_reply":"2022-07-22T16:41:10.730318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x, y = train_data.iloc[:, 1:-2], train_data.iloc[:, [-1]]\nx = x.apply(LabelEncoder().fit_transform)\nx_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.1)\n# y_train.head()\n# x_test.head()\n# y_test.head()\n# pd.Series(x['HomePlanet']).unique()\n","metadata":{"id":"FL3QyHLkgxsw","execution":{"iopub.status.busy":"2022-07-22T16:41:10.734346Z","iopub.execute_input":"2022-07-22T16:41:10.735404Z","iopub.status.idle":"2022-07-22T16:41:10.769887Z","shell.execute_reply.started":"2022-07-22T16:41:10.735361Z","shell.execute_reply":"2022-07-22T16:41:10.768695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr = LogisticRegression(max_iter=10000)\n\nlr.fit(x_train,y_train.values.ravel())\n\nprediction = lr.predict(x_test)\n\nprint(confusion_matrix(y_test, prediction) )\nprint(classification_report(y_test, prediction))","metadata":{"id":"7zXJTkAyihvI","outputId":"22d3ae57-f902-40b3-8489-c6a15cf77d4a","execution":{"iopub.status.busy":"2022-07-22T16:41:10.773445Z","iopub.execute_input":"2022-07-22T16:41:10.774076Z","iopub.status.idle":"2022-07-22T16:41:13.316978Z","shell.execute_reply.started":"2022-07-22T16:41:10.774032Z","shell.execute_reply":"2022-07-22T16:41:13.315255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rfc = RandomForestClassifier()\n\n# model = DecisionTreeClassifier()\n\n\nrfc.fit(x_train,y_train.values.ravel())\n\nprediction = rfc.predict(x_test)\n\nprint(confusion_matrix(y_test, prediction) )\nprint(classification_report(y_test, prediction))","metadata":{"id":"6qoTLiYCNaiO","outputId":"e5ce24cc-f953-4a99-d410-b7bb0f32aaa3","execution":{"iopub.status.busy":"2022-07-22T16:41:13.323297Z","iopub.execute_input":"2022-07-22T16:41:13.323808Z","iopub.status.idle":"2022-07-22T16:41:14.404718Z","shell.execute_reply.started":"2022-07-22T16:41:13.323742Z","shell.execute_reply":"2022-07-22T16:41:14.402057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dt = DecisionTreeClassifier()\n\n\ndt.fit(x_train,y_train.values.ravel())\n\nprediction = dt.predict(x_test)\n\nprint(confusion_matrix(y_test, prediction) )\nprint(classification_report(y_test, prediction))","metadata":{"id":"YzVc_k7INoMs","outputId":"f9fe2a28-21a1-464f-d424-dd7ea10243d7","execution":{"iopub.status.busy":"2022-07-22T16:41:14.406312Z","iopub.execute_input":"2022-07-22T16:41:14.408134Z","iopub.status.idle":"2022-07-22T16:41:14.475300Z","shell.execute_reply.started":"2022-07-22T16:41:14.408089Z","shell.execute_reply":"2022-07-22T16:41:14.473193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import VotingClassifier\n\nevc=VotingClassifier(estimators=[('lr',lr),('rfc',rfc),('dt',dt)],voting='hard')\nevc.fit(x_train, y_train.values.ravel())\n\nprint(\"score on test: \" + str(evc.score(x_test, y_test)))\nprint(\"score on train: \"+ str(evc.score(x_train, y_train)))","metadata":{"id":"ZnIxEV9MOAfs","outputId":"961619c9-962a-44f8-c782-b1a3c5c59e1c","execution":{"iopub.status.busy":"2022-07-22T16:41:14.478796Z","iopub.execute_input":"2022-07-22T16:41:14.479598Z","iopub.status.idle":"2022-07-22T16:41:18.297803Z","shell.execute_reply.started":"2022-07-22T16:41:14.479554Z","shell.execute_reply":"2022-07-22T16:41:18.296439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = test_data.iloc[:, 1:-1]\nX_test = X_test.apply(LabelEncoder().fit_transform)\n\nprediction = evc.predict(X_test)\nprint(prediction)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T16:41:18.299523Z","iopub.execute_input":"2022-07-22T16:41:18.300109Z","iopub.status.idle":"2022-07-22T16:41:18.500095Z","shell.execute_reply.started":"2022-07-22T16:41:18.300046Z","shell.execute_reply":"2022-07-22T16:41:18.498712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission = pd.DataFrame({'PassengerId': test_data['PassengerId'], 'Transported': prediction})\n\nmy_submission.to_csv('submission4.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T16:41:18.501882Z","iopub.execute_input":"2022-07-22T16:41:18.502440Z","iopub.status.idle":"2022-07-22T16:41:18.520284Z","shell.execute_reply.started":"2022-07-22T16:41:18.502398Z","shell.execute_reply":"2022-07-22T16:41:18.519109Z"},"trusted":true},"execution_count":null,"outputs":[]}]}