Measure the quality of a machine learning algorithm

Introduction

Measuring the quality of a machine learning algorithm.

class gemseo.mlearning.qual_measure.quality_measure.MLQualityMeasure(algo, fit_transformers=True)[source]

An abstract quality measure to assess a machine learning algorithm.

This measure can be minimized (e.g. MSEMeasure) or maximized (e.g. R2Measure).

It can be evaluated from the learning dataset, from a test dataset or using resampling techniques such as boostrap, cross-validation or leave-one-out.

The machine learning algorithm is usually trained. If not but required by the evaluation technique, the quality measure will train it.

Lastly, the transformers of the algorithm fitted from the learning dataset can be used as is by the resampling methods or re-fitted for each algorithm trained on a subset of the learning dataset.

Parameters:
  • algo (MLAlgo) – A machine learning algorithm.

  • fit_transformers (bool) –

    Whether to re-fit the transformers when using resampling techniques. If False, use the transformers of the algorithm fitted from the whole learning dataset.

    By default it is set to True.

evaluate(method='learn', samples=None, multioutput=True, **options)[source]

Evaluate the quality measure.

Parameters:
  • method (str) –

    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. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • **options (OptionType | None) – The options of the estimation method (e.g. test_data for the test method, n_replicates for the bootstrap one, …).

Returns:

The value of the quality measure.

Raises:

ValueError – When the name of the method is unknown.

Return type:

MeasureType

abstract evaluate_bootstrap(n_replicates=100, samples=None, multioutput=True, seed=None)[source]

Evaluate the quality measure using the bootstrap technique.

Parameters:
  • n_replicates (int) –

    The number of bootstrap replicates.

    By default it is set to 100.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • seed (int | None) – The seed of the pseudo-random number generator. If None, then an unpredictable generator will be used.

Returns:

The value of the quality measure.

Return type:

MeasureType

abstract evaluate_kfolds(n_folds=5, samples=None, multioutput=True, randomize=True, seed=None)[source]

Evaluate the quality measure using the k-folds technique.

Parameters:
  • n_folds (int) –

    The number of folds.

    By default it is set to 5.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • randomize (bool) –

    Whether to shuffle the samples before dividing them in folds.

    By default it is set to True.

  • seed (int | None) – The seed of the pseudo-random number generator. If None, then an unpredictable generator will be used.

Returns:

The value of the quality measure.

Return type:

MeasureType

abstract evaluate_learn(samples=None, multioutput=True)[source]

Evaluate the quality measure from the learning dataset.

Parameters:
  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_loo(samples=None, multioutput=True)[source]

Evaluate the quality measure using the leave-one-out technique.

Parameters:
  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

Returns:

The value of the quality measure.

Return type:

MeasureType

abstract evaluate_test(test_data, samples=None, multioutput=True)[source]

Evaluate the quality measure using a test dataset.

Parameters:
  • test_data (Dataset) – The test dataset.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

Returns:

The value of the quality measure.

Return type:

MeasureType

classmethod is_better(val1, val2)[source]

Compare the quality between two values.

This method returns True if the first one is better than the second one.

For most measures, a smaller value is “better” than a larger one (MSE etc.). But for some, like an R2-measure, higher values are better than smaller ones. This comparison method correctly handles this, regardless of the type of measure.

Parameters:
  • val1 (float) – The value of the first quality measure.

  • val2 (float) – The value of the second quality measure.

Returns:

Whether val1 is of better quality than val2.

Return type:

bool

BOOTSTRAP: ClassVar[str] = 'bootstrap'

The name of the method to evaluate the measure by bootstrap.

KFOLDS: ClassVar[str] = 'kfolds'

The name of the method to evaluate the measure by cross-validation.

LEARN: ClassVar[str] = 'learn'

The name of the method to evaluate the measure on the learning dataset.

LOO: ClassVar[str] = 'loo'

The name of the method to evaluate the measure by leave-one-out.

SMALLER_IS_BETTER: ClassVar[bool] = True

Whether to minimize or maximize the measure.

TEST: ClassVar[str] = 'test'

The name of the method to evaluate the measure on a test dataset.

algo: MLAlgo

The machine learning algorithm usually trained.

class gemseo.mlearning.qual_measure.quality_measure.MLQualityMeasureFactory(*args, **kwargs)[source]

A factory of MLQualityMeasure.

Parameters:
  • base_class – The base class to be considered.

  • module_names – The fully qualified modules names to be searched.

  • args (Any) –

  • kwargs (Any) –

static cache_clear()

Clear the cache.

