python - numpy: is there an `allclose(np.array, scalar)`? -
in numpy can use allclose(x, y)
function check element-wise approximate equality between 2 arrays. moreover, expression x==5
can check element-wise equality between array , scalar.
is there function combines both functionalities?? is, can compare array , scalar approximate element-wise equality??
the term array or array-like in numpy documentation indicates input converted array np.asarray(in_arg)
or np.asanyarray(in_arg)
. if input scalar converted scalar array:
>>> import numpy np >>> np.asarray(5) # or np.asanyarray array(5)
and functions np.allclose
or np.isclose
element-wise comparison no matter if second argument scalar array, array same shape or array correctly broadcasts first array:
>>> import numpy np >>> arr = np.array([1,2,1,0,1.00001,0.9999999]) >>> np.allclose(arr, 1) false >>> np.isclose(arr, 1) array([ true, false, true, false, true, true], dtype=bool) >>> np.isclose(arr, np.ones((10, 6))) array([[ true, false, true, false, true, true], [ true, false, true, false, true, true], [ true, false, true, false, true, true], [ true, false, true, false, true, true], [ true, false, true, false, true, true], [ true, false, true, false, true, true], [ true, false, true, false, true, true], [ true, false, true, false, true, true], [ true, false, true, false, true, true], [ true, false, true, false, true, true]], dtype=bool)
so no need find function explicitly handles scalars, these functions correctly work scalars.
Comments
Post a Comment