Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use `np.diff` but assume the input starts with an extra zero

Tags:

python

numpy

Given a series of event times v, I can create their interval durations using np.diff(v). Is there a way to have np.diff assume the series starts with an implicit 0., so that it produces an array that has the same length as v?

A manual workaround is:

def diff_from_zero(v):
    return np.diff(np.hstack(([0.], v)))

Is there a way to use diff or another function to have the same result?

like image 372
bluss Avatar asked Jan 26 '17 14:01

bluss


2 Answers

As of 2019, np.diff has the arguments prepend and append that can add a certain value to the array before differentiation. See the docs

This would append the first value to the array, hence the diff operation would return something of len(t) that starts with 0.

>>> t = np.array([1.1, 2.0, 4.5, 4.9, 5.2])
>>> np.diff(t, prepend=t[0])
array([0. , 0.9, 2.5, 0.4, 0.3])

The prepend argument can take other values.

like image 54
Matias Andina Avatar answered Oct 04 '22 15:10

Matias Andina


Given for example:

t = np.array([1.1, 2.0, 4.5, 4.9, 5.2])

We want to compute the consecutive differences in t, including the diff from 0. to the first element in t.

The question gave this way of accomplishing this:

>>> np.diff(np.hstack((0, t)))

And it could be this too:

>>> np.hstack((t[0], np.diff(t)))

But the obscurely-named function ediff1d can do it in one function call:

>>> np.ediff1d(t, to_begin=t[0])
array([ 1.1,  0.9,  2.5,  0.4,  0.3])

Prepending t[0] to the result is the same as computing the difference t[0] - 0., of course. (Assuming t is nonempty).


Timings (not the motivation of the question, but I was curious)

import numpy as np
t = np.random.randn(10000)
%timeit np.diff(np.concatenate(([0], t)))
10000 loops, best of 3: 23.1 µs per loop
%timeit np.diff(np.hstack((0, t)))
10000 loops, best of 3: 31.2 µs per loop
%timeit np.ediff1d(t, to_begin=t[0])
10000 loops, best of 3: 92 µs per loop
like image 25
bluss Avatar answered Oct 04 '22 15:10

bluss