我需要將元組的 np 陣列(短)轉換為整數的 np 陣列。
最明顯的方法不起作用:
# array_of_tuples is given, this is just an example:
array_of_tuples = np.zeros(2, dtype=object)
array_of_tuples[0] = 1,2
array_of_tuples[1] = 2,3
np.array(array_of_tuples, dtype=int)
ValueError: setting an array element with a sequence.
uj5u.com熱心網友回復:
看起來將元組放入固定大小的預分配緩沖區中,dtype 是要走的路。它似乎避免了與計算大小、粗糙度和 dtype 相關的大量開銷。
以下是一些較慢的替代方案和基準:
您可以作弊并創建具有必要數量的欄位的 dtype,因為 numpy 支持將元組轉換為自定義 dtype:
dt = np.dtype([('', int) for _ in range(len(array_of_tuples[0]))]) res = np.empty((len(array_of_tuples), len(array_of_tuples[0])), int) res.view(dt).ravel()[:] = array_of_tuples
您可以堆疊陣列:
np.stack(array_of_tuples, axis=0)
不幸的是,這甚至比其他提出的方法還要慢。
預分配沒有多大幫助:
res = np.empty((len(array_of_tuples), len(array_of_tuples[0])), int) np.stack(array_of_tuples, out=res, axis=0)
嘗試使用 作弊
np.concatenate
,它允許您指定輸出 dtype 也無濟于事:np.concatenate(array_of_tuples, dtype=int).reshape(len(array_of_tuples), len(array_of_tuples[0]))
也沒有預先分配陣列:
res = np.empty((len(array_of_tuples), len(array_of_tuples[0])), int) np.concatenate(array_of_tuples, out=res.ravel())
您也可以嘗試在 python 空間中進行連接,這也很慢:
np.array(sum(array_of_tuples, start=()), dtype=int).reshape(len(array_of_tuples), len(array_of_tuples[0]))
或者
np.reshape(np.sum(array_of_tuples), (len(array_of_tuples), len(array_of_tuples[0])))
array_of_tuples = np.empty(100, dtype=object)
for i in range(len(array_of_tuples)):
array_of_tuples[i] = tuple(range(i, i 100))
%%timeit
res = np.empty((len(array_of_tuples), len(array_of_tuples[0])), int)
for i, res[i] in enumerate(array_of_tuples):
pass
305 μs ± 8.55 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
dt = np.dtype([('', 'int',) for _ in range(100)])
%%timeit
res = np.empty((100, 100), int)
res.view(dt).ravel()[:] = array_of_tuples
334 μs ± 5.59 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit np.array(array_of_tuples.tolist())
478 μs ± 12.9 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%%timeit
res = np.empty((100, 100), int)
np.concatenate(array_of_tuples, out=res.ravel())
500 μs ± 2.3 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit np.concatenate(array_of_tuples, dtype=int).reshape(100, 100)
504 μs ± 7.72 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%%timeit
res = np.empty((100, 100), int)
np.stack(array_of_tuples, out=res, axis=0)
557 μs ± 25.3 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit np.stack(array_of_tuples, axis=0)
577 μs ± 6.7 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit np.array(sum(array_of_tuples, start=()), dtype=int).reshape(100, 100)
1.06 ms ± 11.8 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit np.reshape(np.sum(array_of_tuples), (100, 100))
1.26 ms ± 24.7 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/507382.html