numba np.diff 轴=0

发布于 2025-01-09 06:39:07 字数 525 浏览 1 评论 0原文

使用 numba 时,axis=0np.sum() 可接受的参数,但不适用于 np.diff()。为什么会发生这种情况?我正在使用 2D,因此需要轴规格。

@jit(nopython=True)
def jitsum(y):
    np.sum(y, axis=0)

@jit(nopython=True)
def jitdiff(y): #this one will cause error
    np.diff(y, axis=0)

错误:np_diff_impl() 得到了意外的关键字参数“axis”

2D 中的解决方法是:

@jit(nopython=True)
def jitdiff(y):
    np.diff(y.T).T

While using numba, axis=0 is acceptable parameters for np.sum(), but not with np.diff(). Why is this happening? I'm working with 2D, thus axis specification is needed.

@jit(nopython=True)
def jitsum(y):
    np.sum(y, axis=0)

@jit(nopython=True)
def jitdiff(y): #this one will cause error
    np.diff(y, axis=0)

Error: np_diff_impl() got an unexpected keyword argument 'axis'

A workaround in 2D will be:

@jit(nopython=True)
def jitdiff(y):
    np.diff(y.T).T

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁我热情 2025-01-16 06:39:07

np.diff 在具有 n=1 的二维数组上,axis=1

a[:, 1:] - a[:, :-1]

适用于 axis=0

a[1:, :] - a[:-1, :]

我怀疑上面的行可以用 numba 编译得很好。

np.diff on a 2D array with n=1, axis=1 is just

a[:, 1:] - a[:, :-1]

For axis=0:

a[1:, :] - a[:-1, :]

I suspect that the lines above will compile just fine with numba.

她如夕阳 2025-01-16 06:39:07
def sum(y):
    a=np.sum(y, axis=0)
    b=np.sum(y,axis=1)
    print("Sum along the rows (axis=0):",a)
    print("Sum along the columns (axis=1):",b)

def diff_order1(y):
    a=np.diff(y,axis=0,n=1)
    b=np.diff(y,axis=1,n=1) ## n=1 indicates 1st order difference 
    print("1st order difference along the rows (axis=0):",a)
    print("1st order difference along the columns (axis=1):",b)

def diff_order2(y):
    a=np.diff(y,axis=0,n=2)
    b=np.diff(y,axis=1,n=2) ## n=2 indicates 2nd order difference 
    print("2nd order difference along the rows (axis=0):",a)
    print("2nd order difference along the columns (axis=1):",b)

该函数只是解决两次调用 .diff 函数以获得 2 阶差的问题的另一个版本

def diff_order2_v2(y):
  a=np.diff(np.diff(y,axis=1),axis=1)
  b=np.diff(np.diff(y,axis=0),axis=0)
  print("2nd order difference along the rows (axis=0):",a)
  print("2nd order difference along the columns (axis=1):",b)

尝试运行此代码,我尝试为一阶和二阶差创建求和函数和差函数的函数。

def sum(y):
    a=np.sum(y, axis=0)
    b=np.sum(y,axis=1)
    print("Sum along the rows (axis=0):",a)
    print("Sum along the columns (axis=1):",b)

def diff_order1(y):
    a=np.diff(y,axis=0,n=1)
    b=np.diff(y,axis=1,n=1) ## n=1 indicates 1st order difference 
    print("1st order difference along the rows (axis=0):",a)
    print("1st order difference along the columns (axis=1):",b)

def diff_order2(y):
    a=np.diff(y,axis=0,n=2)
    b=np.diff(y,axis=1,n=2) ## n=2 indicates 2nd order difference 
    print("2nd order difference along the rows (axis=0):",a)
    print("2nd order difference along the columns (axis=1):",b)

This function is just another version of solving the problem calling the .diff function twice for order 2 difference

def diff_order2_v2(y):
  a=np.diff(np.diff(y,axis=1),axis=1)
  b=np.diff(np.diff(y,axis=0),axis=0)
  print("2nd order difference along the rows (axis=0):",a)
  print("2nd order difference along the columns (axis=1):",b)

Try running this code, I tried to create functions for sum function and difference function for 1st order and 2nd order difference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文