python – 在NumPy数组中搜索序列
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 在NumPy数组中搜索序列,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3223字,纯文字阅读大概需要5分钟。
内容图文
![python – 在NumPy数组中搜索序列](/upload/InfoBanner/zyjiaocheng/704/159d7d0e2eee45cfb230575c142d91bd.jpg)
假设我有以下数组:
array([2, 0, 0, 1, 0, 1, 0, 0])
如何获得我出现值序列的索引:[0,0]?因此,这种情况的预期输出将是:[1,2,6,7].
编辑:
1)请注意[0,0]只是一个序列.它可能是[0,0,0]或[4,6,8,9]或[5,2,0],只是任何东西.
2)如果我的数组被修改为:array([2,0,0,0,0,1,0,1,0,0]),具有相同序列[0,0]的预期结果将是[ 1,2,3,4,8,9.
我正在寻找一些NumPy快捷方式.
解决方法:
嗯,这基本上是图像处理中出现的template-matching problem.在这篇文章中列出了两种方法:基于Pure NumPy和基于OpenCV(cv2).
方法#1:使用NumPy,可以在输入数组的整个长度上创建一个滑动索引的2D数组.因此,每行将是元素的滑动窗口.接下来,将每一行与输入序列匹配,这将为矢量化解决方案带来broadcasting.我们寻找所有True行,表明那些是完美的匹配,因此将是匹配的起始索引.最后,使用这些索引,创建一系列延伸到序列长度的索引,以便为我们提供所需的输出.实施将是 –
def search_sequence_numpy(arr,seq):
""" Find sequence in an array using NumPy only.
Parameters
----------
arr : input 1D array
seq : input 1D array
Output
------
Output : 1D Array of indices in the input array that satisfy the
matching of input sequence in the input array.
In case of no match, an empty list is returned.
"""
# Store sizes of input array and sequence
Na, Nseq = arr.size, seq.size
# Range of sequence
r_seq = np.arange(Nseq)
# Create a 2D array of sliding indices across the entire length of input array.
# Match up with the input sequence & get the matching starting indices.
M = (arr[np.arange(Na-Nseq+1)[:,None] + r_seq] == seq).all(1)
# Get the range of those indices as final output
if M.any() >0:
return np.where(np.convolve(M,np.ones((Nseq),dtype=int))>0)[0]
else:
return [] # No match found
方法#2:使用OpenCV(cv2),我们有一个用于模板匹配的内置函数:cv2.matchTemplate.使用这个,我们将得到起始匹配索引.其余步骤与前一种方法相同.这是cv2的实现:
from cv2 import matchTemplate as cv2m
def search_sequence_cv2(arr,seq):
""" Find sequence in an array using cv2.
"""
# Run a template match with input sequence as the template across
# the entire length of the input array and get scores.
S = cv2m(arr.astype('uint8'),seq.astype('uint8'),cv2.TM_SQDIFF)
# Now, with floating point array cases, the matching scores might not be
# exactly zeros, but would be very small numbers as compared to others.
# So, for that use a very small to be used to threshold the scorees
# against and decide for matches.
thresh = 1e-5 # Would depend on elements in seq. So, be careful setting this.
# Find the matching indices
idx = np.where(S.ravel() < thresh)[0]
# Get the range of those indices as final output
if len(idx)>0:
return np.unique((idx[:,None] + np.arange(seq.size)).ravel())
else:
return [] # No match found
样品运行
In [512]: arr = np.array([2, 0, 0, 0, 0, 1, 0, 1, 0, 0])
In [513]: seq = np.array([0,0])
In [514]: search_sequence_numpy(arr,seq)
Out[514]: array([1, 2, 3, 4, 8, 9])
In [515]: search_sequence_cv2(arr,seq)
Out[515]: array([1, 2, 3, 4, 8, 9])
运行时测试
In [477]: arr = np.random.randint(0,9,(100000))
...: seq = np.array([3,6,8,4])
...:
In [478]: np.allclose(search_sequence_numpy(arr,seq),search_sequence_cv2(arr,seq))
Out[478]: True
In [479]: %timeit search_sequence_numpy(arr,seq)
100 loops, best of 3: 11.8 ms per loop
In [480]: %timeit search_sequence_cv2(arr,seq)
10 loops, best of 3: 20.6 ms per loop
看起来像Pure NumPy一样是最安全和最快的!
内容总结
以上是互联网集市为您收集整理的python – 在NumPy数组中搜索序列全部内容,希望文章能够帮你解决python – 在NumPy数组中搜索序列所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。