mixle.models.random_forest module¶
Random forests as a conditional leaf in the mixle estimation framework.
A random forest is discriminative and is not fit by accumulating additive sufficient statistics or by EM, so it does not look like the exponential-family leaves. It still fits the estimation contract cleanly if we treat it as a conditional distribution p(y | x): the observation is a pair (x, y), the accumulator’s “sufficient statistic” is the buffered weighted design matrix, combine() concatenates the per-partition buffers (the map-reduce step is the data shuffle), and estimate() trains the forest in a single non-EM pass over that buffer.
The result is a SequenceEncodableProbabilityDistribution whose seq_log_density returns log p(y | x), so a fitted forest composes with seq_encode / seq_log_density / the top-level log_density helper, and can sit in a slot of a composite/record model or act as a mixture-of-experts component. Because estimate() refits from scratch, run it through optimize(…, max_its=1) (there is no likelihood for EM to iterate); for classification log-density is the forest’s predict_log_proba, for regression it is a Gaussian residual model with a globally estimated noise scale.
The forest itself is a native numpy CART + bagging ensemble (mixle.models._forest), so mixle carries no scikit-learn dependency.
- class RandomForestConditionalSampler(dist, seed=None, *, rng=None)[source]
Bases:
DistributionSamplerSampler for the conditional forest. p(y | x) cannot generate x, so the unconditional sample() is disabled; use sample_y(X) to draw targets given features.
- Parameters:
dist (SequenceEncodableProbabilityDistribution)
seed (int | None)
rng (RandomState | None)
- sample(size=None, *, batched=True)[source]
Raise because the conditional forest has no marginal model for
x.
- class RandomForestConditional(forest, task, sigma=None, n_features=None, name=None, keys=None)[source]
Bases:
SequenceEncodableProbabilityDistributionFitted random forest viewed as a conditional distribution p(y | x).
Observations are (x, y) pairs: x is a feature vector and y is a class label (classification) or a real target (regression). seq_log_density returns log p(y | x) – predict_log_proba for classification, a Gaussian residual density with scale sigma for regression.
- Parameters:
- density(x)[source]
Return
p(y | x)for one feature/target pair.
- log_density(x)[source]
Return
log p(y | x)for one feature/target pair.
- seq_log_density(x)[source]
Return per-row conditional log densities for encoded
(X, y)data.
- sample_y(x, rng)[source]
Draw target values from the fitted conditional forest at feature rows
x.- Parameters:
x (Any)
rng (RandomState)
- Return type:
- sampler(seed=None)[source]
Return a conditional sampler for drawing targets given features.
- Parameters:
seed (int | None)
- Return type:
RandomForestConditionalSampler
- estimator(pseudo_count=None)[source]
Return a fresh estimator with the same task, name, and keyed-accumulation settings.
- Parameters:
pseudo_count (float | None)
- Return type:
RandomForestEstimator
- dist_to_encoder()[source]
Return the encoder for feature/target observation pairs.
- Return type:
RandomForestEncoder
- class RandomForestAccumulator(keys=None, name=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorBuffers the weighted (x, y) design matrix; combine() concatenates partition buffers into the full training set that estimate() fits the forest on.
- update(x, weight, estimate)[source]
Add one weighted feature/target observation to the training buffer.
- initialize(x, weight, rng)[source]
Initialize from one observation using the ordinary update path.
- Parameters:
weight (float)
rng (RandomState | None)
- Return type:
None
- seq_update(x, weights, estimate)[source]
Add an encoded batch and weights to the training buffer.
- seq_initialize(x, weights, rng)[source]
Initialize from an encoded batch using the ordinary batch update path.
- combine(suff_stat)[source]
Merge a buffered
(X, y, weights)tuple from another accumulator.
- value()[source]
Return the buffered design matrix, targets, and weights, or
Noneif empty.
- from_value(x)[source]
Restore the accumulator from a buffered value tuple.
- key_merge(stats_dict)[source]
Merge this accumulator into
stats_dictunderkeyswhen keyed accumulation is enabled.
- key_replace(stats_dict)[source]
Replace this accumulator from
stats_dictunderkeyswhen present.
- acc_to_encoder()[source]
Return the encoder expected by this accumulator.
- Return type:
RandomForestEncoder
- class RandomForestAccumulatorFactory(name=None, keys=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for random-forest accumulators.
- make()[source]
Create a fresh random-forest accumulator.
- Return type:
RandomForestAccumulator
- class RandomForestEstimator(task='auto', n_estimators=100, max_depth=None, min_samples_split=2, min_samples_leaf=1, max_features='auto', random_state=None, min_sigma=1.0e-3, name=None, keys=None)[source]
Bases:
ParameterEstimatorEstimator that fits a native (numpy) random forest as a conditional leaf.
task is ‘classification’, ‘regression’, or ‘auto’ (inferred from the dtype of y). The forest hyperparameters (n_estimators, max_depth, min_samples_split, min_samples_leaf, max_features, random_state) are passed straight to the native ensemble. estimate() trains in one pass on the accumulated weighted data; there is no EM iteration, so drive it with optimize(max_its=1) or call the seq_encode / accumulate / estimate path directly.
- Parameters:
- accumulator_factory()[source]
Return an accumulator factory for weighted feature/target buffers.
- Return type:
RandomForestAccumulatorFactory
- class RandomForestEncoder[source]
Bases:
DataSequenceEncoderEncodes a sequence of (x, y) observations into a (design-matrix, target-vector) pair.