【问题标题】:Numpy Swap/Substitute NoneType Entry with Numpy Array (i.e. vector)使用 Numpy 数组(即向量)的 Numpy 交换/替换 NoneType 条目
【发布时间】:2018-06-04 13:32:01
【问题描述】:

给定一个包含两种元素的 numpy 数组:

  1. “numpy.ndarray”条目和
  2. “NoneType”条目

如何将所有“NoneType”条目替换为例如np.zeros(some_shape)? 这是否也适用于任何类型的单个元素,例如标量而不是 NoneType?

例子:

test_array=
    array([[array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
    array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
    array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8), ..., None, None,
    None],
   [array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
    array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
    array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8), ..., None, None,
    None],
   [array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
    array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
    array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8), ...,
    array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8), None, None],
   ..., 
   [array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
    array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
    array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8), ...,
    array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
    array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8), None],
   [array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
    array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
    array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8), ...,
    array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
    array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8), None],
   [array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
    array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
    array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8), ...,
    array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
    array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
    array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8)]], dtype=object)

test_array 中的数组可能如下所示:

test_array[323]=
   array([array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
   array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
   array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
   array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
   array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8), None, None], dtype=object)

我想用与其他向量相同长度的零向量替换那些“无”条目(此处为位置 0 到 3)。 所以我对每个数组的结果(test_array 中的 test_array[i] 看起来像这样:

test_array[131]=
   array([array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
   array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
   array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
   array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
   array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
   array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
   array([[0, 0, 0, ..., 0, 0, 0]], dtype=uint8)], dtype=object)

所以我想用 np.zeros 数组填充所有 None 条目。确实存在 numpy 函数 np.nan_to_num 但这对我没有帮助,因为我需要类似“np.nan_to_array”的东西。

谢谢!

【问题讨论】:

    标签: arrays numpy python-3.5 nonetype


    【解决方案1】:

    通常我不会在 NumPy 中使用 for 循环,但在你的情况下,你有一个对象数组,它的效率不是很高,处理 None 和存储为对象的子数组的组合非常棘手。所以,保持简单:

    prototype = a[0]
    for i, x in enumerate(a):
        if x is None:
            a[i] = np.zeros_like(prototype)
    

    当然,如果a[0] 为无,则您需要找到prototype。这留作练习。

    【讨论】:

    • 这对我不起作用,是不是在我们获取主数组的子数组后,又缺少一个 for 循环?因为 if 语句“如果 x 为无”总是错误的,因为每个 x 都是一个有效的 numpy 数组。你怎么看?
    • @SolingerMUC:哦,我是针对你的内部数组写的。是的,你需要在它周围再放一个for 循环。试一试。
    • 知道了!感谢您的帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 2015-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    相关资源
    最近更新 更多