gpt4 book ai didi

python - ndarray的快速突变(替换numpy ndarray的部分)

转载 作者:太空宇宙 更新时间:2023-11-03 14:06:14 25 4
gpt4 key购买 nike

我正在寻找最快的方法来替换预分配的 numpy ndarray 的部分。下面您可以看到sim_matrix(2D ndarray)。 “For 循环”逐行迭代它并覆盖由levels (numpy.array) 给出的值。

有一些快速的方法,已找到:

  1. numpy.copyto()
  2. numpy.fromiter()
  3. = 分配给切片

注意:为简单起见,我在每次迭代中分配相同的值,但生产设置中的情况并非如此。在实际设置中,levels 每行包含不同的值。因此,不鼓励使用 numpy.repeatnumpy.hstack 等快捷方式作为答案。

是否有更快的方法来更改 ndarray 的某些部分?

numpy_overwrite.py:

import numpy
panel_size = 365 * 7
rows = 1 * (1+1+2*4) * 10000
# rows = 100
levels = [1778.24] * panel_size
levels_array = numpy.fromiter(levels,dtype='single', count = len(levels))
sim_matrix = numpy.empty([rows, panel_size],dtype='single')

def _copyto(sim_matrix_, level_):
for i in range(sim_matrix_.shape[0]):
numpy.copyto(sim_matrix[i], level_)


def _fromiter(sim_matrix_, level_):
len_level = len(level_)
for i in range(sim_matrix_.shape[0]):
sim_matrix[i] = numpy.fromiter(level_,
dtype='single',
count = len_level)

def _just_assign(sim_matrix_, level_):
for i in range(sim_matrix_.shape[0]):
sim_matrix[i] = level_
<小时/>

numpy_overwrite_test.py

import timeit
import numpy_overwrite as npo
print('_fromiter')
print(timeit.timeit('npo._fromiter(npo.sim_matrix, npo.levels)', setup="import numpy_overwrite as npo;", number=3))
print(timeit.timeit('npo._fromiter(npo.sim_matrix, npo.levels_array)', setup="import numpy_overwrite as npo;", number=3))
print('_copyto')
print(timeit.timeit('npo._copyto(npo.sim_matrix, npo.levels)', setup="import numpy_overwrite as npo;", number=3))
print(timeit.timeit('npo._copyto(npo.sim_matrix, npo.levels_array)', setup="import numpy_overwrite as npo;", number=3))
print('_just_assign')
print(timeit.timeit('npo._just_assign(npo.sim_matrix, npo.levels)', setup="import numpy_overwrite as npo;", number=3))
print(timeit.timeit('npo._just_assign(npo.sim_matrix, npo.levels_array)', setup="import numpy_overwrite as npo;", number=3))
<小时/>

运行测试

_fromiter
=========
from python list: 21.129429172957316
from numpy.array: 61.76275303697912

_copyto
=========
from python list: 52.4522930260282
from numpy.array: 0.7605530479922891

_just_assign
=========
from python list: 52.251478374004364
from numpy.array: 0.6351132979616523

最佳答案

您是否可能只是在寻找这个?

sim_matrix[:] = levels

编辑:要修改单行,应使用sim_matrix[row_id] =levels

顺便说一句,请注意 using mutables as default function arguments .

关于python - ndarray的快速突变(替换numpy ndarray的部分),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48868819/

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