'Average anomaly score of X of the base classifiers.
The anomaly score of an input sample is computed as
the mean anomaly score of the trees in the forest.
The measure of normality of an observation given a tree is the depth
of the leaf containing this observation, which is equivalent to
the number of splittings required to isolate this point. In case of
several observations n_left in the leaf, the average path length of
a n_left samples isolation tree is added.
Parameters
X : {array-like, sparse matrix}, shape (n_samples, n_features)
The training input samples. Sparse matrices are accepted only if
they are supported by the base estimator.
Returns
scores : array of shape (n_samples,)
The anomaly score of the input samples.
The lower, the more abnormal.'

def decision_function(self, X):

    X = check_array(X, accept_sparse='csr')
    n_samples = X.shape[0]
    n_samples_leaf = np.zeros((n_samples, self.n_estimators), order='f')
    depths = np.zeros((n_samples, self.n_estimators), order='f')
    if (self._max_features == X.shape[1]):
        subsample_features = False
    else:
        subsample_features = True
    for (i, (tree, features)) in enumerate(zip(self.estimators_, self.estimators_features_)):
        if subsample_features:
            X_subset = X[:, features]
        else:
            X_subset = X
        leaves_index = tree.apply(X_subset)
        node_indicator = tree.decision_path(X_subset)
        n_samples_leaf[:, i] = tree.tree_.n_node_samples[leaves_index]
        depths[:, i] = np.ravel(node_indicator.sum(axis=1))
        depths[:, i] -= 1
    depths += _average_path_length(n_samples_leaf)
    scores = (2 ** ((- depths.mean(axis=1)) / _average_path_length(self.max_samples_)))
    return (0.5 - scores)
