Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用numpy有效地获取正值对_Python_Logging_Numpy_Filter - Fatal编程技术网

Python 使用numpy有效地获取正值对

Python 使用numpy有效地获取正值对,python,logging,numpy,filter,Python,Logging,Numpy,Filter,我有一个python函数,它接受两个列表,在两个输入中查找在同一索引中都有正值的对,并通过将这两个正值中的每一个追加到两个输出列表中来创建两个输出列表。我有一个工作职能: def get_pairs_in_first_quadrant(x_in, y_in): """If both x_in[i] and y_in[i] are > 0 then both will appended to the output list. If either are negative th

我有一个python函数,它接受两个列表,在两个输入中查找在同一索引中都有正值的对,并通过将这两个正值中的每一个追加到两个输出列表中来创建两个输出列表。我有一个工作职能:

def get_pairs_in_first_quadrant(x_in, y_in):
    """If both x_in[i] and y_in[i] are > 0 then both will appended to the output list. If either are negative
    then the pair of them will be absent from the output list.
    :param x_in: A list of positive or negative floats
    :param y_in: A list of positive or negative floats
    :return: A list of positive floats <= in length to the inputs.
    """
    x_filtered, y_filtered = [], []
    for x, y in zip(x_in, y_in):
        if x > 0 and y > 0:
            x_filtered.append(x)
            y_filtered.append(y)
    return x_filtered, y_filtered
def get_pairs_在第一象限(x_in,y_in):
“”“如果[i]中的x_和[i]中的y_都大于0,则两者都将附加到输出列表中。如果其中一个为负数
然后,它们将从输出列表中消失。
:param x_in:正浮点数或负浮点数的列表
:param y_in:正浮点数或负浮点数的列表

:return:positive float的列表您只需找到两个指数均为正的指数即可:

import numpy as np

a = np.random.random(10) - .5
b = np.random.random(10) - .5

def get_pairs_in_first_quadrant(x_in, y_in):
    i = np.nonzero( (x_in>0) & (y_in>0) )   # main line of interest
    return x_in[i], y_in[i]

print a  # [-0.18012451 -0.40924713 -0.3788772   0.3186816   0.14811581 -0.04021951 -0.21278312 -0.36762629 -0.45369899 -0.46374929]
print b  # [ 0.33005969 -0.03167875  0.11387641  0.22101336  0.38412264 -0.3880842 0.08679424  0.3126209  -0.08760505 -0.40921421]
print get_pairs_in_first_quadrant(a, b)   # (array([ 0.3186816 ,  0.14811581]), array([ 0.22101336,  0.38412264]))

我对Jaime的建议很感兴趣,即只使用布尔索引而不调用
nonzero
,因此我运行了一些计时测试。结果有些有趣,因为它们的优势比与正匹配的数量是非单调的,但基本上,至少就速度而言,使用哪一个并不重要(虽然
非零
通常要快一点,速度可能是非零的两倍左右):

对于不同的阈值,它给出:

0.05 9086 9086
0.0815141201019
0.104746818542

0.5 2535 2535
0.0715141296387
0.153401851654

0.95 21 21
0.027126789093
0.0324990749359

使用。我们在这里讨论的列表有多大?长度可能在100000左右。不要调用布尔数组上的
np。非零
:直接使用它来索引
x\u in
y\u in
@Jaime:我进行了一次速度测试,它似乎没有太大区别。是否还有其他原因更喜欢布尔数组?您是否使用numpy 1.9?那个版本在索引方面有了很大的改进,似乎我的直觉已经过时了…如果你对单个数组进行索引,布尔值仍然会略微领先,但显然不适合更多的数组。
0.05 9086 9086
0.0815141201019
0.104746818542

0.5 2535 2535
0.0715141296387
0.153401851654

0.95 21 21
0.027126789093
0.0324990749359