Evolutionary Fit Module#
The ex_fuzzy.evolutionary_fit module implements evolutionary optimization
for fuzzy rule-based classifiers.
Overview#
This module provides:
BaseFuzzyRulesClassifierfor end-to-end fuzzy classifier training.FitRuleBaseas the optimization problem used internally.
Training performance#
The built-in T1/T2 classification objective automatically uses an optimized CPU evaluator. It decodes each chromosome with the reference rule constructor, computes firing strengths once, and reuses them for dominance scoring, pruning and final winner-rule prediction. Rules that never win a correctly classified sample are still removed. Reporting metrics are computed for the selected model rather than repeatedly for every candidate. Integer labels are counted per class directly for the macro F1.
No public parameters change, and no compiler or additional dependency is needed. Fixed partitions retain their precomputed memberships; optimized partitions are recomputed for each chromosome. Custom losses, nonnumeric internal labels and other fuzzy types retain the full evaluator. EvoX classification also uses this CPU path. Rule selection from candidate rules optimizes the same objective through the reference evaluator; regression retains its own.
To compare exact fitness values, selected chromosomes and predictions while measuring candidate evaluation and complete seeded fits, run from the repository root:
python benchmarks/benchmark_classifier_fitness.py --samples 1000
The script reports median times over three runs for both fixed and optimized partitions, using identical population sizes and generation budgets with early stopping disabled. Run it without competing workloads for useful timings. Speedups depend on data size, rule count and hardware. Population batching and additional parallelism are separate future optimization steps.
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, allow_unknown=False, backend='pymoo', detect_categorical=True, n_gen=70, pop_size=30, patience=10, min_delta=0.0001, random_state=33, var_prob=0.3, sbx_eta=3.0, mutation_eta=7.0, tournament_size=3, algorithm='ga', batch_size=None, initial_data_fraction=None, full_data_at=0.5, inference='winning_rule', mixture_C=1.0, mixture_non_negative=False, mixture_search=False, mixture_pruning='mixture', mixture_prune_threshold=0.02, ordinal_conditions=False, compound_terms=None)[source]#
Bases:
ClassifierMixin,BaseEstimatorClass that is used as a classifier for a fuzzy rule based system. Supports precomputed and optimization of the linguistic variables.
- mixture_ = None#
- __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, allow_unknown=False, backend='pymoo', detect_categorical=True, n_gen=70, pop_size=30, patience=10, min_delta=0.0001, random_state=33, var_prob=0.3, sbx_eta=3.0, mutation_eta=7.0, tournament_size=3, algorithm='ga', batch_size=None, initial_data_fraction=None, full_data_at=0.5, inference='winning_rule', mixture_C=1.0, mixture_non_negative=False, mixture_search=False, mixture_pruning='mixture', mixture_prune_threshold=0.02, ordinal_conditions=False, compound_terms=None)[source]#
Inits the optimizer with the corresponding parameters.
- Parameters:
nRules (int) – number of rules to optimize.
nAnts (int) – max number of antecedents to use.
fuzzy_type (FUZZY_SETS) – 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.
n_linguistic_variables (list | int) – number of terms per variable (an int, or a list with one count per feature) when the partitions are optimized. Ignored when
linguistic_variablesis given: the term counts are then read from those variables, and they may differ between features.linguistic_variables (list[fuzzyVariable]) – list of fuzzyVariables type. If None (default) the optimization process will init+optimize them. When given,
n_linguistic_variablesis ignored.domain (list[float]) – list of the limits for each variable. If None (default) the classifier will compute them empirically.
n_class (int) – number of 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 used to evaluate candidates. Threads disable the fit-local fitness and firing caches, so a serial fit (1, the default) is usually faster; more threads only pay off for an expensive custom loss.
ds_mode (int | str) – inference weighting mode: 0 or ‘dominance’ weights rules by their dominance score, 1 or ‘unweighted’ uses the firing strengths alone, 2 or ‘optimized’ lets the genetic search set a weight per rule.
allow_unknown (bool) – what a sample that fires no rule is predicted as. If True, the unknown class (-1, or ‘Unknown’); if False (default), the most frequent class of the training data. The genetic objective scores such samples the same way, and explainable_predict reports winning rule -1 for them either way.
backend (str) – evolutionary backend to use. Options: ‘pymoo’ (default, CPU) or ‘evox’ (GPU-accelerated). Install with: pip install ex-fuzzy[evox]
detect_categorical (bool) – if True (default) and no categorical_mask is given, the categorical variables are detected from the data with utils.detect_categorical_mask. Ignored when categorical_mask is given or when linguistic_variables are precomputed.
n_gen (int) – number of generations of the genetic search. fit can override it for one call.
pop_size (int) – population size of the genetic search. fit can override it for one call.
patience (int | None) – generations without improvement before the search stops early; None runs every generation.
min_delta (float) – minimum fitness improvement that resets the patience.
random_state (int) – random seed of the genetic search.
var_prob (float) – crossover probability.
sbx_eta (float) – eta parameter of the SBX crossover.
mutation_eta (float) – eta parameter of the polynomial mutation.
tournament_size (int) – size of the selection tournament.
algorithm (str) – genetic search to run. ‘ga’ (default) maximizes one objective, the macro F1 minus the class-coverage and compactness penalties. ‘nsga2’ searches the trade-off between that accuracy (with the coverage penalty) and the share of conditions used, and keeps the whole Pareto front:
fitselects its most accurate solution andselect_pareto_solutionswitches to another. ‘nsga2’ needs the pymoo backend and the built-in loss.batch_size (int | None) – None (default) to compute a candidate’s firing strengths for all the training samples at once, or a number of samples to compute them for at a time, for training sets whose samples x rules arrays do not fit in memory. Every candidate is still scored on all the samples and gets exactly the same score, so the fit is the same; memory falls and time rises (each candidate reads the data two or three times). Needs the built-in loss, Type-1 or interval Type-2 sets and no
candidate_rules. fit can override it for one call.initial_data_fraction (float | None) – None (default) to score every generation on all the training samples, or the share of them, in (0, 1], that the first generation is scored on. The share grows linearly, on stratified random subsets that each contain the previous one, to all the samples by
full_data_at; parents keep the scores of the smaller subsets until then, when the population is re-scored and early stopping starts counting. Saves time on large training sets, but changes the search, so seeded fits differ from those without it. Needs the pymoo backend and nocandidate_rules. fit can override it for one call.full_data_at (float) – share of
n_gen, in (0, 1], after whichinitial_data_fractionhas grown to all the samples; 0.5 (default) reaches them halfway through the search. fit can override it.inference (str) – how the rules decide. ‘winning_rule’ (default): the rule with the largest association degree decides (see
ds_mode). ‘mixture’: every rule contributes; the score of class c isb_c + sum_r W[r, c] * firing_r, the prediction is the class with the largest score, and the weightsWand biasesbare fitted on the training samples after the search, by L2-regularised multinomial logistic regression on the rule firing strengths (seeex_fuzzy.rule_mixtureandfit_mixture). The weights are thenmixture_.ds_modestill sets the winning-rule association that the search and its pruning use.mixture_C (float) – inverse strength of the L2 penalty on the mixture weights, as in scikit-learn’s
LogisticRegression(default 1.0).mixture_non_negative (bool) – if True, every mixture weight is >= 0, so that each rule reads “votes for class c with strength w”; if False (default) the weights are unconstrained and relative.
mixture_search (bool) – if True (needs
inference='mixture'), the genetic search scores each candidate rule base by the macro F1 of the mixture fitted on it, instead of its winning-rule macro F1, with the same penalties and the pruning chosen bymixture_pruning. Each candidate then fits a logistic regression, which makes the search much slower. Needs the built-in loss,algorithm='ga', nobatch_size,initial_data_fractionorcandidate_rules.mixture_pruning (str) – how
mixture_searchprunes each candidate, and the final model, before its mixture is fitted. ‘mixture’ (default): by the rules’ role in the mixture. The weights are fitted on every rule, the rules whose largest centred contribution to a class score on a training sample is at mostmixture_prune_thresholdare deleted, and the weights are fitted again on the survivors (rule_mixture.prune_by_mixture), so a rule that only lowers some classes stays even though it never wins a sample. ‘winning_rule’: as a winning-rule model, deleting the rules whose dominance score is belowtoleranceor that never win a training sample correctly. Withoutmixture_searchthe rules are always pruned as a winning-rule model.mixture_prune_threshold (float) – largest centred contribution, in log-odds, at which
mixture_pruning='mixture'still deletes a rule (default 0.02; 0 deletes only the rules without any effect: those that never fire, repeat the antecedents of an earlier rule, or got equal weights for every class).ordinal_conditions (bool | list) – if True, a rule may also state
x IS at least torx IS at most tfor the termstof a numerical variable other than its lowest and highest, besides a single term. The terms of a variable are ordered as listed, low to high. The condition coverstand every term above (or below) it and has the bounded-sum membershipmin(1, sum of their memberships), which on a strong partition is the shouldered trapezoid of that range. The term gene of the search selects among more options (a variable with n terms offers 3n - 4), so the search space grows. A collection of features (names or indexes) gives the conditions to those variables only; each must be able to have them (3 or more terms, not categorical), the others keep single terms. With True, categorical variables and variables with fewer than 3 terms keep single terms. Type-1 and interval Type-2 sets, not combined withcandidate_rules. Off by default: encoding, rules, printing and fits are then unchanged (docs/source/ordinal_conditions.rst).compound_terms (dict | None) – None (default), or a mapping from a feature (name or index) to a list of
(name, [terms])entries (terms by name or index). Each entry adds one condition to that variable,x IS <name>, whose membership is the bounded sum of the listed terms, which need not be contiguous:('Night', ['Night', 'Evening', 'Late'])over cyclic hours, or a union that wraps around such as[last, first](two shoulders, one at each end of the axis). Each entry needs at least 2 distinct terms and not all of the variable’s terms, the sets must differ from each other and from the ordinal conditions the variable has, or aValueErroris raised. They work with or withoutordinal_conditions, with the same restrictions.
- inference = 'winning_rule'#
- mixture_C = 1.0#
- mixture_non_negative = False#
- mixture_search = False#
- mixture_pruning = 'winning_rule'#
- mixture_prune_threshold = 0.02#
- ordinal_conditions = False#
- compound_terms = None#
- 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=CONSTRUCTOR, pop_size=CONSTRUCTOR, checkpoints=0, candidate_rules=None, initial_rules=None, random_state=CONSTRUCTOR, var_prob=CONSTRUCTOR, sbx_eta=CONSTRUCTOR, mutation_eta=CONSTRUCTOR, tournament_size=CONSTRUCTOR, bootstrap_size=1000, checkpoint_path='', p_value_compute=False, checkpoint_callback=None, patience=CONSTRUCTOR, min_delta=CONSTRUCTOR, batch_size=CONSTRUCTOR, initial_data_fraction=CONSTRUCTOR, full_data_at=CONSTRUCTOR)[source]#
Fits a fuzzy rule based classifier using a genetic algorithm to the given data.
The search settings (n_gen, pop_size, patience, min_delta, random_state, var_prob, sbx_eta, mutation_eta, tournament_size, batch_size, initial_data_fraction and full_data_at) default to the values given to the constructor; passing them here overrides them for this fit only.
- 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.
patience (int | None) – integer. Stop early when the best fitness does not improve for this many generations. Default is 10. Use None to run all generations.
min_delta (float) – float. Minimum fitness improvement required to reset patience. Default is 1e-4.
batch_size (int | None) – None to compute firing strengths for all the samples at once, or the number of samples to compute them for at a time; see the constructor.
initial_data_fraction (float | None) – None, or the share of the samples the first generation is scored on, growing to all of them by
full_data_at; see the constructor.full_data_at (float) – share of the generations after which every sample is used; see the constructor.
- Returns:
the fitted classifier.
- select_pareto_solution(index)[source]#
Make the classifier use another solution of the Pareto front found with
algorithm='nsga2'.pareto_front_lists the solutions from the most accurate on the training data, the onefitselects, to the most compact. Each entry holds its trainingfitness(macro F1 minus the class-coverage penalty), its number ofrulesandconditions, and the fittedrule_base.- Parameters:
index (int) – position of the solution in
pareto_front_; negative indexes count from the end.- Returns:
the classifier.
- Raises:
ValueError – if the classifier has no Pareto front or
indexis out of range.- Return type:
- 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
- explainable_predict(X, out_class_names=False)[source]#
Returns the predicted class for each sample, with the winning rule, its association degree and confidence interval.
- Parameters:
X (array) – np array samples x features.
out_class_names – if True, the predictions are the consequent names instead of the fitted labels.
- Returns:
predictions, winning rules, winning association degrees and confidence intervals.
- Return type:
an ExplainedPrediction named tuple
- 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 consequent names instead of the fitted labels.
- 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.
A fitted classifier predicts the labels it was fitted with (see
classes_). Samples where no rule fires get the most frequent training class, or, withallow_unknown=True, -1 for numeric labels and ‘Unknown’ otherwise. A classifier built from precomputed rules predicts consequent indexes. Withinference='mixture'the prediction is the class with the largest mixture score (seemixture_scores); a sample that fires no rule then gets the class of the largest bias, or the unknown class withallow_unknown=True.- Parameters:
X (array) – np array samples x features.
out_class_names – if True, the output will be the consequent names instead of the fitted labels.
- 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.
Samples where no rule fires get the class distribution of the training data (
rule_base.class_priors), or a uniform one for rule bases without it.With
inference='mixture'they aresoftmax(mixture_scores(X)): the class probabilities of the fitted logistic model on data like its training data. They are not calibrated probabilities, and the class-aware sampling of the training data (if any) shifts them.- Parameters:
X (array) – np array samples x features.
- Returns:
np array samples x classes with the predicted class probabilities.
- Return type:
array
- fit_mixture(X, y)[source]#
Fits the mixture weights of
inference='mixture'on the current rules, which stay as they are.fitcalls it after the search. Call it to give the rules of an already fitted (or loaded) classifier a mixture, afterset_params(inference='mixture')(ormodel.inference = 'mixture'for a classifier pickled by a version that lacks some constructor arguments, whereset_paramsfails), or again after the rules change (for example afterselect_pareto_solution). Labels are those the classifier was fitted with (classes_), or consequent indexes for a classifier built from precomputed rules. Use the training data only: the weights are fitted parameters of the model.- Parameters:
X (array) – np array samples x features.
y (array) – label of each sample.
- Returns:
the classifier.
- Return type:
- mixture_scores(X)[source]#
The mixture score of every class,
b_c + sum_r W[r, c] * firing_r(inference='mixture').- Parameters:
X (array) – np array samples x features.
- Returns:
np array samples x classes, in consequent order.
- Return type:
array
- mixture_contributions(X)[source]#
What each rule adds to each class score,
W[r, c] * firing_r(inference='mixture').The contributions of a sample add up, over the rules, to its scores minus the biases. Rules are in
rule_base.get_rules()order and classes in consequent order.- Parameters:
X (array) – np array samples x features.
- Returns:
np array samples x rules x classes.
- Return type:
array
- print_mixture(return_text=False, decimals=2)[source]#
Prints the rules with their mixture weight for every class, their strongest class and the biases.
- Parameters:
return_text (bool) – if True, the text is returned instead of printed.
decimals (int) – decimals of the weights.
- 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]
- reparametrize_loss(alpha, beta)[source]#
Changes the parameters in the loss function.
The genetic search maximizes the macro F1 of the pruned rule base on the training data minus two penalties:
betatimes the share of classes left without rules, andalphatimes the share of the possible antecedent conditions (nRules * nAnts) the rule base uses. Keep both weights small so that they only decide between rule bases of similar accuracy.- Parameters:
alpha (float) – weight of the compactness penalty. Default
DEFAULT_COMPACTNESS_WEIGHT.beta (float) – weight of the class-coverage penalty. Default
DEFAULT_COVERAGE_WEIGHT.
Note
Does not check for convexity preservation. The user can play with these parameters as it wills.
- __call__(X)[source]#
Returns the predicted class for each sample.
- Parameters:
X (array) – np array samples x features.
- Returns:
np array samples (x 1) with the predicted class.
- Return type:
array
- set_fit_request(*, batch_size='$UNCHANGED$', bootstrap_size='$UNCHANGED$', candidate_rules='$UNCHANGED$', checkpoint_callback='$UNCHANGED$', checkpoint_path='$UNCHANGED$', checkpoints='$UNCHANGED$', full_data_at='$UNCHANGED$', initial_data_fraction='$UNCHANGED$', initial_rules='$UNCHANGED$', min_delta='$UNCHANGED$', mutation_eta='$UNCHANGED$', n_gen='$UNCHANGED$', p_value_compute='$UNCHANGED$', patience='$UNCHANGED$', pop_size='$UNCHANGED$', random_state='$UNCHANGED$', sbx_eta='$UNCHANGED$', tournament_size='$UNCHANGED$', var_prob='$UNCHANGED$')#
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
batch_size (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
batch_sizeparameter infit.bootstrap_size (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
bootstrap_sizeparameter infit.candidate_rules (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
candidate_rulesparameter infit.checkpoint_callback (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
checkpoint_callbackparameter infit.checkpoint_path (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
checkpoint_pathparameter infit.checkpoints (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
checkpointsparameter infit.full_data_at (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
full_data_atparameter infit.initial_data_fraction (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
initial_data_fractionparameter infit.initial_rules (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
initial_rulesparameter infit.min_delta (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
min_deltaparameter infit.mutation_eta (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
mutation_etaparameter infit.n_gen (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
n_genparameter infit.p_value_compute (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
p_value_computeparameter infit.patience (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
patienceparameter infit.pop_size (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
pop_sizeparameter infit.random_state (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
random_stateparameter infit.sbx_eta (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sbx_etaparameter infit.tournament_size (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
tournament_sizeparameter infit.var_prob (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
var_probparameter infit.
- Returns:
self – The updated object.
- Return type:
object
- set_predict_request(*, out_class_names='$UNCHANGED$')#
Configure whether metadata should be requested to be passed to the
predictmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
out_class_names (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
out_class_namesparameter inpredict.- Returns:
self – The updated object.
- Return type:
object
- set_score_request(*, sample_weight='$UNCHANGED$')#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter inscore.- Returns:
self – The updated object.
- Return type:
object
FitRuleBase#
- class ex_fuzzy.evolutionary_fit.FitRuleBase(X, y, nRules, nAnts, n_classes, thread_runner=None, linguistic_variables=None, n_linguistic_variables=3, fuzzy_type=FUZZY_SETS.t1, domain=None, categorical_mask=None, tolerance=0.01, alpha=0.03, beta=0.05, ds_mode=0, allow_unknown=False, backend_name='pymoo', var_names=None, pareto=False, batch_size=None, ordinal_conditions=False, compound_terms=None)[source]#
Bases:
ProblemClass to model, independently of the optimizer, the fitting of a rulebase for a classification problem using Evolutionary strategies. Supports type 1 and iv fs (iv-type 2)
- vl_names = [[], [], ['Low', 'High'], ['Low', 'Medium', 'High'], ['Low', 'Medium', 'High', 'Very High'], ['Very Low', 'Low', 'Medium', 'High', 'Very High']]#
- __init__(X, y, nRules, nAnts, n_classes, thread_runner=None, linguistic_variables=None, n_linguistic_variables=3, fuzzy_type=FUZZY_SETS.t1, domain=None, categorical_mask=None, tolerance=0.01, alpha=0.03, beta=0.05, ds_mode=0, allow_unknown=False, backend_name='pymoo', var_names=None, pareto=False, batch_size=None, ordinal_conditions=False, compound_terms=None)[source]#
Cosntructor method. Initializes the classifier with the number of antecedents, linguist variables and the kind of fuzzy set desired.
- Parameters:
X (array) – np array or pandas dataframe samples x features.
y (array) – np vector containing the target classes. vector sample
nRules (int) – number of rules to optimize.
nAnts (int) – max number of antecedents to use.
n_class – number of classes in the problem. If None (as default) it will be computed from the data.
linguistic_variables (list[fuzzyVariable]) – list of linguistic variables precomputed. If given, the rest of conflicting arguments are ignored.
n_linguistic_variables (int) – number of linguistic variables per antecedent.
fuzzy_type – Define the fuzzy set or fuzzy set extension used as linguistic variable.
domain (list) – list with the upper and lower domains of each input variable. If None (as default) it will stablish the empirical min/max as the limits.
tolerance (float) – float. Tolerance for the size evaluation.
alpha (float) – float. Weight of the compactness penalty in the fitness function (see
BaseFuzzyRulesClassifier.reparametrize_loss).beta (float) – float. Weight of the class-coverage penalty in the fitness function.
ds_mode (int) – int. Mode for the dominance score. 0: normal dominance score, 1: rules without weights, 2: weights optimized for each rule based on the data.
allow_unknown (bool) – if True, a sample that fires no rule is predicted as the unknown class (-1); if False, as the most frequent class of y.
var_names (list) – list of variable names. If None, extracted from DataFrame columns or auto-generated.
pareto (bool) – if True, the problem has two objectives for a Pareto search: one minus the macro F1 with the class-coverage penalty, and the share of the possible conditions used.
alphais unused.batch_size (int | None) – None (default) to compute each candidate’s firing strengths for all the samples at once, or a number of samples to compute them for at a time. Every candidate is still scored on all the samples and gets the same score; only memory and time change. Type-1 and interval Type-2 sets.
ordinal_conditions – if True, a rule may also say
x IS at least torx IS at most tfor the termstof a numerical variable other than its lowest and highest, besides a single term. The term gene then selects among more options (seen_term_codes); categorical variables keep single terms. Type-1 and interval Type-2 sets. Off by default: the encoding is unchanged. A collection of features (names or indexes) restricts the conditions to those variables.compound_terms (dict | None) – mapping from a feature (name or index) to
[(name, [terms])...]: named conditions whose membership is the bounded sum of the listed terms, coded after the terms and the ordinal conditions (seeBaseFuzzyRulesClassifier).
- encode_rulebase(rule_base, optimize_lv)[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 (MasterRuleBase) – rule base object.
optimize_lv (bool) – must be False: only genes over fixed linguistic variables can be encoded.
- Returns:
np array of size self.single_gen_size.
- Raises:
NotImplementedError – if optimize_lv is True.
ValueError – if the problem does not have one antecedent slot per feature.
- Return type:
array
- array_evaluation = True#
Set to False to force the object decoder, for benchmarks and parity tests.
- batch_size = None#
Samples per batch when candidates stream their samples (
batch_size), and the running NumPy’s summation order that streaming reproduces; the latter stays None while every sample is scored at once.
- torch_devices = ('cuda',)#
Device types on which populations may be scored by the exact PyTorch objective. Scoring CPU tensors gains nothing over the NumPy routes, so only CUDA is enabled; parity tests add ‘cpu’ to run the same code.
- fitness_func(ruleBase, X, y, tolerance, alpha=0.0, beta=0.0, precomputed_truth=None)[source]#
Fitness function for the optimization problem.
- Parameters:
ruleBase (RuleBase) – RuleBase object
X (array) – array of train samples. X shape = (n_samples, n_features)
y (array) – array of train labels. y shape = (n_samples,)
tolerance (float) – float. Dominance score below which rules are pruned.
alpha (float) – float. Weight of the compactness penalty (see
BaseFuzzyRulesClassifier.reparametrize_loss).beta (float) – float. Weight of the class-coverage penalty.
precomputed_truth (array) – np array. If given, it will be used as the truth values for the evaluation.
- Returns:
macro F1 minus the penalties. For a Pareto problem, the pair
(accuracy, compactness)instead.- Return type:
float. Fitness value
See Also#
ex_fuzzy.classifiersex_fuzzy.rulesex_fuzzy.rule_miningex_fuzzy.eval_tools