Metrics

The Metrics class does some preprocessing of arrays if required.

Metrics

class SeqMetrics.Metrics(true: ndarray | list, predicted: ndarray | list, replace_nan: int | float | None = None, replace_inf: int | float | None = None, remove_zero: bool = False, remove_neg: bool = False, remove_nan: bool = True, remove_inf: bool = True, metric_type: str = 'regression', np_errstate: dict | None = None)[source]

Bases: object

This class does some pre-processign and handles metadata regaring true and predicted arrays.

The arguments other than true and predicted are dynamic i.e. they can be changed from outside the class. This means the user can change their value after creating the class. This will be useful if we want to calculate an error once by ignoring NaN and then by not ignoring the NaNs. However, the user has to run the method treat_arrays in order to have the changed values impact on true and predicted arrays. For ducussion about impact of performance metric see German climate computing website.

__init__(true: ndarray | list, predicted: ndarray | list, replace_nan: int | float | None = None, replace_inf: int | float | None = None, remove_zero: bool = False, remove_neg: bool = False, remove_nan: bool = True, remove_inf: bool = True, metric_type: str = 'regression', np_errstate: dict | None = None)[source]
Parameters:
  • true (array like,) – ture/observed/actual/target values. This can be anything which can be converted to numpy array.

  • predicted (array like,) – predicted/simulated values. This can be anything which can be converted to numpy array.

  • replace_nan (default None. if not None, then NaNs in true) – and predicted will be replaced by this value.

  • replace_inf (default None, if not None, then inf vlaues in true and) – predicted will be replaced by this value.

  • remove_zero (default False, if True, the zero values in true) – or predicted arrays will be removed. If a zero is found in one array, the corresponding value in the other array will also be removed.

  • remove_neg (default False, if True, the negative values in true) – or predicted arrays will be removed.

  • remove_inf (bool (default=True)) – whether to remove infitinity (np.inf) values from true and predicted arrays or not.

  • remove_nan (bool (default=True)) – whether to remove nan (np.nan) values from true and predicted arrays or not.

  • metric_type (type of metric.) –

  • np_errstate (dict) – any keyword options for np.errstate() to calculate np.log1p

calculate_all(statistics=False, verbose=False, write=False, name=None) dict[source]

calculates errors using all available methods except brier_score.. write: bool, if True, will write the calculated errors in file. name: str, if not None, then must be path of the file in which to write.

Parameters:
  • statistics

  • verbose (bool, optional) – if True, will print the calculated errors. The default is False.

  • write (bool, optional) – if True, will write the calculated errors in file. The default is False.

  • name (str, optional) – if not None, then must be path of the file in which to write. The default is None.

Returns:

dictionary of calculated errors.

Return type:

dict

Examples

>>> import numpy as np
>>> from SeqMetrics import RegressionMetrics
>>> true = np.random.random(100)
>>> predicted = np.random.random(100)
>>> metrics = RegressionMetrics(true, predicted)
>>> metrics.calculate_all()
calculate_minimal() dict[source]

Calculates some basic metrics.

Returns:

Dictionary with all metrics

Return type:

dict

Examples

>>> import numpy as np
>>> from SeqMetrics import RegressionMetrics
>>> true = np.random.random(100)
>>> predicted = np.random.random(100)
>>> metrics = RegressionMetrics(true, predicted)
>>> metrics.calculate_minimal()
calculate_scale_dependent_metrics() dict[source]

Calculates scale dependent metrics

Returns:

Dictionary with all metrics

Return type:

dict

Examples

>>> import numpy as np
>>> from SeqMetrics import RegressionMetrics
>>> true = np.random.random(100)
>>> predicted = np.random.random(100)
>>> metrics = RegressionMetrics(true, predicted)
>>> metrics.calculate_scale_dependent_metrics()
calculate_scale_independent_metrics() dict[source]

Calculates scale independent metrics

Returns:

Dictionary with all metrics

Return type:

dict

Examples

>>> import numpy as np
>>> from SeqMetrics import RegressionMetrics
>>> true = np.random.random(100)
>>> predicted = np.random.random(100)
>>> metrics = RegressionMetrics(true, predicted)
>>> metrics.calculate_scale_independent_metrics()
composite_metrics()[source]
property log1p_p
property log1p_t
property log_p
property log_t
mse(weights=None) float[source]

mean square error

percentage_metrics()[source]
relative_metrics()[source]
property remove_neg
property remove_zero
property replace_inf
property replace_nan
scale_dependent_metrics()[source]
stats(verbose: bool = False) dict[source]

returs some important stats about true and predicted values.