【问题标题】:Reshape data skipping specific number of points重塑数据跳过特定数量的点
【发布时间】:2020-11-05 10:23:20
【问题描述】:

我有一个 1024^3 的数据集要读取。但是这个数组太大了,所以我想每 2^n 点保存一次数据。比如我设置skip = 2,那么数组就是512^3。

import numpy as np
nx = 1024
filename = 'E:/AUTOIGNITION/Z1/Velocity1_inertHIT.bin'
U = np.fromfile(filename, dtype=np.float32, count=nx**3).reshape(nx, nx, nx)

如何使用 reshape 来做到这一点?

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    如果我理解正确,您希望在 3 个维度中的每个维度中对数组的每个 (2^n):th 值进行采样。

    您可以通过如下索引来做到这一点。

    >>> import numpy as np
    >>> n = 1
    >>> example_array = np.zeros((1024,1024,1024))
    >>> N = 2^n
    >>> example_array_sampled = example_array[::N, ::N, ::N]
    >>> example_array_sampled.shape
    (512, 512, 512)
    

    验证它实际上是否均匀采样:

    >>> np.arange(64).reshape((4,4,4))[::2,::2,::2]
    array([[[ 0,  2],
            [ 8, 10]],
    
           [[32, 34],
            [40, 42]]])
    

    此特定代码仍假定您将在对它进行索引之前读取内存中的整个数组。而变量example_array_sampled 将是原始数组的视图。

    有关索引 numpy 数组的更多信息:https://numpy.org/doc/stable/reference/arrays.indexing.html

    关于 numpy 视图的一些信息:https://scipy-cookbook.readthedocs.io/items/ViewsVsCopies.html

    相关问题(一维):subsampling every nth entry in a numpy array

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      相关资源
      最近更新 更多