Zuffy Classifier
Initialisation module for Zuffy.
Zuffy is a sklearn compatible open source python library for the exploration of Fuzzy Pattern Trees. This file defines the package’s public API and its version.
- class zuffy.ZuffyClassifier(class_weight=None, const_range=None, feature_names=None, function_set=(<gplearn.functions._Function object>, <gplearn.functions._Function object>, <gplearn.functions._Function object>), generations=20, init_depth=(2, 10), init_method='half and half', low_memory=False, max_samples=1.0, metric='log loss', multiclassifier='OneVsRestClassifier', n_jobs=1, p_crossover=0.9, p_hoist_mutation=0.01, p_point_mutation=0.01, p_point_replace=0.05, p_subtree_mutation=0.01, parsimony_coefficient=0.001, population_size=1000, random_state=None, stopping_criteria=0.0, tournament_size=20, transformer='sigmoid', verbose=0, warm_start=False)[source]
Bases:
ClassifierMixin,BaseEstimatorA Fuzzy Pattern Tree Classifier which uses genetic programming to infer the model structure, typically with fuzzy operators.
This classifier wraps
gplearn.genetic.SymbolicClassifierand handles multi-class classification usingsklearn.multiclass.OneVsRestClassifierorsklearn.multiclass.OneVsOneClassifier. It uses the OneVsRestClassifier classifier to handle multi-class classifications by default.Most parameters are passed directly to the gplearn.genetic.SymbolicClassifier and further documentation is available on that at https://gplearn.readthedocs.io/en/stable/reference.html#gplearn.genetic.SymbolicClassifier.
Parameters
- class_weightdict, ‘balanced’ or None, default=None
Weights associated with classes in the form
{class_label: weight}. If not given, all classes are supposed to have weight one. The “balanced” mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data asn_samples / (n_classes * np.bincount(y)).- const_rangetuple of two floats, or None, default=None
The range of constants to include in the formulas. If None then no constants will be included in the candidate programs.
- feature_nameslist, optional, default=None
Optional list of feature names, used purely for representations in the print operation or export_graphviz. If None, then X0, X1, etc will be used for representations.
- function_setiterable, default=(COMPLEMENT, MAXIMUM, MINIMUM)
The functions to use when building and evolving programs. This iterable can include strings to indicate individual functions (from gplearn’s built-in set) or custom functions created using make_function from gplearn.functions.
- generationsint, default=20
The number of generations to evolve.
- init_depthtuple of two ints, default=(2, 10)
The range of tree depths for the initial population of naive formulas. Individual trees will randomly choose a maximum depth from this range. When combined with init_method=’half and half’ this yields the well- known ‘ramped half and half’ initialization method.
- init_method{‘half and half’, ‘grow’, ‘full’}, default=’half and half’
The method to use for initializing the population.
‘grow’: Nodes are chosen at random from both functions and terminals, allowing for smaller trees than init_depth allows. Tends to grow asymmetrical trees.
‘full’: Functions are chosen until the init_depth is reached, and then terminals are selected. Tends to grow ‘bushy’ trees.
‘half and half’: Trees are grown through a 50/50 mix of ‘full’ and ‘grow’, making for a mix of tree shapes in the initial population.
- low_memorybool, default=False
When set to
True, only the current generation is retained. Parent information is discarded. For very large populations or runs with many generations, this can result in substantial memory use reduction.- max_samplesfloat, default=1.0
The fraction of samples to draw from X to evaluate each program on.
- metricstr or callable, default=’log loss’
The name of the raw fitness metric. Available options include: - ‘log loss’ aka binary cross-entropy loss. Can also be a custom callable metric.
- multiclassifier{‘OneVsRestClassifier’, ‘OneVsOneClassifier’}, default=’OneVsRestClassifier’
The strategy to use for handling multi-class classification problems.
- n_jobsint, default=1
The number of jobs to run in parallel for fit. If -1, then the number of jobs is set to the number of cores.
- p_crossoverfloat, default=0.9
The probability of performing crossover on a tournament winner.
- p_hoist_mutationfloat, default=0.01
The probability of performing hoist mutation on a tournament winner.
- p_point_mutationfloat, default=0.01
The probability of performing point mutation on a tournament winner.
- p_point_replacefloat, default=0.05
For point mutation only, the probability that any given node will be mutated.
- p_subtree_mutationfloat, default=0.01
The probability of performing subtree mutation on a tournament winner.
- parsimony_coefficientfloat or “auto”, default=0.001
This constant penalizes large programs by adjusting their fitness to be less favorable for selection. Larger values penalize the program more which can control the phenomenon known as ‘bloat’. If “auto” the parsimony coefficient is recalculated for each generation.
- population_sizeint, default=1000
The number of programs in each generation.
- random_stateint, RandomState instance or None, default=None
Controls the pseudo random number generation for reproducibility.
- stopping_criteriafloat, default=0.0
The required metric value required in order to stop evolution early.
- tournament_sizeint, default=20
The number of programs that will compete to become part of the next generation.
- transformerstr or callable, default=’sigmoid’
The name of the function through which the raw decision function is passed. This function will transform the raw decision function into probabilities of each class. Can also be a custom callable transformer.
- verboseint, default=0
Controls the verbosity of the evolution building process.
- warm_startbool, default=False
When set to
True, reuse the solution of the previous call to fit and add more generations to the evolution, otherwise, just fit a new evolution.
Attributes
- classes_ndarray of shape (n_classes,)
The unique class labels observed in y.
- multi_OneVsRestClassifier or OneVsOneClassifier
The underlying scikit-learn multi-class classifier used for training.
- n_features_in_int
Number of features seen during fit.
- feature_names_in_ndarray of str, shape (n_features_in_,)
Names of features seen during fit. Defined only when X has feature names that are all strings.
See Also
gplearn.genetic.SymbolicClassifier : The core genetic programming classifier. sklearn.multiclass.OneVsRestClassifier : Multi-class strategy. sklearn.multiclass.OneVsOneClassifier : Multi-class strategy.
Examples
>>> from sklearn.datasets import load_iris >>> from zuffy.zuffy import ZuffyClassifier # Assuming zuffy is installed and on path >>> X, y = load_iris(return_X_y=True) >>> clf = ZuffyClassifier(random_state=42).fit(X, y) >>> predictions = clf.predict(X) >>> print(predictions.shape) (150,)
- _parameter_constraints = {'class_weight': [<sklearn.utils._param_validation.StrOptions object>, <class 'dict'>, None], 'const_range': [None, <class 'tuple'>], 'feature_names': [<class 'list'>, None], 'function_set': ['array-like'], 'generations': [<sklearn.utils._param_validation.Interval object>], 'init_depth': [<class 'tuple'>], 'init_method': [<sklearn.utils._param_validation.StrOptions object>], 'low_memory': [<class 'bool'>], 'max_samples': [<sklearn.utils._param_validation.Interval object>], 'metric': [<sklearn.utils._param_validation.StrOptions object>, <built-in function callable>], 'multiclassifier': [<sklearn.utils._param_validation.StrOptions object>], 'n_jobs': [<sklearn.utils._param_validation.Interval object>], 'p_crossover': [<sklearn.utils._param_validation.Interval object>], 'p_hoist_mutation': [<sklearn.utils._param_validation.Interval object>], 'p_point_mutation': [<sklearn.utils._param_validation.Interval object>], 'p_point_replace': [<sklearn.utils._param_validation.Interval object>], 'p_subtree_mutation': [<sklearn.utils._param_validation.Interval object>], 'parsimony_coefficient': [<sklearn.utils._param_validation.Interval object>, <sklearn.utils._param_validation.StrOptions object>], 'population_size': [<sklearn.utils._param_validation.Interval object>], 'random_state': ['random_state'], 'stopping_criteria': [<sklearn.utils._param_validation.Interval object>], 'tournament_size': [<sklearn.utils._param_validation.Interval object>], 'transformer': [<sklearn.utils._param_validation.StrOptions object>, <built-in function callable>], 'verbose': [<class 'numbers.Integral'>, <class 'bool'>], 'warm_start': [<class 'bool'>]}
- default_function_set = (<gplearn.functions._Function object>, <gplearn.functions._Function object>, <gplearn.functions._Function object>)
- fit(X, y)[source]
Fit the Fuzzy Pattern Tree Classifier.
Parameters
- Xarray-like of shape (n_samples, n_features)
The training input samples.
- yarray-like of shape (n_samples,)
The target values. An array of int or str.
Returns
- selfobject
Returns self.
- predict(X)[source]
Predict class labels for samples in X.
Parameters
- Xarray-like, shape (n_samples, n_features)
The input samples.
Returns
- yndarray, shape (n_samples,)
The predicted class labels for each sample.
- predict_proba(X)[source]
Predict class probabilities for samples in X.
Parameters
- Xarray-like of shape (n_samples, n_features)
The input samples.
Returns
- probandarray of shape (n_samples, n_classes)
The class probabilities for each sample.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') ZuffyClassifier
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.
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightparameter inscore.
- selfobject
The updated object.