partial_sum/adj_difference?
Does numpy/scipy have a partial_sum and adj_difference function? partial_sum[i] = \sum_{j=0}^{i} x[j] adj_diff[i] = x[i] - x[i-1] : i > 1, x[i] otherwise
A Tuesday 19 February 2008, Neal Becker escrigué:
Does numpy/scipy have a partial_sum and adj_difference function?
partial_sum[i] = \sum_{j=0}^{i} x[j] adj_diff[i] = x[i] - x[i-1] : i > 1, x[i] otherwise
I don't know, but by using views the next should be fairly efficient: # Partial sum In [28]: a = numpy.arange(10) In [29]: ps = numpy.empty(len(a), 'int') In [30]: for i in range(len(a)): ps[i] = a[:i].sum() ....: In [31]: ps Out[31]: array([ 0, 0, 1, 3, 6, 10, 15, 21, 28, 36]) # Adj difference: In [35]: ad = numpy.empty(len(a), 'int') In [36]: ad[0] = a[0] In [37]: ad[1:] = a[1:] - a[:-1] In [38]: ad Out[38]: array([0, 1, 1, 1, 1, 1, 1, 1, 1, 1]) Cheers, --
0,0< Francesc Altet http://www.carabos.com/ V V Cárabos Coop. V. Enjoy Data "-"
-----Original Message----- From: numpy-discussion-bounces@scipy.org on behalf of Francesc Altet Instead of for i in range(len(a)): ps[i] = a[:i].sum() use a.cumsum() Nadav Sent: Tue 19-Feb-08 20:59 To: Discussion of Numerical Python Subject: Re: [Numpy-discussion] partial_sum/adj_difference? A Tuesday 19 February 2008, Neal Becker escrigué:
Does numpy/scipy have a partial_sum and adj_difference function?
partial_sum[i] = \sum_{j=0}^{i} x[j] adj_diff[i] = x[i] - x[i-1] : i > 1, x[i] otherwise
I don't know, but by using views the next should be fairly efficient: # Partial sum In [28]: a = numpy.arange(10) In [29]: ps = numpy.empty(len(a), 'int') In [30]: for i in range(len(a)): ps[i] = a[:i].sum() ....: In [31]: ps Out[31]: array([ 0, 0, 1, 3, 6, 10, 15, 21, 28, 36]) # Adj difference: In [35]: ad = numpy.empty(len(a), 'int') In [36]: ad[0] = a[0] In [37]: ad[1:] = a[1:] - a[:-1] In [38]: ad Out[38]: array([0, 1, 1, 1, 1, 1, 1, 1, 1, 1]) Cheers, --
0,0< Francesc Altet http://www.carabos.com/ V V Cárabos Coop. V. Enjoy Data "-"
Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
On Feb 19, 2008 11:38 AM, Neal Becker <ndbecker2@gmail.com> wrote:
Does numpy/scipy have a partial_sum and adj_difference function?
partial_sum[i] = \sum_{j=0}^{i} x[j]
Make add.accumulate will do the trick: In [1]: add.accumulate(arange(10)) Out[1]: array([ 0, 1, 3, 6, 10, 15, 21, 28, 36, 45]) In [3]: arange(10).cumsum() Out[3]: array([ 0, 1, 3, 6, 10, 15, 21, 28, 36, 45])
adj_diff[i] = x[i] - x[i-1] : i > 1, x[i] otherwise
Well, x[1:] - x[:-1] will give the usual differences. If you need the leading x[0] prefix the x vector with a 0. In [4]: a = arange(10) In [5]: b = a[1:] - a[:-1] In [6]: b Out[6]: array([1, 1, 1, 1, 1, 1, 1, 1, 1]) Chuck
On Feb 19, 2008 11:38 AM, Neal Becker <ndbecker2@gmail.com> wrote:
adj_diff[i] = x[i] - x[i-1] : i > 1, x[i] otherwise
Well, x[1:] - x[:-1] will give the usual differences. If you need the leading x[0] prefix the x vector with a 0.
There's also numpy.diff, and the little known numpy.ediff1d
x=numpy.arange(10) numpy.ediff1d(x,to_begin=0) array([0, 1, 1, 1, 1, 1, 1, 1, 1, 1])
Hi Neal On Tue, Feb 19, 2008 at 01:38:06PM -0500, Neal Becker wrote:
Does numpy/scipy have a partial_sum and adj_difference function?
partial_sum[i] = \sum_{j=0}^{i} x[j]
numpy.cumsum Yikes, the docstring contains "Blah, blah". I'll fix that immediately.
adj_diff[i] = x[i] - x[i-1] : i > 1, x[i] otherwise
numpy.diff Regards Stéfan
On Feb 19, 2008 12:41 PM, Stefan van der Walt <stefan@sun.ac.za> wrote:
Hi Neal
On Tue, Feb 19, 2008 at 01:38:06PM -0500, Neal Becker wrote:
Does numpy/scipy have a partial_sum and adj_difference function?
partial_sum[i] = \sum_{j=0}^{i} x[j]
numpy.cumsum
Yikes, the docstring contains "Blah, blah". I'll fix that immediately.
Gosh, And here I thought you were going to fix that. Deleting the "blahs" isn't a fix, it's a coverup. Now there is no extended documentation at all. Chuck
On Tue, 19 Feb 2008 13:50:04 -0700 "Charles R Harris" <charlesr.harris@gmail.com> wrote:
On Feb 19, 2008 12:41 PM, Stefan van der Walt <stefan@sun.ac.za> wrote:
Hi Neal
On Tue, Feb 19, 2008 at 01:38:06PM -0500, Neal Becker wrote:
Does numpy/scipy have a partial_sum and adj_difference function?
partial_sum[i] = \sum_{j=0}^{i} x[j]
numpy.cumsum
Yikes, the docstring contains "Blah, blah". I'll fix that immediately.
Gosh,
And here I thought you were going to fix that. Deleting the "blahs" isn't a fix, it's a coverup. Now there is no extended documentation at all.
Chuck
;-)
from numpy import cumprod help (cumprod)
Nils
On Tue, Feb 19, 2008 at 01:50:04PM -0700, Charles R Harris wrote:
And here I thought you were going to fix that. Deleting the "blahs" isn't a fix, it's a coverup. Now there is no extended documentation at all.
I wouldn't call "Blah, blah" extended documentation -- in fact, I would've been rather embarrassed if that showed up on my screen during a workshop. "Blah, blah" also doesn't strike me as the ideal TODO marker. We can maybe use some more sensible text, or write a decorator to mark these functions. I'd say we add them to the TODO list for the next doc-day and run with it... Regards Stéfan
On Feb 19, 2008 2:20 PM, Stefan van der Walt <stefan@sun.ac.za> wrote:
On Tue, Feb 19, 2008 at 01:50:04PM -0700, Charles R Harris wrote:
And here I thought you were going to fix that. Deleting the "blahs" isn't a fix, it's a coverup. Now there is no extended documentation at all.
I wouldn't call "Blah, blah" extended documentation -- in fact, I would've been rather embarrassed if that showed up on my screen during a workshop.
"Blah, blah" also doesn't strike me as the ideal TODO marker. We can maybe use some more sensible text, or write a decorator to mark these functions.
It has certainly been effective in practice. Chuck
On Tue, Feb 19, 2008 at 05:36:52PM -0700, Charles R Harris wrote:
On Feb 19, 2008 2:20 PM, Stefan van der Walt <stefan@sun.ac.za> wrote:
On Tue, Feb 19, 2008 at 01:50:04PM -0700, Charles R Harris wrote: > And here I thought you were going to fix that. Deleting the > "blahs" isn't a > fix, it's a coverup. Now there is no extended > documentation at all.
I wouldn't call "Blah, blah" extended documentation -- in fact, I would've been rather embarrassed if that showed up on my screen during a workshop.
"Blah, blah" also doesn't strike me as the ideal TODO marker. We can maybe use some more sensible text, or write a decorator to mark these functions.
It has certainly been effective in practice.
Yes, ironically :) http://projects.scipy.org/scipy/numpy/changeset/4813 http://projects.scipy.org/scipy/numpy/changeset/4814 The first courtesy of Matthew Brett. Regards Stéfan
ti, 2008-02-19 kello 13:38 -0500, Neal Becker kirjoitti:
Does numpy/scipy have a partial_sum and adj_difference function?
partial_sum[i] = \sum_{j=0}^{i} x[j] adj_diff[i] = x[i] - x[i-1] : i > 1, x[i] otherwise
cumsum and diff do something like this:
import numpy a = [1,2,3,4,5,3,1] numpy.cumsum(a) array([ 1, 3, 6, 10, 15, 18, 19]) numpy.diff(a) array([ 1, 1, 1, 1, -2, -2])
participants (8)
-
Charles R Harris
-
Francesc Altet
-
Nadav Horesh
-
Neal Becker
-
Nils Wagner
-
Pauli Virtanen
-
Pierre GM
-
Stefan van der Walt