Discussion:
[Numpy-discussion] calculating the difference of an array
Manuel Wittchen
2010-01-02 10:23:13 UTC
Permalink
Hi,

I want to calculate the difference between the values of a
numpy-array. The formula is:

deltaT = t_(n+1) - t_(n)

My approach to calculate the difference looks like this:

for i in len(ARRAY):
delta_t[i] = ARRAY[(i+1):] - ARRAY[:(len(ARRAY)-1)]

print "result:", delta_t

But I get a TypeError:
File "./test.py", line 19, in <module>
for i in len(ARRAY):
TypeError: 'int' object is not iterable

Where is the mistake in the code?

Regards and a happy new year,
Manuel Wittchen
David Cournapeau
2010-01-02 10:31:32 UTC
Permalink
On Sat, Jan 2, 2010 at 7:23 PM, Manuel Wittchen
Post by Manuel Wittchen
Hi,
I want to calculate the difference between the values of a
deltaT = t_(n+1) - t_(n)
       delta_t[i] = ARRAY[(i+1):] - ARRAY[:(len(ARRAY)-1)]
print "result:", delta_t
File "./test.py", line 19, in <module>
TypeError: 'int' object is not iterable
Where is the mistake in the code?
There are several mistakes :)

Assuming ARRAY is a numpy array, len(ARRAY) will return an int. You
would have the same error if ARRAY was any sequence: you should
iterate over range(len(ARRAY)).

Your formula within the loop is not very clear, and does not seem to
match your formula. ARRAY[i+1:] gives you all the items ARRAY[i+1]
until the end, ARRAY[:len(ARRAY)-1] gives you every item ARRAY[j] for
0 <= j < len(ARRAY)-1, that is the whole array.

I think you want:

for i in range(len(ARRAY)-1):
delta_i[i] = ARRAY[i+1] - ARRAY[i]

Also, using numpy efficiently requires to use vectorization, so actually:

delta_t = ARRAY[1:] - ARRAY[:-1]

gives you a more efficient version.

But really, you should use diff, which implements what you want:

import numpy as np
delta_t = np.diff(ARRAY)

cheers,

David
Emmanuelle Gouillart
2010-01-02 10:34:47 UTC
Permalink
Hello Manuel,

the discrete difference of a numpy array can be written in a very
Post by Manuel Wittchen
a = np.arange(10)**2
a
array([ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81])
Post by Manuel Wittchen
a[1:] - a[:-1]
array([ 1, 3, 5, 7, 9, 11, 13, 15, 17])
Post by Manuel Wittchen
np.diff(a) # another way to calculate the difference
array([ 1, 3, 5, 7, 9, 11, 13, 15, 17])

The error in the example you give is due to the fact that you iterate
over len(ARRAY), which is an integer, hence not an iterable object. You
should write ``for i in range(len(ARRAY))`` instead.

Cheers,

Emmanuelle
Post by Manuel Wittchen
Hi,
I want to calculate the difference between the values of a
deltaT = t_(n+1) - t_(n)
delta_t[i] = ARRAY[(i+1):] - ARRAY[:(len(ARRAY)-1)]
print "result:", delta_t
File "./test.py", line 19, in <module>
TypeError: 'int' object is not iterable
Where is the mistake in the code?
Regards and a happy new year,
Manuel Wittchen
_______________________________________________
NumPy-Discussion mailing list
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Manuel Wittchen
2010-01-02 11:03:23 UTC
Permalink
Hi,

Thanks for your help.
I tried np.diff() before, but the result looked like this:

RESULT = [1, 1, 1, 1]

So I was thinking that np.diff() doesn't iterate over the values of
the array. So I gave the for-loop a try.
Now, seeing your code below, I realized that my mistake was that I
used ARRAY = [0, 1, 2, 3, 4, 5] for the calculations...

Stupid me.
Post by Emmanuelle Gouillart
Hello Manuel,
the discrete difference of a numpy array can be written in a very
Post by Manuel Wittchen
a = np.arange(10)**2
a
array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81])
Post by Manuel Wittchen
a[1:] - a[:-1]
array([ 1,  3,  5,  7,  9, 11, 13, 15, 17])
Post by Manuel Wittchen
np.diff(a) # another way to calculate the difference
array([ 1,  3,  5,  7,  9, 11, 13, 15, 17])
The error in the example you give is due to the fact that you iterate
over len(ARRAY), which is an integer, hence not an iterable object. You
should write ``for i in range(len(ARRAY))`` instead.
Cheers,
Emmanuelle
Post by Manuel Wittchen
Hi,
I want to calculate the difference between the values of a
deltaT = t_(n+1) - t_(n)
      delta_t[i] = ARRAY[(i+1):] - ARRAY[:(len(ARRAY)-1)]
print "result:", delta_t
File "./test.py", line 19, in <module>
TypeError: 'int' object is not iterable
Where is the mistake in the code?
Regards and a happy new year,
Manuel Wittchen
_______________________________________________
NumPy-Discussion mailing list
http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Continue reading on narkive:
Loading...