-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
np.asscalar should pass through scalars #4701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
…rameters passed in to np.where() is evaluated first before going into the function however, the denominator can be zero at that time. I allocated the intermediate result based on different formulas for rate == 0.0 and not equal to and fixes numpy#5046. Also, a easy fix for asocial doesn’t take integer because it assumes passing in an np.array. I convert everything to np.array first and then retrieve the item. Fixed numpy#4701
I'd like to reopen this issue. This is not fixed in numpy 1.13. It doesn't have the fix mentioned here
def asscalar(a):
"""
Convert an array of size 1 to its scalar equivalent.
Parameters
----------
a : ndarray
Input array of size 1.
Returns
-------
out : scalar
Scalar representation of `a`. The output data type is the same type
returned by the input's `item` method.
Examples
--------
>>> np.asscalar(np.array([24]))
24
"""
return a.item() |
Looks like the wrong issue number was used in the commit that closed this |
Allows np.scalar to pass through scalars
Allows numpy.asscalar to pass scalars
BUG: fixes numpy#4701 related to numpy.asscalar
We need to make a decision on this before we get even more PRs attempting to "fix" it. To date:
Un-tagging as easy to stop us getting more of these |
One of the main problems I have with >>> x = np.float64
>>> isscalar(x)
True
>>> type(x)
<class 'numpy.float64'>
>>> type(asscalar(x))
<class 'float'> Overall, we have three conflicting definitions of scalar:
Almost every time I see an internal use of |
As explained in detail in a [numpy issue](numpy/numpy#4701), `np.asscalar` doesn't pass through scalars. This is because it looks for `item()` method that `int` and `float` don't have. A possible workaround suggest there is the one I proposed to you. In this way your script doesn't fail with other environments such as `LunarLanderContinuous-v2`.
Suggestion
It would be very convenient if
np.asscalar()
would pass through scalars. The use-case is to ensure that an argument is a scalar, regardless what is passed in (as long as it behaves as a scalar).Presently these fail as the implementation simply looks for the
.item()
method, which scalars do not possess:Rational
It is common to use
np.asarray()
to ensure that the argument is an array (in the case that it might be a list etc.). In an analogous use case, it would be useful to be able to usenp.asscalar()
to ensure that an argument is a scalar (as long as it can be converted to a scalar).Workaround
Convert the argument to an array first.
Resolution
Perhaps code like the following would work:
The text was updated successfully, but these errors were encountered: