def calc_inbag(n_samples, forest):
if not forest.bootstrap:<EOL><INDENT>e_s = "<STR_LIT>"<EOL>e_s = "<STR_LIT>"<EOL>raise ValueError(e_s)<EOL><DEDENT>n_trees = forest.n_estimators<EOL>inbag = np.zeros((n_samples, n_trees))<EOL>sample_idx = []<EOL>for t_idx in range(n_trees):<EOL><INDENT>sample_idx.append(<EOL>_generate_sample_indices(forest.estimators_[t_idx].random_state,<EOL>n_samples))<EOL>inbag[:, t_idx] = np.bincount(sample_idx[-<NUM_LIT:1>], minlength=n_samples)<EOL><DEDENT>return inbag<EOL>
Derive samples used to create trees in scikit-learn RandomForest objects.

Recovers the samples in each tree from the random state of that tree using
:func:`forest._generate_sample_indices`.

Parameters
----------
n_samples : int
    The number of samples used to fit the scikit-learn RandomForest object.

forest : RandomForest
    Regressor or Classifier object that is already fit by scikit-learn.

Returns
-------
Array that records how many times a data point was placed in a tree.
Columns are individual trees. Rows are the number of times a sample was
used in a tree.