Compare Integer Sampling on a Fixed Budget
A tree's minimum leaf size can range from a few samples to a substantial fraction of the training set. This recipe compares how two integer distributions spend eight evaluations across that range. Use the development version with log-uniform integer support.
Reserve a holdout and fix the validation folds
Fit on the diabetes regression dataset. Use the same observations, folds and tree seed for both distributions so the search seed controls which candidates are proposed.
from statistics import median
from sklearn.datasets import load_diabetes
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import KFold, train_test_split
from sklearn.tree import DecisionTreeRegressor
from sklearn_genetic import GASearchCV
from sklearn_genetic.space import Integer
X, y = load_diabetes(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=17)
folds = KFold(3, shuffle=True, random_state=29)Compare eight distinct candidates per run
Both distributions cover every integer from 1 through 128. Uniform sampling gives equal weight to each integer. Log-uniform sampling spends more of the budget on small leaf sizes. Keep the default smart initializer and set generations=0 to measure the initial candidates before evolution. Check the number of actual cross-validation evaluations in fit_stats_.
def search_leaf_size(distribution, seed):
search = GASearchCV(
estimator=DecisionTreeRegressor(random_state=7),
param_grid={"min_samples_leaf": Integer(1, 128, distribution=distribution)},
cv=folds,
scoring="neg_mean_absolute_error",
population_size=8,
generations=0,
n_jobs=1,
random_state=seed,
error_score="raise",
verbose=False,
).fit(X_train, y_train)
assert search.fit_stats_["cross_validate_calls"] == 8
assert search.fit_stats_["unique_candidates"] == 8
return search
losses = {"uniform": [], "log-uniform": []}
for seed in (11, 13, 17, 19, 23):
for distribution in losses:
search = search_leaf_size(distribution, seed)
losses[distribution].append(-search.best_score_)
print("Distribution | Best CV MAE for each seed | Median")
for distribution, values in losses.items():
print(distribution, [round(value, 2) for value in values], round(median(values), 2))Distribution | Best CV MAE for each seed | Median
uniform [49.12, 49.23, 48.69, 49.18, 49.78] 49.18
log-uniform [49.18, 48.75, 48.69, 49.11, 49.18] 49.11Evaluate the selected sampling policy
Choose the distribution using median validation MAE, where smaller is better. Run one search with that distribution and a fresh seed, then evaluate on the reserved rows. The median measures sensitivity to the search seed for these particular folds. Another estimator, range or dataset can favor a different distribution.
chosen = min(losses, key=lambda name: median(losses[name]))
final = search_leaf_size(chosen, seed=31)
print("Distribution", chosen)
print("Minimum samples per leaf", final.best_params_["min_samples_leaf"])
print("Holdout MAE", round(mean_absolute_error(y_test, final.predict(X_test)), 2))Distribution log-uniform
Minimum samples per leaf 16
Holdout MAE 48.21To combine expert-chosen values with samples from a distribution, see warm-start a search. For the integer probability rule and supported bounds, see the search space API.