CN2Classifier#
- class sklearn_nominal.CN2Classifier(criterion='entropy', max_rule_length=9223372036854775807, max_rules=9223372036854775807, min_rule_support=10, max_error_per_rule=0.99, backend='pandas', class_weight=None)[source]#
A rule-based classifier that performs sequential covering in a CN2 [1] style.
[1] Clark, P. and Niblett, T (1989) The CN2 induction algorithm. Machine Learning 3(4):261-283.
- Args:
- criterion (str, optional): The function to measure the quality of a split.
Supported criteria are “gini” for the Gini impurity and “log_loss” and “entropy” both for the Shannon information gain. Defaults to “entropy”.
- max_rule_length (int, optional): The maximum number of conditions in a rule.
Analogous to the maximum height of a Tree model. Defaults to sys.maxsize.
- max_rules (int, optional): Maximum number of rules for the model.
Analogous to the maximum number of leaves in a Tree model. Defaults to sys.maxsize.
- min_rule_support (int, optional): Minimum number of samples that satisfy
the condition of a rule required to include that rule in the model. Analogous to the
min_samples_leafparameter for Tree models. Defaults to 10.- max_error_per_rule (float, optional): Maximum (absolute) error that the
rule can have. This value depends on the error (criterion) used for the model. Defaults to 0.99.
backend (str, optional): The backend to use for computations. Defaults to DEFAULT_BACKEND. class_weight (dict or “balanced”, optional): Weights associated with classes
in the form
{class_label: weight}. If None, all classes are assumed to have weight one. Defaults to None.- Attributes:
classes_ (ndarray of shape (n_classes,)): The classes labels. n_classes_ (int): The number of classes. 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.n_outputs_ (int): The number of outputs when
fitis performed. model_ (RuleModel): The underlying model object.- See Also:
TreeRegressor: A decision tree regressor with nominal support. NaiveBayesClassifier: A NaiveBayesClassifier with nominal support. ZeroRClassifier: A ZeroR classifier with nominal support. OneRClassifier: A OneR classifier with nominal support. PRISMClassifier: A PRISM classifier with nominal support.
- Examples:
>>> from sklearn.datasets import fetch_openml >>> df = fetch_openml("credit-g",version=2).frame >>> x,y = df.iloc[:,0:-1], df.iloc[:,-1] >>> >>> from sklearn_nominal import CN2Classifier >>> model = CN2Classifier() >>> model.fit(x,y) >>> >>> from sklearn.metrics import accuracy_score >>> y_pred = model.predict(x) >>> print(accuracy_score(y,y_pred)) ... 0.787
Methods
build_error(criterion, class_weight)Builds the error function for the given criterion.
Checks if the model has been fitted.
Returns the complexity of the fitted model.
fit(x, y)Fit the CN2 model according to the given training data.
Computes the class weights based on the input target.
get_dtypes(x)Extracts and maps data types from the input.
Returns the names of the features seen during fit.
Get metadata routing of this object.
get_params([deep])Get parameters for this estimator.
get_y(y)Validates and encodes the target labels.
make_model(d, class_weight)Creates the CN2 trainer for the model.
predict(x)Perform classification on an array of test vectors X.
Return probability estimates for the test data X.
pretty_print([class_names])Returns a string representation of the fitted model.
score(X, y[, sample_weight])Return the mean accuracy on the given test data and labels.
set_dtypes(x)Sets and persists the data types based on the input.
set_fit_request(*[, x])Request metadata passed to the
fitmethod.set_model(model)Sets the underlying backend model and marks it as fitted.
set_params(**params)Set the parameters of this estimator.
set_predict_proba_request(*[, x])Request metadata passed to the
predict_probamethod.set_predict_request(*[, x])Request metadata passed to the
predictmethod.set_score_request(*[, sample_weight])Request metadata passed to the
scoremethod.set_sklearn_tags(tags)Sets scikit-learn tags for the supervised nominal model.
Validates and transforms data for classification fitting.
Validates and prepares input data for prediction.
- build_error(criterion, class_weight)#
Builds the error function for the given criterion.
- Parameters:
- criterionstr
The error criterion to use (e.g., “entropy”, “gini”, “gain_ratio”).
- class_weightnp.ndarray
The class weights to be used by the error function.
- Returns:
- TargetError
An instance of the requested error function from
sklearn_nominal.shared.
- Raises:
- ValueError
If the criterion is not recognized.
- Return type:
TargetError
- check_is_fitted()#
Checks if the model has been fitted.
- Raises:
- NotFittedError
If the
is_fitted_attribute is not set or is False.
- complexity()#
Returns the complexity of the fitted model.
The definition of complexity is backend and model dependent. For trees, it typically represents the number of nodes.
- Returns:
- int or float
The complexity metric of the model.
- Raises:
- NotFittedError
If the model has not been fitted yet.
- fit(x, y)[source]#
Fit the CN2 model according to the given training data.
The CN2 algorithm induces a set of classification rules using a sequential covering (or “separate-and-conquer”) process. It repeatedly identifies a rule that covers a subset of the training data and removes the covered samples until a sufficient number of rules is found or all samples are covered.
- Args:
x (pd.DataFrame or np.ndarray): The training input samples. y (np.ndarray): The target values (class labels) as integers or strings.
- Returns:
self: Returns the instance itself.
- get_class_weights(y)#
Computes the class weights based on the input target.
- Parameters:
- yarray-like of shape (n_samples,)
The target labels.
- Returns:
- np.ndarray
The computed weights for each class, aligned with
self.classes_.
- get_dtypes(x)#
Extracts and maps data types from the input.
This method identifies the data types of the input features to ensure they are correctly handled by the backend.
- Parameters:
- x{array-like, sparse matrix} of shape (n_samples, n_features)
The input data.
- Returns:
- dict or None
A dictionary mapping column names to data types if
xis a DataFrame, otherwise None.
- get_feature_names()#
Returns the names of the features seen during fit.
- Returns:
- ndarray of str or None
The feature names, or None if they were not available during fit (e.g., if input was a numpy array).
- 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.
- get_y(y)#
Validates and encodes the target labels.
- Parameters:
- yarray-like of shape (n_samples,)
The target labels to encode.
- Returns:
- ndarray
The integer-encoded target labels.
- make_model(d, class_weight)[source]#
Creates the CN2 trainer for the model.
- Args:
d (Dataset): The dataset to train on. class_weight (np.ndarray): The weights for each class.
- Returns:
CN2: The CN2 trainer instance.
- predict(x)[source]#
Perform classification on an array of test vectors X.
For each input sample, the algorithm evaluates the learned rules in order. The first rule that matches the sample’s features determines the predicted class. If no rules match, the default class (based on the distribution of uncovered training samples) is used.
- Args:
x (pd.DataFrame or np.ndarray): The input samples.
- Returns:
np.ndarray: Predicted target values for X.
- predict_proba(x)[source]#
Return probability estimates for the test data X.
Probabilities are estimated from the class distribution of training samples covered by the first rule that matches the input sample. If no rules match, the probability distribution is based on the uncovered training samples (the default rule).
- Args:
x (pd.DataFrame or np.ndarray): The input samples.
- Returns:
- np.ndarray: Returns the probability of the sample for each class
in the model.
- pretty_print(class_names=None)#
Returns a string representation of the fitted model.
Delegates the visualization logic to the underlying backend model.
- Parameters:
- class_nameslist of str, optional
Names of the classes to use in the output. If None, default identifiers are used.
- Returns:
- str
A human-readable representation of the model.
- Raises:
- NotFittedError
If the model has not been fitted yet.
- score(X, y, sample_weight=None)#
Return the mean accuracy on the given test 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_dtypes(x)#
Sets and persists the data types based on the input.
This is called during
fitto ensure that subsequent calls topredictcan cast the input data to the same types, preserving nominal/numeric distinctions.- Parameters:
- x{pd.DataFrame, np.ndarray, sparse matrix}
The input data to extract types from.
- Raises:
- ValueError
If the input type is not supported or if the input is not 2D.
- set_fit_request(*, x: bool | None | str = '$UNCHANGED$') CN2Classifier#
Request metadata passed to the
fitmethod.Note that this method is only relevant if
enable_metadata_routing=True(seesklearn.set_config()). Please see 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.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline. Otherwise it has no effect.- Parameters:
- xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
xparameter infit.
- Returns:
- selfobject
The updated object.
- set_model(model)#
Sets the underlying backend model and marks it as fitted.
- Parameters:
- modelsklearn_nominal.backend.core.Model
The trained model instance from the backend.
- 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_predict_proba_request(*, x: bool | None | str = '$UNCHANGED$') CN2Classifier#
Request metadata passed to the
predict_probamethod.Note that this method is only relevant if
enable_metadata_routing=True(seesklearn.set_config()). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredict_probaif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict_proba.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.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline. Otherwise it has no effect.- Parameters:
- xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
xparameter inpredict_proba.
- Returns:
- selfobject
The updated object.
- set_predict_request(*, x: bool | None | str = '$UNCHANGED$') CN2Classifier#
Request metadata passed to the
predictmethod.Note that this method is only relevant if
enable_metadata_routing=True(seesklearn.set_config()). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.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.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline. Otherwise it has no effect.- Parameters:
- xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
xparameter inpredict.
- Returns:
- selfobject
The updated object.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') CN2Classifier#
Request metadata passed to the
scoremethod.Note that this method is only relevant if
enable_metadata_routing=True(seesklearn.set_config()). Please see 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.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline. Otherwise it has no effect.- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightparameter inscore.
- Returns:
- selfobject
The updated object.
- set_sklearn_tags(tags)#
Sets scikit-learn tags for the supervised nominal model.
- Parameters:
- tagsTags
The scikit-learn tags object to be modified.
- validate_data_fit_classification(x, y)#
Validates and transforms data for classification fitting.
This method performs the following transformations: 1. Validates
xandyusing scikit-learn’svalidate_data. 2. Determines the unique classes and stores them inclasses_. 3. EncodesyusingLabelEncoder. 4. Calculates class weights based on theclass_weightparameter. 5. Packages features and the encoded target into a backend-specificDataset(e.g.,PandasDataset).- Parameters:
- xarray-like of shape (n_samples, n_features)
The input features.
- yarray-like of shape (n_samples,) or (n_samples, n_outputs)
The target labels.
- Returns:
- tuple
A tuple containing: - Dataset : The backend-specific dataset object. - np.ndarray : The computed class weights for each class in
classes_.
- Raises:
- ValueError
If
ycontains only one unique class.
- validate_data_predict(x)#
Validates and prepares input data for prediction.
This method ensures the input features match the structure seen during training, handles feature name alignment, and restores data types.
- Parameters:
- xarray-like of shape (n_samples, n_features)
The input data to validate.
- Returns:
- pd.DataFrame
The validated data as a pandas DataFrame, with dtypes restored to match those observed during training.
- Raises:
- NotFittedError
If the model has not been fitted yet.
- ValueError
If the input contains no samples or has inconsistent features.