亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 scipy.spatial.KDTree.query_ball_point

如何使用 scipy.spatial.KDTree.query_ball_point

慕蓋茨4494581 2022-12-20 11:26:45
我正在嘗試使用 Kdtree 數據結構從數組中移除最近的點,最好不要 for 循環。import sysimport timeimport scipy.spatialclass KDTree:    """    Nearest neighbor search class with KDTree    """    def __init__(self, data):        # store kd-tree        self.tree = scipy.spatial.cKDTree(data)    def search(self, inp, k=1):        """        Search NN        inp: input data, single frame or multi frame        """        if len(inp.shape) >= 2:  # multi input            index = []            dist = []            for i in inp.T:                idist, iindex = self.tree.query(i, k=k)                index.append(iindex)                dist.append(idist)            return index, dist        dist, index = self.tree.query(inp, k=k)        return index, dist    def search_in_distance(self, inp, r):        """        find points with in a distance r        """        index = self.tree.query_ball_point(inp, r)        return np.asarray(index)import numpy as npimport matplotlib.pyplot as pltimport matplotlib.animation as animationstart = time.time()fig, ar = plt.subplots()t = 0R = 50.0u = R *np.cos(t)v = R *np.sin(t)x = np.linspace(-100,100,51)y = np.linspace(-100,100,51)xx, yy = np.meshgrid(x,y)points =np.vstack((xx.ravel(),yy.ravel())).TTree = KDTree(points)ind = Tree.search_in_distance([u, v],10.0)ar.scatter(points[:,0],points[:,1],c='k',s=1)infected = points[ind]ar.scatter(infected[:,0],infected[:,1],c='r',s=5)def animate(i):    global R,t,start,points    ar.clear()    u = R *np.cos(t)    v = R *np.sin(t)    ind = Tree.search_in_distance([u, v],10.0)    ar.scatter(points[:,0],points[:,1],c='k',s=1)    infected = points[ind]    ar.scatter(infected[:,0],infected[:,1],c='r',s=5)    #points = np.delete(points,ind)    t+=0.01    end = time.time()    if end - start != 0:        print((end - start), end="\r")        start = endani = animation.FuncAnimation(fig, animate, interval=20)plt.show()  但無論我做什么,我都無法讓 np.delete 處理 ball_query 方法返回的索引。我錯過了什么?我想讓紅色點在點數組的每次迭代中消失。
查看完整描述

1 回答

?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

您的points數組是一個 Nx2 矩陣。您的ind索引是行索引列表。你需要的是指定你需要刪除的軸,最終是這樣的:

points = np.delete(points,ind,axis=0)

此外,一旦刪除索引,請注意下一次迭代/計算中丟失的索引。也許您想要一個副本來刪除點和繪圖,另一個副本用于您不從中刪除的計算。


查看完整回答
反對 回復 2022-12-20
  • 1 回答
  • 0 關注
  • 256 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號