Temporal fuzzy sets#

Temporal Fuzzy Sets Module for Ex-Fuzzy Library

This module extends the base fuzzy sets functionality with temporal-aware fuzzy sets, enabling modeling of time-dependent fuzzy systems and temporal rule bases.

Main Components:
  • TMP_FUZZY_SETS: Enumeration of temporal fuzzy set types

  • TMP_FS: Temporal fuzzy set implementation

  • TMP_fuzzyVariable: Temporal fuzzy variable container

  • TMP_RuleBase: Temporal rule base for complex time-dependent reasoning

The temporal fuzzy sets allow for modeling uncertainty that changes over time, making them suitable for applications like time-series analysis, temporal pattern recognition, and dynamic system modeling.

Key Features:
  • Time-conditional membership functions

  • Temporal rule evaluation

  • Integration with evolutionary optimization

  • Support for both Type-1 and Type-2 temporal fuzzy sets

ex_fuzzy.temporal.TMP_FUZZY_SETS#

alias of NEW_FUZZY_SETS

ex_fuzzy.temporal.NEW_FUZZY_SETS#

alias of FUZZY_SETS

class ex_fuzzy.temporal.temporalFS(std_fuzzy_set, conditional_variable)[source]#

Bases: FS

Class to implement temporal fuzzy sets.

__init__(std_fuzzy_set, conditional_variable)[source]#

Creates a temporal fuzzy set.

Parameters:
  • std_fuzzy_set (FS) – FS. Standard fuzzy set that contains the non-temporal aware memberhsip function.

  • conditional_variable (array) – np.array. The variable that expresses the different temporal moments. Shape (time discrete moments, ).

membership(input, time=None)[source]#

Computes the membership of each sample and in each time for the fs.

Parameters:

input (array) – array temporal_time x samples.

Time:

int. Time moment to compute the membership. If none, looks for a fixed time

Returns:

array temporal_time x samples.

Return type:

array

type()[source]#

Returns the type of the fuzzy set. (temporal)

inside_type()[source]#

Returns the type of the og fuzzy set computed before the time dependency.

fix_time(time)[source]#

Fixes the time of the temporal fuzzy set.

Parameters:

time (int) – int. Time moment to fix.

Returns:

FS. Fuzzy set with the fixed time.

Return type:

None

class ex_fuzzy.temporal.temporalFuzzyVariable(name, fuzzy_sets)[source]#

Bases: fuzzyVariable

Class to implement a temporal fuzzy variable.

__init__(name, fuzzy_sets)[source]#

Creates a temporal fuzzy variable.

Parameters:
  • str – name of the variable.

  • fuzzy_sets (list[temporalFS]) – list of the fuzzy sets pre-time dependencies.

fix_time(time)[source]#

Fixes the time of the temporal fuzzy variable.

Parameters:

time (int) – int. Time moment to fix.

compute_memberships(x, time=None)[source]#

Computes the membership to each of the FS in the fuzzy variables.

Parameters:
  • x (array) – numeric value or array. Computes the membership to each of the IVFS in the fuzzy variables.

  • time (int) – int. Time moment to compute the membership.

Returns:

list of floats. Membership to each of the FS in the fuzzy variables.

Return type:

dict

n_time_moments()[source]#

Returns the number of time moments of the temporal fuzzy variable.

Returns:

int. Number of time moments of the temporal fuzzy variable.

Return type:

int

class ex_fuzzy.temporal.temporalMasterRuleBase(rule_base, time_step_names=None)[source]#

Bases: MasterRuleBase

This class is a temporal extension of the MasterRuleBase class. It includes a list of rule bases for each time step.

__init__(rule_base, time_step_names=None)[source]#

Constructor of the temporalMasterRuleBase class.

Parameters:
  • rule_base (list[MasterRuleBase]) – list of rule bases.

  • time_steps – number of time steps.

add_rule(rule, consequent, time_step)[source]#

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

Parameters:
  • rule (RuleSimple) – rule to add.

  • consequent (int) – index of the rule base to add the rule.

  • time_step (int) – time step of the rule base to add the rule.

get_rulebase_matrix()[source]#

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

Returns:

list with the rulebases for each antecedent in matrix format.

Return type:

list[array]

get_scores()[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.

Return type:

array

compute_firing_strenghts(X, time_moments, precomputed_truth=None)[source]#

Computes the firing strength of each rule for each sample.

Parameters:

X (array) – array with the values of the inputs.

Returns:

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

Return type:

array

winning_rule_predict(X, time_moments)[source]#

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

Parameters:

X (array) – array with the values of the inputs.

Returns:

array with the winning rule for each sample.

Return type:

array

add_rule_base(rule_base, time)[source]#

Adds a rule base to the list of rule bases.

Parameters:

rule_base (RuleBase) – rule base to add.

print_rules(return_rules=False)[source]#

Print all the rules for all the consequents.

get_rules()[source]#

Returns a list with all the rules.

Returns:

list with all the rules.

Return type:

list[RuleSimple]

fuzzy_type()[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.

Return type:

FUZZY_SETS

purge_rules(tolerance=0.001)[source]#

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

Parameters:

tolerance – tolerance to delete the rules.

__getitem__(item)[source]#

Returns the corresponding time rulebase.

Parameters:

item (int) – index of the rulebase.

Returns:

the corresponding rulebase.

Return type:

RuleBase

__len__()[source]#

Returns the number of rule bases.

get_consequents()[source]#

Returns a list with the consequents of each rule base.

Returns:

list with the consequents of each rule base.

Return type:

list[int]

get_rulebases()[source]#

Returns a list with all the rules.

Returns:

list

Return type:

list[RuleBase]

class ex_fuzzy.temporal.TemporalFuzzyRulesClassifier(nRules=30, nAnts=4, fuzzy_type=None, tolerance=0.0, n_linguistic_variables=0, verbose=False, linguistic_variables=None, domain=None, n_class=None, precomputed_rules=None, runner=1)[source]#

Bases: BaseFuzzyRulesClassifier

Class that is used as a classifier for a fuzzy rule based system with time dependencies. Supports precomputed and optimization of the linguistic variables.

__init__(nRules=30, nAnts=4, fuzzy_type=None, tolerance=0.0, n_linguistic_variables=0, verbose=False, linguistic_variables=None, domain=None, n_class=None, precomputed_rules=None, runner=1)[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_linguistic_variables (int) – 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.

kwargs:
  • n_classes: number of classes to predict. Default deduces from data.

fit(X, y, n_gen=50, pop_size=10, time_moments=None, checkpoints=0)[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.

  • time_moments (array) – array of integers. Time moments associated to each sample (when temporal dependencies are present)

Returns:

None. The classifier is fitted to the data.

forward(X, time_moments=None)[source]#

Returns the predicted class for each sample.

Parameters:
  • X (array) – np array samples x features.

  • time_moments (list[int]) – list of integers. Time moments associated to each sample (when temporal dependencies are present)

Returns:

np array samples (x 1) with the predicted class.

Return type:

array

plot_fuzzy_variables()[source]#

Plot the fuzzy partitions in each fuzzy variable.

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]

ex_fuzzy.temporal.eval_temporal_fuzzy_model(fl_classifier, X_train, y_train, X_test, y_test, time_moments=None, test_time_moments=None, plot_rules=True, print_rules=True, plot_partitions=True, return_rules=False, print_accuracy=True, print_matthew=True)[source]#

Function that evaluates a fuzzy rule based model. It also plots the rules and the fuzzy partitions.

Parameters:
  • fl_classifier (BaseFuzzyRulesClassifier) – Fuzzy rule based model.

  • X_train (array) – Training data.

  • y_train (array) – Training labels.

  • X_test (array) – Test data.

  • y_test (array) – Test labels.

  • plot_rules – If True, it plots the rules.

  • print_rules – If True, it prints the rules.

  • plot_partitions – If True, it plots the fuzzy partitions.

Returns:

None

Return type:

None