gpt4 book ai didi

python - 从Python字典中高效地获取dstack数组

转载 作者:行者123 更新时间:2023-11-30 23:42:31 25 4
gpt4 key购买 nike

我有一个字典,它以日期为键,并填充了具有 numpy.array 属性的类。我想使用 np.dstack 从字典中的所有数组中创建一个大数组。我当前的代码是这样的:

import numpy as np
#PARTS is my dictionary
#the .partposit is the attribute that is an array of shape (50000, 12)
ks = sorted(PARTS.keys())
p1 = PARTS[ks[0]].partposit
for k in ks[1:]:
p1 = np.dstack((p1, PARTS[k].partposit))

我的结果正如我所期望的:

In [67]: p1.shape
Out[67]: (50000, 12, 163)

但是,速度相当慢。有没有更有效的方法来做到这一点?

最佳答案

你可以试试这个:

>>> import numpy as np
>>> class A:
... def __init__(self, values):
... self.partposit = values
...
>>> PARTS = dict((index, A(np.zeros((50000, 12)))) for index in xrange(163))
>>> p1 = np.dstack((PARTS[k].partposit for k in sorted(PARTS.keys())))
>>> p1.shape
(50000, 12, 163)
>>>

花了几秒钟才把它堆到我的机器上。

>>> import timeit
>>> timeit.Timer('p1 = np.dstack((PARTS[k].partposit for k in sorted(PARTS.keys())))', "from __main__ import np, PARTS").timeit(number = 1)
2.1245520114898682

numpy.dstack 接受一系列数组并将它们堆叠在一起,因此如果我们只给它列表而不是自己连续堆叠它们,速度会快得多。

numpy.dstack(tup)

Stack arrays in sequence depth wise (along third axis). Takes a sequence of arrays and stack them along the third axis to make a single array.

http://docs.scipy.org/doc/numpy/reference/generated/numpy.dstack.html

我也很好奇你的方法有多长:

>>> import timeit
>>> setup = """
... import numpy as np
... #PARTS is my dictionary
... #the .partposit is the attribute that is an array of shape (50000, 12)
...
... class A:
... def __init__(self, values):
... self.partposit = values
...
... PARTS = dict((index, A(np.zeros((50000, 12)))) for index in xrange(163))
... ks = sorted(PARTS.keys())
... """
>>> stack = """
... p1 = PARTS[ks[0]].partposit
... for k in ks[1:]:
... p1 = np.dstack((p1, PARTS[k].partposit))
... """
>>> timeit.Timer(stack, setup).timeit(number = 1)
67.69684886932373

哎哟!

>>> numpy.__version__
'1.6.1'

$ python --version
Python 2.6.1

我希望这会有所帮助。

关于python - 从Python字典中高效地获取dstack数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11387158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com