{"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":"markdown","source":"## 1. 라이브러리, 데이터 불러오기","metadata":{}},{"cell_type":"code","source":"# 데이터분석 4종 세트\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:32:16.810457Z","iopub.execute_input":"2022-07-22T01:32:16.811750Z","iopub.status.idle":"2022-07-22T01:32:16.817207Z","shell.execute_reply.started":"2022-07-22T01:32:16.811701Z","shell.execute_reply":"2022-07-22T01:32:16.816017Z"},"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-07-22T01:32:17.035236Z","iopub.execute_input":"2022-07-22T01:32:17.036555Z","iopub.status.idle":"2022-07-22T01:32:17.103182Z","shell.execute_reply.started":"2022-07-22T01:32:17.036504Z","shell.execute_reply":"2022-07-22T01:32:17.102204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. EDA","metadata":{}},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:32:17.220935Z","iopub.execute_input":"2022-07-22T01:32:17.221419Z","iopub.status.idle":"2022-07-22T01:32:17.245222Z","shell.execute_reply.started":"2022-07-22T01:32:17.221382Z","shell.execute_reply":"2022-07-22T01:32:17.243676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().mean()*100","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:32:17.400537Z","iopub.execute_input":"2022-07-22T01:32:17.401017Z","iopub.status.idle":"2022-07-22T01:32:17.422425Z","shell.execute_reply.started":"2022-07-22T01:32:17.400976Z","shell.execute_reply":"2022-07-22T01:32:17.421076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:32:17.585394Z","iopub.execute_input":"2022-07-22T01:32:17.586425Z","iopub.status.idle":"2022-07-22T01:32:17.608145Z","shell.execute_reply.started":"2022-07-22T01:32:17.586376Z","shell.execute_reply":"2022-07-22T01:32:17.607178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().mean()*100","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:32:17.785835Z","iopub.execute_input":"2022-07-22T01:32:17.787093Z","iopub.status.idle":"2022-07-22T01:32:17.802326Z","shell.execute_reply.started":"2022-07-22T01:32:17.787036Z","shell.execute_reply":"2022-07-22T01:32:17.800822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.shape, test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:32:17.980797Z","iopub.execute_input":"2022-07-22T01:32:17.982157Z","iopub.status.idle":"2022-07-22T01:32:17.988485Z","shell.execute_reply.started":"2022-07-22T01:32:17.982100Z","shell.execute_reply":"2022-07-22T01:32:17.987078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:32:18.170508Z","iopub.execute_input":"2022-07-22T01:32:18.171377Z","iopub.status.idle":"2022-07-22T01:32:18.196008Z","shell.execute_reply.started":"2022-07-22T01:32:18.171318Z","shell.execute_reply":"2022-07-22T01:32:18.195048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# correlation matrix를 heatmap으로 시각화.\n\ncorr = train.drop(columns=\"Transported\").corr()\nplt.figure(figsize=(6, 6))\nsns.heatmap(data=corr, annot=True, fmt='.2f', cmap='Blues')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:32:18.360496Z","iopub.execute_input":"2022-07-22T01:32:18.361254Z","iopub.status.idle":"2022-07-22T01:32:18.742416Z","shell.execute_reply.started":"2022-07-22T01:32:18.361204Z","shell.execute_reply":"2022-07-22T01:32:18.741045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns[train.dtypes != 'float64']","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:32:18.744448Z","iopub.execute_input":"2022-07-22T01:32:18.744827Z","iopub.status.idle":"2022-07-22T01:32:18.754591Z","shell.execute_reply.started":"2022-07-22T01:32:18.744793Z","shell.execute_reply":"2022-07-22T01:32:18.753213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.pairplot(train.drop(columns=train.columns[train.dtypes != 'float64']))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:32:18.756370Z","iopub.execute_input":"2022-07-22T01:32:18.756902Z","iopub.status.idle":"2022-07-22T01:33:21.181205Z","shell.execute_reply.started":"2022-07-22T01:32:18.756854Z","shell.execute_reply":"2022-07-22T01:33:21.179867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# countplot\nplt.subplots(figsize=(15,20))\nplt.subplot(4,2,1)\nsns.histplot(data=train.Age)\nplt.subplot(4,2,2)\nsns.boxplot(data=train, x='RoomService')\nplt.subplot(4,2,3)\nsns.boxplot(data=train, x='FoodCourt')\nplt.subplot(4,2,4)\nsns.boxplot(data=train, x='ShoppingMall')\nplt.subplot(4,2,5)\nsns.boxplot(data=train, x='Spa')\nplt.subplot(4,2,6)\nsns.boxplot(data=train, x='VRDeck')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:33:21.184742Z","iopub.execute_input":"2022-07-22T01:33:21.185183Z","iopub.status.idle":"2022-07-22T01:33:21.961227Z","shell.execute_reply.started":"2022-07-22T01:33:21.185145Z","shell.execute_reply":"2022-07-22T01:33:21.959757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# countplot\nsns.countplot(data=train, x='Transported')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:33:21.963060Z","iopub.execute_input":"2022-07-22T01:33:21.963547Z","iopub.status.idle":"2022-07-22T01:33:22.078829Z","shell.execute_reply.started":"2022-07-22T01:33:21.963500Z","shell.execute_reply":"2022-07-22T01:33:22.077368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# countplot\nplt.subplots(figsize=(15,10))\nplt.subplot(2,2,1)\nsns.countplot(data=train.fillna('NaN'), x='HomePlanet', hue='Transported')\nplt.subplot(2,2,2)\nsns.countplot(data=train.fillna('NaN'), x='CryoSleep', hue='Transported')\nplt.subplot(2,2,3)\nsns.countplot(data=train.fillna('NaN'), x='Destination', hue='Transported')\nplt.subplot(2,2,4)\nsns.countplot(data=train.fillna('NaN'), x='VIP', hue='Transported')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:33:22.080443Z","iopub.execute_input":"2022-07-22T01:33:22.081546Z","iopub.status.idle":"2022-07-22T01:33:22.861337Z","shell.execute_reply.started":"2022-07-22T01:33:22.081506Z","shell.execute_reply":"2022-07-22T01:33:22.860254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3. 전처리","metadata":{}},{"cell_type":"markdown","source":"#### 결측치 처리","metadata":{}},{"cell_type":"code","source":"# object는 최빈값으로, 나머지는 중앙값으로 결측치 채움\n\nfor col in train.columns:\n    if train[col].dtypes=='object':\n        train[col]=train[col].fillna(train[col].mode()[0])\n    else:\n        train[col]=train[col].fillna(train[col].median())\n\nfor col in test.columns:\n    if test[col].dtypes=='object':\n        test[col]=test[col].fillna(train[col].mode()[0])\n    else:\n        test[col]=test[col].fillna(train[col].median())","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:33:22.862984Z","iopub.execute_input":"2022-07-22T01:33:22.863334Z","iopub.status.idle":"2022-07-22T01:33:22.957479Z","shell.execute_reply.started":"2022-07-22T01:33:22.863302Z","shell.execute_reply":"2022-07-22T01:33:22.955467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:33:22.960415Z","iopub.execute_input":"2022-07-22T01:33:22.963596Z","iopub.status.idle":"2022-07-22T01:33:22.986100Z","shell.execute_reply.started":"2022-07-22T01:33:22.963538Z","shell.execute_reply":"2022-07-22T01:33:22.984860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:33:22.987862Z","iopub.execute_input":"2022-07-22T01:33:22.988843Z","iopub.status.idle":"2022-07-22T01:33:23.012197Z","shell.execute_reply.started":"2022-07-22T01:33:22.988792Z","shell.execute_reply":"2022-07-22T01:33:23.010450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Categorical Data 확인","metadata":{}},{"cell_type":"code","source":"## Unique하여 categorical 되지않는 데이터 구분\n\nobj_features=train.columns[train.dtypes !='float64']\n\nfor id in obj_features :\n    temp = len(train[id].unique())/len(train[id])\n    print(f\"{id} column, {temp :.2}\")","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:33:23.015944Z","iopub.execute_input":"2022-07-22T01:33:23.016582Z","iopub.status.idle":"2022-07-22T01:33:23.036958Z","shell.execute_reply.started":"2022-07-22T01:33:23.016540Z","shell.execute_reply":"2022-07-22T01:33:23.035671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# PassengerId 와 Name, cabin은 Unique하기에 데이터 삭제\ntrain=train.drop(columns=['PassengerId','Name','Cabin'])\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:33:23.038156Z","iopub.execute_input":"2022-07-22T01:33:23.039006Z","iopub.status.idle":"2022-07-22T01:33:23.078151Z","shell.execute_reply.started":"2022-07-22T01:33:23.038966Z","shell.execute_reply":"2022-07-22T01:33:23.077226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=test.drop(columns=['PassengerId','Name','Cabin'])\ntest","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:33:23.079523Z","iopub.execute_input":"2022-07-22T01:33:23.079912Z","iopub.status.idle":"2022-07-22T01:33:23.116435Z","shell.execute_reply.started":"2022-07-22T01:33:23.079880Z","shell.execute_reply":"2022-07-22T01:33:23.115107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## category 하기 위해 Data 구성확인\n\nobj_features = obj_features.drop(['PassengerId','Name','Cabin'])\nfor id in obj_features :\n    temp = train[id].unique()\n    print(f\"{id} column, {temp}\")","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:33:23.118379Z","iopub.execute_input":"2022-07-22T01:33:23.119612Z","iopub.status.idle":"2022-07-22T01:33:23.131218Z","shell.execute_reply.started":"2022-07-22T01:33:23.119551Z","shell.execute_reply":"2022-07-22T01:33:23.130025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# HomePlanet,Destination은  ordinal Encoding\n\n#1. HomePlanet (Europa : 0, Earth : 1, Mars : 2  )\ntrain.HomePlanet=train.HomePlanet.map({'Europa':0,'Earth':1,'Mars':2})\ntest.HomePlanet=test.HomePlanet.map({'Europa':0,'Earth':1,'Mars':2})\n\n\n#2. Destination ('TRAPPIST-1e' : 0, 'PSO J318.5-22' : 1, '55 Cancri e' : 2 )\ntrain.Destination=train.Destination.map({'TRAPPIST-1e':0,'PSO J318.5-22':1,'55 Cancri e':2})\ntest.Destination=test.Destination.map({'TRAPPIST-1e':0,'PSO J318.5-22':1,'55 Cancri e':2})","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:33:23.132613Z","iopub.execute_input":"2022-07-22T01:33:23.133740Z","iopub.status.idle":"2022-07-22T01:33:23.150787Z","shell.execute_reply.started":"2022-07-22T01:33:23.133699Z","shell.execute_reply":"2022-07-22T01:33:23.149496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.loc[:,(train.dtypes== 'object') | (train.dtypes== 'bool') ]","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:33:23.152274Z","iopub.execute_input":"2022-07-22T01:33:23.153470Z","iopub.status.idle":"2022-07-22T01:33:23.175823Z","shell.execute_reply.started":"2022-07-22T01:33:23.153425Z","shell.execute_reply":"2022-07-22T01:33:23.174832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4. Training - Evaluation\n\n - train-validation split\n\n - RandomForestClassfier를 이용하여 학습\n\n - train, Validation 성능 검증\n\n - test data에 대해서 예측!","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split #1\nfrom sklearn.ensemble import RandomForestClassifier #2\nfrom sklearn.metrics import accuracy_score #3\n\n# 학습 데이터 정의\nX = train.drop(columns='Transported')\ny = train.Transported\n\n# train-val split\nX_train, X_val, y_train, y_val = train_test_split(X,y,test_size=0.2, random_state=42)\n\n# Training\nclf = RandomForestClassifier()\nclf.fit(X_train, y_train)\n\n# Evaluation (overfitting check!)\npreds  = clf.predict(X_train)\npreds2 = clf.predict(X_val)\n\nprint('train accuracy : %.4f' % accuracy_score(y_train, preds))\nprint('Validation accuracy : %.4f' % accuracy_score(y_val, preds2))","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:33:23.177359Z","iopub.execute_input":"2022-07-22T01:33:23.178671Z","iopub.status.idle":"2022-07-22T01:33:24.234784Z","shell.execute_reply.started":"2022-07-22T01:33:23.178605Z","shell.execute_reply":"2022-07-22T01:33:24.233589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5. Predition","metadata":{}},{"cell_type":"code","source":"# 테스트 데이터 생성\nX_test = test\n\n# 예측\nresult = clf.predict(X_test) #4\n\n# 제출 파일 생성\nsubmission ['Transported']= result\nsubmission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T01:33:24.236580Z","iopub.execute_input":"2022-07-22T01:33:24.236992Z","iopub.status.idle":"2022-07-22T01:33:24.361380Z","shell.execute_reply.started":"2022-07-22T01:33:24.236958Z","shell.execute_reply":"2022-07-22T01:33:24.360059Z"},"trusted":true},"execution_count":null,"outputs":[]}]}