Numpy数组:当2d数组的行等于另一个2d数组的行时,更改最后一列的值

时间:2015-09-22 16:47:12

标签: python arrays performance numpy

我有一个巨大的2D numpy数组(称为DATA)。我想更改所有DATA行的最后一个值(列),如果这些值类似于相同形状的外部行(称为ExtLine)。

# -*- coding: utf-8 -*-
import numpy

DATA=numpy.array([
    [1,2,3,4,5,6,0],
    [2,5,6,84,1,6,0],
    [9,9,9,9,9,9,0],
    [1,2,3,4,5,6,0],
    [2,5,6,84,1,6,0],
    [0,2,5,4,8,9,0]  ])
# Pool of lines that will be compared to DATA
PoolOfExtLines=numpy.array([[1,2,3,4,5,6,0],[2,5,6,84,1,6,0]])

for j in xrange(PoolOfExtLines.shape[0]):   # loop on pool of lines
    # convert ExtLine into a continous code (to be compare to future lines of DATA
    b=numpy.ascontiguousarray(PoolOfExtLines[j]).view(numpy.dtype((numpy.void, PoolOfExtLines[j].dtype.itemsize * PoolOfExtLines[j].shape[0])))

    for i in xrange(DATA.shape[0]):     # loop on DATA lines
        # convert the current line into a continous code (to be compare to b)
        a=numpy.ascontiguousarray(DATA[i]).view(numpy.dtype((numpy.void, DATA[i].dtype.itemsize * DATA[i].shape[0])))
        if a == b:
            DATA[i,-1]=-1

它会导致我想要修改的DATA数组(在行的末尾标记-1,类似于PoolOfExtLines的那些:

  [[ 1,  2,  3,  4,  5,  6, -1],
   [ 2,  5,  6, 84,  1,  6, -1],
   [ 9,  9,  9,  9,  9,  9,  0],
   [ 1,  2,  3,  4,  5,  6, -1],
   [ 2,  5,  6, 84,  1,  6, -1],
   [ 0,  2,  5,  4,  8,  9,  0]]

我的问题:我认为这段代码可以增强,并且在我想要做的事情上相当复杂。我觉得使用我错过的一些(内置)方法或智能直接(如何?)比较,我可以使代码更清晰,更快。感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可以使用NumPy's broadcasting功能和boolean indexing以矢量化方式解决问题 -

DATA[((DATA == PoolOfExtLines[:,None,:]).all(2)).any(0),-1] = -1

示例运行 -

In [17]: DATA
Out[17]: 
array([[ 1,  2,  3,  4,  5,  6,  0],
       [ 2,  5,  6, 84,  1,  6,  0],
       [ 9,  9,  9,  9,  9,  9,  0],
       [ 1,  2,  3,  4,  5,  6,  0],
       [ 2,  5,  6, 84,  1,  6,  0],
       [ 0,  2,  5,  4,  8,  9,  0]])

In [18]: PoolOfExtLines
Out[18]: 
array([[ 1,  2,  3,  4,  5,  6,  0],
       [ 2,  5,  6, 84,  1,  6,  0]])

In [19]: DATA[((DATA == PoolOfExtLines[:,None,:]).all(2)).any(0),-1] = -1

In [20]: DATA
Out[20]: 
array([[ 1,  2,  3,  4,  5,  6, -1],
       [ 2,  5,  6, 84,  1,  6, -1],
       [ 9,  9,  9,  9,  9,  9,  0],
       [ 1,  2,  3,  4,  5,  6, -1],
       [ 2,  5,  6, 84,  1,  6, -1],
       [ 0,  2,  5,  4,  8,  9,  0]])