{"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":"from fastai.tabular import *\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.model_selection import train_test_split\nimport pandas as pd","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-06T11:50:09.185630Z","iopub.execute_input":"2022-08-06T11:50:09.186546Z","iopub.status.idle":"2022-08-06T11:50:10.598767Z","shell.execute_reply.started":"2022-08-06T11:50:09.186343Z","shell.execute_reply":"2022-08-06T11:50:10.597309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/spaceship-titanic/train.csv\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T11:50:10.601332Z","iopub.execute_input":"2022-08-06T11:50:10.601852Z","iopub.status.idle":"2022-08-06T11:50:10.691217Z","shell.execute_reply.started":"2022-08-06T11:50:10.601803Z","shell.execute_reply":"2022-08-06T11:50:10.689819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-06T11:50:10.693178Z","iopub.execute_input":"2022-08-06T11:50:10.694147Z","iopub.status.idle":"2022-08-06T11:50:10.703561Z","shell.execute_reply.started":"2022-08-06T11:50:10.694096Z","shell.execute_reply":"2022-08-06T11:50:10.701883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.nunique()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T11:50:10.707053Z","iopub.execute_input":"2022-08-06T11:50:10.707807Z","iopub.status.idle":"2022-08-06T11:50:10.738422Z","shell.execute_reply.started":"2022-08-06T11:50:10.707757Z","shell.execute_reply":"2022-08-06T11:50:10.736850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.read_csv(\"/kaggle/input/spaceship-titanic/test.csv\")\ndf_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T11:50:10.740652Z","iopub.execute_input":"2022-08-06T11:50:10.741607Z","iopub.status.idle":"2022-08-06T11:50:10.803946Z","shell.execute_reply.started":"2022-08-06T11:50:10.741553Z","shell.execute_reply":"2022-08-06T11:50:10.802551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modes = df.mode().iloc[0]\ndf.fillna(modes, inplace=True)\ndf_test.fillna(modes, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T11:50:10.805657Z","iopub.execute_input":"2022-08-06T11:50:10.806157Z","iopub.status.idle":"2022-08-06T11:50:10.868220Z","shell.execute_reply.started":"2022-08-06T11:50:10.806109Z","shell.execute_reply":"2022-08-06T11:50:10.867233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cabin = df['Cabin'].str.split('/', expand=True)\ncabin.rename({0: \"Deck\", 1: \"Num\", 2: \"Side\"}, axis=\"columns\", inplace=True)\ncabin","metadata":{"execution":{"iopub.status.busy":"2022-08-06T11:50:10.870278Z","iopub.execute_input":"2022-08-06T11:50:10.870770Z","iopub.status.idle":"2022-08-06T11:50:10.909312Z","shell.execute_reply.started":"2022-08-06T11:50:10.870723Z","shell.execute_reply":"2022-08-06T11:50:10.908242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.join(cabin)\ndf","metadata":{"execution":{"iopub.status.busy":"2022-08-06T11:50:10.910636Z","iopub.execute_input":"2022-08-06T11:50:10.911992Z","iopub.status.idle":"2022-08-06T11:50:10.971645Z","shell.execute_reply.started":"2022-08-06T11:50:10.911900Z","shell.execute_reply":"2022-08-06T11:50:10.970538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cabin = df_test['Cabin'].str.split('/', expand=True)\ncabin.rename({0: \"Deck\", 1: \"Num\", 2: \"Side\"}, axis=\"columns\", inplace=True)\ndf_test = df_test.join(cabin)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T11:50:10.973047Z","iopub.execute_input":"2022-08-06T11:50:10.973745Z","iopub.status.idle":"2022-08-06T11:50:10.994479Z","shell.execute_reply.started":"2022-08-06T11:50:10.973701Z","shell.execute_reply":"2022-08-06T11:50:10.993286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_trn,df_val = train_test_split(df, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T11:50:10.998213Z","iopub.execute_input":"2022-08-06T11:50:10.998960Z","iopub.status.idle":"2022-08-06T11:50:11.013496Z","shell.execute_reply.started":"2022-08-06T11:50:10.998913Z","shell.execute_reply":"2022-08-06T11:50:11.012109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_trn","metadata":{"execution":{"iopub.status.busy":"2022-08-06T11:50:11.015429Z","iopub.execute_input":"2022-08-06T11:50:11.016261Z","iopub.status.idle":"2022-08-06T11:50:11.065868Z","shell.execute_reply.started":"2022-08-06T11:50:11.016210Z","shell.execute_reply":"2022-08-06T11:50:11.064471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_trn = df_trn[\"Transported\"]\ny_val = df_val[\"Transported\"]\n\nfeatures = [\"HomePlanet\", \"CryoSleep\", \"Destination\", \"Age\", \"VIP\", \"RoomService\",\n            \"FoodCourt\", \"ShoppingMall\", \"Spa\", \"VRDeck\", \"Deck\", \"Side\"]\nx_trn = pd.get_dummies(df_trn[features])\nx_val = pd.get_dummies(df_val[features])\nx_test = pd.get_dummies(df_test[features])","metadata":{"execution":{"iopub.status.busy":"2022-08-06T11:50:11.067829Z","iopub.execute_input":"2022-08-06T11:50:11.068254Z","iopub.status.idle":"2022-08-06T11:50:11.118127Z","shell.execute_reply.started":"2022-08-06T11:50:11.068217Z","shell.execute_reply":"2022-08-06T11:50:11.116552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_val","metadata":{"execution":{"iopub.status.busy":"2022-08-06T11:50:11.120596Z","iopub.execute_input":"2022-08-06T11:50:11.121960Z","iopub.status.idle":"2022-08-06T11:50:11.163924Z","shell.execute_reply.started":"2022-08-06T11:50:11.121907Z","shell.execute_reply":"2022-08-06T11:50:11.162370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_trn.shape, x_val.shape, x_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-06T11:50:11.165547Z","iopub.execute_input":"2022-08-06T11:50:11.166745Z","iopub.status.idle":"2022-08-06T11:50:11.176073Z","shell.execute_reply.started":"2022-08-06T11:50:11.166695Z","shell.execute_reply":"2022-08-06T11:50:11.174712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = RandomForestClassifier(n_estimators=100, min_samples_split=20)\nmodel.fit(x_trn, y_trn)\npredictions = model.predict(x_val)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T11:50:11.177834Z","iopub.execute_input":"2022-08-06T11:50:11.178923Z","iopub.status.idle":"2022-08-06T11:50:12.102559Z","shell.execute_reply.started":"2022-08-06T11:50:11.178872Z","shell.execute_reply":"2022-08-06T11:50:12.101229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_val, predictions)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T11:50:22.411510Z","iopub.execute_input":"2022-08-06T11:50:22.411924Z","iopub.status.idle":"2022-08-06T11:50:22.421288Z","shell.execute_reply.started":"2022-08-06T11:50:22.411890Z","shell.execute_reply":"2022-08-06T11:50:22.419812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[0:20]","metadata":{"execution":{"iopub.status.busy":"2022-08-06T11:50:49.688821Z","iopub.execute_input":"2022-08-06T11:50:49.689884Z","iopub.status.idle":"2022-08-06T11:50:49.700496Z","shell.execute_reply.started":"2022-08-06T11:50:49.689841Z","shell.execute_reply":"2022-08-06T11:50:49.699249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = pd.get_dummies(df[features])\ny = df[\"Transported\"]\n\nmodel_2 = RandomForestClassifier(n_estimators=100, min_samples_split=20)\nmodel_2.fit(x, y)\npredictions_2 = model_2.predict(x_val)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T11:52:52.704018Z","iopub.execute_input":"2022-08-06T11:52:52.704465Z","iopub.status.idle":"2022-08-06T11:52:53.680064Z","shell.execute_reply.started":"2022-08-06T11:52:52.704433Z","shell.execute_reply":"2022-08-06T11:52:53.678804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# After training on validation set lol\naccuracy_score(y_val, predictions_2)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T11:53:07.304643Z","iopub.execute_input":"2022-08-06T11:53:07.305101Z","iopub.status.idle":"2022-08-06T11:53:07.314565Z","shell.execute_reply.started":"2022-08-06T11:53:07.305064Z","shell.execute_reply":"2022-08-06T11:53:07.313494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['Transported'] = model_2.predict(x_test)\nsubmission = df_test[['PassengerId','Transported']]\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T11:45:44.598836Z","iopub.execute_input":"2022-08-06T11:45:44.599364Z","iopub.status.idle":"2022-08-06T11:45:44.712743Z","shell.execute_reply.started":"2022-08-06T11:45:44.599327Z","shell.execute_reply":"2022-08-06T11:45:44.711286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-06T11:45:44.714167Z","iopub.execute_input":"2022-08-06T11:45:44.714544Z","iopub.status.idle":"2022-08-06T11:45:44.722836Z","shell.execute_reply.started":"2022-08-06T11:45:44.714505Z","shell.execute_reply":"2022-08-06T11:45:44.721196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!head submission.csv","metadata":{"execution":{"iopub.status.busy":"2022-08-06T11:45:44.724615Z","iopub.execute_input":"2022-08-06T11:45:44.725439Z","iopub.status.idle":"2022-08-06T11:45:45.920208Z","shell.execute_reply.started":"2022-08-06T11:45:44.725363Z","shell.execute_reply":"2022-08-06T11:45:45.918327Z"},"trusted":true},"execution_count":null,"outputs":[]}]}