Fuzzy Rules Functions

This is a the source file that contains the Rule, RuleBase and MasterRuleBase classes to perform fuzzy inference.

class ex_fuzzy.rules.MasterRuleBase(rule_base: list[RuleBase], consequent_names: list[str] = None, ds_mode: int = 0, allow_unknown: bool = False)[source]

Bases: object

This Class encompasses a list of rule bases where each one corresponds to a different class.

add_rule(rule: RuleSimple, consequent: int) None[source]

Adds a rule to the rule base of the given consequent.

Parameters:
  • rule – rule to add.

  • consequent – index of the rule base to add the rule.

add_rule_base(rule_base: RuleBase) None[source]

Adds a rule base to the list of rule bases.

Parameters:

rule_base – rule base to add.

compute_association_degrees(X, precomputed_truth=None)[source]

Returns the winning rule for each sample. Takes into account dominance scores if already computed. :param X: array with the values of the inputs. :return: array with the winning rule for each sample.

compute_firing_strenghts(X, precomputed_truth=None) array[source]

Computes the firing strength of each rule for each sample.

Parameters:
  • X – array with the values of the inputs.

  • precomputed_truth – if not None, the antecedent memberships are already computed. (Used for sped up in genetic algorithms)

Returns:

array with the firing strength of each rule for each sample.

fuzzy_type() FUZZY_SETS[source]

Returns the correspoing type of the RuleBase using the enum type in the fuzzy_sets module.

Returns:

the corresponding fuzzy set type of the RuleBase.

get_antecedents() list[fuzzyVariable][source]

Returns the antecedents of the rule base.

get_consequents() list[int][source]

Returns a list with the consequents of each rule base.

Returns:

list with the consequents of each rule base.

get_consequents_names() list[str][source]

Returns a list with the names of the consequents.

Returns:

list with the names of the consequents.

get_rulebase_matrix() list[array][source]

Returns a list with the rulebases for each antecedent in matrix format.

Returns:

list with the rulebases for each antecedent in matrix format.

get_rulebases() list[RuleBase][source]

Returns a list with all the rules.

Returns:

list

get_rules() list[RuleSimple][source]

Returns a list with all the rules.

Returns:

list with all the rules.

get_scores() array[source]

Returns the dominance score for each rule in all the rulebases.

Returns:

array with the dominance score for each rule in all the rulebases.

get_weights() array[source]

Returns the weights for each rule in all the rulebases.

Returns:

array with the weights for each rule in all the rulebases.

n_linguistic_variables() list[int][source]

Returns the number of linguistic variables in the rule base.

predict(X: array) array[source]

Gives the prediction for each sample (same as winning_rule_predict)

Parameters:

X – array of dims: samples x features.

Returns:

vector of predictions, size: samples,

print_rules(return_rules=False) None[source]

Print all the rules for all the consequents.

Parameters:

autoprint – if True, the rules are printed. If False, the rules are returned as a string.

purge_rules(tolerance=0.001) None[source]

Delete the roles with a dominance score lower than the tolerance.

Parameters:

tolerance – tolerance to delete the rules.

rename_cons(consequent_names: list[str]) None[source]

Renames the consequents of the rule base.

winning_rule_predict(X: array, precomputed_truth=None, out_class_names=False) array[source]

Returns the winning rule for each sample. Takes into account dominance scores if already computed.

Parameters:
  • X – array with the values of the inputs.

  • precomputed_truth – if not None, the antecedent memberships are already computed. (Used for sped up in genetic algorithms)

Returns:

array with the winning rule for each sample.

class ex_fuzzy.rules.Rule(antecedents: list[FS], consequent: FS)[source]

Bases: object

Class of Rule designed to work with one single rule. It contains the whole inference functionally in itself.

consequent_centroid() array[source]

Returns the centroid of the consequent using a Karnik and Mendel algorithm.

membership(x: ~numpy.array, tnorm=<function _myprod>) array[source]

Computes the membership of one input to the antecedents of the rule.

Parameters:
  • x – input to compute the membership.

  • tnorm – t-norm to use in the inference process.

class ex_fuzzy.rules.RuleBase(antecedents: list[~ex_fuzzy.fuzzy_sets.fuzzyVariable], rules: list[~ex_fuzzy.rules.RuleSimple], consequent: ~ex_fuzzy.fuzzy_sets.fuzzyVariable = None, tnorm=<function prod>)[source]

Bases: object

Class optimized to work with multiple rules at the same time. Right now supports only one consequent. (Solution: use one rulebase per consequent to study)

add_rule(new_rule: RuleSimple)[source]

Adds a new rule to the rulebase. :param new_rule: rule to add.

