Re: [Numpy-discussion] indexed assignment testcases

2014-07-06 Thread Benjamin Root
I guess I always treated scalars as something special when it comes to
broadcasting. Seeing these examples, I can see how my grokking of
broadcasting was incomplete.

I still think that the assignment of an array of values (as opposed to a
scalar) to nothing could potentially mask deeper issues, but now I see that
it may be impossible to distinguish from the perfectly normal case.

Cheers!
Ben Root


On Sun, Jul 6, 2014 at 5:48 PM, Nathaniel Smith  wrote:

> On Sun, Jul 6, 2014 at 9:14 PM, Benjamin Root  wrote:
> > as for the broadcasting issue, I can see it for the second case, but the
> > first case still doesn't sit right with me. My understanding of
> broadcasting
> > is to effectively *expand* an array to match the shape of another array
> (or
> > some target shape). In this case, the array is being effectively
> > *contracted* in shape. That makes zero sense to me.
>
> That's how it's always worked though, in all cases of broadcasting;
> nothing special about indexing:
>
> In [8]: a = np.zeros((3, 0))
>
> In [9]: a + 1
> Out[9]: array([], shape=(3, 0), dtype=float64)
>
> In [10]: a + [[1], [2], [3]]
> Out[10]: array([], shape=(3, 0), dtype=float64)
>
> IME it's extremely useful in practice for avoiding special cases when
> some axis has a vary size that can be zero.
>
> --
> Nathaniel J. Smith
> Postdoctoral researcher - Informatics - University of Edinburgh
> http://vorpus.org
> ___
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] indexed assignment testcases

2014-07-06 Thread Nathaniel Smith
On Sun, Jul 6, 2014 at 9:14 PM, Benjamin Root  wrote:
> as for the broadcasting issue, I can see it for the second case, but the
> first case still doesn't sit right with me. My understanding of broadcasting
> is to effectively *expand* an array to match the shape of another array (or
> some target shape). In this case, the array is being effectively
> *contracted* in shape. That makes zero sense to me.

That's how it's always worked though, in all cases of broadcasting;
nothing special about indexing:

In [8]: a = np.zeros((3, 0))

In [9]: a + 1
Out[9]: array([], shape=(3, 0), dtype=float64)

In [10]: a + [[1], [2], [3]]
Out[10]: array([], shape=(3, 0), dtype=float64)

IME it's extremely useful in practice for avoiding special cases when
some axis has a vary size that can be zero.

-- 
Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh
http://vorpus.org
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] indexed assignment testcases

2014-07-06 Thread Sebastian Berg
On So, 2014-07-06 at 16:14 -0400, Benjamin Root wrote:
> re: deprecation warnings... that's what I get when I am working on my
> non-dev box because I am at the conference, and have gotten too used
> to the setup of my dev box...
> 
> 
> as for the broadcasting issue, I can see it for the second case, but
> the first case still doesn't sit right with me. My understanding of
> broadcasting is to effectively *expand* an array to match the shape of
> another array (or some target shape). In this case, the array is being
> effectively *contracted* in shape. That makes zero sense to me.
> 

Well, from a technical point of view, it is more like changing the shape
to whatever fits while setting the stride to 0. I am sure there are a
few places where the doc is not clear.
From a practical point of view, it makes sense if you consider this:

arr[arr < 0] = 0

Where it might be that the array has no elements smaller 0. Though I
admit I would write 0 here, and not [0].

- Sebastian

> 
> Ben
> 
> 
> 
> On Sun, Jul 6, 2014 at 3:59 PM, Charles R Harris
>  wrote:
> 
> 
> 
> On Sun, Jul 6, 2014 at 1:32 PM, Benjamin Root
>  wrote:
> While trying to wrap my head around the issues with
> matplotlib's tri module and the new numpy indexing, I
> have made some test cases where I wonder if warnings
> should be issued.
> 
> 
> import numpy as np
> 
> a = np.ones((10,))
> 
> all_false = np.zeros((10,), dtype=bool)
> 
> a[all_false] = np.array([2.0])   # the shapes don't
> match here
> 
> 
> 
> It broadcasts because the leading dimension is 1.
>  
> 
> 
> 
> mask_in = np.array([False]*8 + [True, True])
> 
> a[mask_in] = np.array([])# raises ValueError as
> expected
> 
> a[mask_in] = np.array([[]])  # no exception because it
> is 2-D, for some reason (on master, but not
> release-0.9b1)
> 
>  
> Now falls back to old behavior and raises a
> DeprecationWarning. You don't see that by default.
>  
> 
> 
> 
> a[mask_in] = np.array([2.0]) # This works and repeats
> 2.0 twice. I thought this wasn't supposed to happen
> anymore?
> 
> 
> 
> Broadcasting again.
> 
> 
> 
> Chuck 
> 
> 
> ___
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> 
> 
> 
> ___
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion



