Metrics

The Metrics class does some preprocessing of arrays if required.

Metrics

class SeqMetrics.Metrics(true: Union[ndarray, list], predicted: Union[ndarray, list], replace_nan: Optional[Union[int, float]] = None, replace_inf: Optional[Union[int, float]] = None, remove_zero: bool = False, remove_neg: bool = False, metric_type: str = 'regression', np_errstate: Optional[dict] = 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: Union[ndarray, list], predicted: Union[ndarray, list], replace_nan: Optional[Union[int, float]] = None, replace_inf: Optional[Union[int, float]] = None, remove_zero: bool = False, remove_neg: bool = False, metric_type: str = 'regression', np_errstate: Optional[dict] = None)[source]
Parameters
  • true (array like,) – ture/observed/actual/target values

  • predicted (array like,) – simulated values

  • 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.

  • 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
  • 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.

treat_values()[source]

This function is applied by default at the start/at the time of initiating the class. However, it can used any time after that. This can be handy if we want to calculate error first by ignoring nan and then by no ignoring nan. Adopting from HydroErr . Removes the nan, negative, and inf values in two numpy arrays