本文介绍了Python:numba 可以在 nopython 模式下处理字符串数组吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 pandas 0.16.2、numpy 1.9.2 和 numba 0.20.

I am using pandas 0.16.2, numpy 1.9.2 and numba 0.20.

有没有办法让 numba 在 nopython 模式下支持字符串数组?或者,我可以以某种方式将字符串转换为 numba 可以识别的数字吗?

Is there any way to get numba to support arrays of strings in nopython mode?Alternatively, could I somehow convert strings to numbers which numba would recognise?

我必须在字符串数组(熊猫数据帧中的一列)上运行某些循环;如果我可以使用 numba,代码会快得多.

I have to run certain loops on an array of strings (a column from a pandas dataframe); if I could use numba the code would be substantially faster.

我想出了这个最小的例子来说明我的意思:

I have come up with this minimal example to show what I mean:

import numpy as np
import numba

x=np.array(['some','text','this','is'])

@numba.jit(nopython=True)
def numba_str(txt):
    x=0
    for i in xrange(txt.size):
        if txt[i]=='text':
            x += 1
    return x

print numba_str(x)

我得到的错误是:

Failed at nopython (nopython frontend)
Undeclared ==([char x 4], str)

谢谢!

推荐答案

Numba 尚不支持字符串(从 20.0 版开始).实际上,支持 "字符序列,但没有操作可以在他们身上使用".

Strings are not yet supported by Numba (as of version 20.0). Actually, "character sequences are supported, but no operations are available on them".

事实上,一种可能的解决方法是将字符解释为数字.对于 ASCII 字符,这很简单,请参阅 Python ordchr 函数.但是,对于您的最小示例,您已经以可读性低得多的函数结束:

Indeed, a possible workaround is to interpret characters as numbers. For ASCII characters this is straightforward, see the Python ord and chr functions. However, already for your minimal example, you end with functions that are a lot less readable:

import numpy as np
import numba

x=np.array(['some','text','this','is'])

@numba.jit(nopython=True)
def numba_str(txt):
    x=0
    for i in xrange(txt.shape[0]):
        if (txt[i,0]==116 and  # 't'
            txt[i,1]==101 and  # 'e'
            txt[i,2]==120 and  # 'x'
            txt[i,3]==116):    # 't'
            x += 1
    return x

print numba_str(x.view(np.uint8).reshape(-1, x.itemsize))

这篇关于Python:numba 可以在 nopython 模式下处理字符串数组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 07:30