简体   繁体   English

在 Python 上使用 Numba 的 Numpy 均值和标准差

[英]Numpy's mean and standard deviation with Numba on Python

I am trying to use Numpy's mean and standard deviation functions insinde a function and they don't seem to be compatible with Numba, although Numba documentation states them as compatible .我正在尝试在 function 中使用 Numpy 的均值和标准差函数,它们似乎与 Numba 不兼容,尽管Numba 文档将它们声明为 compatible

My code is the following:我的代码如下:

import numpy as np
import numba


a = [1, 2, 3, 4, 5, 6]

# @numba.jit(nopython=True, parallel=True)
def nmeanstd(a, n):
    b = []; c = []
    for i in range(n):
        b.append(np.mean(a))
        c.append(np.std(a))
    
    return b, c

mean, std = nmeanstd(a, 10)

The output when looking at mean and std is the expected: output 在查看meanstd时是预期的:

mean
Out[31]: [3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5]

std
Out[32]: 
[1.707825127659933,
 1.707825127659933,
 1.707825127659933,
 1.707825127659933,
 1.707825127659933,
 1.707825127659933,
 1.707825127659933,
 1.707825127659933,
 1.707825127659933,
 1.707825127659933]

But I don't know why, when I uncomment the @numba.jit function the following message appears:但我不知道为什么,当我取消注释@numba.jit function 时出现以下消息:

TypingError: No implementation of function Function(<function mean at 0x11a0e6e50>) found for signature:
 
mean(reflected list(int64)<iv=None>)
 
There are 2 candidate implementations:
      - Of which 2 did not match due to:
      Overload of function 'mean': File: numba/core/typing/npydecl.py: Line 378.
        With argument(s): '(reflected list(int64)<iv=None>)':
       No match.

During: resolving callee type: Function(<function mean at 0x11a0e6e50>)

And the same for std if I comment the line in which I compute the mean.如果我评论计算平均值的行, std也是如此。 What is happening?怎么了? I though they would be running with numba correctly.我虽然他们会正确运行numba Do you know any way of computing the mean and the standard deviation using Numba?您知道使用 Numba 计算均值和标准差的任何方法吗?

The error message shows that Numba does not know how to compute the mean of a list .错误消息显示 Numba 不知道如何计算listmean Your code works fine (with @jit ) if the input list is first converted to a numpy array:如果首先将输入列表转换为 numpy 数组,则您的代码可以正常工作(使用@jit ):

mean, std = nmeanstd(np.array(a), 10)

The documentation shows Numpy arrays work directly with Numba and are very efficient.文档显示 Numpy arrays 直接与 Numba 一起工作并且非常高效。

The code works if you convert "a" into a numpy array mean, std = nmeanstd(np.array(a), 10)如果将“a”转换为 numpy 数组 mean, std = nmeanstd(np.array(a), 10),则代码有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM