使用具有多个条件的numpy.where搜索2D数组

时间:2018-04-18 14:22:45

标签: python python-2.7 numpy

我有一个2D数组,其定义如下:

DECLARE
    next_run_date TIMESTAMP;
BEGIN
    FOR i IN 1..50 LOOP
        DBMS_SCHEDULER.EVALUATE_CALENDAR_STRING('FREQ=HOURLY;BYHOUR=06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23;BYMINUTE=0;BYSECOND=0;BYDAY=Mon', NULL, next_run_date, next_run_date);
        DBMS_OUTPUT.PUT_LINE ( next_run_date );
    END LOOP;
END;

我想找到与所选列的多个条件匹配的行的索引。例如,我想在这个数组中找到

的行
traces = [['x1',11026,0,0,0,0],
          ['x0',11087,0,0,0,1],
          ['x0',11088,0,0,1,3],
          ['x0',11088,0,0,0,3],
          ['x0',11088,0,1,0,1]]

搜索此标准应返回4.

我尝试使用numpy.where但似乎无法使其适用于多种条件

row[0]=='x0' & row[1]==11088 & row[3]==1 & row[5]=1

以上创建了警告

print np.where((traces[:,0] == 'x0') & (traces[:,1] == 11088) & (traces[:,3] == 1) & (traces[:,5] == 1))

我也试图使用FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison print np.where((traces[:,0] == 'x0') & (traces[:,1] == 11088) & (traces[:,3] == 1) & (traces[:,5] == 1)) (array([], dtype=int32),) ,这似乎也不起作用,创建类似的警告。

我可以使用numpy.logical_and在不迭代整个2D数组的情况下执行此操作吗?

由于

2 个答案:

答案 0 :(得分:0)

魔晶球表示问题与阵列的构造方式有关。

traces = [['x1',11026,0,0,0,0],
          ['x0',11087,0,0,0,1],
          ['x0',11088,0,0,1,3],
          ['x0',11088,0,0,0,3],
          ['x0',11088,0,1,0,1]]

traces = np.array(traces)

这表现出所描述的错误。打印结果数组可以看出原因:

print(traces)
# array([['x1', '11026', '0', '0', '0', '0'],
#        ['x0', '11087', '0', '0', '0', '1'],
#        ['x0', '11088', '0', '0', '1', '3'],
#        ['x0', '11088', '0', '0', '0', '3'],
#        ['x0', '11088', '0', '1', '0', '1']],
#       dtype='<U5')

数字已转换为字符串!

作为一个解决方案,显式地将数组构造为“对象数组”:

traces = np.array(traces, dtype='object')

print(np.where((traces[:,0] == 'x0') & (traces[:,1] == 11088) & (traces[:,3] == 1) & (traces[:,5] == 1)))
# (array([4], dtype=int32),)

请注意,尽管这样可行,但对象数组通常不是一个好主意。请考虑用数值替换第一列中的字符串。

答案 1 :(得分:0)

考虑这种比较:

python -m http.server

我们正在寻找所有值等于True的一行(或多行):

>>> traces[:,[0,1,3,5]] == ['x0', 11088, 1, 1]
array([[False, False, False, False],
       [ True, False, False,  True],
       [ True,  True, False, False],
       [ True,  True, False, False],
       [ True,  True,  True,  True]])