Classifiers Module#

The ex_fuzzy.classifiers module provides the main classification interface for the ex-fuzzy library.

Overview#

This module contains the high-level classifier that combines rule mining and genetic optimization for fuzzy classification tasks.

Classes#

RuleMineClassifier([nRules, nAnts, ...])

A classifier that works by mining a set of candidate rules with a minimum support, confidence and lift, and then using a genetic algorithm that chooses the optimal combination of those rules.

RuleMineClassifier#

class ex_fuzzy.classifiers.RuleMineClassifier(nRules=30, nAnts=4, fuzzy_type=None, tolerance=0.0, verbose=False, n_class=None, runner=1, linguistic_variables=None)[source]#

Bases: ClassifierMixin

A classifier that works by mining a set of candidate rules with a minimum support, confidence and lift, and then using a genetic algorithm that chooses the optimal combination of those rules.

The main classifier that mines candidate rules and then optimizes them using genetic algorithms.

Constructor Parameters

__init__(nRules=30, nAnts=4, fuzzy_type=None, tolerance=0.0, verbose=False, n_class=None, runner=1, linguistic_variables=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.

  • type (fuzzy) – FUZZY_SET enum type in fuzzy_sets module. The kind of fuzzy set used.

  • tolerance (float) – tolerance for the support/dominance score of the rules.

  • verbose – if True, prints the progress of the optimization.

  • n_class (int) – number of classes in the problem. If None (default) the classifier will compute it empirically.

  • runner (int) – number of threads to use.

  • linguistic_variables (list[fuzzyVariable]) – linguistic variables per antecedent.

Main Methods

fit(X, y, n_gen=30, pop_size=50, **kwargs)[source]#

Trains the model with the given data.

Parameters:
  • X (array) – samples to train.

  • y (array) – labels for each sample.

  • n_gen (int) – number of generations to compute in the genetic optimization.

  • pop_size (int) – number of subjects per generation.

  • kwargs – additional parameters for the genetic optimization. See fit method in BaseRuleBaseClassifier.

predict(X)[source]#

Predict for each sample the corresponding class.

Parameters:

X (array) – samples to predict.

Returns:

a class for each sample.

Return type:

array

internal_classifier()[source]#
__init__(nRules=30, nAnts=4, fuzzy_type=None, tolerance=0.0, verbose=False, n_class=None, runner=1, linguistic_variables=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.

  • type (fuzzy) – FUZZY_SET enum type in fuzzy_sets module. The kind of fuzzy set used.

  • tolerance (float) – tolerance for the support/dominance score of the rules.

  • verbose – if True, prints the progress of the optimization.

  • n_class (int) – number of classes in the problem. If None (default) the classifier will compute it empirically.

  • runner (int) – number of threads to use.

  • linguistic_variables (list[fuzzyVariable]) – linguistic variables per antecedent.

fit(X, y, n_gen=30, pop_size=50, **kwargs)[source]#

Trains the model with the given data.

Parameters:
  • X (array) – samples to train.

  • y (array) – labels for each sample.

  • n_gen (int) – number of generations to compute in the genetic optimization.

  • pop_size (int) – number of subjects per generation.

  • kwargs – additional parameters for the genetic optimization. See fit method in BaseRuleBaseClassifier.

predict(X)[source]#

Predict for each sample the corresponding class.

Parameters:

X (array) – samples to predict.

Returns:

a class for each sample.

Return type:

array

internal_classifier()[source]#
score(X, y, sample_weight=None)#

Return accuracy on provided data and labels.

In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Test samples.

  • y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True labels for X.

  • sample_weight (array-like of shape (n_samples,), default=None) – Sample weights.

Returns:

score – Mean accuracy of self.predict(X) w.r.t. y.

Return type:

float

Examples#

Basic Usage#

from ex_fuzzy.classifiers import RuleMineClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

# Load data
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Create and train classifier
classifier = RuleMineClassifier(nRules=20, nAnts=4, verbose=True)
classifier.fit(X_train, y_train)

# Make predictions
y_pred = classifier.predict(X_test)
accuracy = classifier.score(X_test, y_test)
print(f"Accuracy: {accuracy:.3f}")

See Also#