{"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-09-01T16:05:28.811015Z","iopub.execute_input":"2021-09-01T16:05:28.811614Z","iopub.status.idle":"2021-09-01T16:05:28.830661Z","shell.execute_reply.started":"2021-09-01T16:05:28.811471Z","shell.execute_reply":"2021-09-01T16:05:28.829205Z"},"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-09-01T16:05:28.833157Z","iopub.execute_input":"2021-09-01T16:05:28.833691Z","iopub.status.idle":"2021-09-01T16:05:30.023684Z","shell.execute_reply.started":"2021-09-01T16:05:28.833638Z","shell.execute_reply":"2021-09-01T16:05:30.022403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.svm import SVR\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.preprocessing import StandardScaler","metadata":{"execution":{"iopub.status.busy":"2021-09-01T16:05:30.025809Z","iopub.execute_input":"2021-09-01T16:05:30.026141Z","iopub.status.idle":"2021-09-01T16:05:30.121298Z","shell.execute_reply.started":"2021-09-01T16:05:30.026112Z","shell.execute_reply":"2021-09-01T16:05:30.120091Z"},"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-09-01T16:05:30.123482Z","iopub.execute_input":"2021-09-01T16:05:30.124042Z","iopub.status.idle":"2021-09-01T16:05:36.978299Z","shell.execute_reply.started":"2021-09-01T16:05:30.123991Z","shell.execute_reply":"2021-09-01T16:05:36.977276Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T16:05:36.979905Z","iopub.execute_input":"2021-09-01T16:05:36.980353Z","iopub.status.idle":"2021-09-01T16:05:36.98727Z","shell.execute_reply.started":"2021-09-01T16:05:36.980297Z","shell.execute_reply":"2021-09-01T16:05:36.985957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"T_df = cleanse(train_dataset)\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-09-01T16:05:36.988599Z","iopub.execute_input":"2021-09-01T16:05:36.988916Z","iopub.status.idle":"2021-09-01T16:05:37.30707Z","shell.execute_reply.started":"2021-09-01T16:05:36.988887Z","shell.execute_reply":"2021-09-01T16:05:37.305697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t_X_train = X_train.drop(['SEQUENCE_NO'],axis=1)\nt_X_valid = X_valid.drop(['SEQUENCE_NO'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T16:05:37.308351Z","iopub.execute_input":"2021-09-01T16:05:37.30865Z","iopub.status.idle":"2021-09-01T16:05:37.318585Z","shell.execute_reply.started":"2021-09-01T16:05:37.308623Z","shell.execute_reply":"2021-09-01T16:05:37.317073Z"},"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(t_X_train, y_train['Y1'], early_stopping_rounds=5, eval_set=[(t_X_valid, y_valid['Y1'])], verbose=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T16:05:37.321273Z","iopub.execute_input":"2021-09-01T16:05:37.321614Z","iopub.status.idle":"2021-09-01T16:05:39.887401Z","shell.execute_reply.started":"2021-09-01T16:05:37.321588Z","shell.execute_reply":"2021-09-01T16:05:39.886402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = T_df.drop(['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-09-01T16:05:39.888981Z","iopub.execute_input":"2021-09-01T16:05:39.889281Z","iopub.status.idle":"2021-09-01T16:05:39.922988Z","shell.execute_reply.started":"2021-09-01T16:05:39.889254Z","shell.execute_reply":"2021-09-01T16:05:39.922223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_param2 = make_pipeline(StandardScaler(), SVR(C=1.0, epsilon=0.2))\nmodel_param2.fit(X_train,y_train['Y2'])","metadata":{"execution":{"iopub.status.busy":"2021-09-01T16:07:52.486737Z","iopub.execute_input":"2021-09-01T16:07:52.48711Z","iopub.status.idle":"2021-09-01T16:15:24.709036Z","shell.execute_reply.started":"2021-09-01T16:07:52.487081Z","shell.execute_reply":"2021-09-01T16:15:24.707971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model_param2 = XGBRegressor(n_estimators=1000, learning_rate=0.01, tree_method='hist')\n# model_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-09-01T16:05:40.027679Z","iopub.status.idle":"2021-09-01T16:05:40.028135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X_valid.drop(['SEQUENCE_NO'],axis=1).describe()\n# X_valid = X_valid.assign(model_param1.predict(t_X_valid))","metadata":{"execution":{"iopub.status.busy":"2021-09-01T16:05:40.029466Z","iopub.status.idle":"2021-09-01T16:05:40.029922Z"},"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(t_X_valid)))}')\nprint(f'Y2 rmse: {sqrt(mean_squared_error(y2, model_param2.predict(X_valid)))}')","metadata":{"execution":{"iopub.status.busy":"2021-09-01T16:15:24.710716Z","iopub.execute_input":"2021-09-01T16:15:24.711015Z","iopub.status.idle":"2021-09-01T16:16:44.111324Z","shell.execute_reply.started":"2021-09-01T16:15:24.710987Z","shell.execute_reply":"2021-09-01T16:16:44.110011Z"},"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-09-01T16:05:40.032303Z","iopub.status.idle":"2021-09-01T16:05:40.032748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'UNIQUE_IDENTIFIER': test_dataset['UNIQUE_IDENTIFIER'], 'Y1': model_param1.predict(valid_test_dataset.drop(['SEQUENCE_NO'],axis=1)), 'Y2': model_param2.predict(valid_test_dataset)})\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T16:05:40.033735Z","iopub.status.idle":"2021-09-01T16:05:40.034138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame({'Cols': X.drop(['SEQUENCE_NO'],axis=1).columns.to_list(), 'Weightage': model_param1.feature_importances_}).sort_values(by=['Weightage'], ascending=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T16:05:40.035025Z","iopub.status.idle":"2021-09-01T16:05:40.035471Z"},"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-09-01T16:05:40.036341Z","iopub.status.idle":"2021-09-01T16:05:40.036749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}