如何找到在2D numpy的阵列簇的大小? [英] How to find cluster sizes in 2D numpy array?

查看:154
本文介绍了如何找到在2D numpy的阵列簇的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是下面的,

我有一个2D numpy的阵列填充有0-2的1,与吸收边界条件(所有外元件是0),例如:

I have a 2D numpy array filled with 0 an 1, with an absorbing boundary condition (all the outer elements are 0) , for example:

[[0 0 0 0 0 0 0 0 0 0]
 [0 0 1 0 0 0 0 0 0 0]
 [0 0 1 0 1 0 0 0 1 0]
 [0 0 0 0 0 0 1 0 1 0]
 [0 0 0 0 0 0 1 0 0 0]
 [0 0 0 0 1 0 1 0 0 0]
 [0 0 0 0 0 1 1 0 0 0]
 [0 0 0 1 0 1 0 0 0 0]
 [0 0 0 0 1 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]]

我想创建一个函数,该阵列及其线性尺寸L作为输入参数,(在这种情况下,L = 10),并返回该数组的簇大小的列表。

I want to create a function that takes this array and its linear dimension L as input parameters, (in this case L = 10) and returns the list of cluster sizes of this array.

通过集群我的意思是数组的元素1的隔离组

By "clusters" I mean the isolated groups of elements 1 of the array

数组元素[I] [j]的是,如果所有的邻居都为零孤立的,它的邻居是的元素:

the array element [ i ][ j ] is isolated if all its neighbours are zeros, and its neighbours are the elements:

[i+1][j]
[i-1][j]
[i][j+1]
[i][j-1]

所以previous数组中,我们有规模的7群(2,1,2,6,1,1,1)

So in the previous array we have 7 clusters of sizes (2,1,2,6,1,1,1)

我试图通过创建两个函数来完成这个任务,第一个是一个递归函数:

I tried to complete this task by creating two functions, the first one is a recursive function:

def clust_size(array,i,j):

    count = 0

    if array[i][j] == 1:

        array[i][j] = 0

        if array[i-1][j] == 1:

            count += 1
            array[i-1][j] = 0
            clust_size(array,i-1,j)

        elif array[i][j-1] == 1:

            count += 1
            array[i-1][j] = 0
            clust_size(array,i,j-1)

        elif array[i+1][j] == 1:

            count += 1
            array[i-1][j] =  0
            clust_size(array,i+1,j)

        elif array[i][j+1] == 1:

            count += 1
            array[i-1][j] = 0
            clust_size(array,i,j+1)

    return count+1         

和它应该返回一个簇的大小。每次函数找到的数组元素等于1它增加计数器计数的值和元素的值就会计算只是一个时间每个1的元素改变为0,以这种方式。
如果该元素的邻居之一等于1,则该函数调用自身该元素上。

and it should return the size of one cluster. Everytime the function finds an array element equal to 1 it increases the value of the counter "count" and changes the value of the element to 0, in this way each '1' element it's counted just one time. If one of the neighbours of the element is equal to 1 then the function calls itself on that element.

第二个功能是:

def clust_list(array,L):

    sizes_list = []

    for i in range(1,L-1):
        for i in range(1,L-1):

           count = clust_size(array,i,j)

           sizes_list.append(count)

    return sizes_list

和它应该返回一个包含簇大小的列表。从1 for循环迭代,以L-1,因为所有的外部因素为0。

and it should return the list containing the cluster sizes. The for loop iterates from 1 to L-1 because all the outer elements are 0.

这不工作,我看不出哪里出错...

This doesn't work and I can't see where the error is...

我想知道是否也许有一个更简单的方法来做到这一点。

I was wondering if maybe there's an easier way to do it.

推荐答案

这似乎是一个渗透的问题。
下面的链接有你的答案,如果你已经安装了SciPy的。

it seems like a percolation problem. The following link has your answer if you have scipy installed.

<一个href=\"http://dragly.org/2013/03/25/working-with-percolation-clusters-in-python/\">http://dragly.org/2013/03/25/working-with-percolation-clusters-in-python/

from pylab import *
from scipy.ndimage import measurements

z2 = array([[0,0,0,0,0,0,0,0,0,0],
    [0,0,1,0,0,0,0,0,0,0],
    [0,0,1,0,1,0,0,0,1,0],
    [0,0,0,0,0,0,1,0,1,0],
    [0,0,0,0,0,0,1,0,0,0],
    [0,0,0,0,1,0,1,0,0,0],
    [0,0,0,0,0,1,1,0,0,0],
    [0,0,0,1,0,1,0,0,0,0],
    [0,0,0,0,1,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0]])

这将确定集群:

lw, num = measurements.label(z2)
print lw
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
   [0, 0, 1, 0, 2, 0, 0, 0, 3, 0],
   [0, 0, 0, 0, 0, 0, 4, 0, 3, 0],
   [0, 0, 0, 0, 0, 0, 4, 0, 0, 0],
   [0, 0, 0, 0, 5, 0, 4, 0, 0, 0],
   [0, 0, 0, 0, 0, 4, 4, 0, 0, 0],
   [0, 0, 0, 6, 0, 4, 0, 0, 0, 0],
   [0, 0, 0, 0, 7, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

下面将计算其面积。

The following will calculate their area.

area = measurements.sum(z2, lw, index=arange(lw.max() + 1))
print area
[ 0.  2.  1.  2.  6.  1.  1.  1.]

这给了你所期望的,但我会认为你将有8名成员由眼渗透集群。

This gives what you expect, although I would think that you would have a cluster with 8 members by eye-percolation.

这篇关于如何找到在2D numpy的阵列簇的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