

def test_importances_asymptotic():

    def binomial(k, n):
        return (0 if ((k < 0) or (k > n)) else comb(int(n), int(k), exact=True))

    def entropy(samples):
        n_samples = len(samples)
        entropy = 0.0
        for count in bincount(samples):
            p = ((1.0 * count) / n_samples)
            if (p > 0):
                entropy -= (p * np.log2(p))
        return entropy

    def mdi_importance(X_m, X, y):
        (n_samples, n_features) = X.shape
        features = list(range(n_features))
        features.pop(X_m)
        values = [np.unique(X[:, i]) for i in range(n_features)]
        imp = 0.0
        for k in range(n_features):
            coef = (1.0 / (binomial(k, n_features) * (n_features - k)))
            for B in combinations(features, k):
                for b in product(*[values[B[j]] for j in range(k)]):
                    mask_b = np.ones(n_samples, dtype=np.bool)
                    for j in range(k):
                        mask_b &= (X[:, B[j]] == b[j])
                    (X_, y_) = (X[mask_b, :], y[mask_b])
                    n_samples_b = len(X_)
                    if (n_samples_b > 0):
                        children = []
                        for xi in values[X_m]:
                            mask_xi = (X_[:, X_m] == xi)
                            children.append(y_[mask_xi])
                        imp += ((coef * ((1.0 * n_samples_b) / n_samples)) * (entropy(y_) - sum([((entropy(c) * len(c)) / n_samples_b) for c in children])))
        return imp
    data = np.array([[0, 0, 1, 0, 0, 1, 0, 1], [1, 0, 1, 1, 1, 0, 1, 2], [1, 0, 1, 1, 0, 1, 1, 3], [0, 1, 1, 1, 0, 1, 0, 4], [1, 1, 0, 1, 0, 1, 1, 5], [1, 1, 0, 1, 1, 1, 1, 6], [1, 0, 1, 0, 0, 1, 0, 7], [1, 1, 1, 1, 1, 1, 1, 8], [1, 1, 1, 1, 0, 1, 1, 9], [1, 1, 1, 0, 1, 1, 1, 0]])
    (X, y) = (np.array(data[:, :7], dtype=np.bool), data[:, 7])
    n_features = X.shape[1]
    true_importances = np.zeros(n_features)
    for i in range(n_features):
        true_importances[i] = mdi_importance(i, X, y)
    clf = ExtraTreesClassifier(n_estimators=500, max_features=1, criterion='entropy', random_state=0).fit(X, y)
    importances = (sum((tree.tree_.compute_feature_importances(normalize=False) for tree in clf.estimators_)) / clf.n_estimators)
    assert_almost_equal(entropy(y), sum(importances))
    assert_less(np.abs((true_importances - tree)).mean(), 0.01)

dataset/ETHPy150Open scikit-learn/scikit-learn/sklearn/ensemble/tests/test_forest.py test_importances_asymptotic/VarMisuse@787/887 `importances`->`tree`