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

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

為什么這個 python 類不能與 numba jitclass 一起使用?

為什么這個 python 類不能與 numba jitclass 一起使用?

陪伴而非守候 2022-04-27 13:45:35
我在 numpy 的幫助下編寫了以下代碼,我想用 numba 提高性能。我不確定為什么它不起作用,因為我已經按照 numba 系統設置了所有變量。我正在嘗試加快此代碼的速度,因為我將來會使用大型數據集。import numpy as npimport mathfrom numba import jitclass from numba import float64,int64spec =[       ('spacing',float64),       ('n_iterations',int64),       ('np_emptyhouses',float64[:,:]),       ('np_agenthouses',float64[:,:]),       ('similarity_threshhold',float64),       ('n_changes',int64)       ]@jitclass(spec)class geo_schelling_update:    def __init__(self,n_iterations,spacing,np_agenthouses,np_emptyhouses,similarity_threshhold):        self.spacing=spacing        self.n_iterations=n_iterations        self.np_emptyhouses=np_emptyhouses        self.np_agenthouses=np_agenthouses        self.similarity_threshhold=similarity_threshhold    def distance_vectorize(self,pointA1, pointA2,agent):        x_square=np.square(pointA1-agent[0])        y_square=np.square(pointA2-agent[1])        dist=np.sqrt(np.array(x_square,dtype=np.float32)+np.array(y_square,dtype=np.float32))        return np.round(dist,4)    def is_unsatisfied_vectorize(self,x,y):        race = np.extract(np.logical_and(np.equal(self.np_agenthouses[:,0],x),np.equal(self.np_agenthouses[:,1],y)),self.np_agenthouses[:,2])[0]        euclid_distance1=round(math.hypot(self.spacing,self.spacing),4)        euclid_distance2=self.spacing        total_agents=np.extract(np.logical_or(np.equal(np.round(np.hypot((self.np_agenthouses[:,0]-(x)),(self.np_agenthouses[:,1]-(y))),4),euclid_distance1),np.equal(np.round(np.hypot((self.np_agenthouses[:,0]-(x)),(self.np_agenthouses[:,1]-(y))),4),euclid_distance2)),self.np_agenthouses[:,2])        if total_agents.size ==0:            return False        else:            return total_agents[total_agents==race].size/total_agents.size<self.similarity_threshhold    
查看完整描述

1 回答

?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

問題在于np.round. 從文檔中并不完全清楚,但您可以通過查看source看到,如果您在數組輸入上使用該函數,則需要提供所有 3 個參數。所以以下不起作用:

nb.jit(nopython=True)def func(x):    return np.round(x)

但以下工作按預期工作:

nb.jit(nopython=True)def func(x):
    out = np.empty_like(x)
    np.round(x, 0, out)    return out

有關完整說明,請參閱文檔。np.around我將在 numba 問題跟蹤器上提出一個問題,因為這在查看文檔時并不明顯。


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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