简体   繁体   English

在Python中:给定一个原始的数字数组,我如何创建一个包含特定范围内原始数组值的新数组?

[英]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 > 2a < 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.

相关问题 如何有效检查numpy数组包含给定范围内的项目? - How to check efficiently numpy array contains item within given range? (Python Numpy)如何从给定的二维数组创建新的 3D 数组? - (Python Numpy) How can I create new 3D array from given 2D array? 如何在不更改 python 中的原始值的情况下将二维数组中的元素分配给新变量? - How to assign an element from a 2D array to a new variable without changing its original value in python? 如何检测数组中的值是否在某个范围内并在Python中返回二进制数组? - How to detect if the values in array are in a certain range and return a binary array in Python? 如何在python中使用groupby更新原始数组 - How to update original array with groupby in python 如何从数组中提取范围内的值 - How to extract values within a range from an array 重塑numpy数组以包含原始数组中的逻辑逻辑块 - Reshape numpy array to contain logical blocks of values from original array 如何从原始数组创建numpy子数组列表,然后在所述列表上应用函数? - How do I create a list of numpy subarrays from an original array and then apply a function over said list? Python - 如何只选择数组中特定范围的数字 - Python - How to only select certain range of numbers in an array 如何从 python 中的数字和单词的原始列表中创建仅包含数字和单词/短语的新列表? - How to create a new list with just numbers and words/phrases from a original list with both numbers and words in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM