scikit-learn/sklearn/tree/_tree.pxd

103 lines
4.3 KiB
Cython
Raw Normal View History

2013-07-09 15:03:00 +08:00
# Authors: Gilles Louppe <g.louppe@gmail.com>
# Peter Prettenhofer <peter.prettenhofer@gmail.com>
# Brian Holt <bdholt1@gmail.com>
# Joel Nothman <joel.nothman@gmail.com>
2014-02-19 22:46:34 +08:00
# Arnaud Joly <arnaud.v.joly@gmail.com>
2015-09-09 03:07:30 +08:00
# Jacob Schreiber <jmschreiber91@gmail.com>
2014-02-19 22:46:34 +08:00
#
# Licence: BSD 3 clause
2012-07-16 19:13:11 +08:00
# See _tree.pyx for details.
import numpy as np
2012-07-16 17:48:32 +08:00
cimport numpy as np
ctypedef np.npy_float32 DTYPE_t # Type of X
ctypedef np.npy_float64 DOUBLE_t # Type of y, sample_weight
ctypedef np.npy_intp SIZE_t # Type for indices and counters
2013-10-09 19:31:36 +08:00
ctypedef np.npy_int32 INT32_t # Signed 32 bit integer
ctypedef np.npy_uint32 UINT32_t # Unsigned 32 bit integer
2012-07-16 17:48:32 +08:00
2015-09-09 03:07:30 +08:00
from ._splitter cimport Splitter
from ._splitter cimport SplitRecord
cdef struct Node:
2014-03-23 00:41:33 +08:00
# Base storage structure for the nodes in a Tree object
SIZE_t left_child # id of the left child of the node
SIZE_t right_child # id of the right child of the node
SIZE_t feature # Feature used for splitting the node
DOUBLE_t threshold # Threshold value at the node
DOUBLE_t impurity # Impurity of the node (i.e., the value of the criterion)
SIZE_t n_node_samples # Number of samples at the node
DOUBLE_t weighted_n_node_samples # Weighted number of samples at the node
2012-07-16 17:48:32 +08:00
cdef class Tree:
2014-03-21 22:34:31 +08:00
# The Tree object is a binary tree structure constructed by the
# TreeBuilder. The tree structure is used for predictions and
# feature importances.
2012-07-16 17:48:32 +08:00
# Input/Output layout
cdef public SIZE_t n_features # Number of features in X
cdef SIZE_t* n_classes # Number of classes in y[:, k]
cdef public SIZE_t n_outputs # Number of outputs in y
cdef public SIZE_t max_n_classes # max(n_classes)
2012-07-16 17:48:32 +08:00
# Inner structures: values are stored separately from node structure,
2014-01-29 23:32:33 +08:00
# since size is determined at runtime.
cdef public SIZE_t max_depth # Max depth of the tree
cdef public SIZE_t node_count # Counter for node IDs
cdef public SIZE_t capacity # Capacity of tree, in terms of nodes
cdef Node* nodes # Array of nodes
cdef double* value # (capacity, n_outputs, max_n_classes) array of values
cdef SIZE_t value_stride # = n_outputs * max_n_classes
2012-07-16 17:48:32 +08:00
# Methods
2014-03-19 00:14:20 +08:00
cdef SIZE_t _add_node(self, SIZE_t parent, bint is_left, bint is_leaf,
SIZE_t feature, double threshold, double impurity,
SIZE_t n_node_samples,
double weighted_n_samples) nogil
cdef void _resize(self, SIZE_t capacity) except *
cdef int _resize_c(self, SIZE_t capacity=*) nogil
2014-01-24 04:50:50 +08:00
cdef np.ndarray _get_value_ndarray(self)
cdef np.ndarray _get_node_ndarray(self)
cpdef np.ndarray predict(self, object X)
2015-10-20 19:15:24 +08:00
cpdef np.ndarray apply(self, object X)
cdef np.ndarray _apply_dense(self, object X)
cdef np.ndarray _apply_sparse_csr(self, object X)
cpdef object decision_path(self, object X)
cdef object _decision_path_dense(self, object X)
cdef object _decision_path_sparse_csr(self, object X)
2015-10-20 19:15:24 +08:00
cpdef compute_feature_importances(self, normalize=*)
# =============================================================================
# Tree builder
# =============================================================================
cdef class TreeBuilder:
2014-03-23 00:41:33 +08:00
# The TreeBuilder recursively builds a Tree object from training samples,
# using a Splitter object for splitting internal nodes and assigning
# values to leaves.
2014-03-21 22:34:31 +08:00
#
2014-03-23 00:41:33 +08:00
# This class controls the various stopping criteria and the node splitting
2014-03-21 22:48:39 +08:00
# evaluation order, e.g. depth-first or best-first.
2014-03-21 22:34:31 +08:00
cdef Splitter splitter # Splitting algorithm
cdef SIZE_t min_samples_split # Minimum number of samples in an internal node
cdef SIZE_t min_samples_leaf # Minimum number of samples in a leaf
cdef double min_weight_leaf # Minimum weight in a leaf
2014-03-19 21:29:57 +08:00
cdef SIZE_t max_depth # Maximal tree depth
cpdef build(self, Tree tree, object X, np.ndarray y,
2015-10-20 19:15:24 +08:00
np.ndarray sample_weight=*,
2015-09-11 16:39:21 +08:00
np.ndarray X_idx_sorted=*)
cdef _check_input(self, object X, np.ndarray y, np.ndarray sample_weight)