[Numpy-discussion] x[None] changes x.shape
v-nijs at kellogg.northwestern.edu
Sat Jan 6 13:19:27 CST 2007
Ah ... I did not make the link between newaxis and None.
While the proposed solution works I found an alternative that gave me
exactly what I was looking for:
def foo(x, sel=())
I.e., passing an empty tuple returns the entire array without changing
x.shape or making a copy.
On 1/5/07 7:00 PM, "Robert Kern" <robert.kern at gmail.com> wrote:
> Vincent Nijs wrote:
>> Say I use a function that expects a boolean array called sel to be passed as
>> an argument:
>> def foo(x,sel = None):
>> return x[sel]
>> If x is a 1-d array and sel is a (1-d) boolean array, x.shape will give (n,)
>> where n is len(x).
>> However, if the default value None is used (i.e., when no boolean array is
>> passed) x.shape will give (1,n).
>> Is that expected behavior?
> Yes. numpy.newaxis is just an alias for None, so x[None] is the same as
>> If so is there an alternative default to I could use that would return the
>> entire x array and have x.shape be (n,)?
> def foo(x, sel=None):
> if sel is None:
> return sel
> return x[sel]
More information about the Numpy-discussion