Return type:

None

create(class_name, **options)

Return an instance of a class.

Parameters:
  • class_name (str) – The name of the class.

  • **options (Any) – The arguments to be passed to the class constructor.

Returns:

The instance of the class.

Raises:

TypeError – If the class cannot be instantiated.

Return type:

Any

get_class(name)

Return a class from its name.

Parameters:

name (str) – The name of the class.

Returns:

The class.

Raises:

ImportError – If the class is not available.

Return type:

type[Any]

get_default_options_values(name)

Return the constructor kwargs default values of a class.

Parameters:

name (str) – The name of the class.

Returns:

The mapping from the argument names to their default values.

Return type:

dict[str, str | int | float | bool]

get_default_sub_options_values(name, **options)

Return the default values of the sub options of a class.

Parameters:
  • name (str) – The name of the class.

  • **options (str) – The options to be passed to the class required to deduce the sub options.

Returns:

The JSON grammar.

Return type:

JSONGrammar

get_library_name(name)

Return the name of the library related to the name of a class.

Parameters:

name (str) – The name of the class.

Returns:

The name of the library.

Return type:

str

get_options_doc(name)

Return the constructor documentation of a class.

Parameters:

name (str) – The name of the class.

Returns:

The mapping from the argument names to their documentation.

Return type:

dict[str, str]

get_options_grammar(name, write_schema=False, schema_path=None)

Return the options JSON grammar for a class.

Attempt to generate a JSONGrammar from the arguments of the __init__ method of the class.

Parameters:
  • name (str) – The name of the class.

  • write_schema (bool) –

    If True, write the JSON schema to a file.

    By default it is set to False.

  • schema_path (str | None) – The path to the JSON schema file. If None, the file is saved in the current directory in a file named after the name of the class.

Returns:

The JSON grammar.

Return type:

JSONGrammar

get_sub_options_grammar(name, **options)

Return the JSONGrammar of the sub options of a class.

Parameters:
  • name (str) – The name of the class.

  • **options (str) – The options to be passed to the class required to deduce the sub options.

Returns:

The JSON grammar.

Return type:

JSONGrammar

is_available(name)

Return whether a class can be instantiated.

Parameters:

name (str) – The name of the class.

Returns:

Whether the class can be instantiated.

Return type:

bool

update()

Search for the classes that can be instantiated.

The search is done in the following order:
  1. The fully qualified module names

  2. The plugin packages

  3. The packages from the environment variables

Return type:

None

property classes: list[str]

The sorted names of the available classes.

Measures for supervised models

Introduction

Here is the baseclass to measure the error of machine learning algorithms.

The concept of error measure is implemented with the MLErrorMeasure class and proposes different evaluation methods.

class gemseo.mlearning.qual_measure.error_measure.MLErrorMeasure(algo, fit_transformers=True)[source]

An abstract error measure for machine learning.

Parameters:
  • algo (MLSupervisedAlgo) – A machine learning algorithm for supervised learning.

  • fit_transformers (bool) –

    Whether to re-fit the transformers when using resampling techniques. If False, use the transformers of the algorithm fitted from the whole learning dataset.

    By default it is set to True.

evaluate(method='learn', samples=None, multioutput=True, **options)

Evaluate the quality measure.

Parameters:
  • method (str) –

    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. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • **options (OptionType | None) – The options of the estimation method (e.g. test_data for the test method, n_replicates for the bootstrap one, …).

Returns:

The value of the quality measure.

Raises:

ValueError – When the name of the method is unknown.

Return type:

MeasureType

evaluate_bootstrap(n_replicates=100, samples=None, multioutput=True, seed=None, as_dict=False)[source]

Evaluate the quality measure using the bootstrap technique.

Parameters:
  • n_replicates (int) –

    The number of bootstrap replicates.

    By default it is set to 100.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • seed (None | None) – The seed of the pseudo-random number generator. If None, then an unpredictable generator will be used.

  • as_dict (bool) –

    Whether to express the measure as a dictionary whose keys are the output names.

    By default it is set to False.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_kfolds(n_folds=5, samples=None, multioutput=True, randomize=True, seed=None, as_dict=False)[source]

Evaluate the quality measure using the k-folds technique.

Parameters:
  • n_folds (int) –

    The number of folds.

    By default it is set to 5.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • randomize (bool) –

    Whether to shuffle the samples before dividing them in folds.

    By default it is set to True.

  • seed (int | None) – The seed of the pseudo-random number generator. If None, then an unpredictable generator will be used.

  • as_dict (bool) –

    Whether to express the measure as a dictionary whose keys are the output names.

    By default it is set to False.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_learn(samples=None, multioutput=True, as_dict=False)[source]

