1

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]
8
  • 2
    Show the full error message please, including the information that tells where the error occurs. Commented Dec 30, 2016 at 10:36
  • File "/home/emre/Documents/LiClipse Workspace/fe_over_f0/fe_f0_08.py", line 70, in <module> sum = np.sqrt((1 / N) * (sum((Cl[i]**2)))) TypeError: 'numpy.float64' object is not iterable Commented Dec 30, 2016 at 10:42
  • It seems you are simply looking for np.average(Cl[n1:n2]**2), where n1 = int(N-(n*periyot/delta)) and n2 = N+1 Commented Dec 30, 2016 at 10:52
  • @VBB I'll try that and inform you, thanks. Commented Dec 30, 2016 at 10:58
  • @ordinary let me know, I can post that as an answer if you want. Commented Dec 30, 2016 at 11:00

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

But I need to sum all of each element of Cl. There must be an alternative way to sum them.
@ordinary You need a different logic then. You are iterating over every element and throwing away sum on every iteration.
Even if you do fix the error @ordinary , if you still use the built-in function sum you'll face another exception during the second iteration since sum will be rebinded to a float. Use a different name for you summation i.e my_sum, not sum.
1

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))))

2 Comments

this is the other issue, sum(numpy.float64) is the first.
Thank you. I changed the code like you said: for i in range(int(N-(n*periyot/delta)), N+1): rms_Cl_sum = 0 rms_Cl_sum += np.sqrt((1 / N) * (sum((Cl[i]**2)))) But unfortunately I get same error. 'numpy.float64' object is not iterable
1

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

2 Comments

Thank you very much for your help. I want to simply calculate RMS. RMS = ( (1 / N ) * (Cl[1]^2 + Cl[2]^2 + Cl[3]^2 + ... Cl[N]^2) ) ^0.5 I think sum() must work for all numbers. I mean Python must have this kind of thing. I feel very disappointed sum does not work for my numbers ( which is like -12.452671)
I added an example of using the Python sum function with a list comprehension. Your error was the result of trying to apply sum to a single number, as opposed to a list or array. Cl[i] is a single value, not a range of values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.