[Numpy-discussion] Consider improving numpy.outer's behavior with zero-dimensional vectors

Sebastian Berg sebastian at sipsolutions.net
Fri Apr 17 16:03:04 EDT 2015


On Fr, 2015-04-17 at 15:18 -0400, josef.pktd at gmail.com wrote:
> On Fri, Apr 17, 2015 at 2:56 PM, Sebastian Berg
<snip>
> > Hehe, yeah, that difference. But if you really want that, you can
> > usually do a1[0, 1, ...] if you don't mind the ugliness.
> 
> I'm not sure what you mean, although it sounds like a nice trick.
> This doesn't work for me
> 

Oh, mindslip. I thought the problem was that maybe scalar assignment
does not remove trailing dimensions. But the actual reason was that you
do not have an array on the right hand side. And the assignment code
isn't sure if you might want to do object assignment in that case, so it
can't do the funny broadcasting of the left hand side (or trailing
dimension removing, whichever way around you like to think of it).

> >>> a1[0, 1, ...]  = [[100]]
> Traceback (most recent call last):
>   File "<pyshell#315>", line 1, in <module>
>     a1[0, 1, ...]  = [[100]]
> ValueError: assignment to 0-d array
> 
> >>> np.__version__
> '1.9.2rc1'
> >>> a1[0, 1,
> 
> Josef
> 
> 
> 
> >
> >> Josef
> >>
> >>
> >> >
> >> >>
> >> >>
> >> >> >
> >> >> >> I guess we will or would have applications for outer along an axis,
> >> >> >> for example if x.shape = (100, 10), then we have
> >> >> >> x[:,None, :] * x[:, :, None]     (I guess)
> >> >> >> Something like this shows up reasonably often in econometrics as
> >> >> >> "Outer Product". However in most cases we can avoid constructing this
> >> >> >> matrix and get the final results in a more memory efficient or faster
> >> >> >> way.
> >> >> >> (example an array of covariance matrices)
> >> >> >
> >> >> >
> >> >> > Not sure I see this.  outer(a, b) should return something that has
> >> >> > shape:
> >> >> > (a.shape + b.shape).  If you're doing it "along an axis", you mean
> >> >> > you're
> >> >> > reshuffling the resulting shape vector?
> >> >>
> >> >> No I'm not reshaping the full tensor product.
> >> >>
> >> >> It's a vectorized version of looping over independent outer products
> >> >>
> >> >> np.array([outer(xi, yi) for xi,yi in zip(x, y)])
> >> >> (which I would never use with outer)
> >> >>
> >> >> but I have code that works similar for a reduce (or reduce_at) loop over
> >> >> this.
> >> >>
> >> >> Josef
> >> >>
> >> >>
> >> >> >>
> >> >> >>
> >> >> >> Josef
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >> >
> >> >> >> > - Sebastian
> >> >> >> >
> >> >> >> >
> >> >> >> >> Best,
> >> >> >> >>
> >> >> >> >> Matthew
> >> >> >> >> _______________________________________________
> >> >> >> >> NumPy-Discussion mailing list
> >> >> >> >> NumPy-Discussion at scipy.org
> >> >> >> >> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> >> >> >> >>
> >> >> >> >
> >> >> >> >
> >> >> >> > _______________________________________________
> >> >> >> > NumPy-Discussion mailing list
> >> >> >> > NumPy-Discussion at scipy.org
> >> >> >> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
> >> >> >> >
> >> >> >> _______________________________________________
> >> >> >> NumPy-Discussion mailing list
> >> >> >> NumPy-Discussion at scipy.org
> >> >> >> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> >> >> >
> >> >> >
> >> >> >
> >> >> > _______________________________________________
> >> >> > NumPy-Discussion mailing list
> >> >> > NumPy-Discussion at scipy.org
> >> >> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
> >> >> >
> >> >> _______________________________________________
> >> >> NumPy-Discussion mailing list
> >> >> NumPy-Discussion at scipy.org
> >> >> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> >> >
> >> >
> >> >
> >> > _______________________________________________
> >> > NumPy-Discussion mailing list
> >> > NumPy-Discussion at scipy.org
> >> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
> >> >
> >> _______________________________________________
> >> NumPy-Discussion mailing list
> >> NumPy-Discussion at scipy.org
> >> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> >
> >
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion at scipy.org
> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
> >
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150417/df890a2e/attachment.sig>


More information about the NumPy-Discussion mailing list