{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":84493,"databundleVersionId":9871156,"sourceType":"competition"},{"sourceId":203900450,"sourceType":"kernelVersion"},{"sourceId":209102443,"sourceType":"kernelVersion"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Models training","metadata":{}},{"cell_type":"markdown","source":"In this notebook I'll show how to train a simple lgbm model.\nIf you want you have to possibility to train several lgbm model, here I'm just using one combination of params for trainings.","metadata":{}},{"cell_type":"code","source":"# Imports\nimport polars as pl\nimport lightgbm as lgb\nimport itertools\nimport glob\nimport numpy as np\nimport os\nimport copy\nfrom pathlib import Path\nfrom sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T22:30:18.628334Z","iopub.execute_input":"2024-12-07T22:30:18.629082Z","iopub.status.idle":"2024-12-07T22:30:18.633712Z","shell.execute_reply.started":"2024-12-07T22:30:18.629048Z","shell.execute_reply":"2024-12-07T22:30:18.632732Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# define columns to read\nfeature_cols = [f'feature_{x:02}' for x in range(79)]\nresponder_cols = [f'responder_{i}' for i in range(9)]\nresponder_lags = [f'responder_{i}_lag_1' for i in range(9)]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T22:30:19.116349Z","iopub.execute_input":"2024-12-07T22:30:19.116999Z","iopub.status.idle":"2024-12-07T22:30:19.121205Z","shell.execute_reply.started":"2024-12-07T22:30:19.116967Z","shell.execute_reply":"2024-12-07T22:30:19.120283Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# define base dir\nDATA_DIR = Path('/kaggle/input/')\nN_PARTITION = 10","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T22:30:19.995178Z","iopub.execute_input":"2024-12-07T22:30:19.995837Z","iopub.status.idle":"2024-12-07T22:30:19.999706Z","shell.execute_reply.started":"2024-12-07T22:30:19.995803Z","shell.execute_reply":"2024-12-07T22:30:19.998695Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class CONFIG:\n    seed = 42\n    target_col = \"responder_6\"\n    feature_cols = [\"symbol_id\", \"time_id\"] + [f\"feature_{idx:02d}\" for idx in range(79)]+ [f\"responder_{idx}_lag_1\" for idx in range(9)]\n    all_cols =  [\"date_id\",\"time_id\", \"symbol_id\", \"weight\"] + [f\"feature_{idx:02d}\" for idx in range(79)]+ [f\"responder_{idx}_lag_1\" for idx in range(9)] + [target_col]\n\n    data_paths = [\n        \"/kaggle/input/js24-preprocessing-create-lags/training.parquet/\",\n    ]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T22:30:28.581590Z","iopub.execute_input":"2024-12-07T22:30:28.581938Z","iopub.status.idle":"2024-12-07T22:30:28.587562Z","shell.execute_reply.started":"2024-12-07T22:30:28.581907Z","shell.execute_reply":"2024-12-07T22:30:28.586575Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model training","metadata":{}},{"cell_type":"code","source":"pl_train = pl.concat([pl.read_parquet(_f, columns=CONFIG.all_cols) for _f in glob.glob(os.path.join(CONFIG.data_paths[0], \"*/*parquet\"))])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T22:30:30.909064Z","iopub.execute_input":"2024-12-07T22:30:30.909432Z","iopub.status.idle":"2024-12-07T22:30:58.333755Z","shell.execute_reply.started":"2024-12-07T22:30:30.909402Z","shell.execute_reply":"2024-12-07T22:30:58.332783Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pl_train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T22:30:58.335444Z","iopub.execute_input":"2024-12-07T22:30:58.335820Z","iopub.status.idle":"2024-12-07T22:30:58.354949Z","shell.execute_reply.started":"2024-12-07T22:30:58.335783Z","shell.execute_reply":"2024-12-07T22:30:58.354096Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pl_train = pl_train.sort(\"date_id\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T22:30:58.355949Z","iopub.execute_input":"2024-12-07T22:30:58.356271Z","iopub.status.idle":"2024-12-07T22:31:09.057555Z","shell.execute_reply.started":"2024-12-07T22:30:58.356246Z","shell.execute_reply":"2024-12-07T22:31:09.056862Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pl_train = pl_train.sort(\"time_id\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T22:31:09.059192Z","iopub.execute_input":"2024-12-07T22:31:09.059468Z","iopub.status.idle":"2024-12-07T22:31:14.563094Z","shell.execute_reply.started":"2024-12-07T22:31:09.059442Z","shell.execute_reply":"2024-12-07T22:31:14.562074Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = pl_train.select(CONFIG.feature_cols).to_numpy()\ny = pl_train.select(CONFIG.target_col).to_numpy().flatten()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T22:31:14.565355Z","iopub.execute_input":"2024-12-07T22:31:14.565763Z","iopub.status.idle":"2024-12-07T22:31:18.815401Z","shell.execute_reply.started":"2024-12-07T22:31:14.565718Z","shell.execute_reply":"2024-12-07T22:31:18.814721Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"weights = pl_train.select([\"weight\"]).to_numpy().flatten()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T22:31:18.816485Z","iopub.execute_input":"2024-12-07T22:31:18.816822Z","iopub.status.idle":"2024-12-07T22:31:18.912207Z","shell.execute_reply.started":"2024-12-07T22:31:18.816789Z","shell.execute_reply":"2024-12-07T22:31:18.911549Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\nweights_train, weights_test = train_test_split(weights, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T22:31:18.913107Z","iopub.execute_input":"2024-12-07T22:31:18.913343Z","iopub.status.idle":"2024-12-07T22:32:41.760169Z","shell.execute_reply.started":"2024-12-07T22:31:18.913320Z","shell.execute_reply":"2024-12-07T22:32:41.759172Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create a LightGBM Dataset\ntrain_data = lgb.Dataset(X_train, label=y_train)\ntest_data = lgb.Dataset(X_test, label=y_test, reference=train_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T22:32:41.762124Z","iopub.execute_input":"2024-12-07T22:32:41.762416Z","iopub.status.idle":"2024-12-07T22:32:41.766544Z","shell.execute_reply.started":"2024-12-07T22:32:41.762390Z","shell.execute_reply":"2024-12-07T22:32:41.765720Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train many lightgbm models and save them\nnum_leaves = [31, 63, 127]\nfeature_fraction = [0.6, 0.8]\nn_estimators = [50, 100]\nlearning_rate = [0.05, 0.1]\n\n# Generate all combinations\nparam_combinations = list(itertools.product(num_leaves, feature_fraction, n_estimators, learning_rate))\n\n# Convert to a list of dictionaries (optional, for LightGBM compatibility)\nparam_dicts = [\n    {\"num_leaves\": nl, \"feature_fraction\": ff, \"n_estimators\": ne, \"learning_rate\": lr}\n    for nl, ff, ne, lr in param_combinations\n]\n\n# Print the combinations\nfor params in param_dicts:\n    print(params)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T22:32:41.767589Z","iopub.execute_input":"2024-12-07T22:32:41.767806Z","iopub.status.idle":"2024-12-07T22:32:41.780778Z","shell.execute_reply.started":"2024-12-07T22:32:41.767784Z","shell.execute_reply":"2024-12-07T22:32:41.779904Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"f\"Nb of combinations: {len(param_dicts)}\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T22:32:41.781758Z","iopub.execute_input":"2024-12-07T22:32:41.781992Z","iopub.status.idle":"2024-12-07T22:32:41.793357Z","shell.execute_reply.started":"2024-12-07T22:32:41.781969Z","shell.execute_reply":"2024-12-07T22:32:41.792582Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Use just one set of param for this notebook\nparam_dicts = [{\"num_leaves\": 31, \"feature_fraction\": 0.8, \"n_estimators\": 100, \"learning_rate\": 0.1}]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T22:33:27.429518Z","iopub.execute_input":"2024-12-07T22:33:27.430109Z","iopub.status.idle":"2024-12-07T22:33:27.434153Z","shell.execute_reply.started":"2024-12-07T22:33:27.430076Z","shell.execute_reply":"2024-12-07T22:33:27.433209Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(len(param_dicts)):\n    # model params\n    input_params = param_dicts[i]\n    \n    # Define Parameters\n    params = {\n        'objective': 'regression',\n        'metric': 'rmse',                                      # Root Mean Squared Error\n        'boosting_type': 'gbdt',                               # Gradient Boosted Decision Trees\n        'num_leaves': input_params['num_leaves'],\n        'learning_rate': input_params['learning_rate'],\n        'feature_fraction': input_params['feature_fraction'],\n        'n_estimators': input_params['n_estimators']      \n    }\n    \n    # Train the model\n    lgbm_model = lgb.train(\n        params,\n        train_data,\n        valid_sets=[train_data, test_data],\n        num_boost_round=50\n    )\n\n    lgbm_model.save_model(f\"lgbm_model_{i}.json\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T22:33:30.328830Z","iopub.execute_input":"2024-12-07T22:33:30.329537Z","iopub.status.idle":"2024-12-07T22:37:39.479003Z","shell.execute_reply.started":"2024-12-07T22:33:30.329472Z","shell.execute_reply":"2024-12-07T22:37:39.478149Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.listdir()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T22:37:39.480360Z","iopub.execute_input":"2024-12-07T22:37:39.481112Z","iopub.status.idle":"2024-12-07T22:37:39.488211Z","shell.execute_reply.started":"2024-12-07T22:37:39.481073Z","shell.execute_reply":"2024-12-07T22:37:39.487548Z"}},"outputs":[],"execution_count":null}]}