signature.asc
Description: This is a digitally signed message part
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] indexed assignment testcases

2014-07-06 Thread Benjamin Root
re: deprecation warnings... that's what I get when I am working on my
non-dev box because I am at the conference, and have gotten too used to the
setup of my dev box...

as for the broadcasting issue, I can see it for the second case, but the
first case still doesn't sit right with me. My understanding of
broadcasting is to effectively *expand* an array to match the shape of
another array (or some target shape). In this case, the array is being
effectively *contracted* in shape. That makes zero sense to me.

Ben


On Sun, Jul 6, 2014 at 3:59 PM, Charles R Harris 
wrote:

>
>
>
> On Sun, Jul 6, 2014 at 1:32 PM, Benjamin Root  wrote:
>
>> While trying to wrap my head around the issues with matplotlib's tri
>> module and the new numpy indexing, I have made some test cases where I
>> wonder if warnings should be issued.
>>
>> import numpy as np
>> a = np.ones((10,))
>> all_false = np.zeros((10,), dtype=bool)
>> a[all_false] = np.array([2.0])   # the shapes don't match here
>>
>
> It broadcasts because the leading dimension is 1.
>
>
>>
>> mask_in = np.array([False]*8 + [True, True])
>> a[mask_in] = np.array([])# raises ValueError as expected
>> a[mask_in] = np.array([[]])  # no exception because it is 2-D, for some
>> reason (on master, but not release-0.9b1)
>>
>
> Now falls back to old behavior and raises a DeprecationWarning. You don't
> see that by default.
>
>
>>
>> a[mask_in] = np.array([2.0]) # This works and repeats 2.0 twice. I
>> thought this wasn't supposed to happen anymore?
>>
>
> Broadcasting again.
>
> Chuck
>
> ___
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] indexed assignment testcases

2014-07-06 Thread Charles R Harris
On Sun, Jul 6, 2014 at 1:32 PM, Benjamin Root  wrote:

> While trying to wrap my head around the issues with matplotlib's tri
> module and the new numpy indexing, I have made some test cases where I
> wonder if warnings should be issued.
>
> import numpy as np
> a = np.ones((10,))
> all_false = np.zeros((10,), dtype=bool)
> a[all_false] = np.array([2.0])   # the shapes don't match here
>

It broadcasts because the leading dimension is 1.


>
> mask_in = np.array([False]*8 + [True, True])
> a[mask_in] = np.array([])# raises ValueError as expected
> a[mask_in] = np.array([[]])  # no exception because it is 2-D, for some
> reason (on master, but not release-0.9b1)
>

Now falls back to old behavior and raises a DeprecationWarning. You don't
see that by default.


>
> a[mask_in] = np.array([2.0]) # This works and repeats 2.0 twice. I thought
> this wasn't supposed to happen anymore?
>

Broadcasting again.

Chuck
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] indexed assignment testcases

2014-07-06 Thread Sebastian Berg
On So, 2014-07-06 at 15:32 -0400, Benjamin Root wrote:
> While trying to wrap my head around the issues with matplotlib's tri
> module and the new numpy indexing, I have made some test cases where I
> wonder if warnings should be issued.
> 
> 
> import numpy as np
> 
> a = np.ones((10,))
> 
> all_false = np.zeros((10,), dtype=bool)
> 
> a[all_false] = np.array([2.0])   # the shapes don't match here
> 

The shapes match using broadcasting. Values shape of (1,) can be
broadcast to indexing result shape of (0,).

> 
> mask_in = np.array([False]*8 + [True, True])
> 
> a[mask_in] = np.array([])# raises ValueError as expected
> 
> a[mask_in] = np.array([[]])  # no exception because it is 2-D, for
> some reason (on master, but not release-0.9b1)
> 
Gives a (maybe not good) deprecation warning in master. But those are
typically invisible...

> 
> a[mask_in] = np.array([2.0]) # This works and repeats 2.0 twice. I
> thought this wasn't supposed to happen anymore?
> 

Again, broadcasting of values onto out shape.

- Sebastian

> 
> Ben Root
> 
> 
> ___
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion



signature.asc
Description: This is a digitally signed message part
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion