{"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)\n\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-07-29T06:11:40.644591Z","iopub.execute_input":"2022-07-29T06:11:40.645436Z","iopub.status.idle":"2022-07-29T06:11:40.673275Z","shell.execute_reply.started":"2022-07-29T06:11:40.645325Z","shell.execute_reply":"2022-07-29T06:11:40.672526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom keras import models,layers","metadata":{"execution":{"iopub.status.busy":"2022-07-29T06:11:40.674544Z","iopub.execute_input":"2022-07-29T06:11:40.675234Z","iopub.status.idle":"2022-07-29T06:11:47.177368Z","shell.execute_reply.started":"2022-07-29T06:11:40.675191Z","shell.execute_reply":"2022-07-29T06:11:47.176748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/spaceship-titanic/train.csv')\ntest = pd.read_csv('/kaggle/input/spaceship-titanic/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-29T06:11:47.178413Z","iopub.execute_input":"2022-07-29T06:11:47.178956Z","iopub.status.idle":"2022-07-29T06:11:47.251873Z","shell.execute_reply.started":"2022-07-29T06:11:47.178921Z","shell.execute_reply":"2022-07-29T06:11:47.251064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train = train[\"Transported\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-29T06:11:47.254412Z","iopub.execute_input":"2022-07-29T06:11:47.254774Z","iopub.status.idle":"2022-07-29T06:11:47.264271Z","shell.execute_reply.started":"2022-07-29T06:11:47.254727Z","shell.execute_reply":"2022-07-29T06:11:47.263351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['PassengerGroup'] = train['PassengerId'].apply(lambda x: x.split('_')[1])\ntest['PassengerGroup'] = test['PassengerId'].apply(lambda x: x.split('_')[1])\n\ntrain['HomePlanet'].fillna(train.HomePlanet.mode()[0], inplace=True) #Earth\ntest['HomePlanet'].fillna(test.HomePlanet.mode()[0], inplace=True) #Earth\n\ntrain['CryoSleep'].fillna(False, inplace=True)\ntest['CryoSleep'].fillna(False, inplace=True)\n\ntrain['VIP'].fillna(train.VIP.mode()[0], inplace=True) \ntest['VIP'].fillna(test.VIP.mode()[0], inplace=True)\n\ntrain['Destination'].fillna(train.Destination.mode()[0], inplace=True)\ntest['Destination'].fillna(test.Destination.mode()[0], inplace=True)\n\ntrain['Age'].fillna(train.Age.mean(), inplace=True)\ntrain[['RoomService', 'FoodCourt', 'ShoppingMall', 'Spa', 'VRDeck']] =\\\ntrain[['RoomService', 'FoodCourt', 'ShoppingMall', 'Spa', 'VRDeck']].fillna(0)\n\ntest['Age'].fillna(test.Age.mean(), inplace=True)\ntest[['RoomService', 'FoodCourt', 'ShoppingMall', 'Spa', 'VRDeck']] =\\\ntest[['RoomService', 'FoodCourt', 'ShoppingMall', 'Spa', 'VRDeck']].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T06:11:47.265891Z","iopub.execute_input":"2022-07-29T06:11:47.266386Z","iopub.status.idle":"2022-07-29T06:11:47.314644Z","shell.execute_reply.started":"2022-07-29T06:11:47.266341Z","shell.execute_reply":"2022-07-29T06:11:47.313831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valores = [\"HomePlanet\",\"CryoSleep\",\"Destination\",\"VIP\",\"Spa\", \"VRDeck\",\"Age\",\"RoomService\",\"FoodCourt\",\"ShoppingMall\",\"PassengerGroup\"]\n\nX_train = pd.get_dummies(train[valores])\nX_test = pd.get_dummies(test[valores])\n\nX_train","metadata":{"execution":{"iopub.status.busy":"2022-07-29T06:11:47.315893Z","iopub.execute_input":"2022-07-29T06:11:47.316201Z","iopub.status.idle":"2022-07-29T06:11:47.372356Z","shell.execute_reply.started":"2022-07-29T06:11:47.316161Z","shell.execute_reply":"2022-07-29T06:11:47.371822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"RANDOM FOREST","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\n\ndef rf(X_train, Y_train, n_estimators=400):\n    clf = RandomForestClassifier(n_estimators=n_estimators, n_jobs=-1,random_state=1)\n    clf.fit(X_train, Y_train)\n    return clf\n\nrandom_forest_en = rf(X_train, Y_train , n_estimators=400)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T06:11:47.373440Z","iopub.execute_input":"2022-07-29T06:11:47.374118Z","iopub.status.idle":"2022-07-29T06:11:50.011807Z","shell.execute_reply.started":"2022-07-29T06:11:47.374084Z","shell.execute_reply":"2022-07-29T06:11:50.011093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def print_result(model, x_train, y_train):\n\n    print('Accuracy in training: ', model.score(x_train, y_train))\n\nprint_result(random_forest_en, X_train, Y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T06:11:50.012657Z","iopub.execute_input":"2022-07-29T06:11:50.012866Z","iopub.status.idle":"2022-07-29T06:11:50.437218Z","shell.execute_reply.started":"2022-07-29T06:11:50.012838Z","shell.execute_reply":"2022-07-29T06:11:50.436238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predrf = random_forest_en.predict(X_test)\nprint('Result: ',predrf)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T06:11:50.439068Z","iopub.execute_input":"2022-07-29T06:11:50.439389Z","iopub.status.idle":"2022-07-29T06:11:50.757675Z","shell.execute_reply.started":"2022-07-29T06:11:50.439346Z","shell.execute_reply":"2022-07-29T06:11:50.757027Z"},"trusted":true},"execution_count":null,"outputs":[]}]}