Evolutionary Algorithms to Fit the rules
This is a the source file that contains the class to train/fit the rulebase using a genetic algorithm.
- class ex_fuzzy.evolutionary_fit.BaseFuzzyRulesClassifier(nRules: int = 30, nAnts: int = 4, fuzzy_type: FUZZY_SETS = FUZZY_SETS.t1, tolerance: float = 0.0, class_names: list[str] = None, n_linguistic_variables: list[int] | int = 3, verbose=False, linguistic_variables: list[fuzzyVariable] = None, domain: list[float] = None, n_class: int = None, precomputed_rules: MasterRuleBase = None, runner: int = 1, ds_mode: int = 0, fuzzy_modifiers: bool = False, allow_unknown: bool = 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.
- 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: array, y: array, n_gen: int = 70, pop_size: int = 30, checkpoints: int = 0, candidate_rules: MasterRuleBase = None, initial_rules: MasterRuleBase = None, random_state: int = 33, var_prob: float = 0.3, sbx_eta: float = 3.0, mutation_eta=7.0, tournament_size=3, bootstrap_size=1000, p_value_compute=False) None [source]
Fits a fuzzy rule based classifier using a genetic algorithm to the given data.
- Parameters:
X – numpy array samples x features
y – labels. integer array samples (x 1)
n_gen – integer. Number of generations to run the genetic algorithm.
pop_size – integer. Population size for each gneration.
checkpoints – integer. Number of checkpoints to save the best rulebase found so far.
candidate_rules – 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 – if these rules exist, the optimization process will start from this set. If None (default) the rules will be generated from scratch.
random_state – integer. Random seed for the optimization process.
var_prob – float. Probability of crossover for the genetic algorithm.
sbx_eta – float. Eta parameter for the SBX crossover.
mutation_eta – float. Eta parameter for the polynomial mutation.
tournament_size – integer. Size of the tournament for the genetic algorithm.
- Returns:
None. The classifier is fitted to the data.
- forward(X: array, out_class_names=False) array [source]
Returns the predicted class for each sample.
- Parameters:
X – 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.
- get_rulebase() list[array] [source]
Get the rulebase obtained after fitting the classifier to the data.
- Returns:
a matrix format for the rulebase.
- load_master_rule_base(rule_base: MasterRuleBase) None [source]
Loads a master rule base to be used in the prediction process.
- Parameters:
rule_base – ruleBase object.
- Returns:
None
- p_value_validation(bootstrap_size: int = 100)[source]
Computes the permutation and bootstrapping p-values for the classifier and its rules.
- Parameters:
bootstrap_size – integer. Number of bootstraps samples to use.
- predict(X: array, out_class_names=False) array [source]
Returns the predicted class for each sample.
- Parameters:
X – 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.
- predict_proba(X: array) array [source]
Returns the predicted class probabilities for each sample.
- Parameters:
X – np array samples x features.
- Returns:
np array samples x classes with the predicted class probabilities.
- print_rules(return_rules: bool = False) None [source]
Print the rules contained in the fitted rulebase.
- class ex_fuzzy.evolutionary_fit.ExploreRuleBases(X: array, y: array, nRules: int, n_classes: int, candidate_rules: MasterRuleBase, thread_runner: StarmapParallelization = None, tolerance: float = 0.01)[source]
Bases:
Problem
Class to model as pymoo problem the fitting of a rulebase to a set of data given a series of candidate rules for a classification problem using Evolutionary strategies Supports type 1 and t2.
- fitness_func(ruleBase: RuleBase, X: array, y: array, tolerance: float, alpha: float = 0.0, beta: float = 0.0, precomputed_truth=None) float [source]
Fitness function for the optimization problem. :param ruleBase: RuleBase object :param X: array of train samples. X shape = (n_samples, n_features) :param y: array of train labels. y shape = (n_samples,) :param tolerance: float. Tolerance for the size evaluation. :return: float. Fitness value.
- class ex_fuzzy.evolutionary_fit.FitRuleBase(X: array, y: array, nRules: int, nAnts: int, n_classes: int, thread_runner: StarmapParallelization = None, linguistic_variables: list[fuzzyVariable] = None, n_linguistic_variables: int = 3, fuzzy_type=FUZZY_SETS.t1, domain: list = None, tolerance: float = 0.01, alpha: float = 0.0, beta: float = 0.0, ds_mode: int = 0, encode_mods: bool = False, allow_unknown: bool = False)[source]
Bases:
Problem
Class to model as pymoo problem the fitting of a rulebase for a classification problem using Evolutionary strategies. Supports type 1 and iv fs (iv-type 2)
- encode_rulebase(rule_base: MasterRuleBase, optimize_lv: bool, encode_mods: bool = False) array [source]
Given a rule base, constructs the corresponding gene associated with that rule base.
GENE STRUCTURE
First: antecedents chosen by each rule. Size: nAnts * nRules (index of the antecedent) Second: Variable linguistics used. Size: nAnts * nRules Third: Parameters for the fuzzy partitions of the chosen variables. Size: nAnts * self.n_linguistic_variables * 8|4 (2 trapezoidal memberships if t2) Four: Consequent classes. Size: nRules
- Parameters:
rule_base – rule base object.
optimize_lv – if True, the gene is prepared to optimize the membership functions.
encode_mods – if True, the gene is prepared to encode the modifiers for the membership functions.
- Returns:
np array of size self.single_gen_size.
- fitness_func(ruleBase: RuleBase, X: array, y: array, tolerance: float, alpha: float = 0.0, beta: float = 0.0, precomputed_truth: array = None) float [source]
Fitness function for the optimization problem. :param ruleBase: RuleBase object :param X: array of train samples. X shape = (n_samples, n_features) :param y: array of train labels. y shape = (n_samples,) :param tolerance: float. Tolerance for the size evaluation. :param alpha: float. Weight for the accuracy term. :param beta: float. Weight for the average rule size term. :param precomputed_truth: np array. If given, it will be used as the truth values for the evaluation. :return: float. Fitness value.