如何在单独的numpy数组中将具有相同值的numpy数组的元素分组 [英] How to group elements of a numpy array with the same value in separate numpy arrays

查看:199
本文介绍了如何在单独的numpy数组中将具有相同值的numpy数组的元素分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像往常一样,我是python的tyro.但是,我有一个很大的项目要编写代码.它是带有Cell Automata的表面流模型.无论如何,我也想在模型中包括建筑物屋顶.假设您有一个ascii文件,其中的建筑物表示为1,而其余的为0.只有这两种状态.现在,我想查找所有指示同一建筑物的相邻单元格,并将它们(或者y,x信息以及更多信息(可能是高程),所以3列)存储在单个建筑物数组中.请记住,尽管对角连接的单元不属于同一建筑物,但建筑物可以具有所有可能的形式.因此,只有北部,南部,西部和东部的牢房可以属于同一建筑物.

As usual intro, I am a tyro in python. However, I got quite a big project to code. It is a surface flow model with Cell Automata. Anyway, I also want to include building roofs in my model. Imagine you have an ascii file indicating buildings with 1s, while the rest is 0. There are just those two states. Now, I want to find all adjacent cells indicating the same building and store them (or rather the information of y,x and one more (maybe elevation),so 3 columns) in an individual building arrays. Keep in mind that buildings can have all possible forms though diagonally connected cells doesn't belong to the same building. So only northern, southern, western and eastern cells can belong to the same building.

我做了作业并在Google上进行了搜索,但到目前为止我找不到令人满意的答案.

I did my homework and googled it but so far I couldn't find a satisfying answer.

示例: 初始的土地覆盖物阵列:

example: initial land-cover array:

([0,0,0,0,0,0,0]
 [0,0,1,0,0,0,0]
 [0,1,1,1,0,1,1]
 [0,1,0,1,0,0,1]
 [0,0,0,0,0,0,0])

输出(我现在需要初始数组中单元格的坐标):

output(I need to now the coordinates of the cells in my initial array):

 building_1=([1,2],[2,1],[2,2],[2,3],[3,1],[3,3])
 building_2=([2,5],[2,6],[3,6])

任何帮助将不胜感激!

推荐答案

您可以使用

You can use the label function from scipy.ndimage to identify the distinct buildings.

这是您的示例数组,其中包含两座建筑物:

Here's your example array, containing two buildings:

In [57]: a
Out[57]: 
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0],
       [0, 1, 1, 1, 0, 1, 1],
       [0, 1, 0, 1, 0, 0, 1],
       [0, 0, 0, 0, 0, 0, 0]])

导入label.

In [58]: from scipy.ndimage import label

label应用于a.它返回两个值:标记位置的数组和找到的不同对象(在这种情况下为建筑物)的数量.

Apply label to a. It returns two values: the array of labeled positions, and the number of distinct objects (buildings, in this case) found.

In [59]: lbl, nlbls = label(a)

In [60]: lbl
Out[60]: 
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0],
       [0, 1, 1, 1, 0, 2, 2],
       [0, 1, 0, 1, 0, 0, 2],
       [0, 0, 0, 0, 0, 0, 0]], dtype=int32)

In [61]: nlbls
Out[61]: 2

要获取建筑物的坐标,可以使用np.where.例如,

To get the coordinates of a building, np.where can be used. For example,

In [64]: np.where(lbl == 2)
Out[64]: (array([2, 2, 3]), array([5, 6, 6]))

它返回一个数组的元组;第k个数组保存第k个维度的坐标.例如,您可以使用np.column_stack将它们组合成一个数组:

It returns a tuple of arrays; the kth array holds the coordinates of the kth dimension. You can use, for example, np.column_stack to combine these into an array:

 In [65]: np.column_stack(np.where(lbl == 2))
 Out[65]: 
 array([[2, 5],
        [2, 6],
        [3, 6]])

您可能需要所有坐标数组的列表.这是创建此类列表的一种方法.

You might want a list of all the coordinate arrays. Here's one way to create such a list.

为方便起见,首先创建标签列表:

For convenience, first create a list of labels:

In [66]: labels = range(1, nlbls+1)

In [67]: labels
Out[67]: [1, 2]

使用列表推导创建坐标数组的列表.

Use a list comprehension to create the list of coordinate arrays.

In [68]: coords = [np.column_stack(where(lbl == k)) for k in labels]

In [69]: coords
Out[69]: 
[array([[1, 2],
       [2, 1],
       [2, 2],
       [2, 3],
       [3, 1],
       [3, 3]]),
 array([[2, 5],
       [2, 6],
       [3, 6]])]

现在,您的建筑物数据位于labelscoords中.例如,第一座建筑物标记为labels[0],其坐标位于coords[0]:

Now your building data is in labels and coords. For example, the first building was labeled labels[0], and its coordinates are in coords[0]:

In [70]: labels[0]
Out[70]: 1

In [71]: coords[0]
Out[71]: 
array([[1, 2],
       [2, 1],
       [2, 2],
       [2, 3],
       [3, 1],
       [3, 3]])

这篇关于如何在单独的numpy数组中将具有相同值的numpy数组的元素分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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