teacher.tree#
The teacher.tree implements the Decision Trees used as a white-box
for the different explainers. Currently, there are two trees implemented as
well as a representation for the rules.
The classes are the following ones:
BaseDecisionTreeBase class with the common methods that will be used for the rest of the decision trees.
ID3Fuzzy adaptation of the ID3 algorithm that implements an inference that allows for an instance to traverse multiple branches of the tree. Used mainly as a baseline for comparison against other algorithms.
FDTMultiway Fuzzy Decision Tree implemented as described here. This tree takes a set of fuzzy variables and a dataset and generates a decision tree, to get later a classification based on different types of inference.
- class teacher.tree.BaseDecisionTree(features, th=0.0001, max_depth=2, min_num_examples=1, prunning=True)[source]#
Base abstract decision tree that provides the basic methods to implement the rest of the decision trees.
- __init__(features, th=0.0001, max_depth=2, min_num_examples=1, prunning=True)[source]#
- Parameters:
features (list) – Sorted list of features as they will appear in the dataset
th (float, optional) – Minimum gain threshold to keep branching the tree, by default 0.0001
max_depth (int, optional) – Maximum depth of the tree, by default 2
min_num_examples (int, optional) – Minimum number of examples per leaf, by default 1
prunning (bool, optional) – Whether or not to prune the tree, by default True
- abstract fit(X, y)[source]#
Build a decision tree classifier from the training set (X, y)
- Parameters:
X (array-like of shape (n_samples, n_features)) – The training input samples. Internally, it will be converted to dtype=np.float64 if possible or dtype=object otherwise.
y (array-like of shape (n_samples,)) – The target values (class labels) as integers or strings.
- predict(X)[source]#
Predict class value for X.
The predicted class for each sample in X is returned.
- Parameters:
X (array-like of shape (n_samples, n_features)) – The training input samples. Internally, it will be converted to dtype=np.float64 if possible or dtype=object otherwise.
- Returns:
y – The predicted classes
- Return type:
array-like of shape (n_samples,)
- score(X, y)[source]#
Return the mean accuracy on the given test data and labels
- Parameters:
X (array-like of shape (n_samples, n_features)) – Test samples.
y (array-like of shape (n_samples,)) – True labels for X
- Returns:
score – Mean accuracy of self.predict(X) wrt. y
- Return type:
- class teacher.tree.ID3(features, th=0.0001, max_depth=2, min_num_examples=1, prunning=True)[source]#
- __init__(features, th=0.0001, max_depth=2, min_num_examples=1, prunning=True)[source]#
- Parameters:
features (list) – Sorted list of features as they will appear in the dataset
th (float, optional) – Minimum gain threshold to keep branching the tree, by default 0.0001
max_depth (int, optional) – Maximum depth of the tree, by default 2
min_num_examples (int, optional) – Minimum number of examples per leaf, by default 1
prunning (bool, optional) – Whether or not to prune the tree, by default True
- fit(X, y, debug=False)[source]#
Build a decision tree classifier from the training set (X, y)
- Parameters:
X (array-like of shape (n_samples, n_features)) – The training input samples. Internally, it will be converted to dtype=np.float64 if possible or dtype=object otherwise.
y (array-like of shape (n_samples,)) – The target values (class labels) as integers or strings.
- predict(X)[source]#
Predict class value for X.
The predicted class for each sample in X is returned.
- Parameters:
X (array-like of shape (n_samples, n_features)) – The training input samples. Internally, it will be converted to dtype=np.float64 if possible or dtype=object otherwise.
- Returns:
y – The predicted classes
- Return type:
array-like of shape (n_samples,)
- score(X, y)[source]#
Return the mean accuracy on the given test data and labels
- Parameters:
X (array-like of shape (n_samples, n_features)) – Test samples.
y (array-like of shape (n_samples,)) – True labels for X
- Returns:
score – Mean accuracy of self.predict(X) wrt. y
- Return type:
- class teacher.tree.FDT(fuzzy_variables, fuzzy_threshold=0.0001, th=0.0001, max_depth=10, min_num_examples=1, prunning=True, t_norm=<ufunc 'minimum'>, voting='agg_vote')[source]#
- __init__(fuzzy_variables, fuzzy_threshold=0.0001, th=0.0001, max_depth=10, min_num_examples=1, prunning=True, t_norm=<ufunc 'minimum'>, voting='agg_vote')[source]#
- Parameters:
fuzzy_variables (list[FuzzyVariable]) – List of the fuzzy variables used in the tree
fuzzy_threshold (float, optional) – Minimum threshold for a pertenence degree to activate a fuzzy set, by default 0.0001
th (float, optional) – Minimum gain threshold to keep branching the tree, by default 0.0001
max_depth (int, optional) – Maximum depth of the tree, by default 2
min_num_examples (int, optional) – Minimum number of examples per leaf, by default 1
prunning (bool, optional) – Whether or not to prune the tree, by default True
t_norm (function, optional) – function to be used as T-norm, by default np.minimum
voting (str, optional) – {‘agg_vote’, ‘max_match’}, method of voting for the inference, by default ‘agg_vote’
- fit(X, y)[source]#
Build a decision tree classifier from the training set (X, y)
- Parameters:
X (array-like of shape (n_samples, n_features)) – The training input samples. Internally, it will be converted to dtype=np.float64 if possible or dtype=object otherwise.
y (array-like of shape (n_samples,)) – The target values (class labels) as integers or strings.
- predict(X)[source]#
Predict class value for X.
The predicted class for each sample in X is returned.
- Parameters:
X (array-like of shape (n_samples, n_features)) – The training input samples. Internally, it will be converted to dtype=np.float64 if possible or dtype=object otherwise.
- Returns:
y – The predicted classes
- Return type:
array-like of shape (n_samples,)
- score(X, y)[source]#
Return the mean accuracy on the given test data and labels
- Parameters:
X (array-like of shape (n_samples, n_features)) – Test samples.
y (array-like of shape (n_samples,)) – True labels for X
- Returns:
score – Mean accuracy of self.predict(X) wrt. y
- Return type:
- class teacher.tree.FBDT(fuzzy_variables, fuzzy_threshold=0.0001, th=0.0001, max_depth=10, min_num_examples=1, prunning=True, t_norm=<ufunc 'minimum'>, voting='agg_vote')[source]#
- __init__(fuzzy_variables, fuzzy_threshold=0.0001, th=0.0001, max_depth=10, min_num_examples=1, prunning=True, t_norm=<ufunc 'minimum'>, voting='agg_vote')[source]#
- Parameters:
fuzzy_variables (list[FuzzyVariable]) – List of the fuzzy variables used in the tree
fuzzy_threshold (float, optional) – Minimum threshold for a pertenence degree to activate a fuzzy set, by default 0.0001
th (float, optional) – Minimum gain threshold to keep branching the tree, by default 0.0001
max_depth (int, optional) – Maximum depth of the tree, by default 2
min_num_examples (int, optional) – Minimum number of examples per leaf, by default 1
prunning (bool, optional) – Whether or not to prune the tree, by default True
t_norm (function, optional) – function to be used as T-norm, by default np.minimum
voting (str, optional) – {‘agg_vote’, ‘max_match’}, method of voting for the inference, by default ‘agg_vote’
- fit(X, y)[source]#
Build a decision tree classifier from the training set (X, y)
- Parameters:
X (array-like of shape (n_samples, n_features)) – The training input samples. Internally, it will be converted to dtype=np.float64 if possible or dtype=object otherwise.
y (array-like of shape (n_samples,)) – The target values (class labels) as integers or strings.
- predict(X)[source]#
Predict class value for X.
The predicted class for each sample in X is returned.
- Parameters:
X (array-like of shape (n_samples, n_features)) – The training input samples. Internally, it will be converted to dtype=np.float64 if possible or dtype=object otherwise.
- Returns:
y – The predicted classes
- Return type:
array-like of shape (n_samples,)
- score(X, y)[source]#
Return the mean accuracy on the given test data and labels
- Parameters:
X (array-like of shape (n_samples, n_features)) – Test samples.
y (array-like of shape (n_samples,)) – True labels for X
- Returns:
score – Mean accuracy of self.predict(X) wrt. y
- Return type:
- class teacher.tree.Rule(antecedent, consequent, weight, simplify=False, multiple_antecedents=False)[source]#
- static map_rule_variables(rule, origin_fuzzy_variables, dest_fuzzy_variables, map_function='intersection')[source]#
Changes the fuzzy variables of the rule for ones that are defined in the same universe
- Parameters:
rule (Rule) – Original rule to map to the new variables
origin_fuzzy_variables (list[FuzzyVariable]) – List with the original fuzzy variables
dest_fuzzy_variables (list[FuzzyVariable]) – List with the destination fuzzy variables
map_function (str, {'intersection', 'simmilarity'}) – Method to check the best fuzzy set to change
- Returns:
Rule with the new variables
- Return type:
- Raises:
ValueError – If the universes of the variables are not the same it raises an error
- matching(instance_membership, t_norm=<built-in function min>)[source]#
Matching that an instance has with the rule If there is some feature or value not existing in the instance, its matching degree is zero
- Parameters:
instance_membership (dict) – Membership of the instance with the format {feature: {value: pertenence degree}}
t_norm (function, optional) – Operation to use as tnorm to get the matching, by default min
- to_json(fuzzy_variables)[source]#
Transform the rule to a json format
- Parameters:
fuzzy_variables (list[FuzzyVariable]) – List with the fuzzy variables of the problem
- Returns:
Json with the rule
- Return type:
- static weighted_vote(rule_list, instance_membership)[source]#
Use the weighted vote inference method to return the consequent associated to an instance and a rule list given the instance membership
- Parameters:
- Returns:
consequent associated with the instance and the rule list
- Return type:
string or number