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#
- class ex_fuzzy.classifiers.RuleMineClassifier(nRules=30, nAnts=4, fuzzy_type=FUZZY_SETS.t1, tolerance=0.0, verbose=False, n_class=None, runner=1, linguistic_variables=None)[source]#
Bases:
ClassifierMixin,BaseEstimatorA 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.
- __init__(nRules=30, nAnts=4, fuzzy_type=FUZZY_SETS.t1, 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
- set_fit_request(*, n_gen='$UNCHANGED$', pop_size='$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:
n_gen (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
n_genparameter infit.pop_size (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
pop_sizeparameter infit.
- 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
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#
ex_fuzzy.evolutionary_fit: Underlying genetic optimizationex_fuzzy.rule_mining: Rule mining functionalityex_fuzzy.fuzzy_sets: Fuzzy set definitions