Do các ý kiến cho câu trả lời khác của tôi, OP có một câu hỏi khác liên quan đến việc tìm kiếm các cụm 2D. Đây là một số câu trả lời.
Được lấy từ thư viện của tôi EEGPY, tôi sử dụng một phương thức find_clusters. Nó thực hiện một cuộc đi bộ qua một mảng 2D, tìm thấy tất cả các cụm ở trên / dưới một ngưỡng nhất định.
Đây là mã của tôi:
import pylab as plt
import numpy as np
from Queue import Queue
def find_clusters[ar,thres,cmp_type="greater"]:
"""For a given 2d-array [test statistic], find all clusters which
are above/below a certain threshold.
"""
if not cmp_type in ["lower","greater","abs_greater"]:
raise ValueError["cmp_type must be in [\"lower\",\"greater\",\"abs_greater\"]"]
clusters = []
if cmp_type=="lower":
ar_in = [arthres].astype[np.bool]
else: #cmp_type=="abs_greater":
ar_in = [abs[ar]>thres].astype[np.bool]
already_visited = np.zeros[ar_in.shape,np.bool]
for i_s in range[ar_in.shape[0]]: #i_s wie i_sample
for i_f in range[ar_in.shape[1]]:
if not already_visited[i_s,i_f]:
if ar_in[i_s,i_f]:
#print "Anzahl cluster:", len[clusters]
mask = np.zeros[ar_in.shape,np.bool]
check_queue = Queue[]
check_queue.put[[i_s,i_f]]
while not check_queue.empty[]:
pos_x,pos_y = check_queue.get[]
if not already_visited[pos_x,pos_y]:
#print pos_x,pos_y
already_visited[pos_x,pos_y] = True
if ar_in[pos_x,pos_y]:
mask[pos_x,pos_y] = True
for coords in [[pos_x-1,pos_y],[pos_x+1,pos_y],[pos_x,pos_y-1],[pos_x,pos_y+1]]: #Direct Neighbors
if 0