Supervised learning#
This module contains the base class for the supervised machine learning algorithms.
Supervised machine learning is a task of learning relationships between input and output variables based on an input-output dataset. One usually distinguishes between two types of supervised machine learning algorithms, based on the nature of the outputs. For a continuous output variable, a regression is performed, while for a discrete output variable, a classification is performed.
Given a set of input variables \(x \in \mathbb{R}^{n_{\text{samples}}\times n_{\text{inputs}}}\) and a set of output variables \(y \in \mathbb{K}^{n_{\text{samples}}\times n_{\text{outputs}}}\), where \(n_{\text{inputs}}\) is the dimension of the input variable, \(n_{\text{outputs}}\) is the dimension of the output variable, \(n_{\text{samples}}\) is the number of training samples and \(\mathbb{K}\) is either \(\mathbb{R}\) or \(\mathbb{N}\) for regression and classification tasks respectively, a supervised learning algorithm seeks to find a function \(f: \mathbb{R}^{n_{\text{inputs}}} \to \mathbb{K}^{n_{\text{outputs}}}\) such that \(y=f(x)\).
In addition, we often want to impose some additional constraints on the function \(f\), mainly to ensure that it has a generalization capacity beyond the training data, i.e. it is able to correctly predict output values of new input values. This is called regularization. Assuming \(f\) is parametrized by a set of parameters \(\theta\), and denoting \(f_\theta\) the parametrized function, one typically seeks to minimize a function of the form
where \(\mu\) is a distance-like measure, typically a mean squared error, a cross entropy in the case of a regression, or a probability to be maximized in the case of a classification, and \(\Omega\) is a regularization term that limits the parameters from over-fitting, typically some norm of its argument.
The supervised
module implements this concept
through the BaseMLSupervisedAlgo
class based on an IODataset
.
- class BaseMLSupervisedAlgo(data, settings_model=None, **settings)[source]
Supervised machine learning algorithm.
Inheriting classes shall overload the
BaseMLSupervisedAlgo._fit()
andBaseMLSupervisedAlgo._predict()
methods.- Parameters:
data (Dataset) -- The training dataset.
settings_model (BaseMLAlgoSettings | None) -- The machine learning algorithm settings as a Pydantic model. If
None
, use**settings
.**settings (Any) -- The machine learning algorithm settings. These arguments are ignored when
settings_model
is notNone
.
- Raises:
ValueError -- When both the variable and the group it belongs to have a transformer.
- DataFormatters
alias of
SupervisedDataFormatters
- Settings
alias of
BaseMLSupervisedAlgoSettings
- predict(input_data)[source]
Predict output data from input data.
The user can specify these input data either as a NumPy array, e.g.
array([1., 2., 3.])
or as a dictionary, e.g.{'a': array([1.]), 'b': array([2., 3.])}
.If the numpy arrays are of dimension 2, their i-th rows represent the input data of the i-th sample; while if the numpy arrays are of dimension 1, there is a single sample.
The type of the output data and the dimension of the output arrays will be consistent with the type of the input data and the size of the input arrays.
- DEFAULT_TRANSFORMER: DefaultTransformerType = mappingproxy({'inputs': <gemseo.mlearning.transformers.scaler.min_max_scaler.MinMaxScaler object>})
The default transformer for the input and output data, if any.
- SHORT_ALGO_NAME: ClassVar[str] = 'BaseMLSupervisedAlgo'
The short name of the machine learning algorithm, often an acronym.
Typically used for composite names, e.g.
f"{algo.SHORT_ALGO_NAME}_{dataset.name}"
orf"{algo.SHORT_ALGO_NAME}_{discipline.name}"
.
- property input_data: RealArray
The input data matrix.
- property input_dimension: int
The input space dimension.
- property output_data: ndarray
The output data matrix.
- property output_dimension: int
The output space dimension.