{"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-01T17:13:56.003215Z","iopub.execute_input":"2021-09-01T17:13:56.004031Z","iopub.status.idle":"2021-09-01T17:13:56.021746Z","shell.execute_reply.started":"2021-09-01T17:13:56.003905Z","shell.execute_reply":"2021-09-01T17:13:56.020465Z"},"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","metadata":{"execution":{"iopub.status.busy":"2021-09-01T17:13:56.023451Z","iopub.execute_input":"2021-09-01T17:13:56.023886Z","iopub.status.idle":"2021-09-01T17:13:57.292945Z","shell.execute_reply.started":"2021-09-01T17:13:56.023843Z","shell.execute_reply":"2021-09-01T17:13:57.292134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.svm import SVR\n# from sklearn.pipeline import make_pipeline\n# from sklearn.preprocessing import StandardScaler","metadata":{"execution":{"iopub.status.busy":"2021-09-01T17:13:57.294350Z","iopub.execute_input":"2021-09-01T17:13:57.294758Z","iopub.status.idle":"2021-09-01T17:13:57.297195Z","shell.execute_reply.started":"2021-09-01T17:13:57.294728Z","shell.execute_reply":"2021-09-01T17:13:57.296603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/beyond-analysis/train.csv')\ntrain_df.describe()","metadata":{"execution":{"iopub.status.busy":"2021-09-01T17:13:57.299014Z","iopub.execute_input":"2021-09-01T17:13:57.299280Z","iopub.status.idle":"2021-09-01T17:14:04.090058Z","shell.execute_reply.started":"2021-09-01T17:13:57.299254Z","shell.execute_reply":"2021-09-01T17:14:04.089150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cleanse(df):\n    df = df.groupby('UNIQUE_IDENTIFIER')\n    df = df.agg(np.mean)\n    # 'CATEGORY_1', 'CATEGORY_2', 'UNIQUE_IDENTIFIER' - categorical columns already removed by agg\n    to_drop = ['DEPOSIT_2', 'DEPOSIT_TRAILS']\n    return df.drop(to_drop, axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T17:14:04.091161Z","iopub.execute_input":"2021-09-01T17:14:04.091463Z","iopub.status.idle":"2021-09-01T17:14:04.096701Z","shell.execute_reply.started":"2021-09-01T17:14:04.091426Z","shell.execute_reply":"2021-09-01T17:14:04.095717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_clean = cleanse(train_df)\n\nX = train_clean.drop(['Y2'], axis=1)\ny = train_clean[['Y1','Y2']]\n\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-01T17:14:04.098264Z","iopub.execute_input":"2021-09-01T17:14:04.098932Z","iopub.status.idle":"2021-09-01T17:14:04.394363Z","shell.execute_reply.started":"2021-09-01T17:14:04.098885Z","shell.execute_reply":"2021-09-01T17:14:04.393260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model_param2 = make_pipeline(StandardScaler(), SVR(C=1.0, epsilon=0.2))\n# model_param2.fit(X_train,y_train['Y2'])","metadata":{"execution":{"iopub.status.busy":"2021-09-01T17:14:04.395822Z","iopub.execute_input":"2021-09-01T17:14:04.396170Z","iopub.status.idle":"2021-09-01T17:14:04.400124Z","shell.execute_reply.started":"2021-09-01T17:14:04.396130Z","shell.execute_reply":"2021-09-01T17:14:04.399084Z"},"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-09-01T17:14:04.403904Z","iopub.execute_input":"2021-09-01T17:14:04.404195Z","iopub.status.idle":"2021-09-01T17:14:14.746360Z","shell.execute_reply.started":"2021-09-01T17:14:04.404168Z","shell.execute_reply":"2021-09-01T17:14:14.745435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t_X_train = X_train.drop(['Y1'],axis=1)\nt_X_valid = X_valid.drop(['Y1'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T17:14:14.748196Z","iopub.execute_input":"2021-09-01T17:14:14.748491Z","iopub.status.idle":"2021-09-01T17:14:14.758398Z","shell.execute_reply.started":"2021-09-01T17:14:14.748463Z","shell.execute_reply":"2021-09-01T17:14:14.757322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t_X_train.columns","metadata":{"execution":{"iopub.status.busy":"2021-09-01T17:14:14.759637Z","iopub.execute_input":"2021-09-01T17:14:14.759912Z","iopub.status.idle":"2021-09-01T17:14:14.773273Z","shell.execute_reply.started":"2021-09-01T17:14:14.759885Z","shell.execute_reply":"2021-09-01T17:14:14.772132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t_X_train = t_X_train.drop(['SEQUENCE_NO'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T17:14:14.774641Z","iopub.execute_input":"2021-09-01T17:14:14.775088Z","iopub.status.idle":"2021-09-01T17:14:14.788954Z","shell.execute_reply.started":"2021-09-01T17:14:14.775047Z","shell.execute_reply":"2021-09-01T17:14:14.788039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t_X_valid = t_X_valid.drop(['SEQUENCE_NO'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T17:14:14.790465Z","iopub.execute_input":"2021-09-01T17:14:14.790842Z","iopub.status.idle":"2021-09-01T17:14:14.804222Z","shell.execute_reply.started":"2021-09-01T17:14:14.790806Z","shell.execute_reply":"2021-09-01T17:14:14.803068Z"},"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-01T17:14:14.807920Z","iopub.execute_input":"2021-09-01T17:14:14.808409Z","iopub.status.idle":"2021-09-01T17:14:17.210865Z","shell.execute_reply.started":"2021-09-01T17:14:14.808311Z","shell.execute_reply":"2021-09-01T17:14:17.209819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y1, y2 = y_valid['Y1'], y_valid['Y2']\nt_X_valid = X_valid.drop(['SEQUENCE_NO','Y1'],axis=1)\nprint(f'Y1 rmse: {mean_squared_error(y1,model_param1.predict(t_X_valid),squared=False)}')\nprint(f'Y2 rmse: {mean_squared_error(y2,model_param2.predict(X_valid),squared=False)}')","metadata":{"execution":{"iopub.status.busy":"2021-09-01T17:14:17.213323Z","iopub.execute_input":"2021-09-01T17:14:17.213636Z","iopub.status.idle":"2021-09-01T17:14:17.456032Z","shell.execute_reply.started":"2021-09-01T17:14:17.213607Z","shell.execute_reply":"2021-09-01T17:14:17.455246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t_X_valid.columns","metadata":{"execution":{"iopub.status.busy":"2021-09-01T17:14:17.457282Z","iopub.execute_input":"2021-09-01T17:14:17.457834Z","iopub.status.idle":"2021-09-01T17:14:17.464302Z","shell.execute_reply.started":"2021-09-01T17:14:17.457798Z","shell.execute_reply":"2021-09-01T17:14:17.463464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_dataset = pd.read_csv('/kaggle/input/beyond-analysis/test.csv')\n# print(test_dataset.shape)\n\n# valid_test_dataset = cleanse(test_dataset)\n# print(valid_test_dataset.shape)\n\n# test_dataset.drop_duplicates(subset=['UNIQUE_IDENTIFIER'], inplace=True)\n# print(test_dataset.shape)\n\n# 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)})\n# output.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T17:14:17.466904Z","iopub.execute_input":"2021-09-01T17:14:17.469093Z","iopub.status.idle":"2021-09-01T17:14:17.474404Z","shell.execute_reply.started":"2021-09-01T17:14:17.469045Z","shell.execute_reply":"2021-09-01T17:14:17.473699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv('/kaggle/input/beyond-analysis/test.csv')\nprint(test_df.shape)\n\nclean_test_df = cleanse(test_df)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T17:14:17.477056Z","iopub.execute_input":"2021-09-01T17:14:17.477407Z","iopub.status.idle":"2021-09-01T17:14:20.284599Z","shell.execute_reply.started":"2021-09-01T17:14:17.477375Z","shell.execute_reply":"2021-09-01T17:14:20.283581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y1 = model_param1.predict(clean_test_df.drop(['SEQUENCE_NO'],axis=1))","metadata":{"execution":{"iopub.status.busy":"2021-09-01T17:14:20.285800Z","iopub.execute_input":"2021-09-01T17:14:20.286115Z","iopub.status.idle":"2021-09-01T17:14:20.419624Z","shell.execute_reply.started":"2021-09-01T17:14:20.286082Z","shell.execute_reply":"2021-09-01T17:14:20.418688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clean_test_df['Y1'] = Y1\n\nY2 = model_param2.predict(clean_test_df)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T17:14:20.423706Z","iopub.execute_input":"2021-09-01T17:14:20.425748Z","iopub.status.idle":"2021-09-01T17:14:20.931769Z","shell.execute_reply.started":"2021-09-01T17:14:20.425699Z","shell.execute_reply":"2021-09-01T17:14:20.930897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t_test_df = test_df.drop_duplicates(subset=['UNIQUE_IDENTIFIER'])","metadata":{"execution":{"iopub.status.busy":"2021-09-01T17:14:20.935912Z","iopub.execute_input":"2021-09-01T17:14:20.938406Z","iopub.status.idle":"2021-09-01T17:14:20.992913Z","shell.execute_reply.started":"2021-09-01T17:14:20.938351Z","shell.execute_reply":"2021-09-01T17:14:20.992078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'UNIQUE_IDENTIFIER': t_test_df['UNIQUE_IDENTIFIER'], 'Y1': Y1, 'Y2': Y2})\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T17:14:20.996422Z","iopub.execute_input":"2021-09-01T17:14:20.996816Z","iopub.status.idle":"2021-09-01T17:14:21.294908Z","shell.execute_reply.started":"2021-09-01T17:14:20.996783Z","shell.execute_reply":"2021-09-01T17:14:21.294009Z"},"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-01T17:14:21.296193Z","iopub.execute_input":"2021-09-01T17:14:21.296493Z","iopub.status.idle":"2021-09-01T17:14:21.628787Z","shell.execute_reply.started":"2021-09-01T17:14:21.296464Z","shell.execute_reply":"2021-09-01T17:14:21.626866Z"},"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-01T17:14:21.629683Z","iopub.status.idle":"2021-09-01T17:14:21.630099Z"},"trusted":true},"execution_count":null,"outputs":[]}]}