MCPcopy
hub / github.com/scikit-learn/scikit-learn / assert_allclose

Function assert_allclose

sklearn/utils/_testing.py:174–244  ·  view source on GitHub ↗

dtype-aware variant of numpy.testing.assert_allclose This variant introspects the least precise floating point dtype in the input argument and automatically sets the relative tolerance parameter to 1e-4 float32 and use 1e-7 otherwise (typically float64 in scikit-learn). `atol`

(
    actual, desired, rtol=None, atol=0.0, equal_nan=True, err_msg="", verbose=True
)

Source from the content-addressed store, hash-verified

172
173
174def assert_allclose(
175 actual, desired, rtol=None, atol=0.0, equal_nan=True, err_msg="", verbose=True
176):
177 """dtype-aware variant of numpy.testing.assert_allclose
178
179 This variant introspects the least precise floating point dtype
180 in the input argument and automatically sets the relative tolerance
181 parameter to 1e-4 float32 and use 1e-7 otherwise (typically float64
182 in scikit-learn).
183
184 `atol` is always left to 0. by default. It should be adjusted manually
185 to an assertion-specific value in case there are null values expected
186 in `desired`.
187
188 The aggregate tolerance is `atol + rtol * abs(desired)`.
189
190 Parameters
191 ----------
192 actual : array_like
193 Array obtained.
194 desired : array_like
195 Array desired.
196 rtol : float, optional, default=None
197 Relative tolerance.
198 If None, it is set based on the provided arrays' dtypes.
199 atol : float, optional, default=0.
200 Absolute tolerance.
201 equal_nan : bool, optional, default=True
202 If True, NaNs will compare equal.
203 err_msg : str, optional, default=''
204 The error message to be printed in case of failure.
205 verbose : bool, optional, default=True
206 If True, the conflicting values are appended to the error message.
207
208 Raises
209 ------
210 AssertionError
211 If actual and desired are not equal up to specified precision.
212
213 See Also
214 --------
215 numpy.testing.assert_allclose
216
217 Examples
218 --------
219 >>> import numpy as np
220 >>> from sklearn.utils._testing import assert_allclose
221 >>> x = [1e-5, 1e-3, 1e-1]
222 >>> y = np.arccos(np.cos(x))
223 >>> assert_allclose(x, y, rtol=1e-5, atol=0)
224 >>> a = np.full(shape=10, fill_value=1e-5, dtype=np.float32)
225 >>> assert_allclose(a, 1e-5)
226 """
227 dtypes = []
228
229 actual, desired = np.asanyarray(actual), np.asanyarray(desired)
230 dtypes = [actual.dtype, desired.dtype]
231

Calls 1

maxFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…