Skip to content

Commit 79da5b5

Browse files
committed
rename functions
1 parent 3ba3931 commit 79da5b5

6 files changed

+52
-80
lines changed

_unittests/ut_mlmodel/test_piecewise_decision_tree_experiment.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def test_criterions(self):
8686
_test_criterion_init(c2, ys, w, 1.0, ind, 0, y.shape[0])
8787
_test_criterion_check(c1)
8888
_test_criterion_check(c2)
89+
self.assertStartsWith("SimpleRegressorCriterion(", str(c2))
8990
i1 = _test_criterion_node_impurity(c1)
9091
i2 = _test_criterion_node_impurity(c2)
9192
_test_criterion_check(c1)
@@ -109,21 +110,18 @@ def test_criterions(self):
109110
_test_criterion_update(c2, i)
110111
_test_criterion_check(c2)
111112
assert_criterion_equal(c1, c2)
113+
self.assertIsInstance(c2.printd(), str)
112114
left1, right1 = _test_criterion_node_impurity_children(c1)
113115
left2, right2 = _test_criterion_node_impurity_children(c2)
114116
self.assertAlmostEqual(left1, left2)
115117
self.assertAlmostEqual(right1, right2, atol=1e-10)
116118
v1 = _test_criterion_node_value(c1)
117119
v2 = _test_criterion_node_value(c2)
118120
self.assertEqual(v1, v2)
119-
try:
120-
# scikit-learn >= 0.24
121-
p1 = _test_criterion_impurity_improvement(c1, 0.0, left1, right1)
122-
p2 = _test_criterion_impurity_improvement(c2, 0.0, left2, right2)
123-
except TypeError:
124-
# scikit-learn < 0.24
125-
p1 = _test_criterion_impurity_improvement(c1, 0.0)
126-
p2 = _test_criterion_impurity_improvement(c2, 0.0)
121+
p1 = _test_criterion_impurity_improvement(c1, 0.0, left1, right1)
122+
p2 = _test_criterion_impurity_improvement(c2, 0.0, left2, right2)
123+
# print(left1, right1, "*", left2, right2)
124+
# print(c2.printd())
127125
self.assertAlmostEqual(p1, p2, atol=1e-10)
128126

129127
X = numpy.array([[1.0, 2.0, 10.0, 11.0]]).T
@@ -155,14 +153,8 @@ def test_criterions(self):
155153
v1 = _test_criterion_node_value(c1)
156154
v2 = _test_criterion_node_value(c2)
157155
self.assertEqual(v1, v2)
158-
try:
159-
# scikit-learn >= 0.24
160-
p1 = _test_criterion_impurity_improvement(c1, 0.0, left1, right1)
161-
p2 = _test_criterion_impurity_improvement(c2, 0.0, left2, right2)
162-
except TypeError:
163-
# scikit-learn < 0.24
164-
p1 = _test_criterion_impurity_improvement(c1, 0.0)
165-
p2 = _test_criterion_impurity_improvement(c2, 0.0)
156+
p1 = _test_criterion_impurity_improvement(c1, 0.0, left1, right1)
157+
p2 = _test_criterion_impurity_improvement(c2, 0.0, left2, right2)
166158
self.assertAlmostEqual(p1, p2)
167159

168160
def test_decision_tree_criterion(self):

_unittests/ut_mlmodel/test_piecewise_decision_tree_experiment_fast.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,8 @@ def test_criterions(self):
104104
v2 = _test_criterion_node_value(c2)
105105
self.assertEqual(v1, v2)
106106
assert_criterion_equal(c1, c2)
107-
try:
108-
# scikit-learn >= 0.24
109-
p1 = _test_criterion_impurity_improvement(c1, 0.0, left1, right1)
110-
p2 = _test_criterion_impurity_improvement(c2, 0.0, left2, right2)
111-
except TypeError:
112-
# scikit-learn < 0.23
113-
p1 = _test_criterion_impurity_improvement(c1, 0.0)
114-
p2 = _test_criterion_impurity_improvement(c2, 0.0)
115-
107+
p1 = _test_criterion_impurity_improvement(c1, 0.0, left1, right1)
108+
p2 = _test_criterion_impurity_improvement(c2, 0.0, left2, right2)
116109
self.assertAlmostEqual(p1, p2)
117110

