RuleTreeCluster#
- class RuleTreeCluster(n_components: int = 2, clus_impurity: str = 'r2', bic_eps: float = 0.0, max_leaf_nodes=inf, min_samples_split=2, max_depth=inf, prune_useless_leaves=True, base_stumps: RegressorMixin | list = None, random_state=None, component_split: str = 'best')[source]#
Bases:
ClusterMixin,RuleTreeRuleTree for clustering: each leaf is a cluster described by the rules of its path.
To split a node, the instances are projected on their principal components (PCA, MCA or FAMD, for numerical, categorical or mixed data); for each component, a regression stump is fitted to predict it, and the best split is kept according to
clus_impurity. A node is split only if the split decreases its BIC (seebic_eps). The tree grows from the largest node. Only tabular data are supported.After fit, the nodes predict integer labels assigned in breadth-first order (‘R’ -> 0, ‘Rl’ -> 1, ‘Rr’ -> 2, …);
predictreturns the label of the leaf of each instance.- Parameters:
n_components (int, default=2) – Number of principal components predicted by the stumps of each node.
clus_impurity ({'r2', 'bic'}, default='r2') – Criterion to choose the split among the ones on the different components: the variance of the component explained by the split (‘r2’) or the Bayesian Information Criterion of the two clusters (‘bic’).
bic_eps (float, default=0.0) – A split is kept only if it decreases the BIC of the node by more than bic_eps times its value.
max_leaf_nodes (int, default=inf) – Maximum number of leaves.
min_samples_split (int, default=2) – Minimum number of instances to split a node.
max_depth (int, default=inf) – Maximum depth of the tree (the root has depth 0).
prune_useless_leaves (bool, default=True) – Whether to merge, after fit, the sibling leaves with the same prediction.
base_stumps (RuleTreeBaseStump or list, default=None) – Regression stumps used to split the nodes, fitted to predict a principal component. If None, DecisionTreeStumpRegressor().
random_state (int, RandomState instance or None, default=None) – Seed of the principal component analysis and of the random choice of the stumps, also given to the stumps without a random_state.
component_split ({'best', 'joint'}, default='best') – How a node is split on its principal components. ‘best’ fits a stump on each component and keeps the best split according to
clus_impurity. ‘joint’ fits one stump on all the components together (a regression with several targets): the split is the one that most decreases the sum of squared distances of the instances from the centers of the two children, in the space of the components, as a k-means with two clusters whose border is a rule;clus_impurityis not used. With ‘joint’, the base stumps must support several targets (e.g. the decision tree, pivot and oblique stumps, not the fair stump).
- Variables:
label_encoder (dict) – Node id -> integer label.
root (RuleTreeNode) – Root of the fitted tree.
candidate_splits (list of tuple) –
(probability, stump, data_type): the stumps usable on the training data, with the data type they are used on.cache (dict) – Values cached by the stumps during fit (see
get_cache()).
Examples
>>> from sklearn.datasets import make_blobs >>> from sklearn.metrics import adjusted_rand_score >>> from RuleTree import RuleTreeCluster >>> X, y = make_blobs(n_samples=300, centers=3, cluster_std=0.5, random_state=0) >>> labels = RuleTreeCluster(max_depth=3, random_state=0).fit_predict(X) >>> round(adjusted_rand_score(y, labels), 2) 0.95
- is_split_useless(data: dict, idx: ndarray, labels: ndarray)[source]#
Whether a split is useless: all the instances go to the same child, or the BIC of the two clusters is not lower than the BIC of the node (by bic_eps times its value).
- Parameters:
data (dict) – Whole data set, as
{data_type: array}.idx (numpy.ndarray) – Indices of the instances in the node.
labels (numpy.ndarray) – Result of the split on idx (1 left, 2 right).
- Returns:
useless
- Return type:
- fit_predict(X, y=None, **kwargs)[source]#
Fit the tree and return the cluster label of each instance.
- Parameters:
X (numpy.ndarray of shape (n_samples, n_features)) – Tabular data.
y (None) – Not used, present for API consistency.
**kwargs (dict) – Arguments passed to fit.
- Returns:
labels – Cluster labels.
- Return type:
numpy.ndarray of shape (n_samples,)
- queue_push(node: RuleTreeNode, idx: ndarray)[source]#
Push a node into the queue, with priority to the nodes with more instances.
- Parameters:
node (RuleTreeNode) – The node.
idx (numpy.ndarray) – Indices of the instances reaching the node.
- make_split(data: dict, y, idx: ndarray, sample_weight=None, node_id: str | None = None) RuleTreeBaseStump[source]#
Split a node on its principal components: the instances are projected with PCA, MCA or FAMD (numerical, categorical or mixed data); with
component_split='best', a stump is fitted to predict each component, and the best split according toclus_impurityis kept; with'joint', one stump is fitted to predict all the components.- Parameters:
data (dict) – Whole data set, as
{data_type: array}(only tabular data).y (numpy.ndarray or None) – Not used by the split (the stumps predict the components).
idx (numpy.ndarray) – Indices of the instances in the node.
sample_weight (None) – Not used, present for API consistency.
node_id (str, default=None) – Identifier of the node, passed to the stumps.
- Returns:
stump – The best stump.
- Return type:
- Raises:
NoSplitFoundWarning – If no component can be split.
- prepare_node(y: ndarray, idx: ndarray, node_id: str, node: RuleTreeNode | None = None) RuleTreeNode[source]#
Create a node (or update
node) whose prediction is its node id, replaced by an integer label in_post_fit_fix().- Parameters:
y (numpy.ndarray or None) – Not used.
idx (numpy.ndarray) – Indices of the instances reaching the node.
node_id (str) – Identifier of the node.
node (RuleTreeNode, default=None) – Existing node to update instead of creating a new one.
- Returns:
node
- Return type: