{"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 pandas as pd\nfrom sklearn.model_selection import train_test_split, GridSearchCV\nfrom sklearn.preprocessing import OrdinalEncoder\nfrom sklearn.impute import KNNImputer\nfrom sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier\nimport lightgbm as lgbm\nimport sklearn.metrics as metrics\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-04T14:45:30.098072Z","iopub.execute_input":"2022-08-04T14:45:30.099319Z","iopub.status.idle":"2022-08-04T14:45:32.516662Z","shell.execute_reply.started":"2022-08-04T14:45:30.099265Z","shell.execute_reply":"2022-08-04T14:45:32.515369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/spaceship-titanic/train.csv\")\ntest = pd.read_csv(\"../input/spaceship-titanic/test.csv\")\nsubmission = pd.read_csv(\"../input/spaceship-titanic/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-04T14:45:32.518846Z","iopub.execute_input":"2022-08-04T14:45:32.519228Z","iopub.status.idle":"2022-08-04T14:45:32.612826Z","shell.execute_reply.started":"2022-08-04T14:45:32.519195Z","shell.execute_reply":"2022-08-04T14:45:32.611937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(), test.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T14:45:32.613969Z","iopub.execute_input":"2022-08-04T14:45:32.614763Z","iopub.status.idle":"2022-08-04T14:45:32.646523Z","shell.execute_reply.started":"2022-08-04T14:45:32.614728Z","shell.execute_reply":"2022-08-04T14:45:32.645476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-04T14:45:32.649235Z","iopub.execute_input":"2022-08-04T14:45:32.650351Z","iopub.status.idle":"2022-08-04T14:45:32.658549Z","shell.execute_reply.started":"2022-08-04T14:45:32.650297Z","shell.execute_reply":"2022-08-04T14:45:32.657086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info(), test.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T14:45:32.660455Z","iopub.execute_input":"2022-08-04T14:45:32.661249Z","iopub.status.idle":"2022-08-04T14:45:32.708295Z","shell.execute_reply.started":"2022-08-04T14:45:32.661211Z","shell.execute_reply":"2022-08-04T14:45:32.707103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T14:45:32.709645Z","iopub.execute_input":"2022-08-04T14:45:32.710155Z","iopub.status.idle":"2022-08-04T14:45:32.750044Z","shell.execute_reply.started":"2022-08-04T14:45:32.710112Z","shell.execute_reply":"2022-08-04T14:45:32.749204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum(), test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T14:45:32.751630Z","iopub.execute_input":"2022-08-04T14:45:32.752457Z","iopub.status.idle":"2022-08-04T14:45:32.771083Z","shell.execute_reply.started":"2022-08-04T14:45:32.752387Z","shell.execute_reply":"2022-08-04T14:45:32.769828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_spaceship_data = pd.concat([train, test], ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T14:45:32.772613Z","iopub.execute_input":"2022-08-04T14:45:32.773039Z","iopub.status.idle":"2022-08-04T14:45:32.784777Z","shell.execute_reply.started":"2022-08-04T14:45:32.772946Z","shell.execute_reply":"2022-08-04T14:45:32.783623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_spaceship_data[\"PassengerGroup\"] = all_spaceship_data[\"PassengerId\"].apply(lambda x: x.split(\"_\")[0])\nall_spaceship_data[\"Deck\"] = all_spaceship_data[\"Cabin\"].apply(lambda x: x if pd.isnull(x) else x.split(\"/\")[0])\nall_spaceship_data[\"Side\"] = all_spaceship_data[\"Cabin\"].apply(lambda x: x if pd.isnull(x) else x.split(\"/\")[2])\nall_spaceship_data.drop([\"PassengerId\", \"Cabin\", \"Name\"], axis = 1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T14:45:32.786626Z","iopub.execute_input":"2022-08-04T14:45:32.787301Z","iopub.status.idle":"2022-08-04T14:45:32.844174Z","shell.execute_reply.started":"2022-08-04T14:45:32.787246Z","shell.execute_reply":"2022-08-04T14:45:32.843020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#using pessenger travel group to fill in some missing values\ntravelGroup = [\"Destination\", \"CryoSleep\", \"VIP\", \"Deck\", \"Side\", \"HomePlanet\"]\n\nfor i in travelGroup:\n    all_spaceship_data[i] = all_spaceship_data.groupby([\"PassengerGroup\"])[i].apply(\n        lambda x: x.fillna(pd.Series.mode(x)[0]) if len(pd.Series.mode(x)) in range(1, 3) else x.fillna(\"None\"))\n    all_spaceship_data.loc[all_spaceship_data[i] == \"None\", i] = None","metadata":{"execution":{"iopub.status.busy":"2022-08-04T14:45:32.847648Z","iopub.execute_input":"2022-08-04T14:45:32.848021Z","iopub.status.idle":"2022-08-04T14:46:05.750841Z","shell.execute_reply.started":"2022-08-04T14:45:32.847985Z","shell.execute_reply":"2022-08-04T14:46:05.749388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#using the mode to fill in the remaining missing values for object columns\nfor i in travelGroup:\n    all_spaceship_data[i] = all_spaceship_data[i].fillna(all_spaceship_data[i].mode()[0])","metadata":{"execution":{"iopub.status.busy":"2022-08-04T14:46:05.752289Z","iopub.execute_input":"2022-08-04T14:46:05.752701Z","iopub.status.idle":"2022-08-04T14:46:05.780727Z","shell.execute_reply.started":"2022-08-04T14:46:05.752665Z","shell.execute_reply":"2022-08-04T14:46:05.778905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"OE = [\"Destination\", \"CryoSleep\", \"VIP\", \"Deck\", \"Side\", \"HomePlanet\", \"PassengerGroup\"]\nordinalEncoder = OrdinalEncoder()\nall_spaceship_data[OE] = ordinalEncoder.fit_transform(all_spaceship_data[OE])","metadata":{"execution":{"iopub.status.busy":"2022-08-04T14:46:05.782788Z","iopub.execute_input":"2022-08-04T14:46:05.783150Z","iopub.status.idle":"2022-08-04T14:46:05.858181Z","shell.execute_reply.started":"2022-08-04T14:46:05.783118Z","shell.execute_reply":"2022-08-04T14:46:05.856779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_spaceship_data [\"Transported\"].replace({True: 1, False: 0}, inplace = True)\n\nX = all_spaceship_data.drop(\"Transported\", axis = 1)\ny = all_spaceship_data[\"Transported\"].dropna()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T14:46:05.859869Z","iopub.execute_input":"2022-08-04T14:46:05.860629Z","iopub.status.idle":"2022-08-04T14:46:05.878273Z","shell.execute_reply.started":"2022-08-04T14:46:05.860571Z","shell.execute_reply":"2022-08-04T14:46:05.877230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# using KNN imputer with a neighbor of 3 to fill in numerical missing values\nknn_imp = KNNImputer(n_neighbors = 3)\nX.loc[:,:] =knn_imp.fit_transform(X)\nbins = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 80] \nlabels = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]\n\n#cutting age in age groups by binning.\nX[\"AgeGroup\"] = pd.cut(x = X[\"Age\"], bins = bins, labels = labels, include_lowest = True)\nX[\"Total_spent\"] = X[\"RoomService\"] + X[\"FoodCourt\"] + X[\"ShoppingMall\"] + X[\"Spa\"] + X[\"VRDeck\"]","metadata":{"execution":{"iopub.status.busy":"2022-08-04T14:46:05.881040Z","iopub.execute_input":"2022-08-04T14:46:05.882099Z","iopub.status.idle":"2022-08-04T14:46:07.411946Z","shell.execute_reply.started":"2022-08-04T14:46:05.882046Z","shell.execute_reply":"2022-08-04T14:46:07.410391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = X.iloc[:8693,:]\ntest = X.iloc[8693:,:]","metadata":{"execution":{"iopub.status.busy":"2022-08-04T14:46:07.413492Z","iopub.execute_input":"2022-08-04T14:46:07.413851Z","iopub.status.idle":"2022-08-04T14:46:07.419756Z","shell.execute_reply.started":"2022-08-04T14:46:07.413821Z","shell.execute_reply":"2022-08-04T14:46:07.418560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.corr()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T14:46:07.421406Z","iopub.execute_input":"2022-08-04T14:46:07.421882Z","iopub.status.idle":"2022-08-04T14:46:07.459229Z","shell.execute_reply.started":"2022-08-04T14:46:07.421832Z","shell.execute_reply":"2022-08-04T14:46:07.458270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = X.iloc[:8693,:]\ntest = X.iloc[8693:,:]","metadata":{"execution":{"iopub.status.busy":"2022-08-04T14:46:07.460463Z","iopub.execute_input":"2022-08-04T14:46:07.461503Z","iopub.status.idle":"2022-08-04T14:46:07.468302Z","shell.execute_reply.started":"2022-08-04T14:46:07.461460Z","shell.execute_reply":"2022-08-04T14:46:07.466576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#lgbm training and prediction\nlg = lgbm.LGBMClassifier(n_estimators = 500, max_depth = 50, learning_rate = 0.02,  random_state=1)\nlg.fit(train, y)\nlgResult = lg.predict(test)\nsubmission[\"Transported\"] = lgResult\nsubmission[\"Transported\"].replace({1: True, 0: False}, inplace = True)\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T14:46:07.470492Z","iopub.execute_input":"2022-08-04T14:46:07.471232Z","iopub.status.idle":"2022-08-04T14:46:08.621158Z","shell.execute_reply.started":"2022-08-04T14:46:07.471192Z","shell.execute_reply":"2022-08-04T14:46:08.620022Z"},"trusted":true},"execution_count":null,"outputs":[]}]}