简体   繁体   中英

numpy.ndarray with zero dimensions and no shape still storing data

I am trying to compute a median of integers and return an integer in numpy . Here is what I think should work, according to the documentation of numpy.median :

import numpy as np
m1 = np.median((1,2,3))
myout = np.zeros_like(m1, dtype=np.intp)
m2 = np.median((1,2,3), out=myout)

However, when typing myout and m2 into my debugging console, the only value I get is <ndarray> . How do I get the value stored in there? Is there are? What am I doing wrong?

Update What I have noticed from your comments is that print(m2) does print 2 , which is what I wanted. However, m2 is still curious, since m2.shape is () and m2.ndim is 0 . This probably causes the debug console to not show the value of 2 . So I think my question now is, how can an ndarray have zero dimensions and still store data?

print(m2)
print(m2.shape)
print(m2.ndim)

I am unable to replicate your bug, this works perfectly for me . Maybe some versioning issues? try using m2 and myout the way you want. Here's what I ran :

>>> import numpy as np
>>> m1 = np.median((1,2,3))
>>> print (m1)
2.0
>>> myout = np.zeros_like(m1, dtype=np.intp)
>>> m2 = np.median((1,2,3), out=myout)
>>> print(myout)
2
>>> print(m2)
2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM