![](/img/trans.png)
[英]How to pick one of two arrays in an axis of multidimensional NumPy array with an 1D index array for that axis
[英]How to change several nested numpy arrays into one multidimensional array?
我有一個帶有這些嵌套的numpy數組的numpy a = np.load('test.npy')
文件:
In [21]: a.shape
Out[21]: (6886, 3)
In [22]: a[0].shape
Out[22]: (3,)
In [23]: a[0][0].shape
Out[23]: (787, 6)
有沒有簡單的方法可以將a
更改a
形狀為(6886, 3, 787, 6)
的4維數組?
我不願意這樣做,但是想到的只是制作所需形狀的第二個數組並將數據切成該數組。 我不得不承認,我在理解每個子陣列的形狀時遇到了困難……這似乎與直覺相反。 無論如何,此解決方案將很慢,但是您只能執行一次並保存陣列,而不再執行。
import numpy as np
a = np.load('test.npy')
b = np.full((6886, 3, 787, 6), np.nan)
for row in range(6886):
for col in range(3):
tmp = a[row][col]
b[row, col, :tmp.shape[0], :tmp.shape[1]] = tmp
這有意義嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.