[英]In Python: Given an original array of numbers, how would I create a new array that contains the values from the original array within a certain range?
import os
import pyfits as ps
import lomb
import numpy as np
import matplotlib.pyplot as plt
hdulist = ps.open('filename')
tbdata = hdulist[1].data
PDCFlux = tbdata.field(7)
PDCFlux = PDCFlux[~np.isnan(PDCFlux)]
psd,freq = lomb.lomb(Timesec,PDCFlux)
logpsd = np.log10(psd)
logfreq = np.log10(freq)
#want an associated 'slope'.
'''PDC Slope'''
PDCFluxrange = PDCFlux.all[np.log10(4*(10**(-5)))<freq<np.log10(4*(10**(-7)))]
In the last line, I attempted to create a new array: PDCFluxrange
that consists of values of the array PDCFlux
for freq
(frequency) between: log10(4*(10**(-5)))
and log10(4*(10**(-7))
. 在最后一行中,我尝试创建一个新数组:
PDCFluxrange
,它包含数组PDCFlux
for freq
(frequency)的值: log10(4*(10**(-5)))
和log10(4*(10**(-7))
。
When I run the program in Pylab, I receive an error message similar to: Value Error: The truth value of an array with more than one element is ambiguous
. 当我在Pylab中运行程序时,我收到类似于以下内容的错误消息:
Value Error: The truth value of an array with more than one element is ambiguous
。 Use a.any()
or a.all()
. 使用
a.any()
或a.all()
。
In which case, I am unsure of how to properly use either of these two functions in my code without creating a loop, or defining a new function; 在这种情况下,我不确定如何在我的代码中正确使用这两个函数中的任何一个,而无需创建循环或定义新函数; I think that there may be a simpler method to accomplish the same thing.
我认为可能有一种更简单的方法来完成同样的事情。
>>> import numpy as np
>>> a = np.arange(10)
>>> a[(a > 2) & (a < 8)]
array([3, 4, 5, 6, 7])
Each condition ( a > 2
and a < 8
) is creating a boolean numpy array which is equal in length to a
, and contains the truth value of your criterion at each position in a
. 每个条件(
a > 2
和a < 8
是创建一个布尔numpy的阵列,其是在长度上等于a
,且含有在每个位置的标准的真值a
。
>>> a > 2
array([False, False, False, True, True, True, True, True, True, True], dtype=bool)
Then, you're using numpy.logical_and to find the positions at which both conditions are true. 然后,您正在使用numpy.logical_and来查找两个条件都为真的位置。
Finally, you're using the resulting array as a boolean or "mask" index array to retrieve only the elements from the original array that meet your criteria. 最后,您将结果数组用作布尔或“掩码”索引数组,以仅检索符合条件的原始数组中的元素。
I think you're getting the ValueError
because while a > 2
is valid (and is really just calling a.__gt__(2)
, a statement like 2 < a < 8
is first calling cmp(2,a)
, which results in the same ValueError
for me: 我认为你得到了
ValueError
因为当a > 2
是有效的(并且实际上只是调用a.__gt__(2)
,像2 < a < 8
这样的语句首先调用cmp(2,a)
,这导致同样的ValueError
对我来说:
>>> a = np.arange(10)
>>> cmp(2,a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I think you want 我想你想要的
PDCFluxrange = [x for x in PDCFlux if x>np.log10(4*(10**(-5))) and x<np.log10(4*(10**(-7)))]
Does that work for you? 那对你有用吗? I hope I'm understanding your code right.
我希望我能正确理解你的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.