Evaluate the quality measure from the learning dataset.

Parameters:
  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • as_dict (bool) –

    Whether to express the measure as a dictionary whose keys are the output names.

    By default it is set to False.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_loo(samples=None, multioutput=True, as_dict=False)[source]

Evaluate the quality measure using the leave-one-out technique.

Parameters:
  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • as_dict (bool) –

    Whether to express the measure as a dictionary whose keys are the output names.

    By default it is set to False.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_test(test_data, samples=None, multioutput=True, as_dict=False)[source]

Evaluate the quality measure using a test dataset.

Parameters:
  • test_data (Dataset) – The test dataset.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • as_dict (bool) –

    Whether to express the measure as a dictionary whose keys are the output names.

    By default it is set to False.

Returns:

The value of the quality measure.

Return type:

MeasureType

classmethod is_better(val1, val2)

Compare the quality between two values.

This method returns True if the first one is better than the second one.

For most measures, a smaller value is “better” than a larger one (MSE etc.). But for some, like an R2-measure, higher values are better than smaller ones. This comparison method correctly handles this, regardless of the type of measure.

Parameters:
  • val1 (float) – The value of the first quality measure.

  • val2 (float) – The value of the second quality measure.

Returns:

Whether val1 is of better quality than val2.

Return type:

bool

BOOTSTRAP: ClassVar[str] = 'bootstrap'

The name of the method to evaluate the measure by bootstrap.

KFOLDS: ClassVar[str] = 'kfolds'

The name of the method to evaluate the measure by cross-validation.

LEARN: ClassVar[str] = 'learn'

The name of the method to evaluate the measure on the learning dataset.

LOO: ClassVar[str] = 'loo'

The name of the method to evaluate the measure by leave-one-out.

SMALLER_IS_BETTER: ClassVar[bool] = True

Whether to minimize or maximize the measure.

TEST: ClassVar[str] = 'test'

The name of the method to evaluate the measure on a test dataset.

algo: MLAlgo

The machine learning algorithm usually trained.

The MSE

The mean squared error to measure the quality of a regression algorithm.

The mse_measure module implements the concept of mean squared error measures for machine learning algorithms.

This concept is implemented through the MSEMeasure class and overloads the MLErrorMeasure._compute_measure() method.

The mean squared error (MSE) is defined by

\[\operatorname{MSE}(\hat{y})=\frac{1}{n}\sum_{i=1}^n(\hat{y}_i-y_i)^2,\]

where \(\hat{y}\) are the predictions and \(y\) are the data points.

class gemseo.mlearning.qual_measure.mse_measure.MSEMeasure(algo, fit_transformers=True)[source]

The Mean Squared Error measure for machine learning.

Parameters:
  • algo (MLRegressionAlgo) – A machine learning algorithm for regression.

  • fit_transformers (bool) –

    Whether to re-fit the transformers when using resampling techniques. If False, use the transformers of the algorithm fitted from the whole learning dataset.

    By default it is set to True.

evaluate(method='learn', samples=None, multioutput=True, **options)

Evaluate the quality measure.

Parameters:
  • method (str) –

    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. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • **options (OptionType | None) – The options of the estimation method (e.g. test_data for the test method, n_replicates for the bootstrap one, …).

Returns:

The value of the quality measure.

Raises:

ValueError – When the name of the method is unknown.

Return type:

MeasureType

evaluate_bootstrap(n_replicates=100, samples=None, multioutput=True, seed=None, as_dict=False)

Evaluate the quality measure using the bootstrap technique.

Parameters:
  • n_replicates (int) –

    The number of bootstrap replicates.

    By default it is set to 100.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • seed (None | None) – The seed of the pseudo-random number generator. If None, then an unpredictable generator will be used.

  • as_dict (bool) –

    Whether to express the measure as a dictionary whose keys are the output names.

    By default it is set to False.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_kfolds(n_folds=5, samples=None, multioutput=True, randomize=True, seed=None, as_dict=False)

Evaluate the quality measure using the k-folds technique.

Parameters:
  • n_folds (int) –

    The number of folds.

    By default it is set to 5.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • randomize (bool) –

    Whether to shuffle the samples before dividing them in folds.

    By default it is set to True.

  • seed (int | None) – The seed of the pseudo-random number generator. If None, then an unpredictable generator will be used.

  • as_dict (bool) –

    Whether to express the measure as a dictionary whose keys are the output names.

    By default it is set to False.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_learn(samples=None, multioutput=True, as_dict=False)

