# predict

Class: CompactTreeBagger

Predict response

## Syntax

```Yfit = predict(B,TBLnew)Yfit = predict(B,Xnew)[Yfit,stdevs] = predict(B,TBLnew)[Yfit,stdevs] = predict(B,Xnew)[Yfit,scores] = predict(B,TBLnew)[Yfit,scores] = predict(B,Xnew)[Yfit,scores,stdevs] = predict(B,TBLnew)[Yfit,scores,stdevs] = predict(B,Xnew)Yfit = predict(B,TBLnew,'param1',val1,'param2',val2,...)Yfit = predict(B,Xnew,'param1',val1,'param2',val2,...)```

## Description

`Yfit = predict(B,TBLnew)` computes the predicted response of the trained ensemble `B` for the predictor data contained in the table `TBLnew`. By default, `predict` takes a democratic (nonweighted) average vote from all trees in the ensemble. In `TBLnew`, rows represent observations and columns represent variables. `Yfit` is a cell array of strings for classification and a numeric array for regression. If you trained `B` using sample data contained in a table, then the input data for this method must also be in a table.

`Yfit = predict(B,Xnew)` computes the predicted response of the trained ensemble `B` for predictor data contained in the matrix `Xnew`. If you trained `B` using sample data contained in a matrix, then the input data for this method must also be in a matrix.

For regression, `[Yfit,stdevs] = predict(B,TBLnew)` or ```[Yfit,stdevs] = predict(B,Xnew)``` also returns standard deviations of the computed responses over the ensemble of the grown trees.

For classification, `[Yfit,scores] = predict(B,TBLnew)` or ```[Yfit,scores] = predict(B,Xnew)``` returns scores for all classes. `scores` is a matrix with one row per observation and one column per class. For each observation and each class, the score generated by each tree is the probability of this observation originating from this class computed as the fraction of observations of this class in a tree leaf. `predict` averages these scores over all trees in the ensemble.

`[Yfit,scores,stdevs] = predict(B,TBLnew)` or ```[Yfit,scores,stdevs] = predict(B,Xnew)``` also returns standard deviations of the computed scores for classification. `stdevs` is a matrix with one row per observation and one column per class, with standard deviations taken over the ensemble of the grown trees.

`Yfit = predict(B,TBLnew,'param1',val1,'param2',val2,...)` or ```Yfit = predict(B,Xnew,'param1',val1,'param2',val2,...)``` specifies optional parameter name/value pairs:

 `'Trees'` Array of tree indices to use for computation of responses. Default is `'all'`. `'TreeWeights'` Array of `NTrees` weights for weighting votes from the specified trees. `'UseInstanceForTree'` Logical matrix of size `Nobs`-by-`NTrees` indicating which trees to use to make predictions for each observation. By default all trees are used for all observations.

## Algorithms

• For regression problems, the predicted response for an observation is the weighted average of the predictions using selected trees only. That is,

`${\stackrel{^}{y}}_{\text{bag}}=\frac{1}{\sum _{t=1}^{T}{\alpha }_{t}I\left(t\in S\right)}\sum _{t=1}^{T}{\alpha }_{t}{\stackrel{^}{y}}_{t}I\left(t\in S\right).$`
• ${\stackrel{^}{y}}_{t}$ is the prediction from tree t in the ensemble.

• S is the set of indices of selected trees that comprise the prediction (see `'``Trees``'` and `'``UseInstanceForTree``'`). $I\left(t\in S\right)$ is 1 if t is in the set S, and 0 otherwise.

• αt is the weight of tree t (see `'``TreeWeights``'`).

• For classification problems, the predicted class for an observation is the class that yields the largest weighted average of the class posterior probabilities (i.e., classification scores) computed using selected trees only. That is,

1. For each class cC and each tree t = 1,...,T, `predict` computes ${\stackrel{^}{P}}_{t}\left(c|x\right)$, which is the estimated posterior probability of class c given observation x using tree t. C is the set of all distinct classes in the training data. For more details on classification tree posterior probabilities, see `fitctree` and `predict`.

2. `predict` computes the weighted average of the class posterior probabilities over the selected trees.

`${\stackrel{^}{P}}_{\text{bag}}\left(c|x\right)=\frac{1}{\sum _{t=1}^{T}{\alpha }_{t}I\left(t\in S\right)}\sum _{t=1}^{T}{\alpha }_{t}{\stackrel{^}{P}}_{t}\left(c|x\right)I\left(t\in S\right).$`
3. The predicted class is the class that yields the largest weighted average.

`${\stackrel{^}{y}}_{\text{bag}}=\underset{c\in C}{\mathrm{arg}\mathrm{max}}\left\{{\stackrel{^}{P}}_{\text{bag}}\left(c|x\right)\right\}.$`