我有一個(gè)獲取 x 和 y 位置并執(zhí)行最近鄰搜索的函數(shù)。我想將此函數(shù)應(yīng)用于兩個(gè)數(shù)組,以便我可以獲得數(shù)組中每個(gè)點(diǎn)的函數(shù)結(jié)果。我在這里給出了一些示例數(shù)據(jù)和我想應(yīng)用的實(shí)際功能。我還調(diào)用了單點(diǎn)函數(shù)。我如何將此函數(shù)應(yīng)用于出現(xiàn)在我的數(shù)組xs和中的匹配索引處的每個(gè) x,y 對(duì)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)
如何在多個(gè)相同大小的數(shù)組上應(yīng)用復(fù)雜函數(shù)以在 python 中使用 numpy 創(chuàng)建新的相同大小的數(shù)組
叮當(dāng)貓咪
2023-06-13 16:26:02