|
1 | | -from effector.space_partitioning import Best |
| 1 | +from effector.space_partitioning import * |
2 | 2 | import numpy as np |
3 | 3 |
|
4 | | -np.random.seed(0) |
5 | | -N = 1000 |
6 | | -D = 3 |
7 | | -# Generate features uniformly in [0, 10]. |
8 | | -X = np.random.uniform(0, 10, size=(N, D)) |
9 | | - |
10 | | -# Create a target variable y with four groups. |
11 | | -# Group 1: x2 < 3 and x3 < 5 -> label 0 |
12 | | -# Group 2: x2 < 3 and x3 >= 5 -> label 1 |
13 | | -# Group 3: x2 >= 3 and x2 < 5 -> label 2 |
14 | | -# Group 4: x2 >= 3 and x2 >= 5 -> label 3 |
15 | | -y = np.empty(N, dtype=int) |
16 | | -for i in range(N): |
17 | | - if X[i, 1] < 3: |
18 | | - y[i] = 0 if X[i, 1] < 1.5 else 1 |
19 | | - else: |
20 | | - y[i] = 2 if X[i, 1] < 5 else 3 |
21 | | - |
22 | | -# Define a heterogeneity function (Gini impurity) that uses the target y. |
23 | | -def heterogeneity(mask): |
24 | | - indices = np.where(mask)[0] |
25 | | - if len(indices) < 50: |
26 | | - return 10000000000 |
27 | | - labels = y[indices] |
28 | | - classes, counts = np.unique(labels, return_counts=True) |
29 | | - p = counts / counts.sum() |
30 | | - return 1 - np.sum(p ** 2) |
31 | | - |
32 | | -# Set axis limits (min and max for each feature). |
33 | | -axis_limits = np.array([[0, 10], [0, 10], [0, 10]]).T |
34 | | - |
35 | | -# We want to allow splits on x1 and x2. To do so, we choose the primary feature as x3 (index 2) |
36 | | -# and explicitly pass candidate conditioning features [0, 1]. |
37 | | -best = Best( |
38 | | - min_heterogeneity_decrease_pcg=0.1, |
39 | | - heter_small_enough=0.0, |
40 | | - max_depth=2, |
41 | | - min_samples_leaf=10, |
42 | | - numerical_features_grid_size=20, |
43 | | - search_partitions_when_categorical=False, |
44 | | -) |
45 | | - |
46 | | -best.compile( |
47 | | - feature=0, # primary feature (x3) -- not used for splitting in this test. |
48 | | - data=X, |
49 | | - heter_func=heterogeneity, |
50 | | - axis_limits=axis_limits, |
51 | | - candidate_conditioning_features=[0, 1, 2], |
52 | | - feature_names=["x1", "x2", "x3"], |
53 | | - target_name="y" |
54 | | -) |
55 | | -tree = best.fit() |
56 | | - |
57 | | -print("Constructed Tree:") |
58 | | -print(tree) |
59 | | - |
60 | | -tree.show_full_tree() |
| 4 | + |
| 5 | +def test_space_partitioning(): |
| 6 | + np.random.seed(0) |
| 7 | + N = 1000 |
| 8 | + D = 3 |
| 9 | + # Generate features uniformly in [0, 10]. |
| 10 | + X = np.random.uniform(0, 10, size=(N, D)) |
| 11 | + |
| 12 | + # Create a target variable y with four groups. |
| 13 | + # Group 1: x2 < 3 and x3 < 5 -> label 0 |
| 14 | + # Group 2: x2 < 3 and x3 >= 5 -> label 1 |
| 15 | + # Group 3: x2 >= 3 and x2 < 5 -> label 2 |
| 16 | + # Group 4: x2 >= 3 and x2 >= 5 -> label 3 |
| 17 | + y = np.empty(N, dtype=int) |
| 18 | + for i in range(N): |
| 19 | + if X[i, 1] < 3: |
| 20 | + y[i] = 0 if X[i, 1] < 1.5 else 1 |
| 21 | + else: |
| 22 | + y[i] = 2 if X[i, 1] < 5 else 3 |
| 23 | + |
| 24 | + # Define a heterogeneity function (Gini impurity) that uses the target y. |
| 25 | + def heterogeneity(mask): |
| 26 | + indices = np.where(mask)[0] |
| 27 | + if len(indices) < 50: |
| 28 | + return 10000000000 |
| 29 | + labels = y[indices] |
| 30 | + classes, counts = np.unique(labels, return_counts=True) |
| 31 | + p = counts / counts.sum() |
| 32 | + return 1 - np.sum(p**2) |
| 33 | + |
| 34 | + def parent_heter_lower(node, is_lower): |
| 35 | + if not is_lower: |
| 36 | + return False |
| 37 | + if node.parent_node is None: |
| 38 | + return is_lower |
| 39 | + |
| 40 | + return parent_heter_lower( |
| 41 | + node.parent_node, |
| 42 | + node.info["weighted_heterogeneity"] |
| 43 | + <= node.parent_node.info["weighted_heterogeneity"], |
| 44 | + ) |
| 45 | + |
| 46 | + # Set axis limits (min and max for each feature). |
| 47 | + axis_limits = np.array([[0, 10], [0, 10], [0, 10]]).T |
| 48 | + |
| 49 | + # We want to allow splits on x1 and x2. To do so, we choose the primary feature as x3 (index 2) |
| 50 | + # and explicitly pass candidate conditioning features [0, 1]. |
| 51 | + best = Best( |
| 52 | + min_heterogeneity_decrease_pcg=0.1, |
| 53 | + heter_small_enough=0.0, |
| 54 | + max_depth=2, |
| 55 | + min_samples_leaf=10, |
| 56 | + numerical_features_grid_size=20, |
| 57 | + search_partitions_when_categorical=False, |
| 58 | + ) |
| 59 | + |
| 60 | + best.compile( |
| 61 | + feature=0, # primary feature (x3) -- not used for splitting in this test. |
| 62 | + data=X, |
| 63 | + heter_func=heterogeneity, |
| 64 | + axis_limits=axis_limits, |
| 65 | + candidate_conditioning_features=[0, 1, 2], |
| 66 | + feature_names=["x1", "x2", "x3"], |
| 67 | + target_name="y", |
| 68 | + ) |
| 69 | + tree = best.fit() |
| 70 | + |
| 71 | + # tree.show_full_tree() |
| 72 | + |
| 73 | + assert tree is not None |
| 74 | + |
| 75 | + heter_decreasing_per_level = all([parent_heter_lower(n, True) for n in tree.nodes]) |
| 76 | + assert heter_decreasing_per_level |
| 77 | + |
| 78 | + ############################ |
| 79 | + |
| 80 | + best_level_wise = BestLevelWise( |
| 81 | + min_heterogeneity_decrease_pcg=0.1, |
| 82 | + heter_small_enough=0.0, |
| 83 | + max_depth=2, |
| 84 | + min_samples_leaf=10, |
| 85 | + numerical_features_grid_size=20, |
| 86 | + search_partitions_when_categorical=False, |
| 87 | + ) |
| 88 | + |
| 89 | + best_level_wise.compile( |
| 90 | + feature=0, # primary feature (x3) -- not used for splitting in this test. |
| 91 | + data=X, |
| 92 | + heter_func=heterogeneity, |
| 93 | + axis_limits=axis_limits, |
| 94 | + candidate_conditioning_features=[0, 1, 2], |
| 95 | + feature_names=["x1", "x2", "x3"], |
| 96 | + target_name="y", |
| 97 | + ) |
| 98 | + tree = best_level_wise.fit() |
| 99 | + |
| 100 | + assert tree is not None |
| 101 | + |
| 102 | + heter_decreasing_per_level = all([parent_heter_lower(n, True) for n in tree.nodes]) |
| 103 | + assert heter_decreasing_per_level |
0 commit comments