[Numpy-discussion] Indexing with callables (was: Yorick-like functionality)

David Huard david.huard at gmail.com
Fri May 15 17:39:31 EDT 2009


Josef,

You're right, you can see it as a moving average. For 1D, correlate(a,
[5,.5]) yields what I expect but does not take an axis keyword. For the 2D
case, I'm rather looking for

>>> ndimage.filters.correlate(b,0.25*np.ones((2,2)))[1:,1:]

So another one-liner... maybe not worth adding to the numpy namespace.

David




On Fri, May 15, 2009 at 4:47 PM, <josef.pktd at gmail.com> wrote:

> On Fri, May 15, 2009 at 4:09 PM, David Huard <david.huard at gmail.com>
> wrote:
> > Pauli and David,
> >
> > Can this indexing syntax do things that are otherwise awkward with the
> > current syntax ? Otherwise, I'm not warm to the idea of making indexing
> more
> > complex than it is.
> >
> > getv : this is useful but it feels a bit redundant with numpy.take. Is
> there
> > a reason why take could not support slices ?
> >
> > Drop_last: I don't think it is worth cluttering the namespace with a one
> > liner.
> >
> > append_one: A generalized stack method with broadcasting capability would
> be
> > more useful in my opinion, eg. ``np.stack(x, 1., axis=1)``
> >
> > zcen: This is indeed useful, particulary in its nd form, that is, when it
> > can be applied to multiples axes to find the center of a 2D or 3D cell in
> > one call. I'm appending the version I use below.
> >
> > Cheers,
> >
> > David
> >
> >
> > # This code is released in the public domain.
> > import numpy as np
> > def __midpoints_1d(a):
> >     """Return `a` linearly interpolated at the mid-points."""
> >     return (a[:-1] + a[1:])/2.
> >
> > def midpoints(a,  axis=None):
> >     """Return `a` linearly interpolated at the mid-points.
> >
> >     Parameters
> >     ----------
> >     a : array-like
> >       Input array.
> >     axis : int or None
> >       Axis along which the interpolation takes place. None stands for all
> > axes.
> >
> >     Returns
> >     -------
> >     out : ndarray
> >       Input array interpolated at the midpoints along the given axis.
> >
> >     Examples
> >     --------
> >     >>> a = [1,2,3,4]
> >     >>> midpoints(a)
> >     array([1.5, 2.5, 3.5])
> >     """
> >     x = np.asarray(a)
> >     if axis is not None:
> >         return np.apply_along_axis(__midpoints_1d,  axis, x)
> >     else:
> >         for i in range(x.ndim):
> >             x = midpoints(x,  i)
> >         return x
> >
>
> zcen is just a moving average, isn't it? For time series (1d),
> correlate works well, for 2d (nd?), there is
>
> >>> a= np.arange(5)
> >>> b = 1.0*a[:,np.newaxis]*np.arange(4)
> >>> ndimage.filters.correlate(b,0.5*np.ones((2,1)))[1:,1:]
> >>> ndimage.filters.correlate(b,0.5*np.ones((2,1)))[1:,1:]
>
> Josef
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20090515/2591dcb0/attachment.html>


More information about the NumPy-Discussion mailing list