LighGBM random forest with 1 tree, predicted probability is not equal to mean label

Viewed 93

I'm running LightGBM with RandomForest with 1 tree (same as a decision tree).

The predicted probability from leaf is not equal to the mean of label.

Can anyone please help me understand why?

Dataset:

enter image description here

Tree:

enter image description here

Probabilities:

enter image description here

Code:

import lightgbm
import pandas as pd
import numpy as np

def make_class(length, name, thrshold, classes):
    a = np.random.rand(length, 1)
    b = np.where(a > thrshold, classes[0], classes[1])
    return pd.DataFrame(b, columns=[name])

a = make_class(100000, "State", 0.7, ["NY", "TX"])
b = make_class(100000, "Phone", 0.3, ["Ios", "Android"])

df = pd.concat([a, b], ignore_index=True, axis=1)
df.columns = ["State", "Phone"]

df.head(2)

mapper = {
    ("TX", "Ios"): 0.3,
    ("NY", "Ios"): 0.85,
    ("TX", "Android"): 1.0,
    ("NY", "Android"): 0.0,
}
mapper

df["odds"] = df.agg(tuple, 1).map(mapper)
df["label"] = np.where(df["odds"] > np.random.rand(len(df["odds"]), 1).flatten(), 1, 0)
df.groupby(["State", "Phone"])["label"].mean()
df.groupby(["State", "Phone"])["label"].agg(["mean", "count"])
df = df.drop("odds", axis=1)

y_train = df["label"].astype("bool")
X_train = df[["State", "Phone"]].copy()

X_train = X_train.astype("category")

train_data = lightgbm.Dataset(X_train, label=y_train, free_raw_data=False)

best_parameters = {
    "application": "binary",
    "metric": "binary_logloss",
    "num_iterations": 1,
    "is_unbalance": "false",
    "boosting": "rf",
    "num_leaves": 16,
    "bagging_freq": 1,
    "bagging_fraction": 0.999,
    "verbose": 5,
    "min_split_gain": 1,  # minimum loss reduction required to make further partition on a leaf node of the tree
    "min_child_samples": 1,  # minimum number of data needed in a leaf
}
best_parameters

evals_result = {}
model = lightgbm.train(
    best_parameters,
    train_data,
    evals_result=evals_result,
)

X_train["prob"] = model.predict(X_train[["State", "Phone"]])
X_train["label"] = df["label"]
X_train.groupby(["Phone", "State"]).agg(["mean", "count"])
0 Answers
Related