liveBook · Manning

Randomization

As explained in Ensemble Methods for Machine Learning

Randomization is the deliberate introduction of stochastic variation into data sampling, feature selection, or model training.

Randomization deliberately injects variation into learning so an ensemble’s models do not all make the same predictions. In bagging, bootstrap samples create different training sets; in randomized trees, feature choices—and sometimes split thresholds—also vary. section 2.6 section 7.2.1

Common ways to apply randomization include:

  • Bootstrap sampling draws training examples with replacement, then aggregates predictions from models trained on the resulting data replicates. section 2.6
  • Random forests combine bootstrap samples with decision trees that evaluate only a random subset of features at each split. section 7.2.1
  • Extra Trees randomize both the candidate feature and splitting threshold, allowing ensemble construction without bootstrap sampling. section 7.2.1
  • Pasting samples without replacement, while random subspaces and random patches vary features alone or features plus training examples. section 2.6

Regression ensembles

On the AutoMPG data set, RandomForestRegressor and ExtraTreesRegressor can be trained with squared-error or absolute-error criteria, then compared using training and test MSE and MAE. section 7.2.1

Listing 7.3 Random forest and Extra Trees for regression
from sklearn.ensemble import RandomForestRegressor, ExtraTreesRegressor
from sklearn.metrics import mean_squared_error, mean_absolute_error
 
ensembles = {                                                              #1
    'Random Forest MSE': RandomForestRegressor(criterion='squared_error'),
    'Random Forest MAE': RandomForestRegressor(criterion='absolute_error'),
    'ExtraTrees MSE': ExtraTreesRegressor(criterion='squared_error'),
    'ExtraTrees MAE': ExtraTreesRegressor(criterion='absolute_error')} 
 
results = pd.DataFrame()                                                   #2
ypred_trn = {}
ypred_tst = {}
 
for method, ensemble in ensembles.items():    
    ensemble.fit(Xtrn, ytrn)                                               #3
 
    ypred_trn[method] = ensemble.predict(Xtrn)                             #4
    ypred_tst[method] = ensemble.predict(Xtst)
 
    res = {'Method-Loss': method,                                          #5
            'Train MSE': mean_squared_error(ytrn, ypred_trn[method]),
            'Train MAE': mean_absolute_error(ytrn, ypred_trn[method]), 
            'Test MSE': mean_squared_error(ytst, ypred_tst[method]),
            'Test MAE': mean_absolute_error(ytst, ypred_tst[method])}
 
    results = pd.concat([results,   
                         pd.DataFrame.from_dict([res])], ignore_index=True)#6

The listing configures four scikit-learn regression ensembles, fits each model, generates training and test predictions, and records evaluation results. section 7.2.1

How Ensemble Methods for Machine Learning covers it

How deep it goes
Explains bootstrap sampling, feature subsets, thresholds, aggregation, and regression evaluation, without developing the underlying tree algorithms.
Examples
Uses AutoMPG regression ensembles, four scikit-learn configurations, and comparisons based on MSE and MAE.
What you can do after
Operate/configure RandomForestRegressor and ExtraTreesRegressor with alternative loss criteria and compare their predictions using MSE and MAE.
How it connects
Connects bagging, random forests, Extra Trees, bootstrap sampling, decision trees, feature sampling, and ensemble prediction.

Also explained in

Compare how 5 books explain randomization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Ensemble Methods for Machine Learning. The section links point to the parts of the book it draws on.