Repository: scikit-learn/scikit-learn
File: sklearn/tree/tests/test_tree.py (Python)

Code before:
        for expected_decrease in [0.05, 0.0001, 0.1]:
            est = TreeEstimator(
                criterion=criterion,
                max_leaf_nodes=max_leaf_nodes,
                min_impurity_decrease=expected_decrease,
                random_state=global_random_seed,
            )
            est.fit(X, y)
            tree = est.tree_
            weighted_impurity = (
                tree.impurity * tree.weighted_n_node_samples / X.shape[0]
            )

            for node in range(tree.node_count):
                # If current node is a not leaf node, check if the split was
                # justified w.r.t the min_impurity_decrease
                if tree.children_left[node] != TREE_LEAF:
                    left = tree.children_left[node]
                    right = tree.children_right[node]

                    actual_decrease = weighted_impurity[node] - (
                        weighted_impurity[left] + weighted_impurity[right]
                    )

                    # Allow a tiny slack to account for
                    # floating-point rounding errors:
                    assert actual_decrease > expected_decrease - 1e-10


def test_pickle():
    """Test pickling preserves Tree properties and performance."""
    for name, TreeEstimator in ALL_TREES.items():
        if "Classifier" in name:
            X, y = iris.data, iris.target
        else:
            X, y = diabetes.data, diabetes.target

        est = TreeEstimator(random_state=0)
        est.fit(X, y)
        score = est.score(X, y)

        # test that all class properties are maintained
        attributes = [
            "max_depth",
            "node_count",
            "capacity",
            "n_classes",
            "children_left",
            "children_right",
            "n_leaves",
            "feature",

Review comment (suggestion):
nit:

```suggestion
                    # Allow a tiny slack to account for floating-point rounding errors:
```

Code after:
        for expected_decrease in [0.05, 0.0001, 0.1]:
            est = TreeEstimator(
                criterion=criterion,
                max_leaf_nodes=max_leaf_nodes,
                min_impurity_decrease=expected_decrease,
                random_state=global_random_seed,
            )
            est.fit(X, y)
            tree = est.tree_
            weighted_impurity = (
                tree.impurity * tree.weighted_n_node_samples / X.shape[0]
            )

            for node in range(tree.node_count):
                # If current node is a not leaf node, check if the split was
                # justified w.r.t the min_impurity_decrease
                if tree.children_left[node] != TREE_LEAF:
                    left = tree.children_left[node]
                    right = tree.children_right[node]

                    actual_decrease = weighted_impurity[node] - (
                        weighted_impurity[left] + weighted_impurity[right]
                    )

                    # Allow a tiny slack to account for floating-point rounding errors:
                    assert actual_decrease > expected_decrease - 1e-10


def test_pickle():
    """Test pickling preserves Tree properties and performance."""
    for name, TreeEstimator in ALL_TREES.items():
        if "Classifier" in name:
            X, y = iris.data, iris.target
        else:
            X, y = diabetes.data, diabetes.target

        est = TreeEstimator(random_state=0)
        est.fit(X, y)
        score = est.score(X, y)

        # test that all class properties are maintained
        attributes = [
            "max_depth",
            "node_count",
            "capacity",
            "n_classes",
            "children_left",
            "children_right",
            "n_leaves",
            "feature",
            "threshold",