我有一個獲取 x 和 y 位置并執行最近鄰搜索的函數。我想將此函數應用于兩個數組,以便我可以獲得數組中每個點的函數結果。我在這里給出了一些示例數據和我想應用的實際功能。我還調用了單點函數。我如何將此函數應用于出現在我的數組xs和中的匹配索引處的每個 x,y 對ys?import scipy.spatialimport scipyimport numpy as npimport pandas as pddef est_bathymetry(x,y,bathymetry_data,gsl_level=1278.3,n=4): grid_xy=np.array([x,y]).T def do_kdtree(grid_xy,points, n): #This function gets the kd tree for the sample, then returns the closest #points to each grid node as indices points=points[:2,:] points=list(points.transpose()) mytree = scipy.spatial.cKDTree(points) dist, indexes = mytree.query(grid_xy, k=n, n_jobs=-1) return dist, indexes dist, indexes = do_kdtree(grid_xy,bathymetry_data.iloc[:,0:2].values.T,n) avg_bath=np.nanmean(np.take(bathymetry_data['Bathymetry'], indexes)) return avg_bath #create sample datax=np.linspace(0,10,11,endpoint=True)xs=np.tile(x,(11,1))ys=xs.Tnp.random.seed(123)data=pd.DataFrame([np.random.uniform(0,10,100),np.random.uniform(0,10,100),np.random.normal(2000,100,100)]).Tdata.columns=['Lat','Long','Bathymetry']#example of function working on single inputavg_bathymetry=est_bathymetry(x=np.min(xs),y=np.min(ys),bathymetry_data=data)
1 回答

UYOU
TA貢獻1878條經驗 獲得超4個贊
我發現使用 np.vectorize 我可以矢量化并應用該函數,這個函數實際上是適用的,但我需要刪除其他輸入。唯一的問題是運行完整數據集需要 225 天。將不得不回到繪圖板。
添加回答
舉報
0/150
提交
取消