pedophysics.utils package

Submodules

pedophysics.utils.similar_arrays module

pedophysics.utils.similar_arrays.arrays_are_similar(a, b)

Calculates if two numpy arrays are similar.

This function determines whether two numpy arrays, a and b, are similar based on two criteria:

The arrays must have the same shape. The elements in corresponding positions must either both be NaN (Not a Number) or be numerically close to each other, within the tolerance defined by numpy.isclose.

Parameters

a (numpy.ndarray): The first array to compare. b (numpy.ndarray): The second array to compare.

Returns

bool:

Returns True if the arrays are considered similar, otherwise False.

Example

>>> a = np.array([1.0, 2.0, np.nan, 4.0])
>>> b = np.array([0.999, 2.001, np.nan, 4.0])
>>> similar = arrays_are_similar(a, b)
>>> print(similar)  # Expected output: True, since the non-NaN elements are close and NaN positions match.

The function assumes that both input arrays are indeed numpy arrays and does not perform type checking. Ensure that the inputs are of the correct type to avoid unexpected behavior.

pedophysics.utils.stats module

pedophysics.utils.stats.R2_score(actual, predicted)

Calculate the coefficient of determination (R^2) of a prediction.

The R^2 score function computes the coefficient of determination, often used to evaluate the performance of a regression model. The best possible score is 1.0. This function is designed to handle arrays with NaN values by ignoring such entries.

Parameters

actualarray-like of shape (n_samples,)

Ground truth (correct) target values.

predictedarray-like of shape (n_samples,)

Estimated targets as returned by a classifier.

Returns

float

R^2 of the prediction.

Notes

This function works with arrays that include NaN values, ignoring such entries during the computation. Therefore, ‘actual’ and ‘predicted’ arrays can have missing values, but they must be of the same shape.

Example

>>> actual = np.array([3, -0.5, 2, 7, 4.2])
>>> predicted = np.array([2.5, 0.0, 2.1, 7.8, 5.3])
>>> R2_score(actual, predicted)
0.9228556485355649

Module contents