python - 是否可以在 guvectorize 函数中返回输出数组形状未知的数组

标签 python arrays numpy numba

我想知道如果我不知道 numba guvectorize 函数先验的数组大小,如何返回数组。 我基本上有一个接收向量的函数,并返回一个数组,其大小将在函数中发生变化。我的问题是我无法先验设置返回数组的形状。

例如:

@guvectorize ([(float64[:],int64[:,:])], '(n),()->(n,2)')
def mysegmentation(Thresholding,StimSegments_time):
    count = 0
    for i in range(Thresholding.shape[0]-1):
        if Thresholding[i]==0. and Thresholding[i+1]==1.:
            StimSegments_time[count,0] = i 
        elif Thresholding[i]==1. and Thresholding[i+1]==0.:
            StimSegments_time[count,1] = i
            count += 1

阈值可以有(1000)个元素,StimSegments_time 可以有(5, 2)或(7, 2)或其他元素(x,2)

最佳答案

我也遇到同样的问题。但我所能提供的帮助是,您不能在装饰器中添加固定数字(即 ->(n,2) 不正确)。

对于这个问题,人们建议在输入中添加一个虚拟数组参数,并在输出中使用其长度,如下所述:

guvectorize not accepting new size variable

关于python - 是否可以在 guvectorize 函数中返回输出数组形状未知的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53847651/

相关文章:

c - 没有 malloc 但有另一种方法的链表

python - 使用类型为 "object"的 numpy 数组创建混合类型的 Pandas Dataframe

python - 在 VS 代码上安装 numpy 时出现“准备元数据 (pyproject.toml) ... 错误”

python - Pandas :根据一列中字符串的特定组合选择行对

python - Appengine错误未出现在日志中

python - 如何在Win11下显示WSL2中的matplotlib窗口?

python - 如何在机器人框架中将列表连接到字符串

python - 类型错误 : 'int' object is not subscriptable

c - C语言按字母顺序对字符串排序

java - 计算数组中的元素数