python - 如何在 Python 中将一个数组中的多个列堆叠在一起?

标签 python arrays numpy reshape large-data

例如,数组的形状是5,4。

a = np.random.randint(10, size= (5, 4))

a = 
[[1 4 5 0]
 [3 1 5 1]
 [4 8 0 9]
 [8 1 5 8]
 [6 4 7 4]]

我希望数组被 reshape 为:

a = 
[[1 4]
 [3 1]
 [4 8]
 [8 1]
 [6 4]
 [5 0]
 [5 1]
 [0 9]
 [5 8]
 [7 4]]

我的原始数组大小约为 200 GB,形状为 80000*480600。我尝试过使用重映射模式,但速度非常慢。

最佳答案

使用numpy.hsplitnumpy.concatenate :-

>>> a = np.random.randint(10, size= (5, 4))
>>> a
array([[8, 5, 8, 9],
       [9, 5, 6, 3],
       [5, 3, 8, 7],
       [9, 0, 9, 9],
       [0, 7, 8, 0]])
>>> t = np.hsplit(a, 2)
>>> t
[array([[8, 5],
       [9, 5],
       [5, 3],
       [9, 0],
       [0, 7]]), array([[8, 9],
       [6, 3],
       [8, 7],
       [9, 9],
       [8, 0]])]
>>> np.concatenate([t[0], t[1]])
array([[8, 5],
       [9, 5],
       [5, 3],
       [9, 0],
       [0, 7],
       [8, 9],
       [6, 3],
       [8, 7],
       [9, 9],
       [8, 0]])

关于python - 如何在 Python 中将一个数组中的多个列堆叠在一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57147205/

相关文章:

python - 使用 OpenCv-python 的失真效果

python - 如何提高索贝尔边缘检测器的效率

python - 持续时间未知的事件的进度条 (PySide)

javascript - 从数组中获取数据

python - 标准化数据集中的所有数字列并比较之前和之后

python - 4-D Numpy 数组的交换排序

C++ 自定义数组容器动态大小

C:打印数组中某个点的水平、垂直和倾斜值

python - 在大矩阵中找到满足特定条件的元素的所有索引给出 MemoryError

python - 使用 matplotlib.pyplot.imsave 将 numpy.ndarray 保存为图像 (.png) 时出错