[Numpy-discussion] Indexing with callables (was: Yorick-like functionality)
josef.pktd@gmai...
josef.pktd@gmai...
Fri May 15 15:47:46 CDT 2009
On Fri, May 15, 2009 at 4:09 PM, David Huard <david.huard@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
More information about the Numpy-discussion
mailing list