![](https://secure.gravatar.com/avatar/d87a1cccf54e87545922851d4eafd8ed.jpg?s=120&d=mm&r=g)
I guess I don't really understand this too well - is the below correct behavior, and if so, why?
x = np.array([1, 2, 3]) y = np.array([[4], [5], [6]]) b = np.broadcast(x, y) b.nd # returns what I'd expect 2 b = np.broadcast(x, y, x, y) b.nd # doesn't return what I'd expect 2 del b # maybe problem is that I have to "clear" b first? # or maybe it's that all args have to be different? ... b = np.broadcast(x, y, x * y) b.nd 2 z = x * y # grasping at straws now z array([[ 4, 8, 12], [ 5, 10, 15], [ 6, 12, 18]]) x array([1, 2, 3]) y array([[4], [5], [6]]) del b # trying everything to get intuitive behavior b = np.broadcast(x, y, z) b.nd 2
Huhn? DG
![](https://secure.gravatar.com/avatar/764323a14e554c97ab74177e0bce51d4.jpg?s=120&d=mm&r=g)
On Wed, Aug 5, 2009 at 13:48, David Goldsmith<d_l_goldsmith@yahoo.com> wrote:
I guess I don't really understand this too well - is the below correct behavior, and if so, why?
x = np.array([1, 2, 3]) y = np.array([[4], [5], [6]]) b = np.broadcast(x, y) b.nd # returns what I'd expect 2 b = np.broadcast(x, y, x, y) b.nd # doesn't return what I'd expect 2
Why don't you expect this? It's the correct answer. (x*y*x*y).shape == (3,3).
del b # maybe problem is that I have to "clear" b first? # or maybe it's that all args have to be different? ... b = np.broadcast(x, y, x * y) b.nd 2 z = x * y # grasping at straws now z array([[ 4, 8, 12], [ 5, 10, 15], [ 6, 12, 18]])
Yup. z.shape == (3,3). Broadcasting that with a (3,1) array or a (3,) array still gives a (3,3) array. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
![](https://secure.gravatar.com/avatar/d87a1cccf54e87545922851d4eafd8ed.jpg?s=120&d=mm&r=g)
--- On Wed, 8/5/09, Robert Kern <robert.kern@gmail.com> wrote:
I guess I don't really understand this too well - is
wrote: the below correct behavior, and if so, why?
b = np.broadcast(x, y, x, y) b.nd # doesn't return what I'd expect 2
Why isn't that 4?
Why don't you expect this? It's the correct answer. (x*y*x*y).shape == (3,3).
del b # maybe problem is that I have to "clear" b first? # or maybe it's that all args have to be different? ... b = np.broadcast(x, y, x * y) b.nd 2
Why isn't that 3? If x0, ..., xN are the arguments to `broadcast` and D = max(x0.nd, ..., xN.nd), is broadcast.nd necessarily <= D? If so, then I think I'm on the road to understanding. DG
![](https://secure.gravatar.com/avatar/764323a14e554c97ab74177e0bce51d4.jpg?s=120&d=mm&r=g)
On Wed, Aug 5, 2009 at 14:38, David Goldsmith<d_l_goldsmith@yahoo.com> wrote:
--- On Wed, 8/5/09, Robert Kern <robert.kern@gmail.com> wrote:
I guess I don't really understand this too well - is
wrote: the below correct behavior, and if so, why?
b = np.broadcast(x, y, x, y) b.nd # doesn't return what I'd expect 2
Why isn't that 4?
Why would it be 4?
Why don't you expect this? It's the correct answer. (x*y*x*y).shape == (3,3).
This is the example you need to pay attention to.
del b # maybe problem is that I have to "clear" b first? # or maybe it's that all args have to be different? ... b = np.broadcast(x, y, x * y) b.nd 2
Why isn't that 3?
If x0, ..., xN are the arguments to `broadcast` and D = max(x0.nd, ..., xN.nd), is broadcast.nd necessarily <= D? If so, then I think I'm on the road to understanding.
It is necessarily == D. Broadcasting is associative. The (x*y*z).shape == (x*(y*z)).shape == ((x*y)*z).shape. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
![](https://secure.gravatar.com/avatar/d87a1cccf54e87545922851d4eafd8ed.jpg?s=120&d=mm&r=g)
--- On Wed, 8/5/09, Robert Kern <robert.kern@gmail.com> wrote:
It is necessarily == D. Broadcasting is associative.
Ah, that's the key I didn't understand! That helps (me) a lot; I'm going to make a "Note" of it in numpy.broadcast's docstring. DG
The (x*y*z).shape == (x*(y*z)).shape == ((x*y)*z).shape.
-- Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco _______________________________________________ Scipy-dev mailing list Scipy-dev@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-dev
![](https://secure.gravatar.com/avatar/d87a1cccf54e87545922851d4eafd8ed.jpg?s=120&d=mm&r=g)
--- On Wed, 8/5/09, David Goldsmith <d_l_goldsmith@yahoo.com> wrote:
It is necessarily == D. Broadcasting is associative. The (x*y*z).shape == (x*(y*z)).shape == ((x*y)*z).shape.
Um:
x = np.array((1, 2, 3)) y = np.array([[4], [5], [6]]) z = x * y x; y; z array([1, 2, 3]) array([[4], [5], [6]]) array([[ 4, 8, 12], [ 5, 10, 15], [ 6, 12, 18]]) B = np.broadcast X = B(x, y, z) Y = B(x, B(y, z)) Z = B(B(x, y), z) X.numiter, Y.numiter, Z.numiter (3, 2, 2) X.nd, Y.nd, Z.nd (2, 1, 2) X.shape, Y.shape, Z.shape ((3, 3), (3,), (3, 3))
Am I doing something wrong? DG
![](https://secure.gravatar.com/avatar/764323a14e554c97ab74177e0bce51d4.jpg?s=120&d=mm&r=g)
On Wed, Aug 5, 2009 at 15:40, David Goldsmith<d_l_goldsmith@yahoo.com> wrote:
--- On Wed, 8/5/09, David Goldsmith <d_l_goldsmith@yahoo.com> wrote:
It is necessarily == D. Broadcasting is associative. The (x*y*z).shape == (x*(y*z)).shape == ((x*y)*z).shape.
Um:
x = np.array((1, 2, 3)) y = np.array([[4], [5], [6]]) z = x * y x; y; z array([1, 2, 3]) array([[4], [5], [6]]) array([[ 4, 8, 12], [ 5, 10, 15], [ 6, 12, 18]]) B = np.broadcast X = B(x, y, z) Y = B(x, B(y, z)) Z = B(B(x, y), z) X.numiter, Y.numiter, Z.numiter (3, 2, 2) X.nd, Y.nd, Z.nd (2, 1, 2) X.shape, Y.shape, Z.shape ((3, 3), (3,), (3, 3))
Am I doing something wrong?
You are passing a broadcast iterator as an argument to broadcast. The broadcast iterator iterates over the inputs in-parallel. This is entirely different from actually operating on the inputs. In [1]: x = array([1, 2, 3]) In [2]: y = array([[4], [5], [6]]) In [3]: z = x * y In [4]: x*y*z Out[4]: array([[ 16, 64, 144], [ 25, 100, 225], [ 36, 144, 324]]) In [5]: (x*y)*z Out[5]: array([[ 16, 64, 144], [ 25, 100, 225], [ 36, 144, 324]]) In [6]: x*(y*z) Out[6]: array([[ 16, 64, 144], [ 25, 100, 225], [ 36, 144, 324]]) In [8]: array(list(broadcast(x,y))) Out[8]: array([[1, 4], [2, 4], [3, 4], [1, 5], [2, 5], [3, 5], [1, 6], [2, 6], [3, 6]]) -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
![](https://secure.gravatar.com/avatar/d87a1cccf54e87545922851d4eafd8ed.jpg?s=120&d=mm&r=g)
Sorry to be "dense", but can someone please show me how to exhibit associativity with the broadcast object - it's that (not broadcasting as a process) I'm trying to understand (and illustrate). DG --- On Wed, 8/5/09, Robert Kern <robert.kern@gmail.com> wrote:
From: Robert Kern <robert.kern@gmail.com> Subject: Re: [SciPy-dev] numpy.broadcast To: "SciPy Developers List" <scipy-dev@scipy.org> Date: Wednesday, August 5, 2009, 2:01 PM On Wed, Aug 5, 2009 at 15:40, David Goldsmith<d_l_goldsmith@yahoo.com> wrote:
--- On Wed, 8/5/09, David Goldsmith <d_l_goldsmith@yahoo.com> wrote:
It is necessarily == D. Broadcasting is associative. The (x*y*z).shape == (x*(y*z)).shape == ((x*y)*z).shape.
Um:
x = np.array((1, 2, 3)) y = np.array([[4], [5], [6]]) z = x * y x; y; z array([1, 2, 3]) array([[4], [5], [6]]) array([[ 4, 8, 12], [ 5, 10, 15], [ 6, 12, 18]]) B = np.broadcast X = B(x, y, z) Y = B(x, B(y, z)) Z = B(B(x, y), z) X.numiter, Y.numiter, Z.numiter (3, 2, 2) X.nd, Y.nd, Z.nd (2, 1, 2) X.shape, Y.shape, Z.shape ((3, 3), (3,), (3, 3))
Am I doing something wrong?
You are passing a broadcast iterator as an argument to broadcast. The broadcast iterator iterates over the inputs in-parallel. This is entirely different from actually operating on the inputs.
In [1]: x = array([1, 2, 3])
In [2]: y = array([[4], [5], [6]])
In [3]: z = x * y
In [4]: x*y*z Out[4]: array([[ 16, 64, 144], [ 25, 100, 225], [ 36, 144, 324]])
In [5]: (x*y)*z Out[5]: array([[ 16, 64, 144], [ 25, 100, 225], [ 36, 144, 324]])
In [6]: x*(y*z) Out[6]: array([[ 16, 64, 144], [ 25, 100, 225], [ 36, 144, 324]])
In [8]: array(list(broadcast(x,y))) Out[8]: array([[1, 4], [2, 4], [3, 4], [1, 5], [2, 5], [3, 5], [1, 6], [2, 6], [3, 6]])
-- Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco _______________________________________________ Scipy-dev mailing list Scipy-dev@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-dev
![](https://secure.gravatar.com/avatar/764323a14e554c97ab74177e0bce51d4.jpg?s=120&d=mm&r=g)
On Wed, Aug 5, 2009 at 17:15, David Goldsmith<d_l_goldsmith@yahoo.com> wrote:
Sorry to be "dense", but can someone please show me how to exhibit associativity with the broadcast object - it's that (not broadcasting as a process) I'm trying to understand (and illustrate).
The broadcast *object* doesn't associate. *Broadcasting* associates. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
![](https://secure.gravatar.com/avatar/d87a1cccf54e87545922851d4eafd8ed.jpg?s=120&d=mm&r=g)
--- On Wed, 8/5/09, Robert Kern <robert.kern@gmail.com> wrote:
The broadcast *object* doesn't associate. *Broadcasting* associates.
I understand that; I guess what I don't understand is the relationship between the object and the process. DG
-- Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco _______________________________________________ Scipy-dev mailing list Scipy-dev@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-dev
![](https://secure.gravatar.com/avatar/764323a14e554c97ab74177e0bce51d4.jpg?s=120&d=mm&r=g)
On Wed, Aug 5, 2009 at 17:23, David Goldsmith<d_l_goldsmith@yahoo.com> wrote:
--- On Wed, 8/5/09, Robert Kern <robert.kern@gmail.com> wrote:
The broadcast *object* doesn't associate. *Broadcasting* associates.
I understand that; I guess what I don't understand is the relationship between the object and the process.
The broadcast object is an iterator. It has attributes .nd, .shape, and .size which tell you the number of dimensions, the shape, and the number of elements of what the broadcasted forms of each the input arrays would be. For N input arrays, iterating over the broadcast object would yield you .size N-tuples with the elements from each input array just as if you were to form the broadcasted arrays for each of the inputs and then iterate over zip(broadcasted_x.flat, broadcasted_y.flat, ...). -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
participants (2)
-
David Goldsmith
-
Robert Kern