AnJE#
- class skbn.AnJE(n_join=2, n_bins=5, strategy='quantile', alpha=1.0, categorical_features=None, bernoulli_features=None, gaussian_features=None, n_jobs=None)#
Averaged n-Join Estimators (AnJE) [Generative].
The geometric mean of the n-join estimators of every subset of
nof the \(a\) attributes (Zaidi et al. [1], Eq. 11):\[P(y | x) \propto P(y) \prod_{\alpha \in \binom{A}{n}} P(x_\alpha | y)^{\frac{(n-1)!(a-n)!}{(a-1)!}}.\]The exponent is the inverse of the number of subsets that contain a given attribute, so that every attribute enters the product with total exponent one. A1JE is Naive Bayes, and AnJE uses the same statistics as A(n-1)DE. Probabilities are m-estimates with
m = alpha: \(P(y) = (\#y + m/|Y|) / (t + m)\) and \(P(x_\alpha | y) = (\#(x_\alpha, y) + m/|X_\alpha|) / (\#y + m)\).Continuous attributes are discretized into
n_binsbins (the original work uses MDL discretization, available asMDLPDiscretizerin a pipeline, orstrategy='mdlp'). A discrete value never seen in training carries no information about the class: the n-joins that contain it are left out for that sample, as a missing value would be.- Parameters:
- n_joinint, default=2
Size
nof the attribute subsets (at least 1). With fewer attributes thann_join, the single join of all attributes is used.- n_binsint, default=5
Number of bins for continuous attributes (at least 2).
- strategy{‘uniform’, ‘quantile’, ‘kmeans’, ‘mdlp’}, default=’quantile’
Binning strategy for continuous attributes.
'mdlp'places the cuts with the supervised entropy discretization of Fayyad and Irani (seeMDLPDiscretizer), which chooses the number of bins of each attribute, son_binsis not used.- alphafloat, default=1.0
Non-negative weight
mof the m-estimates; with0the cells unseen in training have zero probability.- categorical_featuresarray-like of int or bool, default=None
Indices (or a boolean mask) of the attributes to treat as discrete regardless of their dtype.
- bernoulli_featuresarray-like of int or bool, default=None
Indices (or a boolean mask) of the attributes to treat as discrete regardless of their dtype.
- gaussian_featuresarray-like of int or bool, default=None
Indices (or a boolean mask) of the attributes to treat as continuous (binned) regardless of their dtype. The attributes in none of the three parameters are typed from their training values with the rules of
MixedNB, and only those typed as Gaussian are binned. Columns of labels (strings, pandas categories) are discrete and cannot be declared continuous.- n_jobsint, default=None
Number of threads used to count and evaluate the n-joins.
Nonemeans 1 unless in ajoblib.parallel_backendcontext.-1means using all processors.
- Attributes:
- classes_ndarray of shape (n_classes,)
Class labels.
- n_features_in_int
Number of features seen during fit.
- feature_names_in_ndarray of shape (
n_features_in_,) Names of features seen during fit. Defined only when
Xhas feature names that are all strings.- joins_ndarray of shape (n_joins, n)
Attribute subsets of the n-joins, one row per subset, with
n = min(n_join, n_features_in_).
See also
ALRAnJE with discriminatively learned weights.
GaussianAnJEAnJE on the factors of the SPnDEs, with Gaussian children.
AnDEAveraged n-Dependence Estimators.
References
[1]Zaidi, N. A., Webb, G. I., Carman, M. J., Petitjean, F., & Cerquides, J. (2016). ALR^n: Accelerated higher-order logistic regression. Machine Learning, 104(2-3), 151-194.
Examples
>>> import numpy as np >>> from skbn import AnJE >>> rng = np.random.RandomState(0) >>> x0 = rng.randint(0, 2, size=200) >>> x1 = rng.randint(0, 2, size=200) >>> x2 = rng.randint(0, 3, size=200) # noise >>> y = x0 ^ x1 # exclusive or: no attribute is informative on its own >>> X = np.column_stack([x0, x1, x2]) >>> clf = AnJE(n_join=2).fit(X, y) >>> clf.score(X, y) 1.0
Methods
fit(X, y[, sample_weight])Count the n-joins of the training data.
Get metadata routing of this object.
get_params([deep])Get parameters for this estimator.
predict(X)Perform classification on an array of test vectors X.
Return log-probability estimates for the test vector X.
Return probability estimates for the test vector X.
score(X, y[, sample_weight])Return accuracy on provided data and labels.
set_fit_request(*[, sample_weight])Configure whether metadata should be requested to be passed to the
fitmethod.set_params(**params)Set the parameters of this estimator.
set_score_request(*[, sample_weight])Configure whether metadata should be requested to be passed to the
scoremethod.- property cut_points_#
Cut-points of the continuous attributes that the model bins, and None for the discrete attributes.
- fit(X, y, sample_weight=None)#
Count the n-joins of the training data.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
Training vectors, where
n_samplesis the number of samples andn_featuresis the number of features. May contain missing values (NaNor missing labels).- yarray-like of shape (n_samples,)
Target values.
- sample_weightarray-like of shape (n_samples,), default=None
Non-negative weights applied to individual samples.
- Returns:
- selfobject
Returns the instance itself.
- get_metadata_routing()#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
- routingMetadataRequest
A
MetadataRequestencapsulating routing information.
- get_params(deep=True)#
Get parameters for this estimator.
- Parameters:
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns:
- paramsdict
Parameter names mapped to their values.
- predict(X)#
Perform classification on an array of test vectors X.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
The input samples.
- Returns:
- Cndarray of shape (n_samples,)
Predicted target values for X.
- predict_log_proba(X)#
Return log-probability estimates for the test vector X.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
The input samples.
- Returns:
- Cndarray of shape (n_samples, n_classes)
Log-probability of the samples for each class, in the order of
classes_.
- predict_proba(X)#
Return probability estimates for the test vector X.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
The input samples.
- Returns:
- Cndarray of shape (n_samples, n_classes)
Probability of the samples for each class, in the order of
classes_.
- 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:
- Xarray-like of shape (n_samples, n_features)
Test samples.
- yarray-like of shape (n_samples,) or (n_samples, n_outputs)
True labels for
X.- sample_weightarray-like of shape (n_samples,), default=None
Sample weights.
- Returns:
- scorefloat
Mean accuracy of
self.predict(X)w.r.t.y.
- set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') AnJE#
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:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightparameter infit.
- Returns:
- selfobject
The updated object.
- set_params(**params)#
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline). The latter have parameters of the form<component>__<parameter>so that it’s possible to update each component of a nested object.- Parameters:
- **paramsdict
Estimator parameters.
- Returns:
- selfestimator instance
Estimator instance.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') AnJE#
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_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightparameter inscore.
- Returns:
- selfobject
The updated object.
Examples using skbn.AnJE#
Tutorial: from a table of labels to a fitted network
AnJE or ALR: generative and discriminative n-joins
A table of continuous measurements: what to expect