小编Dan*_*ani的帖子

numpy np.all 轴参数的解决方法;与 numba 的兼容性

我有一个函数,给定一个 xy 坐标的 numpy 数组,它过滤那些位于 L 边的盒子内的

import numpy as np
from numba import njit

np.random.seed(65238758)

L = 10
N = 1000
xy = np.random.uniform(0, 50, (N, 2))
box = np.array([
    [0,0],  # lower-left
    [L,L]  # upper-right
]) 

def sinjit(xy, box):
    mask = np.all(np.logical_and(xy >= box[0], xy <= box[1]), axis=1)
    return xy[mask]
Run Code Online (Sandbox Code Playgroud)

如果我运行这个函数,它会返回正确的结果:

sinjit(xy, box)

Output: array([[5.53200522, 7.86890708],
       [4.60188554, 9.15249881],
       [9.072563  , 5.6874726 ],
       [4.48976127, 8.73258166],
       ...
       [6.29683131, 5.34225758],
       [2.68057087, 5.09835442],
       [5.98608603, 4.87845464],
       [2.42049857, 6.34739079],
       [4.28586677, 5.79125413]])
Run Code Online (Sandbox Code Playgroud)

但是,由于我想通过使用 numba 在循环中加速此任务,因此 …

python numpy numba

5
推荐指数
2
解决办法
581
查看次数

如何迭代 cython (或 numba)中的列表列表?

我想要一个函数,它接收列表列表作为参数,每个子列表具有不同的大小,并且可以迭代每个子列表(包含整数),将它们作为广播传递到 numpy 数组并执行不同的操作操作(如平均值)。

让我提供一个不使用 cython 的预期行为的简单示例:

import numpy as np

mask = [[0, 1, 2, 4, 6, 7, 8, 9],
        [0, 1, 2, 4, 6, 7, 8, 9],
        [0, 1, 2, 4, 6, 9],
        [3, 5, 8],
        [0, 1, 2, 4, 6, 7, 8, 9],
        [3, 5, 7],
        [0, 1, 2, 4, 6, 9],
        [0, 1, 4, 5, 7, 8, 9],
        [0, 1, 3, 4, 7, 8, 9],
        [0, 1, 2, 4, 6, 7, 8, 9]] # This is the …
Run Code Online (Sandbox Code Playgroud)

python loops nested-lists cython numba

5
推荐指数
1
解决办法
1900
查看次数

标签 统计

numba ×2

python ×2

cython ×1

loops ×1

nested-lists ×1

numpy ×1