add_rules(new_rules: list[RuleSimple])[source]

Adds a list of new rules to the rulebase.

Parameters:

new_rules – list of rules to add.

compute_antecedents_memberships(x: array) list[dict][source]

Returns a list of of dictionaries that contains the memberships for each x value to the ith antecedents, nth linguistic variable. x must be a vector (only one sample)

Parameters:

x – vector with the values of the inputs.

Returns:

a list with the antecedent truth values for each one. Each list is comprised of a list with n elements, where n is the number of linguistic variables in each variable.

compute_rule_antecedent_memberships(x: array, scaled=False, antecedents_memberships: list[array] = None) array[source]

Computes the antecedent truth value of an input array.

Return an array in shape samples x rules (x 2) (last is iv dimension)

Parameters:
  • x – array with the values of the inputs.

  • scaled – if True, the memberships are scaled according to their sums for each sample.

Returns:

array with the memberships of the antecedents for each rule.

abstract forward(x: array) array[source]

Computes the deffuzified output of the fl inference system.

Return a vector of size (samples, )

Parameters:

x – array with the values of the inputs.

Returns:

array with the deffuzified output.

abstract fuzzy_type() FUZZY_SETS[source]

Returns the corresponding type of the RuleBase using the enum type in the fuzzy_sets module.

Returns:

the type of fuzzy set used in the RuleBase.

get_rulebase_matrix()[source]

Returns a matrix with the antecedents values for each rule.

get_rules() list[RuleSimple][source]

Returns the list of rules in the rulebase.

get_scores()[source]

Returns an array with the dominance score for each rule. (Must been already computed by an evalRule object)

get_weights()[source]

Returns an array with the weights for each rule. (Different from dominance scores: must been already computed by an optimization algorithm)

abstract inference(x: array) array[source]

Computes the fuzzy output of the fl inference system.

Return an array in shape samples x 2 (last is iv dimension)

Parameters:

x – array with the values of the inputs.

Returns:

array with the memberships of the consequents for each rule.

n_linguistic_variables() int[source]

Returns the number of linguistic variables in the rule base.

print_rules(return_rules: bool = False) None[source]

Print the rules from the rule base.

Parameters:

return_rules – if True, the rules are returned as a string.

prune_bad_rules(tolerance=0.01) None[source]

Delete the rules from the rule base that do not have a dominance score superior to the threshold or have 0 accuracy in the training set.

Parameters:

tolerance – threshold for the dominance score.

remove_rule(ix: int) None[source]

Removes the rule in the given index. :param ix: index of the rule to remove.

remove_rules(delete_list: list[int]) None[source]

Removes the rules in the given list of indexes.

Parameters:

delete_list – list of indexes of the rules to remove.

scores() array[source]

Returns the dominance score for each rule.

Returns:

array with the dominance score for each rule.

class ex_fuzzy.rules.RuleBaseGT2(antecedents: list[~ex_fuzzy.fuzzy_sets.fuzzyVariable], rules: list[~ex_fuzzy.rules.RuleSimple], consequent: ~ex_fuzzy.fuzzy_sets.fuzzyVariable = None, tnorm=<function prod>)[source]

Bases: RuleBase

Class optimized to work with multiple rules at the same time. Supports only one consequent. (Use one rulebase per consequent to study classification problems. Check MasterRuleBase class for more documentation)

This class supports gt2 fs. (ONLY FOR CLASSIFICATION PROBLEMS)

alpha_compute_rule_antecedent_memberships(x: array, scaled=True, antecedents_memberships=None) array[source]

Computes the membership for the antecedents for all the alpha cuts.

Parameters:
  • x – array with the values of the inputs.

  • scaled – if True, the memberships are scaled to sum 1 in each sample.

  • antecedents_memberships – precomputed antecedent memberships. Not supported for GT2.

Returns:

array with the memberships of the antecedents for each sample.

compute_rule_antecedent_memberships(x: array, scaled=True, antecedents_memberships=None) array[source]

Computes the membership for the antecedents performing the alpha_cut reduction.

Parameters:
  • x – array with the values of the inputs.

  • scaled – if True, the memberships are scaled to sum 1 in each sample.

  • antecedents_memberships – precomputed antecedent memberships. Not supported for GT2.

Returns:

array with the memberships of the antecedents for each sample.

forward(x: array) array[source]

Computes the deffuzified output of the t2 inference system.

Return a vector of size (samples, )

Parameters:

x – array with the values of the inputs.

Returns:

array with the deffuzified output for each sample.

fuzzy_type() FUZZY_SETS[source]

