{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":72489,"databundleVersionId":8096274,"sourceType":"competition"}],"dockerImageVersionId":30684,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"raw","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"}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_absolute_error\nimport pandas as pd\n\n#Choosing a gradient descent model\nfrom xgboost import XGBRegressor\n","metadata":{"execution":{"iopub.status.busy":"2024-04-19T09:13:57.523306Z","iopub.execute_input":"2024-04-19T09:13:57.523990Z","iopub.status.idle":"2024-04-19T09:13:57.531894Z","shell.execute_reply.started":"2024-04-19T09:13:57.523942Z","shell.execute_reply":"2024-04-19T09:13:57.530333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Referencing my base data\ndata = pd.read_csv('/kaggle/input/playground-series-s4e4/train.csv', index_col = 0)\ntest_data = pd.read_csv('/kaggle/input/playground-series-s4e4/test.csv', index_col = 0)\nindex_data = pd.read_csv('/kaggle/input/playground-series-s4e4/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-04-19T09:13:57.534725Z","iopub.execute_input":"2024-04-19T09:13:57.535311Z","iopub.status.idle":"2024-04-19T09:13:57.846239Z","shell.execute_reply.started":"2024-04-19T09:13:57.535279Z","shell.execute_reply":"2024-04-19T09:13:57.844682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-19T09:13:57.847776Z","iopub.execute_input":"2024-04-19T09:13:57.848147Z","iopub.status.idle":"2024-04-19T09:13:57.870862Z","shell.execute_reply.started":"2024-04-19T09:13:57.848117Z","shell.execute_reply":"2024-04-19T09:13:57.869481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.dtypes","metadata":{"execution":{"iopub.status.busy":"2024-04-19T09:13:57.873252Z","iopub.execute_input":"2024-04-19T09:13:57.873718Z","iopub.status.idle":"2024-04-19T09:13:57.885260Z","shell.execute_reply.started":"2024-04-19T09:13:57.873686Z","shell.execute_reply":"2024-04-19T09:13:57.883788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Convert sex column into float64\ndata['Sex'] = data['Sex'].astype('category')","metadata":{"execution":{"iopub.status.busy":"2024-04-19T09:13:57.889276Z","iopub.execute_input":"2024-04-19T09:13:57.889700Z","iopub.status.idle":"2024-04-19T09:13:57.905506Z","shell.execute_reply.started":"2024-04-19T09:13:57.889671Z","shell.execute_reply":"2024-04-19T09:13:57.904367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = data['Rings']\n","metadata":{"execution":{"iopub.status.busy":"2024-04-19T09:13:57.907255Z","iopub.execute_input":"2024-04-19T09:13:57.907776Z","iopub.status.idle":"2024-04-19T09:13:57.914855Z","shell.execute_reply.started":"2024-04-19T09:13:57.907736Z","shell.execute_reply":"2024-04-19T09:13:57.913441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = data.drop([\"Rings\"], axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T09:13:57.916311Z","iopub.execute_input":"2024-04-19T09:13:57.916767Z","iopub.status.idle":"2024-04-19T09:13:57.929346Z","shell.execute_reply.started":"2024-04-19T09:13:57.916737Z","shell.execute_reply":"2024-04-19T09:13:57.927953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_valid, y_train, y_valid = train_test_split(x, y)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T09:13:57.932338Z","iopub.execute_input":"2024-04-19T09:13:57.933764Z","iopub.status.idle":"2024-04-19T09:13:57.953562Z","shell.execute_reply.started":"2024-04-19T09:13:57.933675Z","shell.execute_reply":"2024-04-19T09:13:57.952415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.dtypes","metadata":{"execution":{"iopub.status.busy":"2024-04-19T09:13:57.955193Z","iopub.execute_input":"2024-04-19T09:13:57.955571Z","iopub.status.idle":"2024-04-19T09:13:57.965513Z","shell.execute_reply.started":"2024-04-19T09:13:57.955540Z","shell.execute_reply":"2024-04-19T09:13:57.964469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_model = XGBRegressor(n_estimators=700, max_depth=7,\n                        enable_categorical=True,\n                        verbosity=2)\n\nmy_model.fit(x_train, y_train, \n             early_stopping_rounds = 10,\n             eval_set = [(x_valid, y_valid)],)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T09:16:49.559390Z","iopub.execute_input":"2024-04-19T09:16:49.559879Z","iopub.status.idle":"2024-04-19T09:16:50.029499Z","shell.execute_reply.started":"2024-04-19T09:16:49.559843Z","shell.execute_reply":"2024-04-19T09:16:50.028605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_1 = my_model.predict(x_valid)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T09:16:51.972652Z","iopub.execute_input":"2024-04-19T09:16:51.973076Z","iopub.status.idle":"2024-04-19T09:16:51.994454Z","shell.execute_reply.started":"2024-04-19T09:16:51.973044Z","shell.execute_reply":"2024-04-19T09:16:51.993418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = mean_absolute_error(predictions_1, y_valid)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T09:16:52.392926Z","iopub.execute_input":"2024-04-19T09:16:52.393369Z","iopub.status.idle":"2024-04-19T09:16:52.400127Z","shell.execute_reply.started":"2024-04-19T09:16:52.393335Z","shell.execute_reply":"2024-04-19T09:16:52.398744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score","metadata":{"execution":{"iopub.status.busy":"2024-04-19T09:16:45.011870Z","iopub.execute_input":"2024-04-19T09:16:45.012279Z","iopub.status.idle":"2024-04-19T09:16:45.019873Z","shell.execute_reply.started":"2024-04-19T09:16:45.012248Z","shell.execute_reply":"2024-04-19T09:16:45.018644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Applying model to the test\ntest_data['Sex'] = test_data['Sex'].astype('category')\ntest_data.dtypes","metadata":{"execution":{"iopub.status.busy":"2024-04-19T09:13:59.227658Z","iopub.execute_input":"2024-04-19T09:13:59.228306Z","iopub.status.idle":"2024-04-19T09:13:59.250782Z","shell.execute_reply.started":"2024-04-19T09:13:59.228276Z","shell.execute_reply":"2024-04-19T09:13:59.249306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nsubmission_1 = my_model.predict(test_data)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Index = index_data['id']\n\noutput = pd.DataFrame({'id': Index,\n                       'Age': submission_1})","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}