在Jupyter笔记本中,我对资源进行了OO建模,但在控制循环中,需要聚合多个对象上的数据,而这些对象与ufunc和类似操作相比效率低下。
要打包功能,我选择了OO,但为了获得高效简洁的代码,我可能不得不将数据提取到存储类中(也许),并将所有ri [0]行压入2d数组中,在这种情况下(2 ,K)。
该类不需要日志,仅需要最后一个条目。
K = 100
class Resource:
def __init__(self):
self.log = np.random( (5,K) )
# log gets filled during simulation
r0 = 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
这将大大提高性能,但是如果单独存储数据,则很难将数据链接到该类。您将如何处理?熊猫DataFrames,np视图还是浅拷贝?
[[...] #r0
[...] ]#r1 same data into one array, efficient but map back to class difficult
答案 0 :(得分:0)
这是我的看法:
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)