user4179448
user4179448

Reputation:

For loop and 'numpy.float64' object is not iterable error

I have a simple for loop to calculate RMS(root mean square) which is defined in sigma summation form:

for i in range(int(N-(n*periyot/delta)), N+1):
    sum = np.sqrt((1 / N) * (sum((Cl[i]**2))))

Then I got this error:

TypeError: 'numpy.float64' object is not iterable

Here are some information about my definitons:

N=40000, n=10.0, periyot=6.451290, delta=0.005  

Cl=[-21.91969   -12.452671   -7.928303  ...,  -0.0833991  -0.0579686
  -0.0823822]

Upvotes: 1

Views: 34129

Answers (3)

hpaulj
hpaulj

Reputation: 231385

Replicating your calculation, a bit simplified:

In [1]: Cl = np.array([-21.91969  , -12.452671 ,  -7.928303 ,  -0.0833991,-0.0579686,-0.0823822])

To calculate a sum in a loop, initial a value, and add to it at each iteration:

In [2]: res = 0
In [3]: for i in range(len(Cl)):
   ...:    res += np.sqrt((1/3)*Cl[i]**2)
   ...:    
In [4]: res
Out[4]: 24.551481812296061

Letting numpy calculate everything (slightly different)

In [5]: np.sqrt((1/3)*Cl**2).sum()
Out[5]: 24.551481812296064

Your range is a little more complicated, but I think that can be accommodated with:

s, e = int(N-(n*periyot/delta)), N+1  # start, end of range

for i in range(s, e): ....

or

np.sqrt((1/N) * Cl[s:e]**2).sum()

But I wonder why you started with that sum((Cl[i]**2))). Where you hoping to square a range of Cl values and then sum them? And repeat that for multiple ranges?

=============

There's a np.sum and a Python sum. Python sum works nicely with a list of numbers, such as those generated by a list comprehension:

In [6]: [np.sqrt((1/3)*Cl[i]**2) for i in range(len(Cl))]
Out[6]: 
[12.655338922053147,
 7.1895529539798462,
 4.5774078712669173,
 0.048150492835172518,
 0.03346818681454574,
 0.047563385346433583]

In [7]: sum([np.sqrt((1/3)*Cl[i]**2) for i in range(len(Cl))])
Out[7]: 24.551481812296061

The errors that result from trying to apply sum to a single value:

In [9]: sum(Cl[0])
....
TypeError: 'numpy.float64' object is not iterable
In [10]: sum(12.234)
...
TypeError: 'float' object is not iterable

In [11]: sum(Cl[:3])     # sum of several items
Out[11]: -42.300663999999998

==========

 RMS = ( (1 / N ) * (Cl[1]^2 + Cl[2]^2 + Cl[3]^2 + ... Cl[N]^2) ) ^0.5

is expressed, for lists as:

 rms = (1/n) * math.sqrt(sum([Cl[1]**2, Cl[2]**2, ....]))
 rms = (1/n) * math.sqrt(sum([Cl[i]**2 for i in range(len(Cl))]))
 rms = (1/n) * math.sqrt(sum([c**2 for c in Cl]))   # iterate on Cl directly
 rms = (1/n) * np.sqrt(np.sum(Cl**2))     # for array Cl

Upvotes: 1

desa
desa

Reputation: 1390

The problem is that you overwrite sum function with sum variable. Try something like this:

my_sum = 0
for i in range(int(N-(n*periyot/delta)), N+1):
    my_sum += np.sqrt((1 / N) * (sum((Cl[i]**2))))

Upvotes: 1

Moses Koledoye
Moses Koledoye

Reputation: 78554

Remove that sum, each element of Cl is a float so you can't possibly call sum on them:

>>> sum(2.4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object is not iterable

If you intend to invoke numpy's broadcasting to perform the power operation then you don't need to index the array.

Upvotes: 3

Related Questions