ONN
- class ordinal_xai.models.onn.ONN(hidden_layers: ~typing.List[int] = [64, 64], output_layer: ~torch.nn.modules.module.Module = <class 'dlordinal.output_layers.stick_breaking_layer.StickBreakingLayer'>, loss_function: ~torch.nn.modules.module.Module = <class 'dlordinal.losses.cdw.CDWCELoss'>, dropout: float = 0.2, max_epochs: int = 1000, batch_size: int = 32, lr: float = 0.0001, optimizer: ~torch.optim.optimizer.Optimizer = <class 'torch.optim.adam.Adam'>, patience: int = 10, min_delta: float = 0.0001, verbose: int = 2)[source]
Bases:
BaseEstimator,BaseOrdinalModelOrdinal Neural Network for ordinal regression.
This class implements an ordinal regression model using a neural network with a triangular loss function. The model uses a fully connected network with configurable architecture and training parameters.
- Parameters:
hidden_layers (List[int], default=[64, 64]) – List of hidden layer sizes
dropout (float, default=0.2) – Dropout rate for regularization
max_epochs (int, default=10000) – Maximum number of training epochs
batch_size (int, default=32) – Batch size for training
lr (float, default=0.001) – Learning rate for optimization
patience (int, default=10) – Number of epochs to wait for improvement before early stopping
min_delta (float, default=0.0001) – Minimum change in validation loss to be considered as improvement
verbose (int, default=2) – Verbosity level for training progress
- feature_names_
Names of features used during training
- Type:
list
- n_features_in_
Number of features seen during training
- Type:
int
- ranks_
Unique ordinal class labels
- Type:
ndarray
- _model
The fitted skorch neural network
- Type:
NeuralNet
- _encoder
Encoder for categorical features
- Type:
OneHotEncoder
- _scaler
Scaler for numerical features
- Type:
StandardScaler
- is_fitted_
Whether the model has been fitted
- Type:
bool
Notes
The model handles both categorical and numerical features automatically
Categorical features are one-hot encoded
Numerical features are standardized
Uses early stopping to prevent overfitting
Automatically uses GPU if available
- __init__(hidden_layers: ~typing.List[int] = [64, 64], output_layer: ~torch.nn.modules.module.Module = <class 'dlordinal.output_layers.stick_breaking_layer.StickBreakingLayer'>, loss_function: ~torch.nn.modules.module.Module = <class 'dlordinal.losses.cdw.CDWCELoss'>, dropout: float = 0.2, max_epochs: int = 1000, batch_size: int = 32, lr: float = 0.0001, optimizer: ~torch.optim.optimizer.Optimizer = <class 'torch.optim.adam.Adam'>, patience: int = 10, min_delta: float = 0.0001, verbose: int = 2)[source]
Initialize the Ordinal Neural Network.
- Parameters:
hidden_layers (List[int], default=[64, 64]) – List of hidden layer sizes
dropout (float, default=0.2) – Dropout rate for regularization
max_epochs (int, default=1000) – Maximum number of training epochs
batch_size (int, default=32) – Batch size for training
lr (float, default=0.001) – Learning rate for optimization
optimizer (torch.optim.Optimizer, default=torch.optim.Adam) – Optimizer for optimization
patience (int, default=10) – Number of epochs to wait for improvement before early stopping
min_delta (float, default=0.0001) – Minimum change in validation loss to be considered as improvement
verbose (int, default=2) – Verbosity level for training progress
- get_params(deep: bool = True) Dict[str, any][source]
Get parameters for this estimator.
- Parameters:
deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns:
Parameter names mapped to their values
- Return type:
dict
- set_params(**params: any) ONN[source]
Set the parameters of this estimator.
- Parameters:
**params (dict) – Estimator parameters
- Returns:
self – The estimator instance
- Return type:
ONN
- fit(X: DataFrame, y: Series) ONN[source]
Fit the Ordinal Neural Network model.
This method fits the model to the training data, handling both categorical and numerical features appropriately. It uses early stopping to prevent overfitting and automatically selects the best device (CPU/GPU) for training.
- Parameters:
X (pd.DataFrame of shape (n_samples, n_features)) – Training data
y (pd.Series of shape (n_samples,)) – Target values
- Returns:
self – The fitted model
- Return type:
ONN
- Raises:
ValueError – If the input data contains invalid values
RuntimeError – If there are issues with the neural network training
- predict(X: DataFrame) ndarray[source]
Predict ordinal class labels.
- Parameters:
X (pd.DataFrame of shape (n_samples, n_features)) – Samples to predict
- Returns:
Predicted ordinal class labels
- Return type:
ndarray of shape (n_samples,)
- Raises:
NotFittedError – If the model has not been fitted
- predict_proba(X: DataFrame) ndarray[source]
Predict class probabilities.
- Parameters:
X (pd.DataFrame of shape (n_samples, n_features)) – Samples to predict probabilities for
- Returns:
Predicted class probabilities
- Return type:
ndarray of shape (n_samples, n_classes)
- Raises:
NotFittedError – If the model has not been fitted
- transform(X: DataFrame, fit: bool = False, no_scaling: bool = False) DataFrame[source]
Transform input data into the format expected by the model.
This method handles both categorical and numerical features: - Categorical features are one-hot encoded - Numerical features are standardized (unless no_scaling=True)
- Parameters:
X (pd.DataFrame of shape (n_samples, n_features)) – Input data to transform
fit (bool, default=False) – Whether to fit new encoder/scaler or use existing ones
no_scaling (bool, default=False) – Whether to skip scaling of numerical features
- Returns:
Transformed data
- Return type:
pd.DataFrame
- Raises:
ValueError – If the input data has different features than training data
- _abc_impl = <_abc._abc_data object>
- set_transform_request(*, fit: bool | None | str = '$UNCHANGED$', no_scaling: bool | None | str = '$UNCHANGED$') ONN
Configure whether metadata should be requested to be passed to the
transformmethod.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 totransformif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it totransform.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.
- fitstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
fitparameter intransform.- no_scalingstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
no_scalingparameter intransform.
- selfobject
The updated object.