Aluode/PerceptionLabPortable
0
1# Authors: The scikit-learn developers
2# SPDX-License-Identifier: BSD-3-Clause
3
4# See _tree.pyx for details.
5
6import numpy as np
7cimport numpy as cnp
8
9from ..utils._typedefs cimport float32_t, float64_t, intp_t, int32_t, uint8_t, uint32_t
10
11from ._splitter cimport Splitter
12from ._splitter cimport SplitRecord
13
14cdef struct Node:
15 # Base storage structure for the nodes in a Tree object
16
17 intp_t left_child # id of the left child of the node
18 intp_t right_child # id of the right child of the node
19 intp_t feature # Feature used for splitting the node
20 float64_t threshold # Threshold value at the node
21 float64_t impurity # Impurity of the node (i.e., the value of the criterion)
22 intp_t n_node_samples # Number of samples at the node
23 float64_t weighted_n_node_samples # Weighted number of samples at the node
24 uint8_t missing_go_to_left # Whether features have missing values
25
26
27cdef struct ParentInfo:
28 # Structure to store information about the parent of a node
29 # This is passed to the splitter, to provide information about the previous split
30
31 float64_t lower_bound # the lower bound of the parent's impurity
32 float64_t upper_bound # the upper bound of the parent's impurity
33 float64_t impurity # the impurity of the parent
34 intp_t n_constant_features # the number of constant features found in parent
35
36cdef class Tree:
37 # The Tree object is a binary tree structure constructed by the
38 # TreeBuilder. The tree structure is used for predictions and
39 # feature importances.
40
41 # Input/Output layout
42 cdef public intp_t n_features # Number of features in X
43 cdef intp_t* n_classes # Number of classes in y[:, k]
44 cdef public intp_t n_outputs # Number of outputs in y
45 cdef public intp_t max_n_classes # max(n_classes)
46
47 # Inner structures: values are stored separately from node structure,
48 # since size is determined at runtime.
49 cdef public intp_t max_depth # Max depth of the tree
50 cdef public intp_t node_count # Counter for node IDs
51 cdef public intp_t capacity # Capacity of tree, in terms of nodes
52 cdef Node* nodes # Array of nodes
53 cdef float64_t* value # (capacity, n_outputs, max_n_classes) array of values
54 cdef intp_t value_stride # = n_outputs * max_n_classes
55
56 # Methods
57 cdef intp_t _add_node(self, intp_t parent, bint is_left, bint is_leaf,
58 intp_t feature, float64_t threshold, float64_t impurity,
59 intp_t n_node_samples,
60 float64_t weighted_n_node_samples,
61 uint8_t missing_go_to_left) except -1 nogil
62 cdef int _resize(self, intp_t capacity) except -1 nogil
63 cdef int _resize_c(self, intp_t capacity=*) except -1 nogil
64
65 cdef cnp.ndarray _get_value_ndarray(self)
66 cdef cnp.ndarray _get_node_ndarray(self)
67
68 cpdef cnp.ndarray predict(self, object X)
69
70 cpdef cnp.ndarray apply(self, object X)
71 cdef cnp.ndarray _apply_dense(self, object X)
72 cdef cnp.ndarray _apply_sparse_csr(self, object X)
73
74 cpdef object decision_path(self, object X)
75 cdef object _decision_path_dense(self, object X)
76 cdef object _decision_path_sparse_csr(self, object X)
77
78 cpdef compute_node_depths(self)
79 cpdef compute_feature_importances(self, normalize=*)
80
81
82# =============================================================================
83# Tree builder
84# =============================================================================
85
86cdef class TreeBuilder:
87 # The TreeBuilder recursively builds a Tree object from training samples,
88 # using a Splitter object for splitting internal nodes and assigning
89 # values to leaves.
90 #
91 # This class controls the various stopping criteria and the node splitting
92 # evaluation order, e.g. depth-first or best-first.
93
94 cdef Splitter splitter # Splitting algorithm
95
96 cdef intp_t min_samples_split # Minimum number of samples in an internal node
97 cdef intp_t min_samples_leaf # Minimum number of samples in a leaf
98 cdef float64_t min_weight_leaf # Minimum weight in a leaf
99 cdef intp_t max_depth # Maximal tree depth
100 cdef float64_t min_impurity_decrease # Impurity threshold for early stopping
101
102 cpdef build(
103 self,
104 Tree tree,
105 object X,
106 const float64_t[:, ::1] y,
107 const float64_t[:] sample_weight=*,
108 const uint8_t[::1] missing_values_in_feature_mask=*,
109 )
110
111 cdef _check_input(
112 self,
113 object X,
114 const float64_t[:, ::1] y,
115 const float64_t[:] sample_weight,
116 )
117
118
119# =============================================================================
120# Tree pruning
121# =============================================================================
122
123# The private function allows any external caller to prune the tree and return
124# a new tree with the pruned nodes. The pruned tree is a new tree object.
125#
126# .. warning:: this function is not backwards compatible and may change without
127# notice.
128cdef void _build_pruned_tree(
129 Tree tree, # OUT
130 Tree orig_tree,
131 const uint8_t[:] leaves_in_subtree,
132 intp_t capacity
133)
134 