【问题标题】:Find the most "empty" place in an array [duplicate]在数组中找到最“空”的地方[重复]
【发布时间】:2019-02-14 21:07:35
【问题描述】:

假设我有以下 numpy 数组:

foo = np.asarray([
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1],
    [1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1],
    [1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0],
    [1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1],
    [1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1],
    [1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
])

如您所见,它包含一些 0 和 1。我正在寻找这个数组中最“空”的地方。我的意思是你发现 0 的更高密度的地方。我想做一个函数,返回那个地方中心的行和列索引。

目前我试图做的是使用这样的函数将该数组切割成相同大小的四个部分:

def blockshaped(arr, nrows, ncols):
    """Return an array of shape (n, nrows, ncols)."""
    h, w = arr.shape
    arr = arr.reshape(h // nrows, nrows, -1, ncols)
    arr = arr.swapaxes(1, 2)
    arr = arr.reshape(-1, nrows, ncols)

    return (arr)

然后我计算每个部分的总和。取总和最低的部分,然后在 4 中再次减少,依此类推......但我觉得这不是正确的事情......

【问题讨论】:

  • 您是在寻找相邻元素的总和为 0 的最大区域,还是这个“地方”必须是的特定形状?因为如果不是,您可以返回任何带有 0 的元素,并且它将具有尽可能高的密度
  • 你需要定义中心,因为中心是凹形的,可能在形状之外
  • 最大为零的区域
  • @OlivierMelançon arg,是的,很难定义:(
  • 你说的区域是什么意思,请说的更具体些

标签: python algorithm numpy


【解决方案1】:

我基本上是从Efficient way to find the largest area of a specific value in a 2D numpy array 借用代码,但它没有找到(或至少是定义)中心。为此,我们可以使用ndimage.measurements.center_of_mass

import numpy as np
from scipy import ndimage

foo = np.asarray([
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1],
    [1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1],
    [1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0],
    [1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1],
    [1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1],
    [1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
])

label, num_label = ndimage.label(foo == 0)
size = np.bincount(label.ravel())
biggest_label = size[1:].argmax() + 1
clump_mask = label == biggest_label

center = ndimage.measurements.center_of_mass(clump_mask)

这给出:(7.238095238095238, 6.571428571428571)

【讨论】:

  • 完美满足我的需求:)
猜你喜欢
  • 2021-06-24
  • 2017-05-30
  • 2020-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-22
  • 1970-01-01
相关资源
最近更新 更多