|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +import numbers |
| 5 | +import numpy as np |
| 6 | + |
| 7 | + |
| 8 | +def _get_n_samples_subsample(n_samples, max_samples): |
| 9 | + """ |
| 10 | + Get the number of samples in a sub-sample without replacement. |
| 11 | + Parameters |
| 12 | + ---------- |
| 13 | + n_samples : int |
| 14 | + Number of samples in the dataset. |
| 15 | + max_samples : int or float |
| 16 | + The maximum number of samples to draw from the total available: |
| 17 | + - if float, this indicates a fraction of the total and should be |
| 18 | + the interval `(0, 1)`; |
| 19 | + - if int, this indicates the exact number of samples; |
| 20 | + - if None, this indicates the total number of samples. |
| 21 | + Returns |
| 22 | + ------- |
| 23 | + n_samples_subsample : int |
| 24 | + The total number of samples to draw for the subsample. |
| 25 | + """ |
| 26 | + if max_samples is None: |
| 27 | + return n_samples |
| 28 | + |
| 29 | + if isinstance(max_samples, numbers.Integral): |
| 30 | + if not (1 <= max_samples <= n_samples): |
| 31 | + msg = "`max_samples` must be in range 1 to {} but got value {}" |
| 32 | + raise ValueError(msg.format(n_samples, max_samples)) |
| 33 | + return max_samples |
| 34 | + |
| 35 | + if isinstance(max_samples, numbers.Real): |
| 36 | + if not (0 < max_samples <= 1): |
| 37 | + msg = "`max_samples` must be in range (0, 1) but got value {}" |
| 38 | + raise ValueError(msg.format(max_samples)) |
| 39 | + return int(round(n_samples * max_samples)) |
| 40 | + |
| 41 | + msg = "`max_samples` should be int or float, but got type '{}'" |
| 42 | + raise TypeError(msg.format(type(max_samples))) |
| 43 | + |
| 44 | + |
| 45 | +def _accumulate_prediction(predict, X, out, lock, *args, **kwargs): |
| 46 | + """ |
| 47 | + This is a utility function for joblib's Parallel. |
| 48 | + It can't go locally in ForestClassifier or ForestRegressor, because joblib |
| 49 | + complains that it cannot pickle it when placed there. |
| 50 | + """ |
| 51 | + prediction = predict(X, *args, check_input=False, **kwargs) |
| 52 | + with lock: |
| 53 | + if len(out) == 1: |
| 54 | + out[0] += prediction |
| 55 | + else: |
| 56 | + for i in range(len(out)): |
| 57 | + out[i] += prediction[i] |
| 58 | + |
| 59 | + |
| 60 | +def _accumulate_prediction_var(predict, X, out, lock, *args, **kwargs): |
| 61 | + """ |
| 62 | + This is a utility function for joblib's Parallel. |
| 63 | + It can't go locally in ForestClassifier or ForestRegressor, because joblib |
| 64 | + complains that it cannot pickle it when placed there. |
| 65 | + Accumulates the mean covariance of a tree prediction. predict is assumed to |
| 66 | + return an array of (n_samples, d) or a tuple of arrays. This method accumulates in the placeholder |
| 67 | + out[0] the (n_samples, d, d) covariance of the columns of the prediction across |
| 68 | + the trees and for each sample (or a tuple of covariances to be stored in each element |
| 69 | + of the list out). |
| 70 | + """ |
| 71 | + prediction = predict(X, *args, check_input=False, **kwargs) |
| 72 | + with lock: |
| 73 | + if len(out) == 1: |
| 74 | + out[0] += np.einsum('ijk,ikm->ijm', |
| 75 | + prediction.reshape(prediction.shape + (1,)), |
| 76 | + prediction.reshape((-1, 1) + prediction.shape[1:])) |
| 77 | + else: |
| 78 | + for i in range(len(out)): |
| 79 | + pred_i = prediction[i] |
| 80 | + out[i] += np.einsum('ijk,ikm->ijm', |
| 81 | + pred_i.reshape(pred_i.shape + (1,)), |
| 82 | + pred_i.reshape((-1, 1) + pred_i.shape[1:])) |
| 83 | + |
| 84 | + |
| 85 | +def _accumulate_prediction_and_var(predict, X, out, out_var, lock, *args, **kwargs): |
| 86 | + """ |
| 87 | + This is a utility function for joblib's Parallel. |
| 88 | + It can't go locally in ForestClassifier or ForestRegressor, because joblib |
| 89 | + complains that it cannot pickle it when placed there. |
| 90 | + Combines `_accumulate_prediction` and `_accumulate_prediction_var` in a single |
| 91 | + parallel run, so that out will contain the mean of the predictions across trees |
| 92 | + and out_var the covariance. |
| 93 | + """ |
| 94 | + prediction = predict(X, *args, check_input=False, **kwargs) |
| 95 | + with lock: |
| 96 | + if len(out) == 1: |
| 97 | + out[0] += prediction |
| 98 | + out_var[0] += np.einsum('ijk,ikm->ijm', |
| 99 | + prediction.reshape(prediction.shape + (1,)), |
| 100 | + prediction.reshape((-1, 1) + prediction.shape[1:])) |
| 101 | + else: |
| 102 | + for i in range(len(out)): |
| 103 | + pred_i = prediction[i] |
| 104 | + out[i] += prediction |
| 105 | + out_var[i] += np.einsum('ijk,ikm->ijm', |
| 106 | + pred_i.reshape(pred_i.shape + (1,)), |
| 107 | + pred_i.reshape((-1, 1) + pred_i.shape[1:])) |
| 108 | + |
| 109 | + |
| 110 | +def _accumulate_oob_preds(tree, X, subsample_inds, alpha_hat, jac_hat, counts, lock): |
| 111 | + mask = np.ones(X.shape[0], dtype=bool) |
| 112 | + mask[subsample_inds] = False |
| 113 | + alpha, jac = tree.predict_alpha_and_jac(X[mask]) |
| 114 | + with lock: |
| 115 | + alpha_hat[mask] += alpha |
| 116 | + jac_hat[mask] += jac |
| 117 | + counts[mask] += 1 |
0 commit comments