{"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":"2021-08-31T17:13:48.646748Z","iopub.execute_input":"2021-08-31T17:13:48.647152Z","iopub.status.idle":"2021-08-31T17:13:48.664500Z","shell.execute_reply.started":"2021-08-31T17:13:48.647063Z","shell.execute_reply":"2021-08-31T17:13:48.663261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from xgboost import XGBRegressor\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.model_selection import train_test_split\nfrom math import sqrt","metadata":{"execution":{"iopub.status.busy":"2021-08-31T17:13:48.666650Z","iopub.execute_input":"2021-08-31T17:13:48.667259Z","iopub.status.idle":"2021-08-31T17:13:49.945342Z","shell.execute_reply.started":"2021-08-31T17:13:48.667211Z","shell.execute_reply":"2021-08-31T17:13:49.943970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = pd.read_csv('/kaggle/input/beyond-analysis/train.csv')\ntrain_dataset.describe()","metadata":{"execution":{"iopub.status.busy":"2021-08-31T17:13:49.947616Z","iopub.execute_input":"2021-08-31T17:13:49.947929Z","iopub.status.idle":"2021-08-31T17:13:57.638048Z","shell.execute_reply.started":"2021-08-31T17:13:49.947900Z","shell.execute_reply":"2021-08-31T17:13:57.636677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset","metadata":{"execution":{"iopub.status.busy":"2021-08-31T17:13:57.639741Z","iopub.execute_input":"2021-08-31T17:13:57.640031Z","iopub.status.idle":"2021-08-31T17:13:58.079411Z","shell.execute_reply.started":"2021-08-31T17:13:57.640002Z","shell.execute_reply":"2021-08-31T17:13:58.077853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t_df = train_dataset\nt_df = t_df.groupby('UNIQUE_IDENTIFIER')\nt_df = t_df.agg(np.mean)\nt_df","metadata":{"execution":{"iopub.status.busy":"2021-08-31T17:13:58.081160Z","iopub.execute_input":"2021-08-31T17:13:58.081582Z","iopub.status.idle":"2021-08-31T17:13:58.453107Z","shell.execute_reply.started":"2021-08-31T17:13:58.081539Z","shell.execute_reply":"2021-08-31T17:13:58.451769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# No missing values, 2 categorical cols = ['CATEGORY_1', 'CATEGORY_2']\n# ['SEQUENCE_NO', 'DEPOSIT_2', 'DEPOSIT_TRAILS', 'CATEGORY_1', 'CATEGORY_2']\n# Split into userwise means/sums\n\ndef cleanse(df):\n    df = df.groupby('UNIQUE_IDENTIFIER')\n    df = df.agg(np.mean)\n    to_drop = ['DEPOSIT_2', 'DEPOSIT_TRAILS']\n#     , 'CATEGORY_1', 'CATEGORY_2', 'UNIQUE_IDENTIFIER'\n    return df.drop(to_drop, axis=1)\n\nT_df = train_dataset\nT_df = cleanse(T_df)\n\nX = T_df.drop(['Y1', 'Y2'], axis=1)\ny = T_df[['Y1', 'Y2']]\n\n# X = cleanse(X)\nX_train, X_valid, y_train, y_valid = train_test_split(X, y)\nprint(X_train.shape, y_train.shape, X_valid.shape, y_valid.shape)","metadata":{"execution":{"iopub.status.busy":"2021-08-31T17:13:58.454910Z","iopub.execute_input":"2021-08-31T17:13:58.455375Z","iopub.status.idle":"2021-08-31T17:13:58.750804Z","shell.execute_reply.started":"2021-08-31T17:13:58.455327Z","shell.execute_reply":"2021-08-31T17:13:58.749349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_param1 = XGBRegressor(n_estimators=1000, learning_rate=0.01, tree_method='hist')\nmodel_param1.fit(X_train, y_train['Y1'], early_stopping_rounds=5, eval_set=[(X_valid, y_valid['Y1'])], verbose=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-31T17:13:58.753203Z","iopub.execute_input":"2021-08-31T17:13:58.753647Z","iopub.status.idle":"2021-08-31T17:14:02.863929Z","shell.execute_reply.started":"2021-08-31T17:13:58.753601Z","shell.execute_reply":"2021-08-31T17:14:02.862700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_param2 = XGBRegressor(n_estimators=1000, learning_rate=0.01, tree_method='hist')\nmodel_param2.fit(X_train, y_train['Y2'], early_stopping_rounds=5, eval_set=[(X_valid, y_valid['Y2'])], verbose=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-31T17:14:02.866383Z","iopub.execute_input":"2021-08-31T17:14:02.866783Z","iopub.status.idle":"2021-08-31T17:14:10.959025Z","shell.execute_reply.started":"2021-08-31T17:14:02.866745Z","shell.execute_reply":"2021-08-31T17:14:10.957790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y1, y2 = y_valid['Y1'], y_valid['Y2']\nprint(f'Y1 rmse: {sqrt(mean_squared_error(y1, model_param1.predict(X_valid)))}')\nprint(f'Y2 rmse: {sqrt(mean_squared_error(y2, model_param1.predict(X_valid)))}')\n# Y1 rmse: 10.26070193311643\n# Y2 rmse: 303.04928245745634","metadata":{"execution":{"iopub.status.busy":"2021-08-31T17:14:10.960477Z","iopub.execute_input":"2021-08-31T17:14:10.960920Z","iopub.status.idle":"2021-08-31T17:14:11.114220Z","shell.execute_reply.started":"2021-08-31T17:14:10.960885Z","shell.execute_reply":"2021-08-31T17:14:11.113362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = pd.read_csv('/kaggle/input/beyond-analysis/test.csv')\nprint(test_dataset.shape)\n\nvalid_test_dataset = cleanse(test_dataset)\nprint(valid_test_dataset.shape)\n\ntest_dataset.drop_duplicates(subset=['UNIQUE_IDENTIFIER'], inplace=True)\nprint(test_dataset.shape)","metadata":{"execution":{"iopub.status.busy":"2021-08-31T17:14:11.115735Z","iopub.execute_input":"2021-08-31T17:14:11.116394Z","iopub.status.idle":"2021-08-31T17:14:14.632627Z","shell.execute_reply.started":"2021-08-31T17:14:11.116350Z","shell.execute_reply":"2021-08-31T17:14:14.631281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(test_dataset['UNIQUE_IDENTIFIER'])\noutput = pd.DataFrame({'UNIQUE_IDENTIFIER': test_dataset['UNIQUE_IDENTIFIER'], 'Y1': model_param1.predict(valid_test_dataset), 'Y2': model_param2.predict(valid_test_dataset)})\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-31T17:14:14.634314Z","iopub.execute_input":"2021-08-31T17:14:14.634960Z","iopub.status.idle":"2021-08-31T17:14:15.486004Z","shell.execute_reply.started":"2021-08-31T17:14:14.634910Z","shell.execute_reply":"2021-08-31T17:14:15.484890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame({'Cols': X.columns.to_list(), 'Weightage': model_param1.feature_importances_}).sort_values(by=['Weightage'], ascending=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-31T17:14:15.487538Z","iopub.execute_input":"2021-08-31T17:14:15.487840Z","iopub.status.idle":"2021-08-31T17:14:15.843522Z","shell.execute_reply.started":"2021-08-31T17:14:15.487811Z","shell.execute_reply":"2021-08-31T17:14:15.842605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame({'Cols': X.columns.to_list(), 'Weightage': model_param2.feature_importances_}).sort_values(by=['Weightage'], ascending=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-31T17:14:15.844750Z","iopub.execute_input":"2021-08-31T17:14:15.845042Z","iopub.status.idle":"2021-08-31T17:14:16.479616Z","shell.execute_reply.started":"2021-08-31T17:14:15.845012Z","shell.execute_reply":"2021-08-31T17:14:16.478662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}