'Apply trees in the ensemble to X, return leaf indices.
.. versionadded:: 0.17
Parameters
X : array-like or sparse matrix, shape = [n_samples, n_features]
The input samples. Internally, its dtype will be converted to
``dtype=np.float32``. If a sparse matrix is provided, it will
be converted to a sparse ``csr_matrix``.
Returns
X_leaves : array_like, shape = [n_samples, n_estimators, n_classes]
For each datapoint x in X and for each tree in the ensemble,
return the index of the leaf x ends up in each estimator.
In the case of binary classification n_classes is 1.'

def apply(self, X):

    self._check_initialized()
    X = self.estimators_[(0, 0)]._validate_X_predict(X, check_input=True)
    (n_estimators, n_classes) = self.estimators_.shape
    leaves = np.zeros((X.shape[0], n_estimators, n_classes))
    for i in range(n_estimators):
        for j in range(n_classes):
            estimator = self.estimators_[(i, j)]
            leaves[:, i, j] = estimator.apply(X, check_input=False)
    return leaves
