簡體   English   中英

如何在多維 numpy arrays 中連接單個維度

[英]How to concatenate single dimensions in multidimensional numpy arrays

lows = np.array([0 for lead in range(10)])

temp_lows = np.array([[1 for points in range(12)] for lead in range(10)]) # Ones are only a placeholder. temp_lows is filled with random int (data)

for lead in range(10):
    lows[lead] = np.concatenate((lows[lead], temp_lows[lead]), axis=None)

執行后返回:

ValueError:使用序列設置數組元素。

基本上我希望 lows 數組成為大數組,我根據每次迭代的提前期添加較小的 temp arrays(此處未顯示)。 例如,在第 1 次迭代中,我希望將 temp_lows[0] 中的值添加到 lows[0],並將 temp_lows[1] 中的值添加到 lows[1],依此類推。 現在的問題,我似乎找不到解決方案來在正確的維度上連續添加較小的 arrays。

編輯1:我的問題不准確。 temp_lows 包含某個提前期的數據,我稱之為“提前期”。 現在,對於每次迭代,我希望我在此迭代中處理的數據 (temp_lows) 在正確的提前期添加到低點數組,以一種連續列表的方式。 我將使用這些列表(從 0 到 9)生成 PDF function。

使用這樣的最終答案創建另一個數組可能更容易:

>>> newLows = np.concatenate((lows[:,np.newaxis], temp_lows), axis=1)
>>> newLows
array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])

我不確定我是否完全遵循編輯的邏輯,也許一些實際數據和預期結果將有助於澄清它。 與此同時,也許這更像你正在尋找的東西?

>>> newLows = np.array([np.concatenate((lows[lead], temp_lows[lead]), axis=None) for lead in range(10)])
...   
>>> newLows
array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])

這里的問題是

np.concatenate((lows[lead], temp_lows[lead]), axis=None)

返回一個數組,並且您試圖將該結果放入數組lows的單個元素槽中。 我不明白這一點,因為每一行都是一樣的:

[0 1 1 1 1 1 1 1 1 1 1 1 1]

有更好的方法來實現結果,例如:

>>> a = np.ones(shape=(10, 13), dtype=np.int64)
>>> a[:,0] -= 1
>>> a
[[0 1 1 1 1 1 1 1 1 1 1 1 1]
 [0 1 1 1 1 1 1 1 1 1 1 1 1]
 [0 1 1 1 1 1 1 1 1 1 1 1 1]
 [0 1 1 1 1 1 1 1 1 1 1 1 1]
 [0 1 1 1 1 1 1 1 1 1 1 1 1]
 [0 1 1 1 1 1 1 1 1 1 1 1 1]
 [0 1 1 1 1 1 1 1 1 1 1 1 1]
 [0 1 1 1 1 1 1 1 1 1 1 1 1]
 [0 1 1 1 1 1 1 1 1 1 1 1 1]
 [0 1 1 1 1 1 1 1 1 1 1 1 1]]

這是擴展列表的正確列表方式:

In [311]: lows = [[0] for lead in range(10)] 
     ...: temp_lows = [[1 for points in range(12)] for lead in range(10)] 
     ...: for lead in range(5): 
     ...:     lows[lead].extend(temp_lows[lead]) 
     ...:                                                                                              
In [312]: lows                                                                                         
Out[312]: 
[[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
 [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
 [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
 [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
 [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
 [0],
 [0],
 [0],
 [0],
 [0]]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM