pyanno4rt.learning_model.frequentist._random_forest
Random forest model.
Overview
Random forest model class. |
Classes
- class pyanno4rt.learning_model.frequentist._random_forest.RandomForestModel(model_label, model_folder_path, dataset, preprocessing_steps, tune_space, tune_evaluations, tune_score, inspect_model, evaluate_model, display_options)[source]
Random forest model class.
This class enables building an individual preprocessing pipeline, fit the random forest model from the input data, inspect the model, make predictions with the model, and assess the predictive performance using multiple evaluation metrics.
The training process includes sequential model-based hyperparameter optimization with tree-structured Parzen estimators and stratified k-fold cross-validation for the objective function evaluation. Cross-validation is also applied to (optionally) inspect the validation feature importances and to generate out-of-folds predictions as a full reconstruction of the input labels for generalization assessment.
- Parameters:
model_label (string) – Label for the random forest model to be used for file naming.
dataset (dict) – Dictionary with the raw data set, the label viewpoint, the label bounds, the feature values and names, and the label values and names after modulation. In a compact way, this represents the input data for the random forest model.
preprocessing_steps (tuple) –
Sequence of labels associated with preprocessing algorithms which make up the preprocessing pipeline for the random forest model. Current available algorithm labels are:
transformers : ‘Equalizer’, ‘StandardScaler’, ‘Whitening’.
tune_space (dict) –
Search space for the Bayesian hyperparameter optimization, including
’n_estimators’ : number of trees in the forest;
’criterion’ : measure for the quality of a split;
’max_depth’ : maximum depth of each tree;
’min_samples_split’ : minimum number of samples required for splitting each node;
’min_samples_leaf’ : minimum number of samples required at each node;
’min_weight_fraction_leaf’ : minimum weighted fraction of the weights sum required at each node;
’max_features’ : maximum number of features taken into account when looking for the best split at each node;
’bootstrap’ : indicator for the use of bootstrap samples to build the trees;
’class_weight’ : weights associated with the classes;
’ccp_alpha’ : complexity parameter for minimal cost-complexity pruning.
tune_evaluations (int) – Number of evaluation steps (trials) for the Bayesian hyperparameter optimization.
tune_score (string) –
Scoring function for the evaluation of the hyperparameter set candidates. Current available scorers are:
’log_loss’ : negative log-likelihood score;
’roc_auc_score’ : area under the ROC curve score.
tune_splits (int) – Number of splits for the stratified cross-validation within each hyperparameter optimization step.
inspect_model (bool) – Indicator for the inspection of the model, e.g. the feature importances.
evaluate_model (bool) – Indicator for the evaluation of the model, e.g. the model KPIs.
oof_splits (int) – Number of splits for the stratified cross-validation within the out-of-folds evaluation step of the random forest model.
- preprocessor
Instance of the class DataPreprocessor, which holds methods to build the preprocessing pipeline, fit with the input features, transform the features, and derive the gradient of the preprocessing algorithms w.r.t the features.
- Type:
object of class DataPreprocessor
- features
Values of the input features.
- Type:
ndarray
- labels
Values of the input labels.
- Type:
ndarray
- configuration
Dictionary with information for the modeling, i.e., the dataset, the preprocessing, and the hyperparameter search space.
- Type:
dict
- model_path
Path for storing and retrieving the random forest model.
- Type:
string
- configuration_path
Path for storing and retrieving the configuration dictionary.
- Type:
string
- hyperparameter_path
Path for storing and retrieving the hyperparameter dictionary.
- Type:
string
- updated_model
Indicator for the update status of the model, triggers recalculating the model inspection and model evaluation classes.
- Type:
bool
- prediction_model
Instance of the class RandomForestClassifier, which holds methods to make predictions from the random forest model.
- Type:
object of class RandomForestClassifier
- inspector
Instance of the class ModelInspector, which holds methods to compute model inspection values, e.g. feature importances.
- Type:
object of class ModelInspector
- training_prediction
Array with the label predictions on the input data.
- Type:
ndarray
- oof_prediction
Array with the out-of-folds predictions on the input data.
- Type:
ndarray
- evaluator
Instance of the class ModelEvaluator, which holds methods to compute the evaluation metrics for a given array with label predictions.
- Type:
object of class ModelEvaluator
Notes
Currently, the preprocessing pipeline for the model is restricted to transformations of the input feature values, e.g. scaling, dimensionality reduction or feature engineering. Transformations which affect the input labels in the same way, e.g. resampling or outlier removal, are not yet possible.
Overview
Methods preprocess(features)Preprocess the input feature vector with the built pipeline.
get_model(features, labels)Get the random forest outcome prediction model by reading from the model file path, the datahub, or by training.
tune_hyperparameters(features, labels)Tune the hyperparameters of the random forest model via sequential model-based optimization using the tree-structured Parzen estimator. As a variation, the objective function is evaluated based on a stratified k-fold cross-validation.
train(features, labels)Train the random forest outcome prediction model.
predict(features)Predict the label values from the feature values.
predict_oof(features, labels)Predict the out-of-folds (OOF) labels using a stratified k-fold cross-validation.
inspect(features, labels, oof_folds).
evaluate(features, labels).
set_file_paths(base_path)Set the paths for model, configuration and hyperparameter files.
Read the random forest outcome prediction model from the model file path.
write_model_to_file(prediction_model)Write the random forest outcome prediction model to the model file path.
Read the configuration dictionary from the configuration file path.
write_configuration_to_file(configuration)Write the configuration dictionary to the configuration file path.
Read the random forest outcome prediction model hyperparameters from the hyperparameter file path.
write_hyperparameters_to_file(hyperparameters)Write the hyperparameter dictionary to the hyperparameter file path.
Members
- preprocess(features)[source]
Preprocess the input feature vector with the built pipeline.
- Parameters:
features (ndarray) – Array of input feature values.
- Returns:
Array of transformed feature values.
- Return type:
ndarray
- get_model(features, labels)[source]
Get the random forest outcome prediction model by reading from the model file path, the datahub, or by training.
- Returns:
Instance of the class RandomForestClassifier, which holds methods to make predictions from the random forest model.
- Return type:
object of class RandomForestClassifier
- tune_hyperparameters(features, labels)[source]
Tune the hyperparameters of the random forest model via sequential model-based optimization using the tree-structured Parzen estimator. As a variation, the objective function is evaluated based on a stratified k-fold cross-validation.
- Returns:
tuned_hyperparameters – Dictionary with the hyperparameter names and values tuned via Bayesian hyperparameter optimization.
- Return type:
dict
- train(features, labels)[source]
Train the random forest outcome prediction model.
- Returns:
prediction_model – Instance of the class RandomForestClassifier, which holds methods to make predictions from the random forest model.
- Return type:
object of class RandomForestClassifier
- predict(features)[source]
Predict the label values from the feature values.
- Parameters:
features (ndarray) – Array of input feature values.
- Returns:
Floating-point label prediction or array of label predictions.
- Return type:
float or ndarray
- predict_oof(features, labels)[source]
Predict the out-of-folds (OOF) labels using a stratified k-fold cross-validation.
- Parameters:
oof_splits (int) – Number of splits for the stratified cross-validation.
- Returns:
Array with the out-of-folds label predictions.
- Return type:
ndarray
- set_file_paths(base_path)[source]
Set the paths for model, configuration and hyperparameter files.
- Parameters:
base_path (string) – Base path from which to access the model files.
- read_model_from_file()[source]
Read the random forest outcome prediction model from the model file path.
- Returns:
Instance of the class RandomForestClassifier, which holds methods to make predictions from the random forest model.
- Return type:
object of class RandomForestClassifier
- write_model_to_file(prediction_model)[source]
Write the random forest outcome prediction model to the model file path.
- Parameters:
prediction_model (object of class RandomForestClassifier) – Instance of the class RandomForestClassifier, which holds methods to make predictions from the random forest model.
- read_configuration_from_file()[source]
Read the configuration dictionary from the configuration file path.
- Returns:
Dictionary with information for the modeling, i.e., the dataset, the preprocessing steps, and the hyperparameter search space.
- Return type:
dict
- write_configuration_to_file(configuration)[source]
Write the configuration dictionary to the configuration file path.
- Parameters:
configuration (dict) – Dictionary with information for the modeling, i.e., the dataset, the preprocessing steps, and the hyperparameter search space.