Skip to content

Commit 04a5437

Browse files
committed
minor changes
1 parent 8351104 commit 04a5437

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

sklearn/tree/_criterion.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ cdef class ClassificationCriterion(Criterion):
279279
def __reduce__(self):
280280
return (type(self),
281281
(self.n_outputs,
282-
sizet_ptr_to_ndarray(self.n_classes, self.n_outputs).copy()),
282+
sizet_ptr_to_ndarray(self.n_classes, self.n_outputs)),
283283
self.__getstate__())
284284

285285
cdef void init(self, DOUBLE_t* y, SIZE_t y_stride,

sklearn/tree/_tree.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ cdef class Tree:
548548
property n_classes:
549549
def __get__(self):
550550
# it's small; copy for memory safety
551-
return sizet_ptr_to_ndarray(self.n_classes, self.n_outputs).copy()
551+
return sizet_ptr_to_ndarray(self.n_classes, self.n_outputs)
552552

553553
property children_left:
554554
def __get__(self):
@@ -615,7 +615,7 @@ cdef class Tree:
615615
def __reduce__(self):
616616
"""Reduce re-implementation, for pickling."""
617617
return (Tree, (self.n_features,
618-
sizet_ptr_to_ndarray(self.n_classes, self.n_outputs).copy(),
618+
sizet_ptr_to_ndarray(self.n_classes, self.n_outputs),
619619
self.n_outputs), self.__getstate__())
620620

621621
def __getstate__(self):

sklearn/tree/_utils.pyx

+2-5
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,10 @@ cdef inline UINT32_t our_rand_r(UINT32_t* seed) nogil:
6262

6363

6464
cdef inline np.ndarray sizet_ptr_to_ndarray(SIZE_t* data, SIZE_t size):
65-
"""Encapsulate data into a 1D numpy array of intp's. Make sure memory allocated in data is
66-
You should ensure that the provided data pointer is not freed while the returned array
67-
is in existence.
68-
"""
65+
"""Encapsulate data into a 1D numpy array of intp's."""
6966
cdef np.npy_intp shape[1]
7067
shape[0] = <np.npy_intp> size
71-
return np.PyArray_SimpleNewFromData(1, shape, np.NPY_INTP, data)
68+
return np.PyArray_SimpleNewFromData(1, shape, np.NPY_INTP, data).copy()
7269

7370

7471
cdef inline SIZE_t rand_int(SIZE_t low, SIZE_t high,

sklearn/tree/tests/test_tree.py

+21-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""
22
Testing for the tree module (sklearn.tree).
33
"""
4+
import copy
45
import pickle
5-
from copy import copy
66
from functools import partial
77
from itertools import product
88
import struct
@@ -1600,6 +1600,7 @@ def test_no_sparse_y_support():
16001600
for name in ALL_TREES:
16011601
yield (check_no_sparse_y_support, name)
16021602

1603+
16031604
def test_mae():
16041605
# check MAE criterion produces correct results
16051606
# on small toy dataset
@@ -1613,21 +1614,26 @@ def test_mae():
16131614
assert_array_equal(dt_mae.tree_.impurity, [7.0/2.3, 3.0/0.7, 4.0/1.6])
16141615
assert_array_equal(dt_mae.tree_.value.flat, [4.0, 6.0, 4.0])
16151616

1617+
16161618
def test_criterion_copy():
1617-
# Let's check whether copy of our criterion has same type and properties as original
1619+
# Let's check whether copy of our criterion has the same type
1620+
# and properties as original
16181621
n_outputs = 3
16191622
n_classes = np.arange(3)
1620-
for _, typename in CRITERIA_CLF.items():
1621-
criteria = typename(n_outputs, n_classes)
1622-
typename_, (n_outputs_, n_classes_), _ = copy(criteria).__reduce__()
1623-
assert_equal(typename, typename_)
1624-
assert_equal(n_outputs, n_outputs_)
1625-
assert_array_equal(n_classes, n_classes_)
1626-
16271623
n_samples = 100
1628-
for _, typename in CRITERIA_REG.items():
1629-
criteria = typename(n_outputs, n_samples)
1630-
typename_, (n_outputs_, n_samples_), _ = copy(criteria).__reduce__()
1631-
assert_equal(typename, typename_)
1632-
assert_equal(n_outputs, n_outputs_)
1633-
assert_equal(n_samples, n_samples_)
1624+
for copy_func in [copy.copy, copy.deepcopy]:
1625+
for _, typename in CRITERIA_CLF.items():
1626+
criteria = typename(n_outputs, n_classes)
1627+
result = copy_func(criteria).__reduce__()
1628+
typename_, (n_outputs_, n_classes_), _ = result
1629+
assert_equal(typename, typename_)
1630+
assert_equal(n_outputs, n_outputs_)
1631+
assert_array_equal(n_classes, n_classes_)
1632+
1633+
for _, typename in CRITERIA_REG.items():
1634+
criteria = typename(n_outputs, n_samples)
1635+
result = copy_func(criteria).__reduce__()
1636+
typename_, (n_outputs_, n_samples_), _ = result
1637+
assert_equal(typename, typename_)
1638+
assert_equal(n_outputs, n_outputs_)
1639+
assert_equal(n_samples, n_samples_)

0 commit comments

Comments
 (0)