我有一个二维0/1数组X
。每列代表一个特定的字母。对于每一行,我想加入X
中值为1的那些字母。
例如:
import numpy as np
abc = np.array(['A','B','C','D','E','F'],dtype=str)
X = np.random.randint(0,2,(5,abc.shape[0]))
res = [np.string_.join('',abc[row==1]) for row in X]
这很好,仅此特定任务是我的代码的瓶颈。因此,由于对字符串和字符等的了解非常有限,我尝试将其移至cython失败。下面的代码仅供参考,但这很糟糕。一次,它并没有完全返回我想要的内容(例如,字符必须转换为Python字符串),更令人担忧的是,我认为代码不稳定。
import numpy as np
cimport numpy as np
cimport cython
from libc.stdlib cimport malloc, free
def join_c(int[:,:] idx, bytes abc):
cdef:
size_t i, j, count
int n = idx.shape[0]
int m = idx.shape[1]
char *arr = <char *>malloc((n*(m+1))*sizeof(char))
count = 0
try:
for i in range(n):
for j in range(m):
if idx[i,j] == 1:
arr[count] = abc[j]
count +=1
arr[count] = ','
count+=1
return [x for x in arr]
finally:
free(arr)
我想看看如何在cython中做到这一点,但我对任何其他快速解决方案感到满意。
答案 0 :(得分:1)
这是一个基于字符串数组的解决方案-
def join_singlechars(abc, X):
# Get mask
mask = X==1
# Get start, stop indices for splitting the concatenated string later on
idx = np.r_[0,mask.sum(1).cumsum()]
# Get concatenated string
n = idx[-1] #sum of 1s in mask
s = np.broadcast_to(abc, X.shape)[mask].tostring()
# Or np.broadcast_to(abc, X.shape)[mask].view('S'+str(n))[0]
return [s[i:j] for i,j in zip(idx[:-1],idx[1:])] # finally split
样品运行-
In [229]: abc
Out[229]: array(['A', 'B', 'C', 'D', 'E', 'F'], dtype='|S1')
In [230]: X
Out[230]:
array([[1, 0, 1, 0, 0, 1],
[1, 1, 0, 1, 1, 0],
[1, 0, 1, 1, 0, 0],
[1, 1, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 1]])
In [231]: join_singlechars(abc, X)
Out[231]: ['ACF', 'ABDE', 'ACD', 'ABDEF', 'ABCF']
大型5000 x 5000
阵列盒的计时-
In [321]: abc = np.array(['A','B','C','D','E','F'],dtype=str)
...: abc = np.resize(abc,5000)
...: np.random.seed(0)
...: X = np.random.randint(0,2,(5000,5000))
In [322]: %timeit [np.string_.join('',abc[row==1]) for row in X]
1 loop, best of 3: 648 ms per loop
In [323]: %timeit join_singlechars(abc, X)
1 loop, best of 3: 209 ms per loop