Predicting and plotting

Predicting

predict takes a Parquet path or glob, or a frame, with the feature columns:

import forest_stream as fs

fit = fs.random_forest("price ~ area + rooms + age", "sales.parquet")

fit.predict("new_homes.parquet")                       # a frame, one row per row
fit.predict("new_homes.parquet", keep=["home_id"],     # carry an identifier through,
            output="predictions.parquet")              # and write to a file

The data are read and predicted in batches (batch_rows, default 250,000), so only a batch’s feature columns are in memory at a time. With output=, the predictions are written to a Parquet file batch by batch and the path is returned, and nothing but a batch is held. Without it the predictions are returned as a frame, which is the only thing that grows with the number of rows. keep= lists columns of the data to carry into the output, in front of the predictions.

What comes back depends on the forest:

Forest

Columns

causal

tau

regression, random forest, decision tree

prediction

classification

class, and prob_<label> for each class

and, when the forest has groups of trees for variances (variance_group_size above 1, or variance=True), variance (variance_<label> for each class).

With fixed effects

A forest fitted with fixed effects ("y ~ x1 + x2 | firm") predicts the outcome net of them: its prediction says how the outcome moves with the features. To add each row’s group effects back, so that the prediction is of the outcome itself, ask for them:

fit = fs.regression_forest("wage ~ age + schooling | worker + firm", "panel.parquet")
fit.predict("panel.parquet", include_fixed_effects=True)   # prediction and fixed_effect
fit.group_effects()                                         # {"worker": table, "firm": table}

prediction is then the forest’s estimate plus the effects, and fixed_effect is the effects’ sum. The data need the fixed-effect columns, and the groups must be ones that were in the data the forest was fitted on. new_groups says what to do with a row whose group was not: "error" (the default), "nan" (a missing prediction) or "zero" (no effect). A group with a single row has no effect of its own (the fixed effect fits that row exactly), so it counts as a new group. The variance of a prediction is the forest’s, and does not allow for the error in the effects.

include_fixed_effects is available for the "backfit" and "outcome_within" methods. With "group_means" the predictions already include what the groups’ means explain, and need the fixed-effect columns to be looked up (see Fixed effects). A causal forest’s effect estimate has no group effect to add.

Plotting

plot() draws with matplotlib, which is not a dependency (pip install forest-stream[plot]), and returns the figure. ax= draws on an axes you made, and title= sets a title.

Kind

Draws

For

"effects"

A histogram of the out-of-bag effect estimates, with the average effect and its interval.

causal forest (default)

"heterogeneity"

The mean effect against a feature (feature=, by default the most important one), in groups of rows of equal weight (groups=). The bars are 1.96 standard errors of each group’s mean estimate, from the spread of the estimates in the group; they are not intervals for the group’s true effect.

causal forest

"projection"

The coefficients of best_linear_projection with intervals. Needs data= (the data the forest was fitted on), and takes terms=.

causal forest

"calibration"

A causal forest: the two coefficients of calibration_test, against their ideal value of one. A regression or classification forest: the mean outcome (a class’s observed share) against the mean out-of-bag prediction, by groups of the prediction, with the diagonal.

all forests

"importance"

Bars of variable_importance (top= limits them).

all

"tree"

A decision tree’s nodes: its splits and the value and rows of each leaf, the branch for a feature below the threshold to the left. max_depth= shows fewer levels.

decision trees (default)

The plots use what the fit already holds: the out-of-bag estimates, the binned features and the weights. They do not read the data again, except "projection". A regression or classification forest’s default is "importance".