{"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":"%%capture\n!pip install -U scikit-learn==1.0","metadata":{"execution":{"iopub.status.busy":"2021-10-11T12:58:14.275732Z","iopub.execute_input":"2021-10-11T12:58:14.27624Z","iopub.status.idle":"2021-10-11T12:58:30.434975Z","shell.execute_reply.started":"2021-10-11T12:58:14.276142Z","shell.execute_reply":"2021-10-11T12:58:30.434078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport zipfile\nimport os\nfrom tqdm import tqdm\nimport gc\nimport time\n\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.ensemble import RandomForestRegressor\nimport xgboost as xgb\nfrom sklearn.metrics import mean_squared_log_error\nfrom sklearn.model_selection import RandomizedSearchCV","metadata":{"execution":{"iopub.status.busy":"2021-10-11T13:09:53.177607Z","iopub.execute_input":"2021-10-11T13:09:53.177919Z","iopub.status.idle":"2021-10-11T13:09:53.183657Z","shell.execute_reply.started":"2021-10-11T13:09:53.177888Z","shell.execute_reply":"2021-10-11T13:09:53.182766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reduce_mem_usage(props):\n    start_mem_usg = props.memory_usage().sum() / 1024**2 \n    NAlist = [] # Keeps track of columns that have missing values filled in. \n    for col in props.columns:\n        if props[col].dtype != object:  # Exclude strings\n            # make variables for Int, max and min\n            IsInt = False\n            mx = props[col].max()\n            mn = props[col].min()\n            \n            # Integer does not support NA, therefore, NA needs to be filled\n            if not np.isfinite(props[col]).all(): \n                NAlist.append(col)\n                props[col].fillna(mn-1,inplace=True)  \n                   \n            # test if column can be converted to an integer\n            asint = props[col].fillna(0).astype(np.int64)\n            result = (props[col] - asint)\n            result = result.sum()\n            if result > -0.01 and result < 0.01:\n                IsInt = True\n            \n            # Make Integer/unsigned Integer datatypes\n            if IsInt:\n                if mn >= 0:\n                    if mx < 255:\n                        props[col] = props[col].astype(np.uint8)\n                    elif mx < 65535:\n                        props[col] = props[col].astype(np.uint16)\n                    elif mx < 4294967295:\n                        props[col] = props[col].astype(np.uint32)\n                    else:\n                        props[col] = props[col].astype(np.uint64)\n                else:\n                    if mn > np.iinfo(np.int8).min and mx < np.iinfo(np.int8).max:\n                        props[col] = props[col].astype(np.int8)\n                    elif mn > np.iinfo(np.int16).min and mx < np.iinfo(np.int16).max:\n                        props[col] = props[col].astype(np.int16)\n                    elif mn > np.iinfo(np.int32).min and mx < np.iinfo(np.int32).max:\n                        props[col] = props[col].astype(np.int32)\n                    elif mn > np.iinfo(np.int64).min and mx < np.iinfo(np.int64).max:\n                        props[col] = props[col].astype(np.int64)    \n            \n            # Make float datatypes 32 bit\n            else:\n                props[col] = props[col].astype(np.float32)\n    \n    mem_usg = props.memory_usage().sum() / 1024**2 \n    print(\"Memory usage of:\", round(start_mem_usg, 1), \" MB\", \" reduced to: \", round(mem_usg, 1),\" MB\")\n    return props, NAlist\n\n\ndef chunk_read_csv(fpath, chunksize=10000000):\n    df = pd.DataFrame()\n    for chunk in pd.read_csv(fpath,chunksize=chunksize):\n        df = pd.concat([df, chunk])\n    return df\n\n\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in tqdm(filenames):\n        zipfile.ZipFile(os.path.join(dirname, filename)).extractall()\n        locals()[filename[:-8]] = reduce_mem_usage(chunk_read_csv(os.path.join(\".\", filename[:-4])))[0]\n        gc.collect()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-11T12:58:31.470234Z","iopub.execute_input":"2021-10-11T12:58:31.470809Z","iopub.status.idle":"2021-10-11T13:01:04.672006Z","shell.execute_reply.started":"2021-10-11T12:58:31.470762Z","shell.execute_reply":"2021-10-11T13:01:04.67108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TO_ENG_DICT = {\n    \"Semana\": \"week_number\", # From Thursday to Wednesday\n    \"Agencia_ID\": \"sales_depot_id\", \n    \"Canal_ID\": \"sales_channel_id\", \n    \"Ruta_SAK\": \"route_id\", # Several routes = Sales Depot\n    \"Cliente_ID\": \"client_id\", \n    \"NombreCliente\": \"client_name\", \n    \"Producto_ID\": \"product_id\", \n    \"NombreProducto\": \"product_name\", \n    \"Venta_uni_hoy\": \"sales_unit_this_week\", # integer\n    \"Producto_ID\": \"product_id\", \n    \"NombreProducto\": \"product_name\", \n    \"Venta_uni_hoy\": \"sales_unit_this_week\", # integer\n    \"Venta_hoy\": \"sales_this_week\", # unit: pesos\n    \"Dev_uni_proxima\": \"returns_unit_next_week\", # integer\n    \"Dev_proxima\": \"returns_next_week\", # unit: pesos\n    \"Demanda_uni_equil\": \"adjusted_demand\", # integer, target\n    \"Town\": \"town\",\n    \"State\": \"state\",\n}\n\nTARGET = \"Demanda_uni_equil\"\n\ncliente_tabla = cliente_tabla.rename(columns=TO_ENG_DICT, copy=False)\nproducto_tabla = producto_tabla.rename(columns=TO_ENG_DICT, copy=False)\ntown_state = town_state.rename(columns=TO_ENG_DICT, copy=False)\ntrain = train.rename(columns=TO_ENG_DICT, copy=False)\ntest = test.rename(columns=TO_ENG_DICT, copy=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-11T13:03:18.386887Z","iopub.execute_input":"2021-10-11T13:03:18.387735Z","iopub.status.idle":"2021-10-11T13:03:18.398703Z","shell.execute_reply.started":"2021-10-11T13:03:18.387694Z","shell.execute_reply":"2021-10-11T13:03:18.397952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(cliente_tabla.head(2))\ndisplay(producto_tabla.head(2))\ndisplay(town_state.head(2))","metadata":{"execution":{"iopub.status.busy":"2021-10-11T13:03:19.146132Z","iopub.execute_input":"2021-10-11T13:03:19.146997Z","iopub.status.idle":"2021-10-11T13:03:19.168081Z","shell.execute_reply.started":"2021-10-11T13:03:19.146952Z","shell.execute_reply":"2021-10-11T13:03:19.16715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(sample_submission.head(2))","metadata":{"execution":{"iopub.status.busy":"2021-10-11T13:03:19.638746Z","iopub.execute_input":"2021-10-11T13:03:19.639467Z","iopub.status.idle":"2021-10-11T13:03:19.649673Z","shell.execute_reply.started":"2021-10-11T13:03:19.639421Z","shell.execute_reply":"2021-10-11T13:03:19.648586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(train.head(2))\ndisplay(test.head(2))","metadata":{"execution":{"iopub.status.busy":"2021-10-11T13:03:19.736083Z","iopub.execute_input":"2021-10-11T13:03:19.736949Z","iopub.status.idle":"2021-10-11T13:03:19.758451Z","shell.execute_reply.started":"2021-10-11T13:03:19.736902Z","shell.execute_reply":"2021-10-11T13:03:19.757558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().any().any(), test.isnull().any().any()","metadata":{"execution":{"iopub.status.busy":"2021-10-11T13:03:20.1454Z","iopub.execute_input":"2021-10-11T13:03:20.146237Z","iopub.status.idle":"2021-10-11T13:03:20.628793Z","shell.execute_reply.started":"2021-10-11T13:03:20.146181Z","shell.execute_reply":"2021-10-11T13:03:20.627786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def unique_in_test(train_vals, test_vals):\n#     test_set = set(test_vals)\n#     intersect_set = set(np.intersect1d(train_vals, test_vals))\n#     diff_set = test_set - intersect_set\n#     return len(list(diff_set)), round(len(list(diff_set)) / len(list(test_set)), 3)\n\n# print(unique_in_test(train.sales_depot_id, test.sales_depot_id))\n# print(unique_in_test(train.sales_channel_id, test.sales_channel_id))\n# print(unique_in_test(train.route_id, test.route_id))\n# print(unique_in_test(train.product_id, test.product_id))\n# print(unique_in_test(train.client_id, test.client_id))","metadata":{"execution":{"iopub.status.busy":"2021-10-11T13:03:20.630439Z","iopub.execute_input":"2021-10-11T13:03:20.630677Z","iopub.status.idle":"2021-10-11T13:03:20.634865Z","shell.execute_reply.started":"2021-10-11T13:03:20.630651Z","shell.execute_reply":"2021-10-11T13:03:20.634038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.sort_values(\"week_number\", inplace=True, ignore_index=True)\ngc.collect(); time.sleep(2); gc.collect()\n\ntest.sort_values(\"id\", inplace=True, ignore_index=True)\ntest.drop(columns=[\"id\"], inplace=True)\ngc.collect(); time.sleep(2); gc.collect()","metadata":{"execution":{"iopub.status.busy":"2021-10-11T13:03:20.636458Z","iopub.execute_input":"2021-10-11T13:03:20.637082Z","iopub.status.idle":"2021-10-11T13:03:34.265072Z","shell.execute_reply.started":"2021-10-11T13:03:20.63705Z","shell.execute_reply":"2021-10-11T13:03:34.264203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature Generation","metadata":{}},{"cell_type":"code","source":"# train = train.head(len(train) // 100)\n# gc.collect()\n# time.sleep(2)\n# test = test.head(len(test) // 100)\n# gc.collect()\n# time.sleep(2)\n# train.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-11T13:03:34.266824Z","iopub.execute_input":"2021-10-11T13:03:34.267036Z","iopub.status.idle":"2021-10-11T13:03:34.271157Z","shell.execute_reply.started":"2021-10-11T13:03:34.267011Z","shell.execute_reply":"2021-10-11T13:03:34.270139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"town_state.town = LabelEncoder().fit_transform(town_state.town.values)\ntown_state.state = LabelEncoder().fit_transform(town_state.state.values)\ntown_state = reduce_mem_usage(town_state)[0]\ntrain = train.merge(town_state, on=\"sales_depot_id\", copy=False)\ntest = test.merge(town_state, on=\"sales_depot_id\", copy=False)\ngc.collect(); time.sleep(2); gc.collect()","metadata":{"execution":{"iopub.status.busy":"2021-10-11T13:03:34.272756Z","iopub.execute_input":"2021-10-11T13:03:34.272992Z","iopub.status.idle":"2021-10-11T13:03:48.546258Z","shell.execute_reply.started":"2021-10-11T13:03:34.272966Z","shell.execute_reply":"2021-10-11T13:03:48.545648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def calc_features(df, col):\n    features_df = df.groupby(col)\\\n                .agg({\"sales_unit_this_week\": [\"mean\", \"max\"],\n                      \"sales_this_week\": \"mean\"})\n    features_df.columns = list(map(\"_\".join, features_df.columns))\n    features_df.columns = features_df.columns.values + \"_{}\".format(col)\n    return features_df\n\ndef add_feature(X_train, X_test, col):\n    features_df = reduce_mem_usage(calc_features(X_train, col))[0]\n    return X_train.merge(features_df, on=col, copy=False, how=\"left\"), X_test.merge(features_df, on=col, copy=False, how=\"left\")","metadata":{"execution":{"iopub.status.busy":"2021-10-11T13:03:48.547159Z","iopub.execute_input":"2021-10-11T13:03:48.548034Z","iopub.status.idle":"2021-10-11T13:03:48.55541Z","shell.execute_reply.started":"2021-10-11T13:03:48.548001Z","shell.execute_reply":"2021-10-11T13:03:48.554431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train, test = add_feature(train, test, \"sales_channel_id\")\n# gc.collect(); time.sleep(2); gc.collect()\n# train, test = add_feature(train, test, \"sales_depot_id\")\n# gc.collect(); time.sleep(2); gc.collect()\n# train, test = add_feature(train, test, \"town\")\n# gc.collect(); time.sleep(2); gc.collect()\n# train, test = add_feature(train, test, \"state\")\n# gc.collect(); time.sleep(2); gc.collect()\n# train, test = add_feature(train, test, \"route_id\")\n# gc.collect(); time.sleep(2); gc.collect()","metadata":{"execution":{"iopub.status.busy":"2021-10-11T13:03:48.556756Z","iopub.execute_input":"2021-10-11T13:03:48.556988Z","iopub.status.idle":"2021-10-11T13:03:48.56876Z","shell.execute_reply.started":"2021-10-11T13:03:48.556963Z","shell.execute_reply":"2021-10-11T13:03:48.567761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.fillna(0, inplace=True)\ntest.fillna(0, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2021-10-11T13:03:51.382838Z","iopub.execute_input":"2021-10-11T13:03:51.383095Z","iopub.status.idle":"2021-10-11T13:03:51.992805Z","shell.execute_reply.started":"2021-10-11T13:03:51.383065Z","shell.execute_reply":"2021-10-11T13:03:51.991565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Cross-Validation Parameters Tuning","metadata":{}},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2021-10-11T14:07:28.583207Z","iopub.execute_input":"2021-10-11T14:07:28.583885Z","iopub.status.idle":"2021-10-11T14:07:30.059888Z","shell.execute_reply.started":"2021-10-11T14:07:28.583831Z","shell.execute_reply":"2021-10-11T14:07:30.058996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test = train.head(int(len(train) * 0.2)), train.tail(len(train) - int(len(train) * 0.9))","metadata":{"execution":{"iopub.status.busy":"2021-10-11T13:04:06.443041Z","iopub.execute_input":"2021-10-11T13:04:06.443348Z","iopub.status.idle":"2021-10-11T13:04:06.448988Z","shell.execute_reply.started":"2021-10-11T13:04:06.443318Z","shell.execute_reply":"2021-10-11T13:04:06.448164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = X_train[\"adjusted_demand\"]\nX_train.drop(columns=[\"week_number\", \"sales_unit_this_week\", \"sales_this_week\", \"returns_unit_next_week\", \"returns_next_week\", \"adjusted_demand\"], \n                     inplace=True)\n\ny_test = X_test[\"adjusted_demand\"]\nX_test.drop(columns=[\"week_number\"], inplace=True)\nX_test.drop(columns=[\"sales_unit_this_week\", \"sales_this_week\", \"returns_unit_next_week\", \"returns_next_week\", \"adjusted_demand\"], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2021-10-11T13:04:08.833452Z","iopub.execute_input":"2021-10-11T13:04:08.834281Z","iopub.status.idle":"2021-10-11T13:04:09.725021Z","shell.execute_reply.started":"2021-10-11T13:04:08.834234Z","shell.execute_reply":"2021-10-11T13:04:09.72404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(X_train), len(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-10-11T13:14:43.052417Z","iopub.execute_input":"2021-10-11T13:14:43.05319Z","iopub.status.idle":"2021-10-11T13:14:43.058444Z","shell.execute_reply.started":"2021-10-11T13:14:43.053149Z","shell.execute_reply":"2021-10-11T13:14:43.05782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Random Forest","metadata":{}},{"cell_type":"code","source":"model = RandomForestRegressor(n_estimators=1, \n                              max_depth=5, \n                              min_samples_leaf=100000, \n                              min_weight_fraction_leaf=0.3, \n                              random_state=42)\n\nparams_grid = {\n    \"n_estimators\": [2, 3],\n    \"max_depth\": [4, 5],\n    \"min_samples_leaf\": [100000, 200000],\n}\n\nsearch = RandomizedSearchCV(model, params_grid, n_iter=1, cv=[[list(range(len(X_train))), list(range(len(X_test)))]], \n                            scoring=\"neg_mean_squared_log_error\")\nsearch.fit(pd.concat([X_train, X_test]).reset_index(drop=True), pd.concat([y_train, y_test]).reset_index(drop=True))","metadata":{"execution":{"iopub.status.busy":"2021-10-11T14:07:37.416449Z","iopub.execute_input":"2021-10-11T14:07:37.416736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dict(filter(lambda x: x[0] in params_grid.keys(), search.best_estimator_.get_params().items()))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"search.best_score_ * (-1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### XGBoost","metadata":{}},{"cell_type":"code","source":"%%time\nboost = xgb.XGBRegressor(max_depth=4, \n                        learning_rate=0.1, \n                        n_estimators=10, \n                        silent=True, \n                        objective='reg:linear', \n                        random_state=42)\nboost.fit(X_train.head(100000), y_train.head(100000), eval_metric='rmse', verbose=True, eval_set=[(X_test.head(10000), y_test.head(10000))],\n              early_stopping_rounds=100)\nmean_squared_log_error(y_test.head(10000), boost.predict(X_test.head(10000)), squared=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-11T12:51:00.967582Z","iopub.execute_input":"2021-10-11T12:51:00.968096Z","iopub.status.idle":"2021-10-11T12:51:01.20783Z","shell.execute_reply.started":"2021-10-11T12:51:00.968063Z","shell.execute_reply":"2021-10-11T12:51:01.206893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rmsle(predictions, dmat):\n    labels = dmat.get_label()\n    diffs = np.log(predictions + 1) - np.log(labels + 1)\n    squared_diffs = np.square(diffs)\n    avg = np.mean(squared_diffs)\n    return ('RMSLE', np.sqrt(avg))\n\nmodel = xgb.XGBRegressor(max_depth=4, \n                        learning_rate=0.1, \n                        n_estimators=10, \n                        silent=True, \n                        objective='reg:linear', \n                        random_state=42)\n\nparams_grid = {\n    \"max_depth\": [4, 5],\n    \"lambda\": [0.0, 0.1],\n}\n\nsearch = RandomizedSearchCV(model, params_grid, n_iter=1, cv=[[list(range(len(X_train))), list(range(len(X_test)))]], \n                            scoring=\"neg_mean_squared_log_error\")\nsearch.fit(pd.concat([X_train, X_test]).reset_index(drop=True), pd.concat([y_train, y_test]).reset_index(drop=True),\n           eval_metric=rmsle, verbose=True, eval_set=[(X_test, y_test)], early_stopping_rounds=100)","metadata":{"execution":{"iopub.status.busy":"2021-10-11T14:07:02.256442Z","iopub.execute_input":"2021-10-11T14:07:02.25676Z","iopub.status.idle":"2021-10-11T14:07:14.36507Z","shell.execute_reply.started":"2021-10-11T14:07:02.256728Z","shell.execute_reply":"2021-10-11T14:07:14.363743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dict(filter(lambda x: x[0] in params_grid.keys(), search.best_estimator_.get_params().items()))","metadata":{"execution":{"iopub.status.busy":"2021-10-11T13:51:32.176875Z","iopub.execute_input":"2021-10-11T13:51:32.177159Z","iopub.status.idle":"2021-10-11T13:51:32.182378Z","shell.execute_reply.started":"2021-10-11T13:51:32.17713Z","shell.execute_reply":"2021-10-11T13:51:32.18156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"search.best_score_ * (-1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Predict","metadata":{}},{"cell_type":"code","source":"train_target = train[\"adjusted_demand\"]\ntrain.drop(columns=[\"week_number\", \"sales_unit_this_week\", \"sales_this_week\", \"returns_unit_next_week\", \"returns_next_week\", \"adjusted_demand\"], \n                     inplace=True)\n\ntest.drop(columns=[\"week_number\"], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2021-10-11T11:40:30.9544Z","iopub.execute_input":"2021-10-11T11:40:30.955172Z","iopub.status.idle":"2021-10-11T11:40:32.95085Z","shell.execute_reply.started":"2021-10-11T11:40:30.955124Z","shell.execute_reply":"2021-10-11T11:40:32.950007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nmodel = RandomForestRegressor(n_estimators=2, \n                              max_depth=5, \n                              min_samples_leaf=100000, \n                              min_weight_fraction_leaf=0.3, \n                              random_state=42)\nmodel.fit(train, train_target)","metadata":{"execution":{"iopub.status.busy":"2021-10-11T11:41:26.466076Z","iopub.execute_input":"2021-10-11T11:41:26.466492Z","iopub.status.idle":"2021-10-11T11:42:59.239579Z","shell.execute_reply.started":"2021-10-11T11:41:26.466447Z","shell.execute_reply":"2021-10-11T11:42:59.238918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2021-10-11T11:43:12.244155Z","iopub.execute_input":"2021-10-11T11:43:12.245325Z","iopub.status.idle":"2021-10-11T11:43:12.827495Z","shell.execute_reply.started":"2021-10-11T11:43:12.245279Z","shell.execute_reply":"2021-10-11T11:43:12.826372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission[TARGET] = preds\nsample_submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-11T11:44:20.942552Z","iopub.execute_input":"2021-10-11T11:44:20.942908Z","iopub.status.idle":"2021-10-11T11:44:46.231752Z","shell.execute_reply.started":"2021-10-11T11:44:20.942874Z","shell.execute_reply":"2021-10-11T11:44:46.230801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()\nprint(1)","metadata":{"execution":{"iopub.status.busy":"2021-10-11T12:46:04.699497Z","iopub.execute_input":"2021-10-11T12:46:04.699927Z","iopub.status.idle":"2021-10-11T12:46:04.815031Z","shell.execute_reply.started":"2021-10-11T12:46:04.699896Z","shell.execute_reply":"2021-10-11T12:46:04.814377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nboost = xgb.XGBRegressor(max_depth=4, \n                        learning_rate=0.1, \n                        n_estimators=10, \n                        silent=True, \n                        objective='reg:linear', \n                        random_state=42)\n\nboost.fit(X_train, y_train, eval_metric='rmse', verbose=True, eval_set=[(X_test, y_test)], early_stopping_rounds=100)\npreds = boost.predict(test)","metadata":{"execution":{"iopub.status.busy":"2021-10-11T12:51:38.563503Z","iopub.execute_input":"2021-10-11T12:51:38.563862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = RandomForestRegressor()","metadata":{"execution":{"iopub.status.busy":"2021-10-11T09:37:48.362873Z","iopub.execute_input":"2021-10-11T09:37:48.363664Z","iopub.status.idle":"2021-10-11T09:37:48.36748Z","shell.execute_reply.started":"2021-10-11T09:37:48.36362Z","shell.execute_reply":"2021-10-11T09:37:48.36653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = xgb.XGBRegressor()","metadata":{"execution":{"iopub.status.busy":"2021-10-11T09:36:54.709068Z","iopub.execute_input":"2021-10-11T09:36:54.709357Z","iopub.status.idle":"2021-10-11T09:36:54.71357Z","shell.execute_reply.started":"2021-10-11T09:36:54.709327Z","shell.execute_reply":"2021-10-11T09:36:54.712678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit","metadata":{"execution":{"iopub.status.busy":"2021-10-11T09:36:57.51505Z","iopub.execute_input":"2021-10-11T09:36:57.515459Z","iopub.status.idle":"2021-10-11T09:36:57.525433Z","shell.execute_reply.started":"2021-10-11T09:36:57.515429Z","shell.execute_reply":"2021-10-11T09:36:57.524449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}