ex_fuzzy.evolutionary_fit.BaseFuzzyRulesClassifier#
- class ex_fuzzy.evolutionary_fit.BaseFuzzyRulesClassifier(nRules=30, nAnts=4, fuzzy_type=FUZZY_SETS.t1, tolerance=0.0, class_names=None, n_linguistic_variables=3, verbose=False, linguistic_variables=None, categorical_mask=None, domain=None, n_class=None, precomputed_rules=None, runner=1, ds_mode=0, fuzzy_modifiers=False, allow_unknown=False)[source]#
Bases:
ClassifierMixin
Class that is used as a classifier for a fuzzy rule based system. Supports precomputed and optimization of the linguistic variables.
- __init__(nRules=30, nAnts=4, fuzzy_type=FUZZY_SETS.t1, tolerance=0.0, class_names=None, n_linguistic_variables=3, verbose=False, linguistic_variables=None, categorical_mask=None, domain=None, n_class=None, precomputed_rules=None, runner=1, ds_mode=0, fuzzy_modifiers=False, allow_unknown=False)[source]#
Inits the optimizer with the corresponding parameters.
- Parameters:
nRules (int) – number of rules to optimize.
nAnts (int) – max number of antecedents to use.
type (fuzzy) – FUZZY_SET enum type in fuzzy_sets module. The kind of fuzzy set used.
tolerance (float) – tolerance for the dominance score of the rules.
n_linguist_variables – number of linguistic variables per antecedent.
verbose – if True, prints the progress of the optimization.
linguistic_variables (list[fuzzyVariable]) – list of fuzzyVariables type. If None (default) the optimization process will init+optimize them.
domain (list[float]) – list of the limits for each variable. If None (default) the classifier will compute them empirically.
n_class (int) – names of the classes in the problem. If None (default) the classifier will compute it empirically.
precomputed_rules (MasterRuleBase) – MasterRuleBase object. If not None, the classifier will use the rules in the object and ignore the conflicting parameters.
runner (int) – number of threads to use. If None (default) the classifier will use 1 thread.
ds_mode (int) – mode for the dominance score. 0: normal dominance score, 1: rules without weights, 2: weights optimized for each rule based on the data.
fuzzy_modifiers (bool) – if True, the classifier will use the modifiers in the optimization process.
allow_unknown (bool) – if True, the classifier will allow the unknown class in the classification process. (Which would be a -1 value)
Methods
__init__
([nRules, nAnts, fuzzy_type, ...])Inits the optimizer with the corresponding parameters.
customized_loss
(loss_function)Function to customize the loss function used for the optimization.
explainable_predict
(X[, out_class_names])Returns the predicted class for each sample.
fit
(X, y[, n_gen, pop_size, checkpoints, ...])Fits a fuzzy rule based classifier using a genetic algorithm to the given data.
forward
(X[, out_class_names])Returns the predicted class for each sample.
Get the rulebase obtained after fitting the classifier to the data.
load_master_rule_base
(rule_base)Loads a master rule base to be used in the prediction process.
p_value_validation
([bootstrap_size])Computes the permutation and bootstrapping p-values for the classifier and its rules.
Plot the fuzzy partitions in each fuzzy variable.
predict
(X[, out_class_names])Returns the predicted class for each sample.
Returns the predicted class memberships for each sample.
Returns the predicted class probabilities for each sample.
predict_proba_rules
(X[, truth_degrees])Returns the predicted class probabilities for each sample.
Prints the bootstrap results for each rule.
print_rules
([return_rules, bootstrap_results])Print the rules contained in the fitted rulebase.
Renames the linguist labels so that high, low and so on are consistent.
reparametrice_loss
(alpha, beta)Changes the parameters in the loss function.
score
(X, y[, sample_weight])Return accuracy on provided data and labels.
- __init__(nRules=30, nAnts=4, fuzzy_type=FUZZY_SETS.t1, tolerance=0.0, class_names=None, n_linguistic_variables=3, verbose=False, linguistic_variables=None, categorical_mask=None, domain=None, n_class=None, precomputed_rules=None, runner=1, ds_mode=0, fuzzy_modifiers=False, allow_unknown=False)[source]#
Inits the optimizer with the corresponding parameters.
- Parameters:
nRules (int) – number of rules to optimize.
nAnts (int) – max number of antecedents to use.
type (fuzzy) – FUZZY_SET enum type in fuzzy_sets module. The kind of fuzzy set used.
tolerance (float) – tolerance for the dominance score of the rules.
n_linguist_variables – number of linguistic variables per antecedent.
verbose – if True, prints the progress of the optimization.
linguistic_variables (list[fuzzyVariable]) – list of fuzzyVariables type. If None (default) the optimization process will init+optimize them.
domain (list[float]) – list of the limits for each variable. If None (default) the classifier will compute them empirically.
n_class (int) – names of the classes in the problem. If None (default) the classifier will compute it empirically.
precomputed_rules (MasterRuleBase) – MasterRuleBase object. If not None, the classifier will use the rules in the object and ignore the conflicting parameters.
runner (int) – number of threads to use. If None (default) the classifier will use 1 thread.
ds_mode (int) – mode for the dominance score. 0: normal dominance score, 1: rules without weights, 2: weights optimized for each rule based on the data.
fuzzy_modifiers (bool) – if True, the classifier will use the modifiers in the optimization process.
allow_unknown (bool) – if True, the classifier will allow the unknown class in the classification process. (Which would be a -1 value)
- customized_loss(loss_function)[source]#
Function to customize the loss function used for the optimization.
- Parameters:
loss_function – function that takes as input the true labels and the predicted labels and returns a float.
- Returns:
None
- fit(X, y, n_gen=70, pop_size=30, checkpoints=0, candidate_rules=None, initial_rules=None, random_state=33, var_prob=0.3, sbx_eta=3.0, mutation_eta=7.0, tournament_size=3, bootstrap_size=1000, checkpoint_path='', p_value_compute=False, checkpoint_callback=None)[source]#
Fits a fuzzy rule based classifier using a genetic algorithm to the given data.
- Parameters:
X (array) – numpy array samples x features
y (array) – labels. integer array samples (x 1)
n_gen (int) – integer. Number of generations to run the genetic algorithm.
pop_size (int) – integer. Population size for each gneration.
checkpoints (int) – integer. Number of checkpoints to save the best rulebase found so far.
candidate_rules (MasterRuleBase) – if these rules exist, the optimization process will choose the best rules from this set. If None (default) the rules will be generated from scratch.
initial_rules (MasterRuleBase) – if these rules exist, the optimization process will start from this set. If None (default) the rules will be generated from scratch.
random_state (int) – integer. Random seed for the optimization process.
var_prob (float) – float. Probability of crossover for the genetic algorithm.
sbx_eta (float) – float. Eta parameter for the SBX crossover.
checkpoint_path (str) – string. Path to save the checkpoints. If None (default) the checkpoints will be saved in the current directory.
mutation_eta (float) – float. Eta parameter for the polynomial mutation.
tournament_size (int) – integer. Size of the tournament for the genetic algorithm.
checkpoint_callback (Callable[[int, MasterRuleBase], None]) – function. Callback function that get executed at each checkpoint (‘checkpoints’ must be greater than 0), its arguments are the generation number and the rule_base of the checkpoint.
- Returns:
None. The classifier is fitted to the data.
- Return type:
None
- p_value_validation(bootstrap_size=100)[source]#
Computes the permutation and bootstrapping p-values for the classifier and its rules.
- Parameters:
bootstrap_size (int) – integer. Number of bootstraps samples to use.
- load_master_rule_base(rule_base)[source]#
Loads a master rule base to be used in the prediction process.
- Parameters:
rule_base (MasterRuleBase) – ruleBase object.
- Returns:
None
- Return type:
None
- forward(X, out_class_names=False)[source]#
Returns the predicted class for each sample.
- Parameters:
X (array) – np array samples x features.
out_class_names – if True, the output will be the class names instead of the class index.
- Returns:
np array samples (x 1) with the predicted class.
- Return type:
array
- predict(X, out_class_names=False)[source]#
Returns the predicted class for each sample.
- Parameters:
X (array) – np array samples x features.
out_class_names – if True, the output will be the class names instead of the class index.
- Returns:
np array samples (x 1) with the predicted class.
- Return type:
array
- predict_proba_rules(X, truth_degrees=True)[source]#
Returns the predicted class probabilities for each sample.
- Parameters:
X (array) – np array samples x features.
truth_degrees (bool) – if True, the output will be the truth degrees of the rules. If false, will return the association degrees i.e. the truth degree multiplied by the weights/dominance of the rules. (depending on the inference mode chosen)
- Returns:
np array samples x classes with the predicted class probabilities.
- Return type:
array
- predict_membership_class(X)[source]#
Returns the predicted class memberships for each sample.
- Parameters:
X (array) – np array samples x features.
- Returns:
np array samples x classes with the predicted class probabilities.
- Return type:
array
- predict_proba(X)[source]#
Returns the predicted class probabilities for each sample.
- Parameters:
X (array) – np array samples x features.
- Returns:
np array samples x classes with the predicted class probabilities.
- Return type:
array
- print_rules(return_rules=False, bootstrap_results=False)[source]#
Print the rules contained in the fitted rulebase.
- rename_fuzzy_variables()[source]#
Renames the linguist labels so that high, low and so on are consistent. It does so usually after an optimization process.
- Returns:
None. Names are sorted accorded to the central point of the fuzzy memberships.
- Return type:
None
- get_rulebase()[source]#
Get the rulebase obtained after fitting the classifier to the data.
- Returns:
a matrix format for the rulebase.
- Return type:
list[array]