Evaluate the quality measure from the learning dataset.

Parameters:
  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • as_dict (bool) –

    Whether to express the measure as a dictionary whose keys are the output names.

    By default it is set to False.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_loo(samples=None, multioutput=True, as_dict=False)

Evaluate the quality measure using the leave-one-out technique.

Parameters:
  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • as_dict (bool) –

    Whether to express the measure as a dictionary whose keys are the output names.

    By default it is set to False.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_test(test_data, samples=None, multioutput=True, as_dict=False)

Evaluate the quality measure using a test dataset.

Parameters:
  • test_data (Dataset) – The test dataset.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • as_dict (bool) –

    Whether to express the measure as a dictionary whose keys are the output names.

    By default it is set to False.

Returns:

The value of the quality measure.

Return type:

MeasureType

classmethod is_better(val1, val2)

Compare the quality between two values.

This method returns True if the first one is better than the second one.

For most measures, a smaller value is “better” than a larger one (MSE etc.). But for some, like an R2-measure, higher values are better than smaller ones. This comparison method correctly handles this, regardless of the type of measure.

Parameters:
  • val1 (float) – The value of the first quality measure.

  • val2 (float) – The value of the second quality measure.

Returns:

Whether val1 is of better quality than val2.

Return type:

bool

BOOTSTRAP: ClassVar[str] = 'bootstrap'

The name of the method to evaluate the measure by bootstrap.

KFOLDS: ClassVar[str] = 'kfolds'

The name of the method to evaluate the measure by cross-validation.

LEARN: ClassVar[str] = 'learn'

The name of the method to evaluate the measure on the learning dataset.

LOO: ClassVar[str] = 'loo'

The name of the method to evaluate the measure by leave-one-out.

SMALLER_IS_BETTER: ClassVar[bool] = True

Whether to minimize or maximize the measure.

TEST: ClassVar[str] = 'test'

The name of the method to evaluate the measure on a test dataset.

algo: MLAlgo

The machine learning algorithm usually trained.

The R2

The R2 to measure the quality of a regression algorithm.

The r2_measure module implements the concept of R2 measures for machine learning algorithms.

This concept is implemented through the R2Measure class and overloads the MLErrorMeasure._compute_measure() method.

The R2 is defined by

\[R_2(\hat{y}) = 1 - \frac{\sum_i (\hat{y}_i - y_i)^2} {\sum_i (y_i-\bar{y})^2},\]

where \(\hat{y}\) are the predictions, \(y\) are the data points and \(\bar{y}\) is the mean of \(y\).

class gemseo.mlearning.qual_measure.r2_measure.R2Measure(algo, fit_transformers=True)[source]

The R2 measure for machine learning.

Parameters:
  • algo (MLRegressionAlgo) – A machine learning algorithm for regression.

  • fit_transformers (bool) –

    Whether to re-fit the transformers when using resampling techniques. If False, use the transformers of the algorithm fitted from the whole learning dataset.

    By default it is set to True.

evaluate(method='learn', samples=None, multioutput=True, **options)

Evaluate the quality measure.

Parameters:
  • method (str) –

    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. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • **options (OptionType | None) – The options of the estimation method (e.g. test_data for the test method, n_replicates for the bootstrap one, …).

Returns:

The value of the quality measure.

Raises:

ValueError – When the name of the method is unknown.

Return type:

MeasureType

evaluate_bootstrap(n_replicates=100, samples=None, multioutput=True, seed=None, as_dict=False)[source]

Evaluate the quality measure using the bootstrap technique.

