【问题标题】:How to do mapping of 3d arrays如何进行 3d 数组的映射
【发布时间】:2020-11-22 16:27:38
【问题描述】:

我有一个 numpy 形状的数组 (20,512,512)。 我想在点 (:,100,100) (:,105,100), (:,110,100) 处获取与数组匹配的长度为 20 的数组列表 等等。 我知道我需要使用地图,但我该怎么做呢?

非常感谢 尤瓦尔

【问题讨论】:

  • 匹配数组是什么意思?您的数组不能重新整形为形状为 (x, 100,100) 的数组
  • 为什么是map?不要混淆map 函数(替代列表理解)、mapping(例如dict)和numy 索引。
  • 我的意思是使用 map = [[True, False, False, False....],[False, True, true...]] 并获得长度为 20 的序列,在A[map] 中此二维数组中 True 的点。
  • 我们通常将其称为布尔掩码,尽管术语并不固定。

标签: python numpy mapping


【解决方案1】:

您可以通过索引切片来实现。

这是一维数组的示例。

a = np.arange(200)
a[100::5]                           # from index 100 to end with increments of 5

>>> array([100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160,
           165, 170, 175, 180, 185, 190, 195])

适合你的情况

shape = [20,512,512]
arr   = np.random.randint(0, 100, shape)     # creating an array with the required shape
arr[:, 100::5, 100]                          # index slicing

>>> array([[88, 74, 45, ..., 72, 33, 63],
           [88, 26, 53, ..., 47, 78, 16],
           [26, 54, 85, ..., 89, 81, 66],
           ...,
           [76,  1, 11, ...,  3, 74,  7],
           [93, 34, 84, ..., 84, 73, 79],
           [77, 10, 61, ...,  4, 21, 19]])
arr[:, 100::5, 100].shape

>>> (20, 83)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    相关资源
    最近更新 更多