118111
X = numpy.array([[1.0, 2.0, 10.0, 11.0]]).T
@@ -144,17 +137,11 @@ def test_criterions(self):
144137
v1 = _test_criterion_node_value(c1)
145138
v2 = _test_criterion_node_value(c2)
146139
self.assertEqual(v1, v2)
147-
try:
148-
# scikit-learn >= 0.24
149-
p1 = _test_criterion_impurity_improvement(c1, 0.0, left1, right1)
150-
p2 = _test_criterion_impurity_improvement(c2, 0.0, left2, right2)
151-
except TypeError:
152-
# scikit-learn < 0.23
153-
p1 = _test_criterion_impurity_improvement(c1, 0.0)
154-
p2 = _test_criterion_impurity_improvement(c2, 0.0)
140+
p1 = _test_criterion_impurity_improvement(c1, 0.0, left1, right1)
141+
p2 = _test_criterion_impurity_improvement(c2, 0.0, left2, right2)
155142
self.assertAlmostEqual(p1, p2)
156143

157-
def test_decision_tree_criterion(self):
144+
def test_decision_tree_criterion_fast(self):
158145
X = numpy.array([[1.0, 2.0, 10.0, 11.0]]).T
159146
y = numpy.array([0.9, 1.1, 1.9, 2.1])
160147
clr1 = DecisionTreeRegressor(max_depth=1)

_unittests/ut_mlmodel/test_piecewise_decision_tree_experiment_linear.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,8 @@ def test_criterions(self):
118118
v1 = _test_criterion_node_value(c1)
119119
v2 = _test_criterion_node_value(c2)
120120
self.assertEqual(v1, v2)
121-
try:
122-
# scikit-learn >= 0.24
123-
p1 = _test_criterion_impurity_improvement(c1, 0.0, left1, right1)
124-
p2 = _test_criterion_impurity_improvement(c2, 0.0, left2, right2)
125-
except TypeError:
126-
# scikit-learn < 0.23
127-
p1 = _test_criterion_impurity_improvement(c1, 0.0)
128-
p2 = _test_criterion_impurity_improvement(c2, 0.0)
121+
p1 = _test_criterion_impurity_improvement(c1, 0.0, left1, right1)
122+
p2 = _test_criterion_impurity_improvement(c2, 0.0, left2, right2)
129123
self.assertGreater(p1, p2 - 1.0)
130124

131125
dest = numpy.empty((2,))
@@ -145,7 +139,7 @@ def test_criterions_check_value(self):
145139
c2.node_beta(coef)
146140
self.assertEqual(coef[:2], numpy.array([1, 10]))
147141

148-
def test_decision_tree_criterion(self):
142+
def test_decision_tree_criterion_linear(self):
149143
X = numpy.array([[1.0, 2.0, 10.0, 11.0]]).T
150144
y = numpy.array([0.9, 1.1, 1.9, 2.1])
151145
clr1 = DecisionTreeRegressor(max_depth=1)

mlinsights/mlmodel/piecewise_tree_regression_criterion.pyx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ cimport numpy as cnp
66
cnp.import_array()
77

88
from sklearn.tree._criterion cimport SIZE_t, DOUBLE_t
9-
from sklearn.tree._criterion cimport Criterion
109
from ._piecewise_tree_regression_common cimport CommonRegressorCriterion
1110

1211

@@ -68,6 +67,13 @@ cdef class SimpleRegressorCriterion(CommonRegressorCriterion):
6867
if self.sample_i == NULL:
6968
self.sample_i = <SIZE_t*> calloc(n_samples, sizeof(SIZE_t))
7069

