我尝试使用numpy来快速文本分析。确切地说是搭配分析。让我们假设我有以下字符串,我将其转换为numpy数组:
text = np.array(['a', 'b', 'c', 'd', 'e', 'b', 'f', 'g'])
字符串
假设我想从这个数组中获取字母'b'的左右上下文。假设左边有1个元素,右边有2个元素。所以我想有这样的东西:
['a', 'c', 'd'] + ['e', 'f', 'g']
型
有没有可能用Numpy广播所有的操作?我只是在文本上循环,但这非常耗时。
我试过np.select,np.where和np.mask
谢谢你的帮助:)
3条答案
按热度按时间ttcibm8c1#
一种可能的方法是找到
b
值索引(使用np.where(arr == 'b')
)来进一步索引相邻值:个字符
ntjbwcob2#
我相信前面的答案是要走的路,如果你真的想使用numpy.但如果它是适用的,我会建议你给予尝试regex功能在你的文本模式任务.对于这个任务,下面的函数将解决它使用
re
包.字符串
'acd','efg']
iugsix8n3#
或许你可以考虑每一个窗口的4个字母?
字符串
out
:型
arr
:型