{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":5260,"databundleVersionId":37827,"sourceType":"competition"}],"dockerImageVersionId":30497,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import gc\nimport lightgbm as lgb\nimport numpy as np \nimport pandas as pd \n#import re\nfrom scipy import stats\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.preprocessing import LabelEncoder\n\nle = LabelEncoder()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2026-01-12T09:12:50.982504Z","iopub.execute_input":"2026-01-12T09:12:50.982912Z","iopub.status.idle":"2026-01-12T09:12:53.381194Z","shell.execute_reply.started":"2026-01-12T09:12:50.982860Z","shell.execute_reply":"2026-01-12T09:12:53.380131Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#int8\t-128 ～ 127\n#int16\t-32,768 ～ 32,767\n#int32\t-2,147,483,648 ～ 2,147,483,647\n#int64\t-9,223,372,036,854,775,808 ～ 9,223,372,036,854,775,807","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:12:53.383427Z","iopub.execute_input":"2026-01-12T09:12:53.383853Z","iopub.status.idle":"2026-01-12T09:12:53.388367Z","shell.execute_reply.started":"2026-01-12T09:12:53.383815Z","shell.execute_reply":"2026-01-12T09:12:53.387300Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Data fields\n\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# 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) (This is the target you will predict)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:12:53.389797Z","iopub.execute_input":"2026-01-12T09:12:53.390204Z","iopub.status.idle":"2026-01-12T09:12:53.402924Z","shell.execute_reply.started":"2026-01-12T09:12:53.390169Z","shell.execute_reply":"2026-01-12T09:12:53.401855Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cliente = pd.read_csv('/kaggle/input/grupo-bimbo-inventory-demand/cliente_tabla.csv.zip')\ncliente['NombreCliente(First)'] = cliente['NombreCliente'].str.split(' ').str.get(0)\ncliente = cliente.drop_duplicates(subset='Cliente_ID')\ncliente['NombreCliente(First)'] = le.fit_transform(cliente['NombreCliente(First)'])\ncliente","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:12:53.405595Z","iopub.execute_input":"2026-01-12T09:12:53.406023Z","iopub.status.idle":"2026-01-12T09:12:57.205508Z","shell.execute_reply.started":"2026-01-12T09:12:53.405988Z","shell.execute_reply":"2026-01-12T09:12:57.204504Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cliente.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:12:57.207110Z","iopub.execute_input":"2026-01-12T09:12:57.207418Z","iopub.status.idle":"2026-01-12T09:12:57.275811Z","shell.execute_reply.started":"2026-01-12T09:12:57.207391Z","shell.execute_reply":"2026-01-12T09:12:57.274768Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cliente['NombreCliente(First)'] = cliente['NombreCliente(First)'].astype(np.int32)\ncliente = cliente.drop('NombreCliente', axis=1)\ncliente","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:12:57.277123Z","iopub.execute_input":"2026-01-12T09:12:57.277508Z","iopub.status.idle":"2026-01-12T09:12:57.299751Z","shell.execute_reply.started":"2026-01-12T09:12:57.277469Z","shell.execute_reply":"2026-01-12T09:12:57.298669Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"producto = pd.read_csv('/kaggle/input/grupo-bimbo-inventory-demand/producto_tabla.csv.zip')\nproducto['NombreProducto(First)'] = producto['NombreProducto'].str.split(' ').str.get(0)   \nproducto['NombreProducto(Weight)'] = producto['NombreProducto'].str.extract('([0-9]+g)')\nproducto['NombreProducto(First)'] = le.fit_transform(producto['NombreProducto(First)']) \nproducto['NombreProducto(Weight)'] = le.fit_transform(producto['NombreProducto(Weight)'])\nproducto","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:12:57.300992Z","iopub.execute_input":"2026-01-12T09:12:57.301357Z","iopub.status.idle":"2026-01-12T09:12:57.354517Z","shell.execute_reply.started":"2026-01-12T09:12:57.301320Z","shell.execute_reply":"2026-01-12T09:12:57.353486Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"producto.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:12:57.355890Z","iopub.execute_input":"2026-01-12T09:12:57.356189Z","iopub.status.idle":"2026-01-12T09:12:57.376550Z","shell.execute_reply.started":"2026-01-12T09:12:57.356164Z","shell.execute_reply":"2026-01-12T09:12:57.375520Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"producto['NombreProducto(First)'] = producto['NombreProducto(First)'].astype(np.int16)\nproducto['NombreProducto(Weight)'] = producto['NombreProducto(Weight)'].astype(np.int16)\nproducto = producto.drop('NombreProducto', axis=1)\nproducto","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:12:57.377771Z","iopub.execute_input":"2026-01-12T09:12:57.378068Z","iopub.status.idle":"2026-01-12T09:12:57.392073Z","shell.execute_reply.started":"2026-01-12T09:12:57.378044Z","shell.execute_reply":"2026-01-12T09:12:57.390952Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"town_state = pd.read_csv('/kaggle/input/grupo-bimbo-inventory-demand/town_state.csv.zip')\n\ndef glue(lst):\n  txt = ''\n\n  for i, s in enumerate(lst):\n   if i == 0:\n     txt += s\n       \n   else:\n     txt += ' ' + s\n       \n  return txt\n\ntown_state['Town'] = town_state['Town'].fillna('').str.split(' ').str[1:].apply(glue)\n\ntown_state['Town'] = le.fit_transform(town_state['Town']) \ntown_state['State'] = le.fit_transform(town_state['State'])\ntown_state","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:12:57.396924Z","iopub.execute_input":"2026-01-12T09:12:57.397478Z","iopub.status.idle":"2026-01-12T09:12:57.426067Z","shell.execute_reply.started":"2026-01-12T09:12:57.397446Z","shell.execute_reply":"2026-01-12T09:12:57.424962Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"town_state.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:12:57.427600Z","iopub.execute_input":"2026-01-12T09:12:57.428040Z","iopub.status.idle":"2026-01-12T09:12:57.447760Z","shell.execute_reply.started":"2026-01-12T09:12:57.428002Z","shell.execute_reply":"2026-01-12T09:12:57.446789Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"town_state['Town'] = town_state['Town'].astype(np.int16)\ntown_state['State'] = town_state['State'].astype(np.int8)\ntown_state","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:12:57.449344Z","iopub.execute_input":"2026-01-12T09:12:57.449735Z","iopub.status.idle":"2026-01-12T09:12:57.463696Z","shell.execute_reply.started":"2026-01-12T09:12:57.449700Z","shell.execute_reply":"2026-01-12T09:12:57.462648Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_reader = pd.read_csv('/kaggle/input/grupo-bimbo-inventory-demand/train.csv.zip', chunksize=10000)\ntrain = pd.concat((r for r in train_reader), ignore_index=True)\ntrain","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:12:57.464881Z","iopub.execute_input":"2026-01-12T09:12:57.465264Z","iopub.status.idle":"2026-01-12T09:14:27.051806Z","shell.execute_reply.started":"2026-01-12T09:12:57.465236Z","shell.execute_reply":"2026-01-12T09:14:27.050582Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:14:27.052991Z","iopub.execute_input":"2026-01-12T09:14:27.053256Z","iopub.status.idle":"2026-01-12T09:14:56.580949Z","shell.execute_reply.started":"2026-01-12T09:14:27.053234Z","shell.execute_reply":"2026-01-12T09:14:56.579989Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Semana'] = train['Semana'].astype(np.int8)\ntrain['Agencia_ID'] = train['Agencia_ID'].astype(np.int16)\ntrain['Canal_ID'] = train['Canal_ID'].astype(np.int8)\ntrain['Ruta_SAK'] = train['Ruta_SAK'].astype(np.int16)\ntrain['Cliente_ID'] = train['Cliente_ID'].astype(np.int32)\ntrain['Producto_ID'] = train['Producto_ID'].astype(np.int32)\n#train['Venta_uni_hoy'] = train['Venta_uni_hoy'].astype(np.int16)\n#train['Venta_hoy'] = train['Venta_hoy'].astype(np.float16)\n#train['Dev_uni_proxima'] = train['Dev_uni_proxima'].astype(np.int16)\n#train['Dev_proxima'] = train['Dev_proxima'].astype(np.float16)\n#train['Demanda_uni_equil'] = train['Demanda_uni_equil'].astype(np.int16)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:14:56.582254Z","iopub.execute_input":"2026-01-12T09:14:56.582653Z","iopub.status.idle":"2026-01-12T09:15:01.738371Z","shell.execute_reply.started":"2026-01-12T09:14:56.582599Z","shell.execute_reply":"2026-01-12T09:15:01.737324Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Precio(C-P)'] = train['Venta_hoy'] / train['Venta_uni_hoy']\nprecio_cp = train[['Cliente_ID', 'Producto_ID', 'Venta_uni_hoy', 'Venta_hoy', 'Precio(C-P)']].copy()\nprecio_cp = precio_cp.drop(columns=['Venta_uni_hoy', 'Venta_hoy'], axis=1)\nprecio_cp = precio_cp.groupby(['Cliente_ID', 'Producto_ID']).mean().reset_index()\nprecio_cp","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:15:01.739585Z","iopub.execute_input":"2026-01-12T09:15:01.739894Z","iopub.status.idle":"2026-01-12T09:15:30.511423Z","shell.execute_reply.started":"2026-01-12T09:15:01.739871Z","shell.execute_reply":"2026-01-12T09:15:30.510269Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target = train['Demanda_uni_equil']\ntrain = train.drop(columns=['Venta_uni_hoy', 'Venta_hoy', 'Dev_uni_proxima', 'Dev_proxima', 'Demanda_uni_equil'], axis=1)\n#train = pd.merge(train, cliente, on='Cliente_ID', how='left').drop('Cliente_ID', axis=1)\ntrain","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:15:30.512746Z","iopub.execute_input":"2026-01-12T09:15:30.513128Z","iopub.status.idle":"2026-01-12T09:15:31.023824Z","shell.execute_reply.started":"2026-01-12T09:15:30.513092Z","shell.execute_reply":"2026-01-12T09:15:31.022701Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#1876\nprecio_p = precio_cp.drop('Cliente_ID', axis=1).groupby('Producto_ID').apply(lambda x: x.mode()).drop(['Producto_ID'], axis=1).reset_index().drop(['level_1'], axis=1)\nprecio_p = precio_p.rename(columns = {'Precio(C-P)': 'Precio(P)'})\nprecio_p = precio_p.drop_duplicates(subset=['Producto_ID'])\nprecio_p","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:17:40.921161Z","iopub.execute_input":"2026-01-12T09:17:40.921540Z","iopub.status.idle":"2026-01-12T09:17:46.440079Z","shell.execute_reply.started":"2026-01-12T09:17:40.921510Z","shell.execute_reply":"2026-01-12T09:17:46.439098Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"precio_c = precio_cp.drop('Producto_ID', axis=1).groupby('Cliente_ID')['Precio(C-P)'].median().to_frame().reset_index()\nprecio_c['Precio(C-P)'] = precio_c['Precio(C-P)'].round(2)\nprecio_c = precio_c.rename(columns = {'Precio(C-P)': 'Precio(C)'})\nprecio_c","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:18:10.002704Z","iopub.execute_input":"2026-01-12T09:18:10.003071Z","iopub.status.idle":"2026-01-12T09:18:11.297222Z","shell.execute_reply.started":"2026-01-12T09:18:10.003041Z","shell.execute_reply":"2026-01-12T09:18:11.295942Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/grupo-bimbo-inventory-demand/test.csv.zip')\ntest = test.drop(columns=['id'], axis=1)\ntest = pd.merge(test, precio_cp, on=['Cliente_ID', 'Producto_ID'], how='left')\ntest = pd.merge(test, precio_c, on=['Cliente_ID'], how='left')\ntest = pd.merge(test, precio_p, on=['Producto_ID'], how='left')\ntest = test.bfill(axis=1)\ntest = test.drop(['Precio(C)', 'Precio(P)'], axis=1)\ntest","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:18:11.298958Z","iopub.execute_input":"2026-01-12T09:18:11.299268Z","iopub.status.idle":"2026-01-12T09:18:35.574607Z","shell.execute_reply.started":"2026-01-12T09:18:11.299242Z","shell.execute_reply":"2026-01-12T09:18:35.573536Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Semana'] = test['Semana'].astype(np.int8)\ntest['Agencia_ID'] = test['Agencia_ID'].astype(np.int16)\ntest['Canal_ID'] = test['Canal_ID'].astype(np.int8)\ntest['Ruta_SAK'] = test['Ruta_SAK'].astype(np.int16)\ntest['Cliente_ID'] = test['Cliente_ID'].astype(np.int32)\ntest['Producto_ID'] = test['Producto_ID'].astype(np.int32)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:18:35.575750Z","iopub.execute_input":"2026-01-12T09:18:35.576052Z","iopub.status.idle":"2026-01-12T09:18:36.247064Z","shell.execute_reply.started":"2026-01-12T09:18:35.576027Z","shell.execute_reply":"2026-01-12T09:18:36.246015Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_test = pd.concat([train, test])\ntrain_test","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:18:36.249366Z","iopub.execute_input":"2026-01-12T09:18:36.249690Z","iopub.status.idle":"2026-01-12T09:18:37.390579Z","shell.execute_reply.started":"2026-01-12T09:18:36.249663Z","shell.execute_reply":"2026-01-12T09:18:37.389563Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_test = pd.merge(train_test, cliente, on='Cliente_ID', how='left').drop('Cliente_ID', axis=1)\ntrain_test = pd.merge(train_test, producto, on='Producto_ID', how='left').drop('Producto_ID', axis=1)\ntrain_test = pd.merge(train_test, town_state, on='Agencia_ID', how='left').drop('Agencia_ID', axis=1)\ntrain_test['Precio(C-P)'] = train_test['Precio(C-P)'].astype(np.float32)\ntrain_test","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:18:37.391774Z","iopub.execute_input":"2026-01-12T09:18:37.392083Z","iopub.status.idle":"2026-01-12T09:19:22.612585Z","shell.execute_reply.started":"2026-01-12T09:18:37.392056Z","shell.execute_reply":"2026-01-12T09:19:22.611389Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_test.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:19:22.613998Z","iopub.execute_input":"2026-01-12T09:19:22.614311Z","iopub.status.idle":"2026-01-12T09:19:40.972851Z","shell.execute_reply.started":"2026-01-12T09:19:22.614284Z","shell.execute_reply":"2026-01-12T09:19:40.971803Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"del train\ndel test\ndel precio_cp\ndel precio_c\ndel precio_p \ndel cliente\ndel producto\ndel town_state\n\ngc.collect()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:19:40.974252Z","iopub.execute_input":"2026-01-12T09:19:40.974663Z","iopub.status.idle":"2026-01-12T09:19:41.124922Z","shell.execute_reply.started":"2026-01-12T09:19:40.974607Z","shell.execute_reply":"2026-01-12T09:19:41.123839Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = train_test[:74180464]\ntest = train_test[74180464:]\ntrain","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:19:41.126441Z","iopub.execute_input":"2026-01-12T09:19:41.126763Z","iopub.status.idle":"2026-01-12T09:19:41.142159Z","shell.execute_reply.started":"2026-01-12T09:19:41.126737Z","shell.execute_reply":"2026-01-12T09:19:41.141130Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = test.reset_index().drop('index', axis=1)\ntest","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:19:41.143534Z","iopub.execute_input":"2026-01-12T09:19:41.143940Z","iopub.status.idle":"2026-01-12T09:19:41.311386Z","shell.execute_reply.started":"2026-01-12T09:19:41.143903Z","shell.execute_reply":"2026-01-12T09:19:41.310232Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(train, target, test_size=0.3) \n\nmodel = lgb.LGBMRegressor(objective='regression', metric='rmse') \nmodel.fit(x_train, y_train)\n\npred = model.predict(x_test)\nrmse = mean_squared_error(y_test, pred, squared=False) \nprint(f\"Root Mean Squared Error (RMSE) on Test Set: {rmse}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:19:41.314988Z","iopub.execute_input":"2026-01-12T09:19:41.315283Z","iopub.status.idle":"2026-01-12T09:22:51.370870Z","shell.execute_reply.started":"2026-01-12T09:19:41.315259Z","shell.execute_reply":"2026-01-12T09:22:51.369851Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/grupo-bimbo-inventory-demand/sample_submission.csv.zip')\nsubmission","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:22:51.372734Z","iopub.execute_input":"2026-01-12T09:22:51.373029Z","iopub.status.idle":"2026-01-12T09:22:52.404099Z","shell.execute_reply.started":"2026-01-12T09:22:51.373005Z","shell.execute_reply":"2026-01-12T09:22:52.402999Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission['Demanda_uni_equil'] = model.predict(test)\nsubmission['Demanda_uni_equil'] = submission['Demanda_uni_equil'].astype(np.int8)\nsubmission","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:22:52.405379Z","iopub.execute_input":"2026-01-12T09:22:52.405676Z","iopub.status.idle":"2026-01-12T09:23:03.868226Z","shell.execute_reply.started":"2026-01-12T09:22:52.405652Z","shell.execute_reply":"2026-01-12T09:23:03.866987Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T09:23:03.869506Z","iopub.execute_input":"2026-01-12T09:23:03.869822Z","iopub.status.idle":"2026-01-12T09:23:12.001301Z","shell.execute_reply.started":"2026-01-12T09:23:03.869795Z","shell.execute_reply":"2026-01-12T09:23:12.000160Z"}},"outputs":[],"execution_count":null}]}