|
| 1 | +import numpy as np |
| 2 | +from sklearn.base import RegressorMixin, ClassifierMixin |
| 3 | +from .mobtree import MoBTreeRegressor, MoBTreeClassifier |
| 4 | + |
| 5 | + |
| 6 | +__all__ = ["CARTRegressor", "CARTClassifier"] |
| 7 | + |
| 8 | + |
| 9 | +class CARTRegressor(MoBTreeRegressor, RegressorMixin): |
| 10 | + |
| 11 | + def __init__(self, max_depth=3, min_samples_leaf=50, min_impurity_decrease=0, |
| 12 | + split_features=None, feature_names=None, random_state=0): |
| 13 | + |
| 14 | + super(CARTRegressor, self).__init__(max_depth=max_depth, |
| 15 | + min_samples_leaf=min_samples_leaf, |
| 16 | + min_impurity_decrease=min_impurity_decrease, |
| 17 | + split_features=split_features, |
| 18 | + feature_names=feature_names, |
| 19 | + random_state=random_state) |
| 20 | + |
| 21 | + def build_root(self): |
| 22 | + |
| 23 | + root_impurity = self.y.var() |
| 24 | + return root_impurity |
| 25 | + |
| 26 | + def build_leaf(self, sample_indice): |
| 27 | + |
| 28 | + best_estimator = None |
| 29 | + predict_func = lambda x: np.mean(self.y[sample_indice]) |
| 30 | + best_impurity = self.y[sample_indice].var() |
| 31 | + return predict_func, best_estimator, best_impurity |
| 32 | + |
| 33 | + def node_split(self, sample_indice): |
| 34 | + |
| 35 | + node_x = self.x[sample_indice] |
| 36 | + node_y = self.y[sample_indice] |
| 37 | + n_samples, n_features = node_x.shape |
| 38 | + |
| 39 | + best_impurity = np.inf |
| 40 | + best_feature = None |
| 41 | + best_threshold = None |
| 42 | + best_left_indice = None |
| 43 | + best_right_indice = None |
| 44 | + for feature_indice in self.split_features: |
| 45 | + |
| 46 | + current_feature = node_x[:, feature_indice] |
| 47 | + sortted_indice = np.argsort(current_feature) |
| 48 | + sortted_feature = current_feature[sortted_indice] |
| 49 | + feature_range = sortted_feature[-1] - sortted_feature[0] |
| 50 | + if feature_range < self.EPSILON: |
| 51 | + continue |
| 52 | + |
| 53 | + sum_left = 0 |
| 54 | + sum_total = np.sum(node_y) |
| 55 | + sq_sum_total = np.sum(node_y ** 2) |
| 56 | + for i, _ in enumerate(sortted_indice): |
| 57 | + |
| 58 | + if ((i + 1) < self.min_samples_leaf) or ((n_samples - i - 1) < self.min_samples_leaf): |
| 59 | + continue |
| 60 | + |
| 61 | + n_left = i + 1 |
| 62 | + n_right = n_samples - i - 1 |
| 63 | + sum_left += node_y[sortted_indice[i]] |
| 64 | + current_impurity = (sq_sum_total / n_samples - (sum_left / n_left) ** 2 * n_left / n_samples - |
| 65 | + ((sum_total - sum_left) / n_right) ** 2 * n_right / n_samples) |
| 66 | + |
| 67 | + if current_impurity < best_impurity: |
| 68 | + best_position = i + 1 |
| 69 | + best_feature = feature_indice |
| 70 | + best_impurity = current_impurity |
| 71 | + best_threshold = (sortted_feature[i] + sortted_feature[i + 1]) / 2 |
| 72 | + |
| 73 | + sortted_indice = np.argsort(node_x[:, best_feature]) |
| 74 | + best_left_indice = sample_indice[sortted_indice[:best_position]] |
| 75 | + best_right_indice = sample_indice[sortted_indice[best_position:]] |
| 76 | + best_left_impurity = node_y[sortted_indice[:best_position]].var() |
| 77 | + best_right_impurity = node_y[sortted_indice[best_position:]].var() |
| 78 | + node = {"feature": best_feature, "threshold": best_threshold, "left": best_left_indice, "right": best_right_indice, |
| 79 | + "impurity": best_impurity, "left_impurity": best_left_impurity, "right_impurity": best_right_impurity} |
| 80 | + return node |
| 81 | + |
| 82 | + |
| 83 | +class CARTClassifier(MoBTreeClassifier, ClassifierMixin): |
| 84 | + |
| 85 | + def __init__(self, max_depth=3, min_samples_leaf=50, min_impurity_decrease=0, |
| 86 | + split_features=None, feature_names=None, random_state=0): |
| 87 | + |
| 88 | + super(CARTClassifier, self).__init__(max_depth=max_depth, |
| 89 | + min_samples_leaf=min_samples_leaf, |
| 90 | + min_impurity_decrease=min_impurity_decrease, |
| 91 | + split_features=split_features, |
| 92 | + feature_names=feature_names, |
| 93 | + random_state=random_state) |
| 94 | + |
| 95 | + def build_root(self): |
| 96 | + |
| 97 | + p = self.y.mean() |
| 98 | + root_impurity = - p * np.log2(p) - (1 - p) * np.log2((1 - p)) if (p > 0) and (p < 1) else 0 |
| 99 | + return root_impurity |
| 100 | + |
| 101 | + def build_leaf(self, sample_indice): |
| 102 | + |
| 103 | + best_estimator = None |
| 104 | + predict_func = lambda x: np.ones(x.shape[0]) * self.y[sample_indice].mean() |
| 105 | + best_impurity = self.get_loss(self.y[sample_indice], predict_func(self.x[sample_indice])) |
| 106 | + return predict_func, best_estimator, best_impurity |
| 107 | + |
| 108 | + def node_split(self, sample_indice): |
| 109 | + |
| 110 | + node_x = self.x[sample_indice] |
| 111 | + node_y = self.y[sample_indice] |
| 112 | + n_samples, n_features = node_x.shape |
| 113 | + |
| 114 | + best_feature = None |
| 115 | + best_position = None |
| 116 | + best_threshold = None |
| 117 | + best_left_indice = None |
| 118 | + best_right_indice = None |
| 119 | + best_impurity = np.inf |
| 120 | + best_left_impurity = np.inf |
| 121 | + best_right_impurity = np.inf |
| 122 | + for feature_indice in self.split_features: |
| 123 | + |
| 124 | + current_feature = node_x[:, feature_indice] |
| 125 | + sortted_indice = np.argsort(current_feature) |
| 126 | + sortted_feature = current_feature[sortted_indice] |
| 127 | + feature_range = sortted_feature[-1] - sortted_feature[0] |
| 128 | + if feature_range < self.EPSILON: |
| 129 | + continue |
| 130 | + |
| 131 | + sum_left = 0 |
| 132 | + sum_total = np.sum(node_y) |
| 133 | + for i, _ in enumerate(sortted_indice): |
| 134 | + |
| 135 | + if ((i + 1) < self.min_samples_leaf) or ((n_samples - i - 1) < self.min_samples_leaf): |
| 136 | + continue |
| 137 | + |
| 138 | + n_left = i + 1 |
| 139 | + n_right = n_samples - i - 1 |
| 140 | + sum_left += node_y[sortted_indice[i]] |
| 141 | + |
| 142 | + left_impurity = 0 |
| 143 | + right_impurity = 0 |
| 144 | + pleft = sum_left / n_left |
| 145 | + pright = (sum_total - sum_left) / n_right |
| 146 | + if (pleft > 0) and (pleft < 1): |
| 147 | + left_impurity = (- pleft * np.log2(pleft) - (1 - pleft) * np.log2((1 - pleft))) |
| 148 | + if (pright > 0) and (pright < 1): |
| 149 | + right_impurity = (- pright * np.log2(pright) - (1 - pright) * np.log2((1 - pright))) |
| 150 | + current_impurity = (n_left / n_samples * left_impurity + n_right / n_samples * right_impurity) |
| 151 | + |
| 152 | + if current_impurity < best_impurity: |
| 153 | + best_position = i + 1 |
| 154 | + best_feature = feature_indice |
| 155 | + best_impurity = current_impurity |
| 156 | + best_threshold = (sortted_feature[i] + sortted_feature[i + 1]) / 2 |
| 157 | + |
| 158 | + if best_position is not None: |
| 159 | + sortted_indice = np.argsort(node_x[:, best_feature]) |
| 160 | + best_left_indice = sample_indice[sortted_indice[:best_position]] |
| 161 | + best_right_indice = sample_indice[sortted_indice[best_position:]] |
| 162 | + |
| 163 | + pleft = node_y[sortted_indice[:best_position]].mean() |
| 164 | + pright = node_y[sortted_indice[best_position:]].mean() |
| 165 | + best_left_impurity = - pleft * np.log2(pleft) - (1 - pleft) * np.log2((1 - pleft)) if (pleft > 0) and (pleft < 1) else 0 |
| 166 | + best_right_impurity = - pright * np.log2(pright) - (1 - pright) * np.log2((1 - pright)) if (pright > 0) and (pright < 1) else 0 |
| 167 | + node = {"feature": best_feature, "threshold": best_threshold, "left": best_left_indice, "right": best_right_indice, |
| 168 | + "impurity": best_impurity, "left_impurity": best_left_impurity, "right_impurity": best_right_impurity} |
| 169 | + return node |
0 commit comments