【问题标题】:Finding Max Shape of Multiple .npz files查找多个 .npz 文件的最大形状
【发布时间】:2016-10-13 14:41:13
【问题描述】:

我有许多形状可能不同的 .npz 文件,我想找出哪个文件具有较大的形状。 npzs 中有 2 个数组,我正在寻找第二个数组中最大的一个。以下 sn-p 有效,但返回形状所需的时间比我预期的要长。这是实现这一目标的最有效方法吗?我担心缩放,因为目前需要几秒钟才能找到最大形状[1],而且我只循环 4 个数组

frameMax =0 
for f in npzs:
    d = np.load(f,mmap_mode='r')
    if d['arr_0'].shape[1]>frameMax:
        frameMax = d['arr_0'].shape[1]
    d=None

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    请记住,I/O 操作可能相对较慢。也就是说,您可以使用内置的 max 将查找最大值的逻辑减少到以下内容,该函数将在 O(n) 时间运行,并且无需执行您所做的分配:

    frameMax = max([np.load(f,mmap_mode='r')['arr_0'].shape[1] for f in npzs])
    

    【讨论】:

    • 您可能是正确的,这很可能是 I/O 瓶颈。这并没有真正加快任何速度,但它肯定看起来更时尚。谢谢,@moses-koledoye
    猜你喜欢
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    • 2017-03-12
    • 2019-02-14
    • 2015-05-13
    • 2010-09-05
    相关资源
    最近更新 更多