{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-04T08:18:24.807700Z","iopub.execute_input":"2022-08-04T08:18:24.808721Z","iopub.status.idle":"2022-08-04T08:18:24.817137Z","shell.execute_reply.started":"2022-08-04T08:18:24.808677Z","shell.execute_reply":"2022-08-04T08:18:24.816046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# LOAD DATA","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/spaceship-titanic/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:24.931767Z","iopub.execute_input":"2022-08-04T08:18:24.932438Z","iopub.status.idle":"2022-08-04T08:18:24.967950Z","shell.execute_reply.started":"2022-08-04T08:18:24.932399Z","shell.execute_reply":"2022-08-04T08:18:24.966704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:25.049395Z","iopub.execute_input":"2022-08-04T08:18:25.050248Z","iopub.status.idle":"2022-08-04T08:18:25.083782Z","shell.execute_reply.started":"2022-08-04T08:18:25.050202Z","shell.execute_reply":"2022-08-04T08:18:25.082522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:25.099756Z","iopub.execute_input":"2022-08-04T08:18:25.100187Z","iopub.status.idle":"2022-08-04T08:18:25.122695Z","shell.execute_reply.started":"2022-08-04T08:18:25.100150Z","shell.execute_reply":"2022-08-04T08:18:25.121474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:25.162681Z","iopub.execute_input":"2022-08-04T08:18:25.163367Z","iopub.status.idle":"2022-08-04T08:18:25.183324Z","shell.execute_reply.started":"2022-08-04T08:18:25.163319Z","shell.execute_reply":"2022-08-04T08:18:25.182245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:25.225443Z","iopub.execute_input":"2022-08-04T08:18:25.226215Z","iopub.status.idle":"2022-08-04T08:18:25.262175Z","shell.execute_reply.started":"2022-08-04T08:18:25.226172Z","shell.execute_reply":"2022-08-04T08:18:25.261247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['CryoSleep'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:25.273033Z","iopub.execute_input":"2022-08-04T08:18:25.275695Z","iopub.status.idle":"2022-08-04T08:18:25.285718Z","shell.execute_reply.started":"2022-08-04T08:18:25.275651Z","shell.execute_reply":"2022-08-04T08:18:25.284313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Transported'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:25.324454Z","iopub.execute_input":"2022-08-04T08:18:25.325751Z","iopub.status.idle":"2022-08-04T08:18:25.335371Z","shell.execute_reply.started":"2022-08-04T08:18:25.325694Z","shell.execute_reply":"2022-08-04T08:18:25.334202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['HomePlanet'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:25.372473Z","iopub.execute_input":"2022-08-04T08:18:25.373242Z","iopub.status.idle":"2022-08-04T08:18:25.384245Z","shell.execute_reply.started":"2022-08-04T08:18:25.373194Z","shell.execute_reply":"2022-08-04T08:18:25.383075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['VIP'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:25.420966Z","iopub.execute_input":"2022-08-04T08:18:25.421647Z","iopub.status.idle":"2022-08-04T08:18:25.430821Z","shell.execute_reply.started":"2022-08-04T08:18:25.421601Z","shell.execute_reply":"2022-08-04T08:18:25.429763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cb = df['Cabin'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:25.467389Z","iopub.execute_input":"2022-08-04T08:18:25.467987Z","iopub.status.idle":"2022-08-04T08:18:25.477046Z","shell.execute_reply.started":"2022-08-04T08:18:25.467953Z","shell.execute_reply":"2022-08-04T08:18:25.475944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(range(len(cb[:50])), cb[:50])","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:25.516163Z","iopub.execute_input":"2022-08-04T08:18:25.517274Z","iopub.status.idle":"2022-08-04T08:18:25.685241Z","shell.execute_reply.started":"2022-08-04T08:18:25.517224Z","shell.execute_reply":"2022-08-04T08:18:25.684128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Cabin'] = df['Cabin'].apply(lambda s: s if str(s) in cb[:50] else 'others')","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:25.687839Z","iopub.execute_input":"2022-08-04T08:18:25.688713Z","iopub.status.idle":"2022-08-04T08:18:26.066764Z","shell.execute_reply.started":"2022-08-04T08:18:25.688666Z","shell.execute_reply":"2022-08-04T08:18:26.065630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cb","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:26.068101Z","iopub.execute_input":"2022-08-04T08:18:26.068448Z","iopub.status.idle":"2022-08-04T08:18:26.079112Z","shell.execute_reply.started":"2022-08-04T08:18:26.068415Z","shell.execute_reply":"2022-08-04T08:18:26.077753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Cabin'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:26.081336Z","iopub.execute_input":"2022-08-04T08:18:26.081874Z","iopub.status.idle":"2022-08-04T08:18:26.091202Z","shell.execute_reply.started":"2022-08-04T08:18:26.081839Z","shell.execute_reply":"2022-08-04T08:18:26.090078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Categorical Data Visualization","metadata":{}},{"cell_type":"code","source":"sns.countplot(x='HomePlanet' , data=df , hue='Transported')","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:26.092536Z","iopub.execute_input":"2022-08-04T08:18:26.092952Z","iopub.status.idle":"2022-08-04T08:18:26.296703Z","shell.execute_reply.started":"2022-08-04T08:18:26.092882Z","shell.execute_reply":"2022-08-04T08:18:26.295536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='CryoSleep' , data=df , hue='Transported')","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:26.298120Z","iopub.execute_input":"2022-08-04T08:18:26.301174Z","iopub.status.idle":"2022-08-04T08:18:26.510768Z","shell.execute_reply.started":"2022-08-04T08:18:26.301134Z","shell.execute_reply":"2022-08-04T08:18:26.509982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='HomePlanet' , data=df , hue='CryoSleep')","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:26.511799Z","iopub.execute_input":"2022-08-04T08:18:26.512673Z","iopub.status.idle":"2022-08-04T08:18:26.713153Z","shell.execute_reply.started":"2022-08-04T08:18:26.512640Z","shell.execute_reply":"2022-08-04T08:18:26.712076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='HomePlanet' , data=df , hue='Destination')","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:26.714587Z","iopub.execute_input":"2022-08-04T08:18:26.714976Z","iopub.status.idle":"2022-08-04T08:18:26.933047Z","shell.execute_reply.started":"2022-08-04T08:18:26.714946Z","shell.execute_reply":"2022-08-04T08:18:26.931760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='Destination' , data=df , hue='Transported')","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:26.936455Z","iopub.execute_input":"2022-08-04T08:18:26.936801Z","iopub.status.idle":"2022-08-04T08:18:27.136943Z","shell.execute_reply.started":"2022-08-04T08:18:26.936770Z","shell.execute_reply":"2022-08-04T08:18:27.135627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data preprocessing","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:27.138519Z","iopub.execute_input":"2022-08-04T08:18:27.138966Z","iopub.status.idle":"2022-08-04T08:18:27.207422Z","shell.execute_reply.started":"2022-08-04T08:18:27.138920Z","shell.execute_reply":"2022-08-04T08:18:27.206347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:27.209678Z","iopub.execute_input":"2022-08-04T08:18:27.210611Z","iopub.status.idle":"2022-08-04T08:18:27.217773Z","shell.execute_reply.started":"2022-08-04T08:18:27.210543Z","shell.execute_reply":"2022-08-04T08:18:27.216534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_num =df[['RoomService' , 'FoodCourt' , 'ShoppingMall' , 'Spa' , 'VRDeck' , 'Age']]\nX_cat = df[['HomePlanet' , 'CryoSleep' , 'Destination' , 'VIP']]\ny = df['Transported']","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:27.219289Z","iopub.execute_input":"2022-08-04T08:18:27.219725Z","iopub.status.idle":"2022-08-04T08:18:27.228941Z","shell.execute_reply.started":"2022-08-04T08:18:27.219682Z","shell.execute_reply":"2022-08-04T08:18:27.227993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_cat = pd.get_dummies(X_cat)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:27.230346Z","iopub.execute_input":"2022-08-04T08:18:27.230900Z","iopub.status.idle":"2022-08-04T08:18:27.247688Z","shell.execute_reply.started":"2022-08-04T08:18:27.230867Z","shell.execute_reply":"2022-08-04T08:18:27.246425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()\nscaler.fit(X_num)\nX_scaled = scaler.transform(X_num)\nX_scaled = pd.DataFrame(data=X_scaled, index=X_num.index, columns=X_num.columns)\nX = pd.concat([X_scaled, X_cat], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:27.249901Z","iopub.execute_input":"2022-08-04T08:18:27.250211Z","iopub.status.idle":"2022-08-04T08:18:27.262482Z","shell.execute_reply.started":"2022-08-04T08:18:27.250182Z","shell.execute_reply":"2022-08-04T08:18:27.261449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:27.263938Z","iopub.execute_input":"2022-08-04T08:18:27.264447Z","iopub.status.idle":"2022-08-04T08:18:27.283229Z","shell.execute_reply.started":"2022-08-04T08:18:27.264413Z","shell.execute_reply":"2022-08-04T08:18:27.282196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# XGBoost model creation/training","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom xgboost import XGBClassifier\nfrom sklearn.metrics import classification_report","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:27.284596Z","iopub.execute_input":"2022-08-04T08:18:27.285613Z","iopub.status.idle":"2022-08-04T08:18:27.468503Z","shell.execute_reply.started":"2022-08-04T08:18:27.285547Z","shell.execute_reply":"2022-08-04T08:18:27.467579Z"},"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 = 0.3, random_state=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:27.469664Z","iopub.execute_input":"2022-08-04T08:18:27.470525Z","iopub.status.idle":"2022-08-04T08:18:27.482008Z","shell.execute_reply.started":"2022-08-04T08:18:27.470490Z","shell.execute_reply":"2022-08-04T08:18:27.480961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_xgb = XGBClassifier()\nmodel_xgb.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:27.485062Z","iopub.execute_input":"2022-08-04T08:18:27.486087Z","iopub.status.idle":"2022-08-04T08:18:28.316564Z","shell.execute_reply.started":"2022-08-04T08:18:27.485989Z","shell.execute_reply":"2022-08-04T08:18:28.315441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model_xgb.predict(X_test)\nprint(classification_report(y_test, pred))","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:28.318177Z","iopub.execute_input":"2022-08-04T08:18:28.318610Z","iopub.status.idle":"2022-08-04T08:18:28.342239Z","shell.execute_reply.started":"2022-08-04T08:18:28.318575Z","shell.execute_reply":"2022-08-04T08:18:28.341028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(16,8))\nplt.barh(X.columns, model_xgb.feature_importances_)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-04T08:18:28.343903Z","iopub.execute_input":"2022-08-04T08:18:28.344520Z","iopub.status.idle":"2022-08-04T08:18:28.620480Z","shell.execute_reply.started":"2022-08-04T08:18:28.344485Z","shell.execute_reply":"2022-08-04T08:18:28.619533Z"},"trusted":true},"execution_count":null,"outputs":[]}]}