Parameters:
  • n_replicates (int) –

    The number of bootstrap replicates.

    By default it is set to 100.

  • samples (list[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • seed (int | None) – The seed of the pseudo-random number generator. If None, then an unpredictable generator will be used.

  • as_dict (bool) –

    Whether to express the measure as a dictionary whose keys are the output names.

    By default it is set to False.

Returns:

The value of the quality measure.

Return type:

NoReturn

evaluate_kfolds(n_folds=5, samples=None, multioutput=True, randomize=True, seed=None, as_dict=False)[source]

Evaluate the quality measure using the k-folds technique.

Parameters:
  • n_folds (int) –

    The number of folds.

    By default it is set to 5.

  • samples (list[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • randomize (bool) –

    Whether to shuffle the samples before dividing them in folds.

    By default it is set to True.

  • seed (int | None) – The seed of the pseudo-random number generator. If None, then an unpredictable generator will be used.

  • as_dict (bool) –

    Whether to express the measure as a dictionary whose keys are the output names.

    By default it is set to False.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_learn(samples=None, multioutput=True, as_dict=False)

Evaluate the quality measure from the learning dataset.

Parameters:
  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • as_dict (bool) –

    Whether to express the measure as a dictionary whose keys are the output names.

    By default it is set to False.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_loo(samples=None, multioutput=True, as_dict=False)

Evaluate the quality measure using the leave-one-out technique.

Parameters:
  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • as_dict (bool) –

    Whether to express the measure as a dictionary whose keys are the output names.

    By default it is set to False.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_test(test_data, samples=None, multioutput=True, as_dict=False)

Evaluate the quality measure using a test dataset.

Parameters:
  • test_data (Dataset) – The test dataset.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • as_dict (bool) –

    Whether to express the measure as a dictionary whose keys are the output names.

    By default it is set to False.

Returns:

The value of the quality measure.

Return type:

MeasureType

classmethod is_better(val1, val2)

Compare the quality between two values.

This method returns True if the first one is better than the second one.

For most measures, a smaller value is “better” than a larger one (MSE etc.). But for some, like an R2-measure, higher values are better than smaller ones. This comparison method correctly handles this, regardless of the type of measure.

Parameters:
  • val1 (float) – The value of the first quality measure.

  • val2 (float) – The value of the second quality measure.

Returns:

Whether val1 is of better quality than val2.

Return type:

bool

BOOTSTRAP: ClassVar[str] = 'bootstrap'

The name of the method to evaluate the measure by bootstrap.

KFOLDS: ClassVar[str] = 'kfolds'

The name of the method to evaluate the measure by cross-validation.

LEARN: ClassVar[str] = 'learn'

The name of the method to evaluate the measure on the learning dataset.

LOO: ClassVar[str] = 'loo'

The name of the method to evaluate the measure by leave-one-out.

SMALLER_IS_BETTER: ClassVar[bool] = False

Whether to minimize or maximize the measure.

TEST: ClassVar[str] = 'test'

The name of the method to evaluate the measure on a test dataset.

algo: MLAlgo

The machine learning algorithm usually trained.

The F1 (for classification)

The F1 to measure the quality of a classification algorithm.

The F1 is defined by

\[F_1 = 2\frac{\mathit{precision}\mathit{recall}} {\mathit{precision}+\mathit{recall}}\]

where \(\mathit{precision}\) is the number of correctly predicted positives divided by the total number of predicted positives and \(\mathit{recall}\) is the number of correctly predicted positives divided by the total number of true positives.

class gemseo.mlearning.qual_measure.f1_measure.F1Measure(algo, fit_transformers=True)[source]

The F1 measure for machine learning.

Parameters:
  • algo (MLClassificationAlgo) – A machine learning algorithm for classification.

  • fit_transformers (bool) –

    Whether to re-fit the transformers when using resampling techniques. If False, use the transformers of the algorithm fitted from the whole learning dataset.

    By default it is set to True.

evaluate(method='learn', samples=None, multioutput=True, **options)

Evaluate the quality measure.

Parameters:
  • method (str) –

    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. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • **options (OptionType | None) – The options of the estimation method (e.g. test_data for the test method, n_replicates for the bootstrap one, …).

Returns:

The value of the quality measure.

Raises:

ValueError – When the name of the method is unknown.

Return type:

MeasureType

evaluate_bootstrap(n_replicates=100, samples=None, multioutput=True, seed=None, as_dict=False)

Evaluate the quality measure using the bootstrap technique.

Parameters:
  • n_replicates (int) –

    The number of bootstrap replicates.

    By default it is set to 100.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • seed (None | None) – The seed of the pseudo-random number generator. If None, then an unpredictable generator will be used.

  • as_dict (bool) –

    Whether to express the measure as a dictionary whose keys are the output names.

    By default it is set to False.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_kfolds(n_folds=5, samples=None, multioutput=True, randomize=True, seed=None, as_dict=False)

Evaluate the quality measure using the k-folds technique.

Parameters:
  • n_folds (int) –

    The number of folds.

    By default it is set to 5.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • randomize (bool) –

    Whether to shuffle the samples before dividing them in folds.

    By default it is set to True.

  • seed (int | None) – The seed of the pseudo-random number generator. If None, then an unpredictable generator will be used.

  • as_dict (bool) –

    Whether to express the measure as a dictionary whose keys are the output names.

    By default it is set to False.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_learn(samples=None, multioutput=True, as_dict=False)

Evaluate the quality measure from the learning dataset.

Parameters:
  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • as_dict (bool) –

    Whether to express the measure as a dictionary whose keys are the output names.

    By default it is set to False.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_loo(samples=None, multioutput=True, as_dict=False)

Evaluate the quality measure using the leave-one-out technique.

Parameters:
  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • as_dict (bool) –

    Whether to express the measure as a dictionary whose keys are the output names.

    By default it is set to False.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_test(test_data, samples=None, multioutput=True, as_dict=False)

Evaluate the quality measure using a test dataset.

Parameters:
  • test_data (Dataset) – The test dataset.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • as_dict (bool) –

    Whether to express the measure as a dictionary whose keys are the output names.

    By default it is set to False.

Returns:

The value of the quality measure.

Return type:

MeasureType

classmethod is_better(val1, val2)

Compare the quality between two values.

This method returns True if the first one is better than the second one.

For most measures, a smaller value is “better” than a larger one (MSE etc.). But for some, like an R2-measure, higher values are better than smaller ones. This comparison method correctly handles this, regardless of the type of measure.

Parameters:
  • val1 (float) – The value of the first quality measure.

  • val2 (float) – The value of the second quality measure.

Returns:

Whether val1 is of better quality than val2.

Return type:

bool

BOOTSTRAP: ClassVar[str] = 'bootstrap'

The name of the method to evaluate the measure by bootstrap.

KFOLDS: ClassVar[str] = 'kfolds'

The name of the method to evaluate the measure by cross-validation.

LEARN: ClassVar[str] = 'learn'

The name of the method to evaluate the measure on the learning dataset.

LOO: ClassVar[str] = 'loo'

The name of the method to evaluate the measure by leave-one-out.

SMALLER_IS_BETTER: ClassVar[bool] = False

Whether to minimize or maximize the measure.

TEST: ClassVar[str] = 'test'

The name of the method to evaluate the measure on a test dataset.

algo: MLAlgo

The machine learning algorithm usually trained.

Measures for clustering models

Introduction

Here is the baseclass to measure the quality of machine learning algorithms.

The concept of clustering quality measure is implemented with the MLClusteringMeasure class and proposes different evaluation methods.

class gemseo.mlearning.qual_measure.cluster_measure.MLClusteringMeasure(algo, fit_transformers=True)[source]

An abstract clustering measure for clustering algorithms.

Parameters:
  • algo (MLClusteringAlgo) – A machine learning algorithm for clustering.

  • fit_transformers (bool) –

    Whether to re-fit the transformers when using resampling techniques. If False, use the transformers of the algorithm fitted from the whole learning dataset.

    By default it is set to True.

evaluate(method='learn', samples=None, multioutput=True, **options)

Evaluate the quality measure.

Parameters:
  • method (str) –

    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. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • **options (OptionType | None) – The options of the estimation method (e.g. test_data for the test method, n_replicates for the bootstrap one, …).

Returns:

The value of the quality measure.

Raises:

ValueError – When the name of the method is unknown.

Return type:

MeasureType

abstract evaluate_bootstrap(n_replicates=100, samples=None, multioutput=True, seed=None)

Evaluate the quality measure using the bootstrap technique.

Parameters:
  • n_replicates (int) –

    The number of bootstrap replicates.

    By default it is set to 100.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • seed (int | None) – The seed of the pseudo-random number generator. If None, then an unpredictable generator will be used.

Returns:

The value of the quality measure.

Return type:

MeasureType

abstract evaluate_kfolds(n_folds=5, samples=None, multioutput=True, randomize=True, seed=None)

Evaluate the quality measure using the k-folds technique.

Parameters:
  • n_folds (int) –

    The number of folds.

    By default it is set to 5.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • randomize (bool) –

    Whether to shuffle the samples before dividing them in folds.

    By default it is set to True.

  • seed (int | None) – The seed of the pseudo-random number generator. If None, then an unpredictable generator will be used.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_learn(samples=None, multioutput=True)[source]

Evaluate the quality measure from the learning dataset.

Parameters:
  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_loo(samples=None, multioutput=True)

Evaluate the quality measure using the leave-one-out technique.

Parameters:
  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

Returns:

The value of the quality measure.

Return type:

MeasureType

abstract evaluate_test(test_data, samples=None, multioutput=True)

Evaluate the quality measure using a test dataset.

Parameters:
  • test_data (Dataset) – The test dataset.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

Returns:

The value of the quality measure.

Return type:

MeasureType

classmethod is_better(val1, val2)

Compare the quality between two values.

This method returns True if the first one is better than the second one.

For most measures, a smaller value is “better” than a larger one (MSE etc.). But for some, like an R2-measure, higher values are better than smaller ones. This comparison method correctly handles this, regardless of the type of measure.

Parameters:
  • val1 (float) – The value of the first quality measure.

  • val2 (float) – The value of the second quality measure.

Returns:

Whether val1 is of better quality than val2.

Return type:

bool

BOOTSTRAP: ClassVar[str] = 'bootstrap'

The name of the method to evaluate the measure by bootstrap.

KFOLDS: ClassVar[str] = 'kfolds'

The name of the method to evaluate the measure by cross-validation.

LEARN: ClassVar[str] = 'learn'

The name of the method to evaluate the measure on the learning dataset.

LOO: ClassVar[str] = 'loo'

The name of the method to evaluate the measure by leave-one-out.

SMALLER_IS_BETTER: ClassVar[bool] = True

Whether to minimize or maximize the measure.

TEST: ClassVar[str] = 'test'

The name of the method to evaluate the measure on a test dataset.

algo: MLAlgo

The machine learning algorithm usually trained.

class gemseo.mlearning.qual_measure.cluster_measure.MLPredictiveClusteringMeasure(algo, fit_transformers=True)[source]

An abstract clustering measure for predictive clustering algorithms.

Parameters:
  • algo (MLPredictiveClusteringAlgo) – A machine learning algorithm for predictive clustering.

  • fit_transformers (bool) –

    Whether to re-fit the transformers when using resampling techniques. If False, use the transformers of the algorithm fitted from the whole learning dataset.

    By default it is set to True.

evaluate(method='learn', samples=None, multioutput=True, **options)

Evaluate the quality measure.

Parameters:
  • method (str) –

    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. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • **options (OptionType | None) – The options of the estimation method (e.g. test_data for the test method, n_replicates for the bootstrap one, …).

Returns:

The value of the quality measure.

Raises:

ValueError – When the name of the method is unknown.

Return type:

MeasureType

evaluate_bootstrap(n_replicates=100, samples=None, multioutput=True, seed=None)[source]

Evaluate the quality measure using the bootstrap technique.

Parameters:
  • n_replicates (int) –

    The number of bootstrap replicates.

    By default it is set to 100.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • seed (int | None) – The seed of the pseudo-random number generator. If None, then an unpredictable generator will be used.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_kfolds(n_folds=5, samples=None, multioutput=True, randomize=True, seed=None)[source]

Evaluate the quality measure using the k-folds technique.

Parameters:
  • n_folds (int) –

    The number of folds.

    By default it is set to 5.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • randomize (bool) –

    Whether to shuffle the samples before dividing them in folds.

    By default it is set to True.

  • seed (int | None) – The seed of the pseudo-random number generator. If None, then an unpredictable generator will be used.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_learn(samples=None, multioutput=True)

Evaluate the quality measure from the learning dataset.

Parameters:
  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_loo(samples=None, multioutput=True)

Evaluate the quality measure using the leave-one-out technique.

Parameters:
  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_test(test_data, samples=None, multioutput=True)[source]

Evaluate the quality measure using a test dataset.

Parameters:
  • test_data (Dataset) – The test dataset.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

Returns:

The value of the quality measure.

Return type:

MeasureType

classmethod is_better(val1, val2)

Compare the quality between two values.

This method returns True if the first one is better than the second one.

For most measures, a smaller value is “better” than a larger one (MSE etc.). But for some, like an R2-measure, higher values are better than smaller ones. This comparison method correctly handles this, regardless of the type of measure.

Parameters:
  • val1 (float) – The value of the first quality measure.

  • val2 (float) – The value of the second quality measure.

Returns:

Whether val1 is of better quality than val2.

Return type:

bool

BOOTSTRAP: ClassVar[str] = 'bootstrap'

The name of the method to evaluate the measure by bootstrap.

KFOLDS: ClassVar[str] = 'kfolds'

The name of the method to evaluate the measure by cross-validation.

LEARN: ClassVar[str] = 'learn'

The name of the method to evaluate the measure on the learning dataset.

LOO: ClassVar[str] = 'loo'

The name of the method to evaluate the measure by leave-one-out.

SMALLER_IS_BETTER: ClassVar[bool] = True

Whether to minimize or maximize the measure.

TEST: ClassVar[str] = 'test'

The name of the method to evaluate the measure on a test dataset.

algo: MLAlgo

The machine learning algorithm usually trained.

The silhouette

The silhouette coefficient to assess a clustering.

The silhouette coefficient \(s_i\) is a measure of how similar a point \(x_i\) is to its own cluster \(C_{k_i}\) (cohesion) compared to other clusters (separation):

\[s_i = \frac{b_i-a_i}{\max(a_i,b_i)}\]

with \(a_i=\frac{1}{|C_{k_i}|-1} \sum_{j\in C_{k_i}\setminus\{i\} } \|x_i-x_j\|\) and \(b_i = \underset{\ell=1,\cdots,K\atop{\ell\neq k_i}}{\min} \frac{1}{|C_\ell|} \sum_{j\in C_\ell} \|x_i-x_j\|\)

where

  • \(K\) is the number of clusters,

  • \(C_k\) are the indices of the points belonging to the cluster \(k\),

  • \(|C_k|\) is the size of \(C_k\).

class gemseo.mlearning.qual_measure.silhouette.SilhouetteMeasure(algo, fit_transformers=True)[source]

The silhouette coefficient to assess a clustering.

Parameters:
  • algo (MLPredictiveClusteringAlgo) – A clustering algorithm.

  • fit_transformers (bool) –

    Whether to re-fit the transformers when using resampling techniques. If False, use the transformers of the algorithm fitted from the whole learning dataset.

    By default it is set to True.

evaluate(method='learn', samples=None, multioutput=True, **options)

Evaluate the quality measure.

Parameters:
  • method (str) –

    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. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • **options (OptionType | None) – The options of the estimation method (e.g. test_data for the test method, n_replicates for the bootstrap one, …).

Returns:

The value of the quality measure.

Raises:

ValueError – When the name of the method is unknown.

Return type:

MeasureType

evaluate_bootstrap(n_replicates=100, samples=None, multioutput=True, seed=None)[source]

Evaluate the quality measure using the bootstrap technique.

Parameters:
  • n_replicates (int) –

    The number of bootstrap replicates.

    By default it is set to 100.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • seed (int | None) – The seed of the pseudo-random number generator. If None, then an unpredictable generator will be used.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_kfolds(n_folds=5, samples=None, multioutput=True, randomize=True, seed=None)[source]

Evaluate the quality measure using the k-folds technique.

Parameters:
  • n_folds (int) –

    The number of folds.

    By default it is set to 5.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

  • randomize (bool) –

    Whether to shuffle the samples before dividing them in folds.

    By default it is set to True.

  • seed (int | None) – The seed of the pseudo-random number generator. If None, then an unpredictable generator will be used.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_learn(samples=None, multioutput=True)

Evaluate the quality measure from the learning dataset.

Parameters:
  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_loo(samples=None, multioutput=True)

Evaluate the quality measure using the leave-one-out technique.

Parameters:
  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

Returns:

The value of the quality measure.

Return type:

MeasureType

evaluate_test(test_data, samples=None, multioutput=True)[source]

Evaluate the quality measure using a test dataset.

Parameters:
  • test_data (Dataset) – The test dataset.

  • samples (Sequence[int] | None) – The indices of the learning samples. If None, use the whole learning dataset.

  • multioutput (bool) –

    If True, return the quality measure for each output component. Otherwise, average these measures.

    By default it is set to True.

Returns:

The value of the quality measure.

Return type:

MeasureType

classmethod is_better(val1, val2)

Compare the quality between two values.

This method returns True if the first one is better than the second one.

For most measures, a smaller value is “better” than a larger one (MSE etc.). But for some, like an R2-measure, higher values are better than smaller ones. This comparison method correctly handles this, regardless of the type of measure.

Parameters:
  • val1 (float) – The value of the first quality measure.

  • val2 (float) – The value of the second quality measure.

Returns:

Whether val1 is of better quality than val2.

Return type:

bool

BOOTSTRAP: ClassVar[str] = 'bootstrap'

The name of the method to evaluate the measure by bootstrap.

KFOLDS: ClassVar[str] = 'kfolds'

The name of the method to evaluate the measure by cross-validation.

LEARN: ClassVar[str] = 'learn'

The name of the method to evaluate the measure on the learning dataset.

LOO: ClassVar[str] = 'loo'

The name of the method to evaluate the measure by leave-one-out.

SMALLER_IS_BETTER: ClassVar[bool] = False

Whether to minimize or maximize the measure.

TEST: ClassVar[str] = 'test'

The name of the method to evaluate the measure on a test dataset.

algo: MLAlgo

The machine learning algorithm usually trained.

Examples

See the examples about quality measures.