Returns the correspoing type of the RuleBase using the enum type in the fuzzy_sets module.

Returns:

the corresponding fuzzy set type of the RuleBase.

inference(x: array) array[source]

Computes the output of the gt2 inference system.

Return an array in shape samples x alpha_cuts

Parameters:

x – array with the values of the inputs.

Returns:

array with the memberships of the consequents for each sample.

class ex_fuzzy.rules.RuleBaseT1(antecedents: list[~ex_fuzzy.fuzzy_sets.fuzzyVariable], rules: list[~ex_fuzzy.rules.RuleSimple], consequent: ~ex_fuzzy.fuzzy_sets.fuzzyVariable = None, tnorm=<function prod>)[source]

Bases: RuleBase

Class optimized to work with multiple rules at the same time. Supports only one consequent. (Use one rulebase per consequent to study classification problems. Check MasterRuleBase class for more documentation)

This class supports t1 fs.

forward(x: array) array[source]

Same as inference() in the t1 case.

Return a vector of size (samples, )

Parameters:

x – array with the values of the inputs.

Returns:

array with the deffuzified output for each sample.

fuzzy_type() FUZZY_SETS[source]

Returns the correspoing type of the RuleBase using the enum type in the fuzzy_sets module.

Returns:

the corresponding fuzzy set type of the RuleBase.

inference(x: array) array[source]

Computes the output of the t1 inference system.

Return an array in shape samples.

Parameters:

x – array with the values of the inputs.

Returns:

array with the output of the inference system for each sample.

class ex_fuzzy.rules.RuleBaseT2(antecedents: list[~ex_fuzzy.fuzzy_sets.fuzzyVariable], rules: list[~ex_fuzzy.rules.RuleSimple], consequent: ~ex_fuzzy.fuzzy_sets.fuzzyVariable = None, tnorm=<function prod>)[source]

Bases: RuleBase

Class optimized to work with multiple rules at the same time. Supports only one consequent. (Use one rulebase per consequent to study classification problems. Check MasterRuleBase class for more documentation)

This class supports iv approximation for t2 fs.

forward(x: array) array[source]

Computes the deffuzified output of the t2 inference system.

Return a vector of size (samples, )

Parameters:

x – array with the values of the inputs.

Returns:

array with the deffuzified output for each sample.

fuzzy_type() FUZZY_SETS[source]

Returns the correspoing type of the RuleBase using the enum type in the fuzzy_sets module.

Returns:

the corresponding fuzzy set type of the RuleBase.

inference(x: array) array[source]

Computes the iv output of the t2 inference system.

Return an array in shape samples x 2 (last is iv dimension)

Parameters:

x – array with the values of the inputs.

Returns:

array with the memberships of the consequents for each sample.

exception ex_fuzzy.rules.RuleError(message: str)[source]

Bases: Exception

Exception raised when a rule is not well defined.

class ex_fuzzy.rules.RuleSimple(antecedents: list[int], consequent: int = 0, modifiers: array = None)[source]

Bases: object

Class designed to represent rules in its simplest form to optimize the computation in a rulebase.

It indicates the antecedent values using a vector coded in this way:

ith entry: -1 means this variable is not used. 0-N indicates that the variable linguistic used for the ith input is that one.

ex_fuzzy.rules.compute_antecedents_memberships(antecedents: list[fuzzyVariable], x: array) list[dict][source]

Returns a list of of dictionaries that contains the memberships for each x value to the ith antecedents, nth linguistic variable. x must be a vector (only one sample)

Parameters:

x – vector with the values of the inputs.

Returns:

a list with the antecedent truth values for each one. Each list is comprised of a list with n elements, where n is the number of linguistic variables in each variable.

ex_fuzzy.rules.construct_rule_base(rule_matrix: array, nclasses: int, consequents: array, antecedents: list[fuzzyVariable], rule_weights: array, class_names: list = None) MasterRuleBase[source]

Constructs a rule base from a matrix of rules.

Parameters:
  • rule_matrix – matrix with the rules.

  • consequents – array with the consequents per rule.

  • antecedents – list of fuzzy variables.

  • class_names – list with the names of the classes.

ex_fuzzy.rules.generate_rule_string(rule: RuleSimple, antecedents: list) str[source]

Generates a string with the rule.

Parameters:
  • rule – rule to generate the string.

  • antecedents – list of fuzzy variables.

  • modifiers – array with the modifiers for the antecedents.

ex_fuzzy.rules.list_rules_to_matrix(rule_list: list[RuleSimple]) array[source]

Returns a matrix out of the rule list.

Parameters:

rule_list – list of rules.

Returns:

matrix with the antecedents of the rules.