我正在寻找以下问题的解决方案:
假设我有一个形状为(4,4)的数组:
[5. 4. 5. 4.]
[2. 3. 5. 5.]
[2. 1. 5. 1.]
[1. 3. 1. 3.]
在此数组中,有一列的值“ 5”连续出现3次。也就是说,它们不会分散在整个列中,如下例所示。
[5.] # This
[1.] # Should
[5.] # Not
[5.] # Count
现在,假设我有一个形状(M,N)的较大数组,并且各个整数值在1-5的相同范围内。如何计算每列中一行出现的相同值的最大数量?此外,是否有可能获得这些值出现的索引?上面示例的预期输出为
Found 3 in a row of number 5 in column 2
(0,2), (1,2), (2,2)
我假设如果搜索应关注行,则实现方式将相似。如果不是,我很想知道这是怎么做到的。
方法1
这是一种方法-
def find_longest_island_indices(a, values):
b = np.pad(a, ((1,1),(0,0)), 'constant')
shp = np.array(b.shape)[::-1] - [0,1]
maxlens = []
final_out = []
for v in values:
m = b==v
idx = np.flatnonzero((m[:-1] != m[1:]).T)
s0,s1 = idx[::2], idx[1::2]
l = s1-s0
maxidx = l.argmax()
longest_island_flatidx = np.r_[s0[maxidx]:s1[maxidx]]
r,c = np.unravel_index(longest_island_flatidx, shp)
final_out.append(np.c_[c,r])
maxlens.append(l[maxidx])
return maxlens, final_out
样品运行-
In [169]: a
Out[169]:
array([[5, 4, 5, 4],
[2, 3, 5, 5],
[2, 1, 5, 1],
[1, 3, 1, 3]])
In [173]: maxlens
Out[173]: [1, 2, 1, 1, 3]
In [174]: out
Out[174]:
[array([[3, 0]]), array([[1, 0],
[2, 0]]), array([[1, 1]]), array([[0, 1]]), array([[0, 2],
[1, 2],
[2, 2]])]
# With "pretty" printing
In [171]: maxlens, out = find_longest_island_indices(a, [1,2,3,4,5])
...: for l,o,i in zip(maxlens,out,[1,2,3,4,5]):
...: print "For "+str(i)+" : L= "+str(l)+", Idx = "+str(o.tolist())
For 1 : L= 1, Idx = [[3, 0]]
For 2 : L= 2, Idx = [[1, 0], [2, 0]]
For 3 : L= 1, Idx = [[1, 1]]
For 4 : L= 1, Idx = [[0, 1]]
For 5 : L= 3, Idx = [[0, 2], [1, 2], [2, 2]]
方法#2
经过一些修改并输出了最大长度岛的开始和结束索引,这是一个-
def find_longest_island_indices_v2(a, values):
b = np.pad(a.T, ((0,0),(1,1)), 'constant')
shp = b.shape
out = []
for v in values:
m = b==v
idx = np.flatnonzero(m.flat[:-1] != m.flat[1:])
s0,s1 = idx[::2], idx[1::2]
l = s1-s0
maxidx = l.argmax()
start_index = np.unravel_index(s0[maxidx], shp)[::-1]
end_index = np.unravel_index(s1[maxidx]-1, shp)[::-1]
maxlen = l[maxidx]
out.append([v,maxlen, start_index, end_index])
return out
样品运行-
In [251]: a
Out[251]:
array([[5, 4, 5, 4],
[2, 3, 5, 5],
[2, 1, 5, 1],
[1, 3, 1, 3]])
In [252]: out = find_longest_island_indices_v2(a, [1,2,3,4,5])
In [255]: out
Out[255]:
[[1, 1, (3, 0), (3, 0)],
[2, 2, (1, 0), (2, 0)],
[3, 1, (1, 1), (1, 1)],
[4, 1, (0, 1), (0, 1)],
[5, 3, (0, 2), (2, 2)]]
# With some pandas styled printing
In [253]: import pandas as pd
In [254]: pd.DataFrame(out, columns=['Val','MaxLen','StartIdx','EndIdx'])
Out[254]:
Val MaxLen StartIdx EndIdx
0 1 1 (3, 0) (3, 0)
1 2 2 (1, 0) (2, 0)
2 3 1 (1, 1) (1, 1)
3 4 1 (0, 1) (0, 1)
4 5 3 (0, 2) (2, 2)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句