InstanceSpace Python v0.4.1

instancespace.utils.get_classifier_fcn

Registry mapping PythiaOptions.classifier names to scikit-learn estimators.

Mirrors MATLAB's ISAgetClassifierFcn.m dispatch table, including its per-classifier tunable-hyperparameter ranges (classifierBayesVars/ sobolToParams in core/PYTHIA.m) - every registered classifier is tunable, not just 'svm'. 'nb' needs a custom estimator (KernelNB) since MATLAB tunes it via kernel-density estimation (fitcnb(..., 'DistributionNames', 'kernel', 'Width', ...)), which scikit-learn's GaussianNB has no equivalent for - it only fits a per-feature Gaussian, with no bandwidth to tune.

Matching search ranges does not imply identical fitted models or optimizer trajectories across MATLAB and scikit-learn backends.

class KernelNB(sklearn.base.ClassifierMixin, sklearn.base.BaseEstimator):

Kernel-density-estimated Naive Bayes, matching MATLAB's fitcnb.

MATLAB tunes 'nb' via fitcnb(..., 'DistributionNames', 'kernel', 'Width', p1) - a per-feature Gaussian-kernel density estimate per class, not the single fitted Gaussian GaussianNB assumes. There is no scikit-learn estimator for this, so this fits one univariate sklearn.neighbors.KernelDensity per (class, feature) and classifies by the class with the highest total log-density plus log-prior - the standard kernel-NB decision rule.

KernelNB(bandwidth: float = 1.0)

Create an unfitted KernelNB with the given kernel bandwidth.

bandwidth
def fit( self, x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], y: numpy.ndarray[typing.Any, numpy.dtype[numpy.bool_]], sample_weight: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]] | None = None) -> KernelNB:

Fit one KernelDensity per (class, feature), plus class priors.

def predict_proba( self, x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]) -> numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]:

Posterior class probabilities, columns ordered by self.classes_.

def predict( self, x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]) -> numpy.ndarray[typing.Any, numpy.dtype[numpy.bool_]]:

Predict the class with the highest posterior probability.

def set_fit_request(unknown):

A descriptor for request methods.

New in version 1.3.

Parameters
  • name (str): The name of the method for which the request function should be created, e.g. "fit" would create a set_fit_request function.
  • keys (list of str): A list of strings which are accepted parameters by the created function, e.g. ["sample_weight"] if the corresponding method accepts it as a metadata.
  • validate_keys (bool, default=True): Whether to check if the requested parameters fit the actual parameters of the method.
Notes

This class is a descriptor 1 and uses PEP-362 to set the signature of the returned function 2.

References
def set_predict_request(unknown):

A descriptor for request methods.

New in version 1.3.

Parameters
  • name (str): The name of the method for which the request function should be created, e.g. "fit" would create a set_fit_request function.
  • keys (list of str): A list of strings which are accepted parameters by the created function, e.g. ["sample_weight"] if the corresponding method accepts it as a metadata.
  • validate_keys (bool, default=True): Whether to check if the requested parameters fit the actual parameters of the method.
Notes

This class is a descriptor 1 and uses PEP-362 to set the signature of the returned function 2.

References
def set_predict_proba_request(unknown):

A descriptor for request methods.

New in version 1.3.

Parameters
  • name (str): The name of the method for which the request function should be created, e.g. "fit" would create a set_fit_request function.
  • keys (list of str): A list of strings which are accepted parameters by the created function, e.g. ["sample_weight"] if the corresponding method accepts it as a metadata.
  • validate_keys (bool, default=True): Whether to check if the requested parameters fit the actual parameters of the method.
Notes

This class is a descriptor 1 and uses PEP-362 to set the signature of the returned function 2.

References
def set_score_request(unknown):

A descriptor for request methods.

New in version 1.3.

Parameters
  • name (str): The name of the method for which the request function should be created, e.g. "fit" would create a set_fit_request function.
  • keys (list of str): A list of strings which are accepted parameters by the created function, e.g. ["sample_weight"] if the corresponding method accepts it as a metadata.
  • validate_keys (bool, default=True): Whether to check if the requested parameters fit the actual parameters of the method.
Notes

This class is a descriptor 1 and uses PEP-362 to set the signature of the returned function 2.

References
class ParamSpec(typing.NamedTuple):

One tunable hyperparameter's MATLAB-facing search range.

Mirrors one column of MATLAB's classifierBayesVars/sobolToParams (core/PYTHIA.m) - the range a classifier's hyperparameter is searched over, for both the 'sobol' and 'bayes' tuning strategies. low and high always use the values MATLAB reports and accepts in opts.pythia.params; conversion to the estimator's own units happens at the final boundary.

Attributes
  • sklearn_name (str): The estimator's own constructor keyword for this hyperparameter.
  • label (str): Human-readable label, matching ISAgetClassifierFcn.m's p1label/p2label (used for the PYTHIA summary table's columns).
  • low (float): Lower bound of the search range, in MATLAB/reporting units.
  • high (float): Upper bound of the search range, in MATLAB/reporting units.
  • log_scale (bool): Whether to sample/search log-uniformly rather than linearly.
  • is_int (bool): Whether a sampled value must be rounded to an integer.
  • categories (tuple[str, ...] | None): Set only for categorical parameters (KNN's distance metric); low/high are unused when this is set.
  • to_estimator (Callable[[float], float] | None): Converts a MATLAB/reporting-unit value to the estimator's own units. Identity if omitted.
  • to_reported (Callable[[float], float] | None): Converts an estimator value back to MATLAB/reporting units. Identity if omitted. Kept separate from to_estimator because KernelScale and sklearn's gamma are not related by a self-inverse transform.
ParamSpec( sklearn_name: str, label: str, low: float, high: float, log_scale: bool = False, is_int: bool = False, categories: tuple[str, ...] | None = None, to_estimator: Callable[[float], float] | None = None, to_reported: Callable[[float], float] | None = None)

Create new instance of ParamSpec(sklearn_name, label, low, high, log_scale, is_int, categories, to_estimator, to_reported)

sklearn_name: str

Alias for field number 0

label: str

Alias for field number 1

low: float

Alias for field number 2

high: float

Alias for field number 3

log_scale: bool

Alias for field number 4

is_int: bool

Alias for field number 5

categories: tuple[str, ...] | None

Alias for field number 6

to_estimator: Callable[[float], float] | None

Alias for field number 7

to_reported: Callable[[float], float] | None

Alias for field number 8

def sample(self, x: float) -> float | int | str:

Map x in [0,1] through MATLAB units into estimator units.

def dimension(self) -> skopt.space.space.Dimension:

Return this parameter's estimator-unit skopt search dimension.

def reported(self, value: float | int | str) -> float:

Return the value to store in PythiaOutput, in MATLAB's own units.

def from_precalc(self, value: float) -> float | int | str:

Map a raw opts.pythia.params value (MATLAB units) to this classifier's own.

Inverse of reported()'s numeric branch - used for pre-calculated hyperparameters (PythiaOptions.params), which arrive already in MATLAB's reporting units (e.g. a 1-based category index for KNN's Distance, Lambda rather than sklearn's inverse C), not as a [0,1] search-space fraction the way sample()'s input is.

class ClassifierSpec(typing.NamedTuple):

A registry entry describing how to build and tune one classifier type.

Attributes
  • build (Callable[[int | None, bool], ClassifierMixin]): Returns an unfitted estimator, given a random seed (None for non-deterministic) and whether a polynomial kernel was requested (meaningful only for 'svm'; ignored otherwise).
  • supports_sample_weight (bool): Whether the estimator's fit() accepts a sample_weight argument - needed to honour PythiaOptions.use_weights (cost-sensitive classification). Not every scikit-learn classifier does.
  • param1 (ParamSpec): The first (and, for some classifiers, only) tunable hyperparameter.
  • param2 (ParamSpec | None): The second tunable hyperparameter, or None if this classifier only has one (ISAgetClassifierFcn.m's p2label = 'N/A' case).
ClassifierSpec( build: Callable[[int | None, bool], sklearn.base.ClassifierMixin], supports_sample_weight: bool, param1: ParamSpec, param2: ParamSpec | None)

Create new instance of ClassifierSpec(build, supports_sample_weight, param1, param2)

build: Callable[[int | None, bool], sklearn.base.ClassifierMixin]

Alias for field number 0

supports_sample_weight: bool

Alias for field number 1

param1: ParamSpec

Alias for field number 2

param2: ParamSpec | None

Alias for field number 3

def get_classifier_fcn(name: str) -> ClassifierSpec:

Look up a classifier registry entry by PythiaOptions.classifier name.

Raises
  • ValueError: If name isn't a registered classifier.