logistic_regression.py 3.53 KB
Newer Older
1
# coding: utf-8
2
3
"""Comparison of `binary` and `xentropy` objectives.

4
5
6
7
8
9
10
11
BLUF: The `xentropy` objective does logistic regression and generalizes
to the case where labels are probabilistic (i.e. numbers between 0 and 1).

Details: Both `binary` and `xentropy` minimize the log loss and use
`boost_from_average = TRUE` by default. Possibly the only difference
between them with default settings is that `binary` may achieve a slight
speed improvement by assuming that the labels are binary instead of
probabilistic.
12
"""
13
14
15
16
17
18
19

import time

import numpy as np
import pandas as pd
from scipy.special import expit

20
21
import lightgbm as lgb

22
23
24
#################
# Simulate some binary data with a single categorical and
#   single continuous predictor
25
rng = np.random.default_rng(seed=0)
26
N = 1000
27
X = pd.DataFrame({"continuous": range(N), "categorical": np.repeat([0, 1, 2, 3, 4], N / 5)})
28
CATEGORICAL_EFFECTS = [-1, -1, -2, -2, 2]
29
30
LINEAR_TERM = np.array(
    [-0.5 + 0.01 * X["continuous"][k] + CATEGORICAL_EFFECTS[X["categorical"][k]] for k in range(X.shape[0])]
31
) + rng.normal(loc=0, scale=1, size=X.shape[0])
32
TRUE_PROB = expit(LINEAR_TERM)
33
Y = rng.binomial(n=1, p=TRUE_PROB, size=N)
34
DATA = {
35
36
37
38
39
    "X": X,
    "probability_labels": TRUE_PROB,
    "binary_labels": Y,
    "lgb_with_binary_labels": lgb.Dataset(X, Y),
    "lgb_with_probability_labels": lgb.Dataset(X, TRUE_PROB),
40
41
42
43
44
45
}


#################
# Set up a couple of utilities for our experiments
def log_loss(preds, labels):
46
    """Logarithmic loss with non-necessarily-binary labels."""
47
48
49
50
51
    log_likelihood = np.sum(labels * np.log(preds)) / len(preds)
    return -log_likelihood


def experiment(objective, label_type, data):
52
53
54
55
    """Measure performance of an objective.

    Parameters
    ----------
56
    objective : {'binary', 'xentropy'}
57
        Objective function.
58
    label_type : {'binary', 'probability'}
59
60
61
62
63
64
65
66
67
        Type of the label.
    data : dict
        Data for training.

    Returns
    -------
    result : dict
        Experiment summary stats.
    """
68
    nrounds = 5
69
    lgb_data = data[f"lgb_with_{label_type}_labels"]
70
    params = {"objective": objective, "feature_fraction": 1, "bagging_fraction": 1, "verbose": -1, "seed": 123}
71
72
    time_zero = time.time()
    gbm = lgb.train(params, lgb_data, num_boost_round=nrounds)
73
    y_fitted = gbm.predict(data["X"])
74
    y_true = data[f"{label_type}_labels"]
75
    duration = time.time() - time_zero
76
    return {"time": duration, "correlation": np.corrcoef(y_fitted, y_true)[0, 1], "logloss": log_loss(y_fitted, y_true)}
77
78
79
80


#################
# Observe the behavior of `binary` and `xentropy` objectives
81
82
print("Performance of `binary` objective with binary labels:")
print(experiment("binary", label_type="binary", data=DATA))
83

84
85
print("Performance of `xentropy` objective with binary labels:")
print(experiment("xentropy", label_type="binary", data=DATA))
86

87
88
print("Performance of `xentropy` objective with probability labels:")
print(experiment("xentropy", label_type="probability", data=DATA))
89
90
91
92
93
94
95
96
97

# Trying this throws an error on non-binary values of y:
#   experiment('binary', label_type='probability', DATA)

# The speed of `binary` is not drastically different than
#   `xentropy`. `xentropy` runs faster than `binary` in many cases, although
#   there are reasons to suspect that `binary` should run faster when the
#   label is an integer instead of a float
K = 10
98
99
A = [experiment("binary", label_type="binary", data=DATA)["time"] for k in range(K)]
B = [experiment("xentropy", label_type="binary", data=DATA)["time"] for k in range(K)]
100
101
print(f"Best `binary` time: {min(A)}")
print(f"Best `xentropy` time: {min(B)}")