{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Copyright (c) Recommenders contributors.\n", "\n", "Licensed under the MIT License." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# LightGBM: A Highly Efficient Gradient Boosting Decision Tree\n", "This notebook will give you an example of how to train a LightGBM model to estimate click-through rates on an e-commerce advertisement. We will train a LightGBM based model on the Criteo dataset.\n", "\n", "[LightGBM](https://github.com/Microsoft/LightGBM) is a gradient boosting framework that uses tree-based learning algorithms. It is designed to be distributed and efficient with the following advantages:\n", "* Fast training speed and high efficiency.\n", "* Low memory usage.\n", "* Great accuracy.\n", "* Support of parallel and GPU learning.\n", "* Capable of handling large-scale data." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Global Settings and Imports" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "System version: 3.11.9 (main, Apr 19 2024, 16:48:06) [GCC 11.2.0]\n", "LightGBM version: 4.4.0\n" ] } ], "source": [ "import os\n", "import sys\n", "import numpy as np\n", "import lightgbm as lgb\n", "import category_encoders as ce\n", "from tempfile import TemporaryDirectory\n", "from sklearn.metrics import roc_auc_score, log_loss\n", "\n", "import recommenders.datasets.criteo as criteo\n", "import recommenders.models.lightgbm.lightgbm_utils as lgb_utils\n", "from recommenders.utils.notebook_utils import store_metadata\n", "\n", "print(\"System version: {}\".format(sys.version))\n", "print(\"LightGBM version: {}\".format(lgb.__version__))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Parameter Setting\n", "Let's set the main related parameters for LightGBM now. Basically, the task is a binary classification (predicting click or no click), so the objective function is set to binary logloss, and 'AUC' metric, is used as a metric which is less effected by imbalance in the classes of the dataset.\n", "\n", "Generally, we can adjust the number of leaves (MAX_LEAF), the minimum number of data in each leaf (MIN_DATA), maximum number of trees (NUM_OF_TREES), the learning rate of trees (TREE_LEARNING_RATE) and EARLY_STOPPING_ROUNDS (to avoid overfitting) in the model to get better performance.\n", "\n", "Besides, we can also adjust some other listed parameters to optimize the results. [In this link](https://github.com/Microsoft/LightGBM/blob/master/docs/Parameters.rst), a list of all the parameters is shown. Also, some advice on how to tune these parameters can be found [in this url](https://github.com/Microsoft/LightGBM/blob/master/docs/Parameters-Tuning.rst). " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [ "parameters" ] }, "outputs": [], "source": [ "MAX_LEAF = 64\n", "MIN_DATA = 20\n", "NUM_OF_TREES = 100\n", "TREE_LEARNING_RATE = 0.15\n", "EARLY_STOPPING_ROUNDS = 20\n", "METRIC = \"auc\"\n", "SIZE = \"sample\"" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "params = {\n", " \"task\": \"train\",\n", " \"boosting_type\": \"gbdt\",\n", " \"num_class\": 1,\n", " \"objective\": \"binary\",\n", " \"metric\": METRIC,\n", " \"num_leaves\": MAX_LEAF,\n", " \"min_data\": MIN_DATA,\n", " \"boost_from_average\": True,\n", " # set it according to your cpu cores.\n", " \"num_threads\": 20,\n", " \"feature_fraction\": 0.8,\n", " \"learning_rate\": TREE_LEARNING_RATE,\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data Preparation\n", "Here we use CSV format as the example data input. Our example data is a sample (about 100 thousand samples) from [Criteo dataset](https://www.kaggle.com/c/criteo-display-ad-challenge). The Criteo dataset is a well-known industry benchmarking dataset for developing CTR prediction models, and it's frequently adopted as evaluation dataset by research papers. The original dataset is too large for a lightweight demo, so we sample a small portion from it as a demo dataset.\n", "\n", "Specifically, there are 39 columns of features in Criteo, where 13 columns are numerical features (I1-I13) and the other 26 columns are categorical features (C1-C26)." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████| 8.58k/8.58k [00:00<00:00, 10.4kKB/s]\n" ] }, { "data": { "text/html": [ "
| \n", " | Label | \n", "I1 | \n", "I2 | \n", "I3 | \n", "I4 | \n", "I5 | \n", "I6 | \n", "I7 | \n", "I8 | \n", "I9 | \n", "... | \n", "C17 | \n", "C18 | \n", "C19 | \n", "C20 | \n", "C21 | \n", "C22 | \n", "C23 | \n", "C24 | \n", "C25 | \n", "C26 | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "0 | \n", "1.0 | \n", "1 | \n", "5.0 | \n", "0.0 | \n", "1382.0 | \n", "4.0 | \n", "15.0 | \n", "2.0 | \n", "181.0 | \n", "... | \n", "e5ba7672 | \n", "f54016b9 | \n", "21ddcdc9 | \n", "b1252a9d | \n", "07b5194c | \n", "NaN | \n", "3a171ecb | \n", "c5c50484 | \n", "e8b83407 | \n", "9727dd16 | \n", "
| 1 | \n", "0 | \n", "2.0 | \n", "0 | \n", "44.0 | \n", "1.0 | \n", "102.0 | \n", "8.0 | \n", "2.0 | \n", "2.0 | \n", "4.0 | \n", "... | \n", "07c540c4 | \n", "b04e4670 | \n", "21ddcdc9 | \n", "5840adea | \n", "60f6221e | \n", "NaN | \n", "3a171ecb | \n", "43f13e8b | \n", "e8b83407 | \n", "731c3655 | \n", "
| 2 | \n", "0 | \n", "2.0 | \n", "0 | \n", "1.0 | \n", "14.0 | \n", "767.0 | \n", "89.0 | \n", "4.0 | \n", "2.0 | \n", "245.0 | \n", "... | \n", "8efede7f | \n", "3412118d | \n", "NaN | \n", "NaN | \n", "e587c466 | \n", "ad3062eb | \n", "3a171ecb | \n", "3b183c5c | \n", "NaN | \n", "NaN | \n", "
| 3 | \n", "0 | \n", "NaN | \n", "893 | \n", "NaN | \n", "NaN | \n", "4392.0 | \n", "NaN | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "... | \n", "1e88c74f | \n", "74ef3502 | \n", "NaN | \n", "NaN | \n", "6b3a5ca6 | \n", "NaN | \n", "3a171ecb | \n", "9117a34a | \n", "NaN | \n", "NaN | \n", "
| 4 | \n", "0 | \n", "3.0 | \n", "-1 | \n", "NaN | \n", "0.0 | \n", "2.0 | \n", "0.0 | \n", "3.0 | \n", "0.0 | \n", "0.0 | \n", "... | \n", "1e88c74f | \n", "26b3c7a7 | \n", "NaN | \n", "NaN | \n", "21c9516a | \n", "NaN | \n", "32c7478e | \n", "b34f3128 | \n", "NaN | \n", "NaN | \n", "
5 rows × 40 columns
\n", "| \n", " | I1 | \n", "I2 | \n", "I3 | \n", "I4 | \n", "I5 | \n", "I6 | \n", "I7 | \n", "I8 | \n", "I9 | \n", "I10 | \n", "... | \n", "C17 | \n", "C18 | \n", "C19 | \n", "C20 | \n", "C21 | \n", "C22 | \n", "C23 | \n", "C24 | \n", "C25 | \n", "C26 | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "1.0 | \n", "1 | \n", "5.0 | \n", "0.0 | \n", "1382.0 | \n", "4.0 | \n", "15.0 | \n", "2.0 | \n", "181.0 | \n", "1.0 | \n", "... | \n", "1 | \n", "1 | \n", "1 | \n", "1 | \n", "1 | \n", "11 | \n", "1 | \n", "1 | \n", "1 | \n", "1 | \n", "
| 1 | \n", "2.0 | \n", "0 | \n", "44.0 | \n", "1.0 | \n", "102.0 | \n", "8.0 | \n", "2.0 | \n", "2.0 | \n", "4.0 | \n", "1.0 | \n", "... | \n", "2 | \n", "2 | \n", "1 | \n", "2 | \n", "2 | \n", "11 | \n", "1 | \n", "2 | \n", "1 | \n", "2 | \n", "
| 2 | \n", "2.0 | \n", "0 | \n", "1.0 | \n", "14.0 | \n", "767.0 | \n", "89.0 | \n", "4.0 | \n", "2.0 | \n", "245.0 | \n", "1.0 | \n", "... | \n", "3 | \n", "3 | \n", "1222 | \n", "4 | \n", "3 | \n", "1 | \n", "1 | \n", "3 | \n", "49 | \n", "8199 | \n", "
| 3 | \n", "NaN | \n", "893 | \n", "NaN | \n", "NaN | \n", "4392.0 | \n", "NaN | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "NaN | \n", "... | \n", "4 | \n", "4 | \n", "1222 | \n", "4 | \n", "4 | \n", "11 | \n", "1 | \n", "4 | \n", "49 | \n", "8199 | \n", "
| 4 | \n", "3.0 | \n", "-1 | \n", "NaN | \n", "0.0 | \n", "2.0 | \n", "0.0 | \n", "3.0 | \n", "0.0 | \n", "0.0 | \n", "1.0 | \n", "... | \n", "4 | \n", "5 | \n", "1222 | \n", "4 | \n", "5 | \n", "11 | \n", "2 | \n", "5 | \n", "49 | \n", "8199 | \n", "
5 rows × 39 columns
\n", "