Bagging Example#

Imports#

import matplotlib.pyplot as plt
import numpy as np

from cristal import BaggingDyCF, DyCF, DyCFPlotter, IMPLEMENTED_REGULARIZATION_OPTIONS
from cristal.utils.data import make_T_rotated

Generate data#

n_samples = 5_001
data = make_T_rotated(n_samples)
2025-08-05 13:58:51,098 	 WARNING 	 CRISTAL.data.make_T_rotated 	 n_samples must be even to create two normal distributions. Adjusting n_samples = 5001 to 5000.

Bagging parameters#

n_estimators = 10
n_values = [5, 5, 5, 6, 7, 8, 5, 4, 3, 2]
training_samples = 1000

Create BaggingDyCF and DyCF instances#

bagging_model = BaggingDyCF(n_values, n_estimators=n_estimators, regularization=IMPLEMENTED_REGULARIZATION_OPTIONS.CONSTANT)
model = DyCF(n=n_values[0], regularization=IMPLEMENTED_REGULARIZATION_OPTIONS.CONSTANT)

Fit the models#

bagging_model.fit(data, n_samples=training_samples)
<cristal.christoffel.BaggingDyCF at 0x7ff0301d1e80>
model.fit(data)
<cristal.christoffel.DyCF at 0x7ff03039a3f0>

Plot the results#

levels = [2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 30, 50, 100, 5000, 10000, 20000, 30000]
bagging_plotter = DyCFPlotter(bagging_model)
model_plotter = DyCFPlotter(model)
fig, axs = plt.subplots(1, 2, figsize=(15, 6))

bagging_plotter.levelset(data, levels=levels, fig=fig, ax=axs[0], close=False, show=False)
model_plotter.levelset(data, levels=levels, fig=fig, ax=axs[1], close=False, show=False)
plt.tight_layout()
plt.show()
../../_images/e06c36f6b4c77a2d4760cd68e345b3e486fab2faa7916eb0c704bd8c01c268a6.png