70+
def __str__(self):
71+
"usual"
72+
return (
73+
f"SimpleRegressorCriterion(n_outputs={self.n_outputs}, "
74+
f"n_samples={self.n_samples})"
75+
)
76+
7177
@cython.boundscheck(False)
7278
cdef int init(self, const DOUBLE_t[:, ::1] y,
7379
const DOUBLE_t[:] sample_weight,
@@ -84,6 +90,21 @@ cdef class SimpleRegressorCriterion(CommonRegressorCriterion):
8490
return self.init_with_X(y, sample_weight, weighted_n_samples,
8591
sample_indices, start, end)
8692

93+
def printd(self):
94+
"debug print"
95+
rows = [str(self)]
96+
rows.append(f" start={self.start}, pos={self.pos}, end={self.end}")
97+
rows.append(f" weighted_n_samples={self.weighted_n_samples}")
98+
rows.append(f" weighted_n_node_samples={self.weighted_n_node_samples}")
99+
rows.append(f" sample_sum_w={self.sample_sum_w}")
100+
rows.append(f" sample_sum_wy={self.sample_sum_wy}")
101+
rows.append(f" weighted_n_left={self.weighted_n_left} "
102+
f"weighted_n_right={self.weighted_n_right}")
103+
for ki in range(self.start, self.end):
104+
rows.append(f" ki={ki}, sample_i={self.sample_i[ki]}, "
105+
f"sample_w={self.sample_w[ki]}, sample_wy={self.sample_wy[ki]}")
106+
return "\n".join(rows)
107+
87108
@cython.boundscheck(False)
88109
cdef int init_with_X(self,
89110
const DOUBLE_t[:, ::1] y,

mlinsights/mlmodel/quantile_regression.py

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,13 @@ def __init__(
5959
positive=False,
6060
verbose=False,
6161
):
62-
try:
63-
LinearRegression.__init__(
64-
self,
65-
fit_intercept=fit_intercept,
66-
copy_X=copy_X,
67-
n_jobs=n_jobs,
68-
positive=positive,
69-
)
70-
except TypeError:
71-
# scikit-learn<0.24
72-
LinearRegression.__init__(
73-
self, fit_intercept=fit_intercept, copy_X=copy_X, n_jobs=n_jobs
74-
)
62+
LinearRegression.__init__(
63+
self,
64+
fit_intercept=fit_intercept,
65+
copy_X=copy_X,
66+
n_jobs=n_jobs,
67+
positive=positive,
68+
)
7569
self.max_iter = max_iter
7670
self.verbose = verbose
7771
self.delta = delta
@@ -138,18 +132,12 @@ def compute_z(Xm, beta, Y, W, delta=0.0001):
138132
else:
139133
Xm = X
140134

141-
try:
142-
clr = LinearRegression(
143-
fit_intercept=False,
144-
copy_X=self.copy_X,
145-
n_jobs=self.n_jobs,
146-
positive=self.positive,
147-
)
148-
except AttributeError:
149-
# scikit-learn<0.24
150-
clr = LinearRegression(
151-
fit_intercept=False, copy_X=self.copy_X, n_jobs=self.n_jobs
152-
)
135+
clr = LinearRegression(
136+
fit_intercept=False,
137+
copy_X=self.copy_X,
138+
n_jobs=self.n_jobs,
139+
positive=self.positive,
140+
)
153141

154142
W = numpy.ones(X.shape[0]) if sample_weight is None else sample_weight
155143
self.n_iter_ = 0

mlinsights/mltree/tree_digitize.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,5 @@ def add_nodes(parent, i, j, is_left):
137137
cl.tree_.value[:, 0, 0] = numpy.array(values, dtype=numpy.float64)
138138
cl.n_outputs = 1
139139
cl.n_outputs_ = 1
140-
try:
141-
# scikit-learn >= 0.24
142-
cl.n_features_in_ = 1
143-
except AttributeError:
144-
# scikit-learn < 0.24
145-
cl.n_features_ = 1
146-
try:
147-
# for scikit-learn<=0.23.2
148-
cl.n_features_ = 1
149-
except AttributeError:
150-
pass
140+
cl.n_features_in_ = 1
151141
return cl

0 commit comments

Comments
 (0)