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

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

如何在多個相同大小的數組上應用復雜函數以在 python 中使用 numpy 創建新的相同大小的數組

如何在多個相同大小的數組上應用復雜函數以在 python 中使用 numpy 創建新的相同大小的數組

叮當貓咪 2023-06-13 16:26:02
我有一個獲取 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 天。將不得不回到繪圖板。



查看完整回答
反對 回復 2023-06-13
  • 1 回答
  • 0 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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