11

私は C++ のバックグラウンドから Python を学び始めています。私が探しているのは、多次元点の 2D (numpy) 配列 (これも numpy 配列) で最も近い (最も近い) 多次元クエリ ポイントをすばやく簡単に見つける方法です。scipy に kd ツリーがあることは知っていますが、これは私が望んでいるものではないと思います。まず、2D 配列の多次元点の値を変更します。次に、2D 配列内の各ポイントの位置 (座標) が重要になります。これは、隣接するポイントも変更するためです。

2D配列を通過し、クエリポイントと配列内のポイントの間の距離を測定しながら、最小のものを追跡する関数を作成できます(scipy空間距離関数を使用して距離を測定します)。これを行う組み込み関数はありますか?Pythonで配列を反復処理することをできるだけ避けようとしています。また、多数のクエリ ポイントがあるため、少なくとも 2 つの "for ループ" が存在します。1 つはクエリ ポイントを反復し、各クエリについては、2D 配列を反復して最小距離を見つけるためのループです。

アドバイスをありがとう。

4

4 に答える 4

6

簡潔にすることが目標である場合は、次のワンライナーを実行できます。

In [14]: X = scipy.randn(10,2)

In [15]: X
Out[15]: 
array([[ 0.85831163,  1.45039761],
       [ 0.91590236, -0.64937523],
       [-1.19610431, -1.07731673],
       [-0.48454195,  1.64276509],
       [ 0.90944798, -0.42998205],
       [-1.17765553,  0.20858178],
       [-0.29433563, -0.8737285 ],
       [ 0.5115424 , -0.50863231],
       [-0.73882547, -0.52016481],
       [-0.14366935, -0.96248649]])

In [16]: q = scipy.array([0.91, -0.43])

In [17]: scipy.argmin([scipy.inner(q-x,q-x) for x in X])
Out[17]: 4

複数のクエリ ポイントがある場合:

In [18]: Q = scipy.array([[0.91, -0.43], [-0.14, -0.96]])

In [19]: [scipy.argmin([scipy.inner(q-x,q-x) for x in X]) for q in Q]
Out[19]: [4, 9]
于 2011-12-16T00:39:50.010 に答える
4

ブロードキャストは、この種のことに非常に役立ちます。これが必要かどうかはわかりませんが、ここではブロードキャストを使用して、p (3 空間内の 1 点) と X (3 空間内の 10 点のセット) の間の変位を見つけます。

import numpy as np

def closest(X, p):
    disp = X - p
    return np.argmin((disp*disp).sum(1))

X = np.random.random((10, 3))
p = np.random.random(3)

print X
#array([[ 0.68395953,  0.97882991,  0.68826511],
#       [ 0.57938059,  0.24713904,  0.32822283],
#       [ 0.06070267,  0.06561339,  0.62241713],
#       [ 0.93734468,  0.73026772,  0.33755815],
#       [ 0.29370809,  0.76298588,  0.68728743],
#       [ 0.66248449,  0.6023311 ,  0.76704199],
#       [ 0.53490144,  0.96555923,  0.43994738],
#       [ 0.23780428,  0.75525843,  0.46067472],
#       [ 0.84240565,  0.82573202,  0.56029917],
#       [ 0.66751884,  0.31561133,  0.19244683]])
print p
#array([ 0.587416 ,  0.4181857,  0.2539029])
print closest(X, p)
#9
于 2011-12-16T02:03:43.070 に答える
1

すべての距離を計算するscipy.spatial.distance.cdist( X, Y ) か、動的データに RTree を使用できます: http://gispython.org/rtree/docs/class.html

于 2011-12-15T23:46:27.993 に答える
1

検索を高速化し、動的なアイテムの挿入をサポートするには、参照点 (0,0) までの距離によって大なり小なり演算子が定義される 2D アイテムのバイナリ ツリーを使用できます。

def dist(x1,x2):
    return np.sqrt( (float(x1[0])-float(x2[0]))**2 +(float(x1[1])-float(x2[1]))**2 )

class Node(object):

    def __init__(self, item=None,):
        self.item = item
        self.left = None
        self.right = None

    def __repr__(self):
        return '{}'.format(self.item)

    def _add(self, value, center):
        new_node = Node(value)
        if not self.item:
            self.item = new_node        
        else:
        vdist = dist(value,center)
        idist = dist(self.item,center)
            if vdist > idist:
                self.right = self.right and self.right._add(value, center) or new_node
            elif vdist < idist:
                self.left = self.left and self.left._add(value, center) or new_node
            else:
                print("BSTs do not support repeated items.")

        return self # this is necessary!!!

    def _isLeaf(self):
        return not self.right and not self.left

class BSTC(object):

    def __init__(self, center=[0.0,0.0]):
        self.root = None
    self.count = 0
    self.center = center

    def add(self, value):
        if not self.root:
            self.root = Node(value)
        else:
            self.root._add(value,self.center)
    self.count += 1

    def __len__(self): return self.count

    def closest(self, target):
            gap = float("inf")
            closest = float("inf")
            curr = self.root
            while curr:
                if dist(curr.item,target) < gap:
                    gap = dist(curr.item, target)
                    closest = curr
                if target == curr.item:
                    break
                elif dist(target,self.center) < dist(curr.item,self.center):
                    curr = curr.left
                else:
                    curr = curr.right
            return closest.item, gap


import util

bst = util.BSTC()
print len(bst)

arr = [(23.2323,34.34535),(23.23,36.34535),(53.23,34.34535),(66.6666,11.11111)]
for i in range(len(arr)): bst.add(arr[i])

f = (11.111,22.2222)
print bst.closest(f)
print map(lambda x: util.dist(f,x), arr)
于 2016-12-23T10:23:38.627 に答える