簡體   English   中英

將numpy點數組分配給2D方格

[英]Assign numpy array of points to a 2D square grid

由於速度問題,我超越了之前的問題。 我有一個Lat / Lon坐標點數組,我想將它們分配給從相同大小的單元格的2D方格網格派生的索引代碼。 這是一個如何的例子。 讓我們叫points (稱他們為[XY]對)的六分我含坐標第一個數組:

points = [[ 1.5  1.5]
 [ 1.1  1.1]
 [ 2.2  2.2]
 [ 1.3  1.3]
 [ 3.4  1.4]
 [ 2.   1.5]]

然后我有另一個數組,包含[minx,miny,maxx,maxy]形式的兩個單元格的頂點坐標; 讓我們稱之為bounds

bounds = [[ 0.  0.  2.  2.]
 [ 2.  2.  3.  3.]]

我想找到哪個點位於哪個邊界,然后分配從bounds數組索引派生的代碼(在這種情況下,第一個單元格有代碼0,第二個單元格等等......)。 由於單元格是正方形,因此計算每個單元格中每個單元格的最簡單方法是評估:

x > minx & x < maxx & y > miny & y < maxy

這樣生成的數組將顯示為:

results = [0 0 1 0 NaN NaN]

其中NaN表示該點在細胞外。 在我的實際情況中,元素的數量是在10 ^ 4個單元格中找到10 ^ 6個點的順序。 有沒有辦法使用numpy數組快速完成這種事情?

編輯:澄清一下, results數組預期意味着第一個點在第一個單元格內( bounds數組的0個索引),所以第二個,第一個點在bounds數組的第二個單元格bounds ,依此類推......

這是針對您的問題的矢量化方法。 它應該顯着加快速度。

import numpy as np
def findCells(points, bounds):
    # make sure points is n by 2 (pool.map might send us 1D arrays)
    points = points.reshape((-1,2))

    # check for each point if all coordinates are in bounds
    # dimension 0 is bound
    # dimension 1 is is point
    allInBounds = (points[:,0] > bounds[:,None,0])
    allInBounds &= (points[:,1] > bounds[:,None,1])
    allInBounds &= (points[:,0] < bounds[:,None,2])
    allInBounds &= (points[:,1] < bounds[:,None,3])


    # now find out the positions of all nonzero (i.e. true) values
    # nz[0] contains the indices along dim 0 (bound)
    # nz[1] contains the indices along dim 1 (point)
    nz = np.nonzero(allInBounds)

    # initialize the result with all nan
    r = np.full(points.shape[0], np.nan)
    # now use nz[1] to index point position and nz[0] to tell which cell the
    # point belongs to
    r[nz[1]] = nz[0]
    return r

def findCellsParallel(points, bounds, chunksize=100):
    import multiprocessing as mp
    from functools import partial

    func = partial(findCells, bounds=bounds)

    # using python3 you could also do 'with mp.Pool() as p:'  
    p = mp.Pool()
    try:
        return np.hstack(p.map(func, points, chunksize))
    finally:
        p.close()

def main():
    nPoints = 1e6
    nBounds = 1e4

    # points = np.array([[ 1.5, 1.5],
    #                    [ 1.1, 1.1],
    #                    [ 2.2, 2.2],
    #                    [ 1.3, 1.3],
    #                    [ 3.4, 1.4],
    #                    [ 2. , 1.5]])

    points = np.random.random([nPoints, 2])

    # bounds = np.array([[0,0,2,2],
    #                    [2,2,3,3]])

    # bounds = np.array([[0,0,1.4,1.4],
    #                    [1.4,1.4,2,2],
    #                    [2,2,3,3]])

    bounds = np.sort(np.random.random([nBounds, 2, 2]), 1).reshape(nBounds, 4)

    r = findCellsParallel(points, bounds)
    print(points[:10])
    for bIdx in np.unique(r[:10]):
        if np.isnan(bIdx):
            continue
        print("{}: {}".format(bIdx, bounds[bIdx]))
    print(r[:10])

if __name__ == "__main__":
    main()

編輯:
嘗試使用大量數據會給我一個MemoryError 如果您使用multiprocessing.Pool及其map函數,您可以避免這種情況甚至加快速度,請參閱更新的代碼。

結果:

>time python test.py
[[ 0.69083585  0.19840985]
 [ 0.31732711  0.80462512]
 [ 0.30542996  0.08569184]
 [ 0.72582609  0.46687164]
 [ 0.50534322  0.35530554]
 [ 0.93581095  0.36375539]
 [ 0.66226118  0.62573407]
 [ 0.08941219  0.05944215]
 [ 0.43015872  0.95306899]
 [ 0.43171644  0.74393729]]
9935.0: [ 0.31584562  0.18404152  0.98215445  0.83625487]
9963.0: [ 0.00526106  0.017255    0.33177741  0.9894455 ]
9989.0: [ 0.17328876  0.08181912  0.33170444  0.23493507]
9992.0: [ 0.34548987  0.15906761  0.92277442  0.9972481 ]
9993.0: [ 0.12448765  0.5404578   0.33981119  0.906822  ]
9996.0: [ 0.41198261  0.50958195  0.62843379  0.82677092]
9999.0: [ 0.437169    0.17833114  0.91096133  0.70713434]
[ 9999.  9993.  9989.  9999.  9999.  9935.  9999.  9963.  9992.  9996.]

real 0m 24.352s
user 3m  4.919s
sys  0m  1.464s

您可以使用嵌套循環來檢查條件並將結果作為生成器生成:

points = [[ 1.5  1.5]
 [ 1.1  1.1]
 [ 2.2  2.2]
 [ 1.3  1.3]
 [ 3.4  1.4]
 [ 2.   1.5]]

bounds = [[ 0.  ,0. , 2.,  2.],
 [ 2.  ,2.  ,3.,  3.]]

import numpy as np

def pos(p,b):
  for x,y in p:
    flag=False
    for index,dis in enumerate(b):
      minx,miny,maxx,maxy=dis
      if x > minx and x < maxx and y > miny and y < maxy :
        flag=True
        yield index
    if not flag:
        yield 'NaN'


print list(pos(points,bounds))

結果:

[0, 0, 1, 0, 'NaN', 'NaN']

我會這樣做:

import numpy as np

points = np.random.rand(10,2)

xmin = [0.25,0.5]
ymin = [0.25,0.5]

results = np.zeros(len(points))

for i in range(len(xmin)):
     bool_index_array = np.greater(points, [xmin[i],ymin[i]])
     print "boolean index of (x,y) greater (xmin, ymin): ", bool_index_array
     indicies_of_true_true = np.where(bool_index_array[:,0]*bool_index_array[:,1]==1)[0]
     print "indices of [True,True]: ", indicies_of_true_true
     results[indicies_of_true_true] += 1

print "results: ", results

[out]: [ 1.  1.  1.  2.  0.  0.  1.  1.  1.  1.]

這使用較低的邊界將您的點分類為組:

  • 1(如果xmin [0] <x <= xmin [1]&ymin [0] <y <= ymin [1])
  • 2(如果x> xmin [1]&y> ymin [1])
  • 如果上述條件均未滿足,則為0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM