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

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

如何在 OO 中輕松高效地存儲 numpy ufuncs 的模擬數據

如何在 OO 中輕松高效地存儲 numpy ufuncs 的模擬數據

qq_遁去的一_1 2021-09-28 17:08:38
在 jupyter notebook 中,我對資源進行 OO 建模,但在控制循環中需要聚合多個對象的數據,與 ufunc 和類似操作相比效率低下。為了打包功能,我選擇了面向對象,但為了高效簡潔的代碼,我可能必須將數據提取到存儲類中(可能)并將所有 ri[0] 行推送到二維數組中,在本例中為 (2,K)。該類不需要日志,只需要最后一個條目。K = 100class Resource:    def __init__(self):        self.log = np.random( (5,K) )        # log gets filled during simulationr0 = Resource()r1 = Resource()# while control loop:    #aggregate control data    for k in K:        total_row_0 = r0.log[0][k] + r1.log[0][k]    #do sth with the totals and loop again這將大大提高性能,但如果單獨存儲,我很難將數據鏈接到類。你會如何處理這個問題?pandas DataFrames、np View 還是淺拷貝?[[...] #r0 [...] ]#r1 same data into one array, efficient but map back to class difficult 
查看完整描述

1 回答

?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

這是我的看法:


import numpy as np


K = 3

class Res:

    logs = 2

    def __init__(self):

        self.log = None


    def set_log(self, view):

        self.log = view


batteries = [Res(),  Res()]

d = {'Res': np.random.random( (Res.logs * len(batteries), K) )}        


for i in range(len(batteries)):

    view = d['Res'].view()[i::len(batteries)][:]

    batteries[i].set_log(view)


print(d)

batteries[1].log[1][2] = 1#test modifies view of last entry of second Res of second log

print(d)


查看完整回答
反對 回復 2021-09-28
  • 1 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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