计算 numpy 数组中的连续数 python
counting consecutive ones in numpy array python
numpy 的新手。我有一个由 1 和 0 组成的二维数组,我试图对角线扫描一定长度的连续数组。一旦找到模式,函数应该 return 模式开始的索引,即伸展中第一个“1”的位置。
这是我最好的尝试:
def find_pattern(array2D, patternlength):
ones_count = 0
pattern_location = []
diag = [array2D.diagonal(i) for i in range(array2D.shape[1]-1,-array2D.shape[0],-1)]
for index, match in np.ndenumerate(diag):
if match == 1:
ones_count +=1
else:
ones_count == 0
if ones_count == patternlength:
pattern_location.append(index)
return pattern_location
但是,当尝试 运行 这会产生一个 ValueError:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我明白为什么会出现错误,但我不知道如何解决。 any() 或 all() 似乎不适合我正在寻找一段连续的实例。
我正在寻找一种解决方案,它不涉及使用额外的包,例如 pandas 和 itertools。
谢谢!
我认为你过于复杂了,怎么样:
import numpy as np
def find_pattern(array2D, patternlength):
res=[]
for k in range(-array2D.shape[0]+1, array2D.shape[1]):
diag=np.diag(array2D, k=k)
if(len(diag)>=patternlength):
for i in range(len(diag)-patternlength+1):
if(all(diag[i:i+patternlength]==1)):
res.append((i+abs(k), i) if k<0 else (i, i+abs(k)))
return res
示例输入:
test=np.random.choice([0,1], (12, 14))
print(test)
print(find_pattern(test,3))
returns:
[[1 1 1 0 0 0 1 0 1 1 1 0 1 1]
[1 1 0 0 0 1 1 1 1 0 1 0 0 1]
[1 0 1 1 1 0 1 0 1 1 1 0 0 1]
[1 1 0 1 0 1 1 0 0 0 1 0 0 0]
[0 1 0 1 0 1 1 0 1 0 0 0 1 1]
[1 1 0 0 0 1 0 1 0 1 0 1 0 0]
[1 0 1 0 0 0 1 1 0 1 1 1 1 1]
[1 1 1 1 1 1 0 0 0 0 0 0 0 0]
[1 0 1 1 1 0 1 0 0 0 0 0 0 1]
[1 0 1 0 0 1 1 1 1 1 0 1 0 0]
[0 0 1 0 0 1 0 0 1 1 0 1 1 0]
[0 0 0 0 0 1 1 0 1 1 0 0 0 0]]
[(6, 0), (5, 1), (6, 2), (7, 3), (7, 5), (8, 6), (9, 7), (0, 0), (1, 1), (2, 4), (3, 5), (4, 8), (0, 6), (1, 8)]
numpy 的新手。我有一个由 1 和 0 组成的二维数组,我试图对角线扫描一定长度的连续数组。一旦找到模式,函数应该 return 模式开始的索引,即伸展中第一个“1”的位置。 这是我最好的尝试:
def find_pattern(array2D, patternlength):
ones_count = 0
pattern_location = []
diag = [array2D.diagonal(i) for i in range(array2D.shape[1]-1,-array2D.shape[0],-1)]
for index, match in np.ndenumerate(diag):
if match == 1:
ones_count +=1
else:
ones_count == 0
if ones_count == patternlength:
pattern_location.append(index)
return pattern_location
但是,当尝试 运行 这会产生一个 ValueError:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我明白为什么会出现错误,但我不知道如何解决。 any() 或 all() 似乎不适合我正在寻找一段连续的实例。
我正在寻找一种解决方案,它不涉及使用额外的包,例如 pandas 和 itertools。
谢谢!
我认为你过于复杂了,怎么样:
import numpy as np
def find_pattern(array2D, patternlength):
res=[]
for k in range(-array2D.shape[0]+1, array2D.shape[1]):
diag=np.diag(array2D, k=k)
if(len(diag)>=patternlength):
for i in range(len(diag)-patternlength+1):
if(all(diag[i:i+patternlength]==1)):
res.append((i+abs(k), i) if k<0 else (i, i+abs(k)))
return res
示例输入:
test=np.random.choice([0,1], (12, 14))
print(test)
print(find_pattern(test,3))
returns:
[[1 1 1 0 0 0 1 0 1 1 1 0 1 1]
[1 1 0 0 0 1 1 1 1 0 1 0 0 1]
[1 0 1 1 1 0 1 0 1 1 1 0 0 1]
[1 1 0 1 0 1 1 0 0 0 1 0 0 0]
[0 1 0 1 0 1 1 0 1 0 0 0 1 1]
[1 1 0 0 0 1 0 1 0 1 0 1 0 0]
[1 0 1 0 0 0 1 1 0 1 1 1 1 1]
[1 1 1 1 1 1 0 0 0 0 0 0 0 0]
[1 0 1 1 1 0 1 0 0 0 0 0 0 1]
[1 0 1 0 0 1 1 1 1 1 0 1 0 0]
[0 0 1 0 0 1 0 0 1 1 0 1 1 0]
[0 0 0 0 0 1 1 0 1 1 0 0 0 0]]
[(6, 0), (5, 1), (6, 2), (7, 3), (7, 5), (8, 6), (9, 7), (0, 0), (1, 1), (2, 4), (3, 5), (4, 8), (0, 6), (1, 8)]