gpt4 book ai didi

python - 有没有办法理解零簇,然后将其从 numpy 数组中删除?

转载 作者:太空宇宙 更新时间:2023-11-03 20:44:44 24 4
gpt4 key购买 nike

我想从 numpy 数组中间删除零(但不应删除所有零)

stackoverflow 上的多个示例演示了删除零,但我仍然发现很难为我的问题编写逻辑。

import numpy as np

a = np.array([255,255,255,255,255
,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
,255,255,255,255,255,255,255,255,255,255,255,255,255,0,255,255,255,255
,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,207,0,0
,159,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
,255,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,88,239,255,255,255,255,255,255,255
,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,231,88])

我有一个数组 a,其中有一些非零值和一大群零(以及非零值中间的一些零)。我想删除那个大的零簇或找到该大的簇开始的索引。然后将数组缩减为以下形式:

a1 = [255,255,255,255,255
,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
,255,255,255,255,255,255,255,255,255,255,255,255,255,0,255,255,255,255
,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,207,0,0
,159,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
,255,64]

a2=[88,239,255,255,255,255,255,255,255
,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,231,88]

请注意,数组 a1 中仍然有一些零。仅当其中存在一定数量的连续零(例如 10 个零)时,该操作才应删除零。我可以通过循环遍历数组来完成此操作,但如果可以建议一种更简单、更快的方法,那就太好了。

最佳答案

没有任何导入,使用单个循环:

def remove_clusters(my_array, cluster_value, consecutive_max=10):
my_result = [[]]
cluster_list = []
for e in my_array:
if e == cluster_value:
cluster_list.append(e)
else:
if len(cluster_list) <= consecutive_max:
my_result[-1].extend(cluster_list)
else:
my_result.append([])
cluster_list = []
my_result[-1].append(e)
return my_result

我使用itertools.groupby得到了这个,它简化了一些代码:

def remove_clusters(my_array, cluster_value=0, max_consecutive=10):
from itertools import groupby
my_result = [[]]
for k,g in groupby(my_array):
g = list(g)
if k != cluster_value or len(g) <= max_consecutive:
my_result[-1].extend(g)
else:
my_result.append([])
return my_result

然后你可以这样做:

a1, a2 = remove_clusters(a)

最后,一个使用functools.reduce的非常丑陋的oneliner

from itertools import groupby
from functools import reduce
a1, a2 = reduce(lambda x,y: x + [[]] if not y[0] and len(y)>10
else x[:-1] + [x[-1]+y],
map(lambda x: list(x[1]), groupby(a)),
[[]])

我很想解释一下这条直线,但我已经不明白了。

关于python - 有没有办法理解零簇,然后将其从 numpy 数组中删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56668855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com