Python For 循环和 'numpy.float64' 对象不是可迭代错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41394749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:53:08  来源:igfitidea点击:

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

pythonpython-3.xnumpyfor-loop

提问by hpaulj

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

我有一个简单的 for 循环来计算以 sigma 求和形式定义的 RMS(均方根):

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]

回答by Moses Koledoye

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

删除那个sum,每个元素Cl都是一个浮点数,所以你不可能对它们调用 sum:

>>> 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 broadcastingto perform the power operation then you don't need to index the array.

如果您打算调用 numpy 的广播来执行电源操作,那么您不需要索引数组。

回答by desa

The problem is that you overwrite sumfunction with sumvariable. Try something like this:

问题是你sumsum变量覆盖了函数。尝试这样的事情:

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

回答by hpaulj

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 numpycalculate everything (slightly different)

numpy计算一切(略有不同)

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 Clvalues and then sum them? And repeat that for multiple ranges?

但我想知道你为什么开始这样做sum((Cl[i]**2)))。您希望在哪里对一系列Cl值求平方然后对它们求和?并重复多个范围?

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

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

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

有一个np.sum和一个 Python sum。Pythonsum可以很好地处理数字列表,例如列表推导式生成的数字列表:

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 sumto a single value:

尝试应用于sum单个值导致的错误:

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