{"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":"'''Importing Data Manipulattion Modules'''\nimport re\nimport numpy as np\nimport pandas as pd\nfrom pandas.core.dtypes.dtypes import CategoricalDtype\nfrom scipy.stats import norm, skew\nfrom scipy.special import boxcox1p\npd.set_option('display.float_format', lambda x: '{:.3f}'.format(x))\npd.set_option(\"display.max_columns\", 81)\npd.set_option(\"display.max_rows\", 101)\npd.set_option(\"display.max_colwidth\", 100)\n\n'''Seaborn and Matplotlib Visualization'''\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nplt.style.use('bmh')                    \nsns.set_style({'axes.grid':False}) \nsns.set_style('whitegrid')\n%matplotlib inline\n\n'''Validation'''\nfrom sklearn.model_selection import KFold, cross_val_score\n\n'''Ignore deprecation and future, and user warnings.'''\nimport warnings as wrn\nwrn.filterwarnings('ignore', category = DeprecationWarning) \nwrn.filterwarnings('ignore', category = FutureWarning) \nwrn.filterwarnings('ignore', category = UserWarning) \n\n'''Importing Modeling Interested Modules'''\nfrom sklearn.base import BaseEstimator\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.preprocessing import StandardScaler, RobustScaler\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.linear_model import LinearRegression, LassoCV, RidgeCV, ElasticNetCV\nfrom sklearn.svm import SVR\nfrom sklearn.kernel_ridge import KernelRidge\nfrom lightgbm import LGBMRegressor\n#3.532seconds","metadata":{"execution":{"iopub.status.busy":"2023-05-23T13:58:57.881087Z","iopub.execute_input":"2023-05-23T13:58:57.881542Z","iopub.status.idle":"2023-05-23T13:59:00.481826Z","shell.execute_reply.started":"2023-05-23T13:58:57.881508Z","shell.execute_reply":"2023-05-23T13:59:00.480671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dtype = {\n    'Semana':'int8',\n    'Cliente_ID':'int32',\n    'Producto_ID':'int32',\n    'id': 'int32',\n    'Canal_ID': 'int8',\n    'NombreCliente':'category',\n    'Prom':'int8',\n    'price':'float16',\n    'wtpcs':'float16',\n    'product_med':'float16',\n    'client_med':'float16',\n    'popular_name_med':'float16',\n    'town_id_med':'float16',\n    'ruta_id_med':'float16',\n    'brand_med':'float16',\n    'property_med':'float16',\n    'agencia_med':'int16',\n    'cliente_type_med':'int16',\n    'top10': 'int8',\n    'sum of demand': 'int32',\n    'town_client': 'int32',\n    \n\n}\ntest_df = pd.read_csv(\"/kaggle/input/tsai-doc/test_df_Tsai.csv\",dtype=dtype)\n#24.506seconds","metadata":{"execution":{"iopub.status.busy":"2023-05-17T12:55:16.011267Z","iopub.execute_input":"2023-05-17T12:55:16.011619Z","iopub.status.idle":"2023-05-17T12:55:30.205452Z","shell.execute_reply.started":"2023-05-17T12:55:16.011588Z","shell.execute_reply":"2023-05-17T12:55:30.204384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-05-17T12:55:30.206550Z","iopub.execute_input":"2023-05-17T12:55:30.206828Z","iopub.status.idle":"2023-05-17T12:55:30.243895Z","shell.execute_reply.started":"2023-05-17T12:55:30.206804Z","shell.execute_reply":"2023-05-17T12:55:30.241978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train\ndtype = {\n    'Semana':'int8',\n    'Cliente_ID':'int32',\n    'Producto_ID':'int32',\n    'Canal_ID': 'int8',\n    'Demanda_uni_equil':'int16',\n    'NombreCliente':'category',\n    'Prom':'int8',\n    'price':'float16',\n    'wtpcs':'float16',\n    'product_med':'int16',\n    'client_med':'int16',\n    'popular_name_med':'int16',\n    'town_id_med':'int16',\n    'ruta_id_med':'int16',\n    'brand_med':'int16',\n    'property_med':'int16',\n    'agencia_med':'int16',\n    'cliente_type_med':'int16',\n    'top10': 'int8',\n    'sum of demand': 'int32',\n    'town_client': 'int32',\n}\ntrain_df = pd.read_csv('/kaggle/input/tsai-doc/train_df_Tsai.csv',dtype=dtype)\n#129.096s","metadata":{"execution":{"iopub.status.busy":"2023-05-23T13:59:04.542707Z","iopub.execute_input":"2023-05-23T13:59:04.543124Z","iopub.status.idle":"2023-05-23T14:01:11.310085Z","shell.execute_reply.started":"2023-05-23T13:59:04.543093Z","shell.execute_reply":"2023-05-23T14:01:11.308620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[(train_df['Semana']==9)]","metadata":{"execution":{"iopub.status.busy":"2023-05-23T12:44:04.442120Z","iopub.execute_input":"2023-05-23T12:44:04.442472Z","iopub.status.idle":"2023-05-23T12:44:05.303520Z","shell.execute_reply.started":"2023-05-23T12:44:04.442442Z","shell.execute_reply":"2023-05-23T12:44:05.302090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-05-17T12:56:40.190237Z","iopub.execute_input":"2023-05-17T12:56:40.190524Z","iopub.status.idle":"2023-05-17T12:56:40.209500Z","shell.execute_reply.started":"2023-05-17T12:56:40.190500Z","shell.execute_reply":"2023-05-17T12:56:40.208031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dtype = {\n    'Semana':'int8',\n    'Cliente_ID':'float32',\n    'Producto_ID':'float32',\n    'Lag1': 'float16',\n    'Lag2': 'float16',\n    'Lag3':'float16',\n    'Lag4':'float16',\n    'Lag5':'float16',\n    'cp_value':'float16'   \n\n}\ntest_lag_df = pd.read_csv(\"/kaggle/input/new-mod-lag/test_mod_lag(1).csv\",dtype=dtype)\n#6.634seconds","metadata":{"execution":{"iopub.status.busy":"2023-05-17T12:56:40.210980Z","iopub.execute_input":"2023-05-17T12:56:40.211610Z","iopub.status.idle":"2023-05-17T12:56:46.066294Z","shell.execute_reply.started":"2023-05-17T12:56:40.211575Z","shell.execute_reply":"2023-05-17T12:56:46.065293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_lag_df.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-05-17T12:56:46.067831Z","iopub.execute_input":"2023-05-17T12:56:46.068472Z","iopub.status.idle":"2023-05-17T12:56:46.083220Z","shell.execute_reply.started":"2023-05-17T12:56:46.068398Z","shell.execute_reply":"2023-05-17T12:56:46.082081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dtype = {\n    'Semana':'int8',\n    'Cliente_ID':'float32',\n    'Producto_ID':'float32',\n    'Lag1': 'float16',\n    'Lag2': 'float16',\n    'Lag3':'float16',\n    'Lag4':'float16',\n    'Lag5':'float16',\n    'cp_value':'float16'   \n\n}\ntrain_lag_df = pd.read_csv(\"/kaggle/input/new-mod-lag/train_mod_lag.csv\",dtype=dtype)\n#86.63seconds","metadata":{"execution":{"iopub.status.busy":"2023-05-23T14:01:11.312247Z","iopub.execute_input":"2023-05-23T14:01:11.312658Z","iopub.status.idle":"2023-05-23T14:02:10.956849Z","shell.execute_reply.started":"2023-05-23T14:01:11.312624Z","shell.execute_reply":"2023-05-23T14:02:10.954992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_lag_df.loc[(train_lag_df['Semana']==9)]","metadata":{"execution":{"iopub.status.busy":"2023-05-23T12:45:05.592621Z","iopub.execute_input":"2023-05-23T12:45:05.593183Z","iopub.status.idle":"2023-05-23T12:45:06.543485Z","shell.execute_reply.started":"2023-05-23T12:45:05.593128Z","shell.execute_reply":"2023-05-23T12:45:06.542620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_lag_df.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-05-17T12:57:19.549894Z","iopub.execute_input":"2023-05-17T12:57:19.550206Z","iopub.status.idle":"2023-05-17T12:57:19.565027Z","shell.execute_reply.started":"2023-05-17T12:57:19.550174Z","shell.execute_reply":"2023-05-17T12:57:19.563909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" test = pd.merge(test_df,test_lag_df,\n                    how='left',\n                    left_on=['Semana','Cliente_ID','Producto_ID'], \n                    right_on=['Semana','Cliente_ID','Producto_ID'],\n                    left_index=False, right_index=False, sort=True,\n                    suffixes=('_x', '_y'), copy=False, )\n#28seconds","metadata":{"execution":{"iopub.status.busy":"2023-05-17T12:57:19.569517Z","iopub.execute_input":"2023-05-17T12:57:19.569851Z","iopub.status.idle":"2023-05-17T12:57:43.224315Z","shell.execute_reply.started":"2023-05-17T12:57:19.569822Z","shell.execute_reply":"2023-05-17T12:57:43.222955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntest.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-05-17T12:57:43.225836Z","iopub.execute_input":"2023-05-17T12:57:43.226204Z","iopub.status.idle":"2023-05-17T12:57:43.248293Z","shell.execute_reply.started":"2023-05-17T12:57:43.226176Z","shell.execute_reply":"2023-05-17T12:57:43.246580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.merge(train_df,train_lag_df,\n                    how='left',\n                    left_on=['Semana','Cliente_ID','Producto_ID'], \n                    right_on=['Semana','Cliente_ID','Producto_ID'],\n                    left_index=False, right_index=False, sort=True,\n                    suffixes=('_x', '_y'), copy=False, )\ndel train_df\ndel train_lag_df","metadata":{"execution":{"iopub.status.busy":"2023-05-23T14:02:10.958981Z","iopub.execute_input":"2023-05-23T14:02:10.959428Z","iopub.status.idle":"2023-05-23T14:04:27.393105Z","shell.execute_reply.started":"2023-05-23T14:02:10.959386Z","shell.execute_reply":"2023-05-23T14:04:27.390755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.loc[(train['Semana']==9)]","metadata":{"execution":{"iopub.status.busy":"2023-05-23T12:48:08.138453Z","iopub.execute_input":"2023-05-23T12:48:08.138806Z","iopub.status.idle":"2023-05-23T12:48:09.999898Z","shell.execute_reply.started":"2023-05-23T12:48:08.138773Z","shell.execute_reply":"2023-05-23T12:48:09.998783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.reset_index(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-23T14:04:27.396647Z","iopub.execute_input":"2023-05-23T14:04:27.397053Z","iopub.status.idle":"2023-05-23T14:04:27.613145Z","shell.execute_reply.started":"2023-05-23T14:04:27.397013Z","shell.execute_reply":"2023-05-23T14:04:27.611457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train.to_csv('train_df.csv', index=False)\n# test.to_csv('test_df.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-23T13:29:10.190970Z","iopub.execute_input":"2023-05-23T13:29:10.191830Z","iopub.status.idle":"2023-05-23T13:29:27.997652Z","shell.execute_reply.started":"2023-05-23T13:29:10.191787Z","shell.execute_reply":"2023-05-23T13:29:27.994128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dtype = {\n    'Semana': 'int8',\n    'Agencia_ID': 'int16',\n    'Canal_ID': 'int8',\n    'Ruta_SAK': 'int16',\n    'Cliente_ID': 'int32',\n    'Producto_ID': 'int32',\n    'Venta_uni_hoy': 'int16',\n    'Venta_hoy': 'float32',\n    'Dev_uni_proxima': 'int32',\n    'Dev_proxima': 'float32',\n    'Demanda_uni_equil': 'int16',\n}\nuse_df = pd.read_csv(\"/kaggle/input/grupo-bimbo-inventory-demand/train.csv.zip\", dtype=dtype)\nother=use_df.loc[(use_df['Semana']==9)]\ndel use_df","metadata":{"execution":{"iopub.status.busy":"2023-05-23T16:56:55.623411Z","iopub.execute_input":"2023-05-23T16:56:55.623766Z","iopub.status.idle":"2023-05-23T16:58:31.245175Z","shell.execute_reply.started":"2023-05-23T16:56:55.623736Z","shell.execute_reply":"2023-05-23T16:58:31.243994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ind=train[['index','Semana','Cliente_ID','Producto_ID']].copy()\nind=ind.loc[(ind['Semana']==9)]\nind","metadata":{"execution":{"iopub.status.busy":"2023-05-23T14:04:27.615243Z","iopub.execute_input":"2023-05-23T14:04:27.615849Z","iopub.status.idle":"2023-05-23T14:04:29.873559Z","shell.execute_reply.started":"2023-05-23T14:04:27.615774Z","shell.execute_reply":"2023-05-23T14:04:29.872529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"use = pd.merge(other,ind,\n                    how='left',\n                    left_on=['Semana','Cliente_ID','Producto_ID'], \n                    right_on=['Semana','Cliente_ID','Producto_ID'],\n                    left_index=False, right_index=False, sort=True,\n                    suffixes=('_x', '_y'), copy=False, )","metadata":{"execution":{"iopub.status.busy":"2023-05-23T16:58:31.249309Z","iopub.execute_input":"2023-05-23T16:58:31.250268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"use=use['Semana']=9.1\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df=train.loc[(train['Semana']<9)]\ntest_df=train.loc[(train['Semana']==9)]\ndel train\n","metadata":{"execution":{"iopub.status.busy":"2023-05-23T14:04:29.875236Z","iopub.execute_input":"2023-05-23T14:04:29.875602Z","iopub.status.idle":"2023-05-23T14:04:37.852353Z","shell.execute_reply.started":"2023-05-23T14:04:29.875572Z","shell.execute_reply":"2023-05-23T14:04:37.851184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.drop(['index','Cliente_ID','Producto_ID','Semana','Prom','wtpcs','price','Town_ID','cp_value','town_client','town_id_med'],axis=1, inplace=True)\ntest_df.drop(['Cliente_ID','Producto_ID','Semana','Prom','wtpcs','price','Town_ID','cp_value','town_client','town_id_med','Demanda_uni_equil'],axis=1, inplace=True)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-23T14:04:37.853771Z","iopub.execute_input":"2023-05-23T14:04:37.854110Z","iopub.status.idle":"2023-05-23T14:04:39.903126Z","shell.execute_reply.started":"2023-05-23T14:04:37.854082Z","shell.execute_reply":"2023-05-23T14:04:39.901933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''We should use the log transform of the target value'''\nclass MyEstimator(BaseEstimator):\n    def __init__(self, model):\n        self.model = model\n        \n    def fit(self, X, y):\n        self.model.fit(X, np.log1p(y))\n        return self \n\n    def predict(self, X):\n        predicts = np.expm1(self.model.predict(X))\n        mask = (predicts <= 0)\n        predicts[mask] = 0\n        return predicts\n\n'''KFold for cross validation'''\nkf = KFold(n_splits=5, shuffle=True, random_state=0)\n\n'''Define the validation function'''\ndef rmsle_cv(model, X, y, cv=kf):\n    rmsle=np.sqrt(-cross_val_score(model,X, y,scoring=\"neg_mean_squared_log_error\",cv=cv))\n    return(rmsle)\n\n'''Define evaluation function for Convienience'''\ndef evaluation_model(model, train_X, train_y, test_X):\n    cv = rmsle_cv(model, train_X, train_y)\n    print(\"Folds of rmsle:\")\n    print(cv)\n    cv_mean = np.round(cv.mean(), 5)\n    cv_std = np.round(cv.std(), 5)\n    sample_prediction = model.predict(test_X.loc[:3, :])\n    return {'cv_mean': cv_mean, 'cv_std': cv_std, 'sample_prediction': sample_prediction}\n\n'''Define Hyperparameters Tuning Function'''\ndef tune_hyperparameters(model, param_grid, train_X, train_y):\n    grid = GridSearchCV(\n        model, param_grid, \n        scoring='neg_mean_squared_log_error',\n        cv=3, n_jobs=-1,\n    )\n    grid.fit(train_X, train_y)\n    best_params = grid.best_params_ \n    best_score = np.round(np.sqrt(-1 * grid.best_score_), 5)\n    return best_params, best_score","metadata":{"execution":{"iopub.status.busy":"2023-05-23T14:04:39.904781Z","iopub.execute_input":"2023-05-23T14:04:39.905149Z","iopub.status.idle":"2023-05-23T14:04:39.919282Z","shell.execute_reply.started":"2023-05-23T14:04:39.905114Z","shell.execute_reply":"2023-05-23T14:04:39.917591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"skewness = train_df.select_dtypes(include=['int8', 'int16', 'int32', 'int64', 'float32', 'float64']).apply(lambda x: skew(x))\nskew_index = skewness[abs(skewness) >= 0.75].index\nskewness[skew_index].sort_values(ascending=False)\n\nskewness = test_df.select_dtypes(include=['int8', 'int16', 'int32', 'int64', 'float32', 'float64']).apply(lambda x: skew(x))\nskew_index = skewness[abs(skewness) >= 0.75].index\nskewness[skew_index].sort_values(ascending=False)\n\n\nlam = 0.01\nfor column in skew_index:\n    train_df[column] = boxcox1p(train_df[column], lam)\n    test_df[column] = boxcox1p(test_df[column], lam)","metadata":{"execution":{"iopub.status.busy":"2023-05-23T14:04:39.921275Z","iopub.execute_input":"2023-05-23T14:04:39.921870Z","iopub.status.idle":"2023-05-23T14:05:13.656985Z","shell.execute_reply.started":"2023-05-23T14:04:39.921838Z","shell.execute_reply":"2023-05-23T14:05:13.655507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nrandom.seed(1000)\nsample_train_df = train_df.sample(9999999)\ndel train_df\nsample_train_df['log_target'] = np.log1p(sample_train_df['Demanda_uni_equil'])\n","metadata":{"execution":{"iopub.status.busy":"2023-05-23T14:05:13.660889Z","iopub.execute_input":"2023-05-23T14:05:13.661327Z","iopub.status.idle":"2023-05-23T14:05:25.537434Z","shell.execute_reply.started":"2023-05-23T14:05:13.661293Z","shell.execute_reply":"2023-05-23T14:05:25.535738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''Adjust dataframe for modeling'''\n# train_y = train_df['Demanda_uni_equil']\n# train_df.drop(['Demanda_uni_equil'], axis=1, inplace=True)\n# train_X = train_df\ntest_X = test_df.drop('index', axis=1)\n'''Transform categorical features to dummy variables'''\n# train_X = pd.get_dummies(train_X)\ntest_X = pd.get_dummies(test_X)\n# del test_df","metadata":{"execution":{"iopub.status.busy":"2023-05-23T14:05:25.539247Z","iopub.execute_input":"2023-05-23T14:05:25.539759Z","iopub.status.idle":"2023-05-23T14:05:31.017887Z","shell.execute_reply.started":"2023-05-23T14:05:25.539716Z","shell.execute_reply":"2023-05-23T14:05:31.016618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_train_df['Demanda_uni_equil'] = np.expm1(sample_train_df['log_target']).astype('int32')","metadata":{"execution":{"iopub.status.busy":"2023-05-23T14:05:31.019687Z","iopub.execute_input":"2023-05-23T14:05:31.020113Z","iopub.status.idle":"2023-05-23T14:05:31.243758Z","shell.execute_reply.started":"2023-05-23T14:05:31.020080Z","shell.execute_reply":"2023-05-23T14:05:31.242527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''Prepare sample train for the fast training'''\nsample_train_y = sample_train_df['Demanda_uni_equil']\nsample_train_df.drop(['Demanda_uni_equil', 'log_target'], axis=1, inplace=True)\nsample_train_X = sample_train_df\n\nsample_train_X = pd.get_dummies(sample_train_X)\ndel sample_train_df","metadata":{"execution":{"iopub.status.busy":"2023-05-23T14:05:31.245205Z","iopub.execute_input":"2023-05-23T14:05:31.245585Z","iopub.status.idle":"2023-05-23T14:05:34.005601Z","shell.execute_reply.started":"2023-05-23T14:05:31.245554Z","shell.execute_reply":"2023-05-23T14:05:34.003587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = LGBMRegressor(learning_rate=0.01, n_estimators=3000,\n                      num_leaves=5,\n                      objective='regression',\n                      max_bin=55, bagging_fraction=0.8,\n                      bagging_freq=5, feature_fraction=0.2319,\n                      feature_fraction_seed=9, bagging_seed=9,\n                      min_data_in_leaf=6, min_sum_hessian_in_leaf=11)\nlgb_model = MyEstimator(model)\nlgb_model.fit(sample_train_X, sample_train_y)\nlgb_eval = evaluation_model(lgb_model, sample_train_X, sample_train_y, test_X)\nprint(lgb_eval)","metadata":{"execution":{"iopub.status.busy":"2023-05-23T14:05:34.007812Z","iopub.execute_input":"2023-05-23T14:05:34.008418Z","iopub.status.idle":"2023-05-23T16:27:25.387142Z","shell.execute_reply.started":"2023-05-23T14:05:34.008361Z","shell.execute_reply":"2023-05-23T16:27:25.382592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def output_submission_file(model, test_X, filename='submission.csv'):\n    prediction = model.predict(test_X)\n    df = pd.DataFrame({'index': test_df['index'], 'Demanda_uni_equil': prediction})\n    print(f'{df.shape}')\n    print(f'{df.head(5)}')\n    df.to_csv(filename, index=False)\n    df.to_csv(filename + '.gz', index=False, compression='gzip')","metadata":{"execution":{"iopub.status.busy":"2023-05-23T16:40:37.738577Z","iopub.execute_input":"2023-05-23T16:40:37.739002Z","iopub.status.idle":"2023-05-23T16:40:37.748151Z","shell.execute_reply.started":"2023-05-23T16:40:37.738969Z","shell.execute_reply":"2023-05-23T16:40:37.746448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_submission_file(lgb_model, test_X)\ndel test_df","metadata":{"execution":{"iopub.status.busy":"2023-05-23T16:40:38.127799Z","iopub.execute_input":"2023-05-23T16:40:38.128507Z","iopub.status.idle":"2023-05-23T16:56:55.608156Z","shell.execute_reply.started":"2023-05-23T16:40:38.128470Z","shell.execute_reply":"2023-05-23T16:56:55.606787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del skewness\ndel skew_index\ndel sample_train_y\ndel sample_train_X\ndel test_X","metadata":{"execution":{"iopub.status.busy":"2023-05-23T16:56:55.611050Z","iopub.execute_input":"2023-05-23T16:56:55.611489Z","iopub.status.idle":"2023-05-23T16:56:55.621712Z","shell.execute_reply.started":"2023-05-23T16:56:55.611452Z","shell.execute_reply":"2023-05-23T16:56:55.620455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=pd.read_csv(\"/kaggle/working/submission.csv\")\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"other","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final = pd.merge(other,pred,\n                    how='left',\n                    left_on=['index'], \n                    right_on=['index'],\n                    left_index=False, right_index=False, sort=True,\n                    suffixes=('_x', '_y'), copy=False, )","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final.to_csv('final2.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}