[Numpy-discussion] Apply a vector function to each row of a matrix
David Huard
david.huard@gmail....
Fri Oct 10 10:27:24 CDT 2008
On Thu, Oct 9, 2008 at 2:48 PM, Neal Becker <ndbecker2@gmail.com> wrote:
> David Huard wrote:
>
> > On Thu, Oct 9, 2008 at 9:40 AM, Neal Becker <ndbecker2@gmail.com> wrote:
> >
> >> David Huard wrote:
> >>
> >> > Neal,
> >> >
> >> > Look at: apply_along_axis
> >> >
> >> >
> >> I guess it'd be:
> >>
> >> b = empty_like(a)
> >> for row in a.shape[0]:
> >> b[row,:] = apply_along_axis (func, row, a)
> >>
> >
> >> I don't suppose there is a way to do this without explicitly writing a
> >> loop.
> >
> >
> > Have you tried
> >
> > b = apply_along_axis(func, 1, a)
> >
> > It should work.
> >
> Yes, thanks.
>
> The doc for apply_along_axis is not clear.
>
> For one thing, it says:
> The output array. The shape of outarr depends on the return value of
> func1d. If it returns arrays with the same shape as the input arrays it
> receives, outarr has the same shape as arr.
>
> What happens if the 'if' clause is not true?
>
The shape along the axis is determined by the result's shape of your
function.
def func(x):
...: return x[::2]
...:
> [3]: a = random.rand(3,4)
> [4]: a
<[4]:
array([[ 0.95979758, 0.37350614, 0.77423741, 0.62520089],
[ 0.69060211, 0.91480227, 0.60105525, 0.20184552],
[ 0.31540644, 0.19919848, 0.72567385, 0.63987393]])
> [5]: apply_along_axis(func, 1, a)
<[5]:
array([[ 0.95979758, 0.77423741],
[ 0.69060211, 0.60105525],
[ 0.31540644, 0.72567385]])
I've edited the docstring at
http://sd-2116.dedibox.fr/pydocweb/doc/numpy.lib.shape_base.apply_along_axis/
Feel free to improve on it.
David
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20081010/d47dc191/attachment.html
More information about the Numpy-discussion
mailing list