-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
np.average of memmap returns memmap 0d array instead of numpy scalar #7403
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
The cause is the change from While it's definitely a change in behavior we should think carefully about, the new behavior seems more consistent with the rest of numpy. If no weights are given I suggest we should keep the old Possibly we might also think about updating |
Pretty much all the operations are weird anyway with >>> import numpy as np
>>> a = np.memmap('a.bin', shape=10, dtype=np.uint8, mode='w+')
>>> a[:] = 1
>>> b = a * 3
>>> type(b)
<class 'numpy.core.memmap.memmap'>
>>> b.base
array([3, 3, 3, 3, 3, 3, 3, 3, 3, 3], dtype=uint8)
Anyway this is not directly the topic of this issue. I am +1 for a deprecation cycle to change the behavior of average and other aggregation operations. |
Re ufuncs on memmaps: Maybe we just need to add def __array_wrap__(self, arr, context=None):
if arr.shape == ():
return arr[()]
return arr.view(np.ndarray) or similar to the memmap definition. I just tried it, and it seems to work without failing any tests. |
I think this is a good idea (unless there are ufuncs that do inplace operations?). Also to fix the joblib/joblib@b68a868#diff-19004d02cec64e3d448b33565de338ebR94 I never finished that work but if people are interested I could contribute it to numpy as a saner alternative to |
We just hit this too over at dipy/dipy#1074 Is it really desirable to return a scalar of a particular subclass in this case? Should the scalars be special-cased? It's unfortunate that a numpy array and a subclass return a different type of object when reducing to a scalar. |
@matthew-brett It looks like this is already fixed in master via #7406 |
Ugh - and I see that I knew about this fix from my comments echoed to that thread - thanks for the reminder. |
Output since 5ceab8f (a couple of days ago):
Output before that (e.g. in 1.10.4):
This causes the scikit-learn using numpy master tests to fail, see this Travis log.
We have been bitten in the past by this in scikit-learn with
.sum
that has the same surprising behaviour (see scikit-learn/scikit-learn#6225). Shouldn't reduce operations on a memmap array always return a numpy scalar ?cc @ogrisel.
The text was updated successfully, but these errors were encountered: