gemseo / mlearning / core

Hide inherited members

selection module

This module contains a class to select a machine learning algorithm from a list.

Machine learning is used to find relations or underlying structures in data. There is however no algorithm that is universally better than the others for an arbitrary problem. As for optimization, there is no free lunch for machine learning [Wol96].

Provided a quality measure, one can thus compare the performances of different machine learning algorithms.

This process can be easily performed using the class MLAlgoSelection.

A machine learning algorithm is built using a set of (hyper)parameters, before the learning takes place. In order to choose the best hyperparameters, a simple grid search over different values may be sufficient. The MLAlgoSelection does this. It can also perform a more advanced form of optimization than a simple grid search over predefined values, using the class MLAlgoCalibration.

See also

ml_algo calibration

class gemseo.mlearning.core.selection.MLAlgoSelection(dataset, measure, measure_evaluation_method_name=EvaluationMethod.LEARN, samples=None, **measure_options)[source]

Bases: object

Machine learning algorithm selector.

Parameters:
  • dataset (Dataset) – The learning dataset.

  • measure (str | type[BaseMLAlgoQuality]) – The name of a quality measure to measure the quality of the machine learning algorithms.

  • measure_evaluation_method_name (BaseMLAlgoQuality.EvaluationMethod) –

    The name of the method to evaluate the quality measure.

    By default it is set to “LEARN”.

  • samples (Sequence[int] | None) – The indices of the learning samples to consider. Other indices are neither used for training nor for testing. If None, use all the samples.

  • **measure_options (MeasureOptionType) – The options for the method to evaluate the quality measure. The option ‘multioutput’ will be set to False.

Raises:

ValueError – If the unsupported “multioutput” option is enabled.

add_candidate(name, calib_space=None, calib_algo=None, **option_lists)[source]

Add a machine learning algorithm candidate.

Parameters:
  • name (str) – The name of a machine learning algorithm.

  • calib_space (DesignSpace | None) – The design space defining the parameters to be calibrated with an MLAlgoCalibration. If None, do not perform calibration.

  • calib_algo (ScenarioInputDataType | None) – The name and the parameters of the optimization algorithm, e.g. {“algo”: “fullfact”, “n_samples”: 10}. If None, do not perform calibration.

  • **option_lists – The parameters for the machine learning algorithm candidate. Each parameter has to be enclosed within a list. The list may contain different values to try out for the given parameter, or only one.

Return type:

None

Examples

>>> selector.add_candidate(
>>>     "LinearRegressor",
>>>     penalty_level=[0, 0.1, 1, 10, 20],
>>>     l2_penalty_ratio=[0, 0.5, 1],
>>>     fit_intercept=[True],
>>> )
select(return_quality=False)[source]

Select the best model.

The model is chosen through a grid search over candidates and their options, as well as an eventual optimization over the parameters in the calibration space.

Parameters:

return_quality (bool) –

Whether to return the quality of the best model.

By default it is set to False.

Returns:

The best model and its quality if required.

Return type:

BaseMLAlgo | tuple[BaseMLAlgo, float]

candidates: list[tuple[BaseMLAlgo, float]]

The candidate machine learning algorithms, after possible calibration, and their quality measures.

dataset: Dataset

The learning dataset.

factory: MLAlgoFactory

The factory used for the instantiation of machine learning algorithms.

measure: type[BaseMLAlgoQuality]

The name of a quality measure to measure the quality of the machine learning algorithms.

measure_options: dict[str, int | Dataset]

The options for the method to evaluate the quality measure.

Examples using MLAlgoSelection

Machine learning algorithm selection example

Machine learning algorithm selection example