簡體   English   中英

為什么 Numpy 不能從 Numpy Arrays 的列表中生成數組?

[英]Why Can't Numpy Produce an Array from a List of Numpy Arrays?

我正在編寫一些代碼來按向量之間的角度對向量進行分組。 例如,我可能有一個向量數組:

vectors = np.array([[1, 0, 0], [1.1, 0, 0], [0, 2, 2]])

例如,可接受的角度偏差為 0.1 弧度。 目前,我正在像這樣的 while 循環中執行此操作:

groups = []
while not vectors.size == 0:
    vector = vectors[0]
    angles = (vectors @ vector)/(np.linalg.norm(vector, axis=1))
    angles = np.arccos(angles/np.linalg.norm(vector))
    group = vectors[angles <= angle]
    groups.append(group)
    vectors = vectors[angles > angle]
return np.array(groups)

我希望這將返回具有以下形式的 numpy 數組:

expected_array = np.array([[[1, 0, 0], [1.1, 0, 0]], [[0, 2, 2]]])

但相反,我得到以下信息:

actual_array = np.array([array([[1. , 0. , 0. ], [1.1, 0. , 0. ]]),
                         array([[0. , 2, 2]])])

為什么 Numpy 沒有注意到列表包含 arrays 並給我我的期望? 有沒有辦法讓 Numpy 注意到這一點? 還是您總是必須使用 np.concatenate 或類似的東西才能獲得所需的結果?

“我希望這會返回一個 numpy 數組,格式如下:”

In [420]: np.array([[[1, 0, 0], [1.1, 0, 0]], [[0, 2, 2]]])
<ipython-input-420-a1f3305ab5c3>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray. np.array([[[1, 0, 0], [1.1, 0, 0]], [[0, 2, 2]]])

Out[420]: array([list([[1, 0, 0], [1.1, 0, 0]]), list([[0, 2, 2]])], dtype=object)

這真的是你所期望的嗎? 保留列表嵌套的數組?

vstack (或concatenate )可以將列表/數組與列表連接起來,形成一個二維數組:

In [421]: np.vstack([[[1, 0, 0], [1.1, 0, 0]], [[0, 2, 2]]])
Out[421]: 
array([[1. , 0. , 0. ],
       [1.1, 0. , 0. ],
       [0. , 2. , 2. ]])

將那些 2 arrays 轉換回列表:

In [422]: _420.tolist()
Out[422]: [[[1, 0, 0], [1.1, 0, 0]], [[0, 2, 2]]]
In [423]: _421.tolist()
Out[423]: [[1.0, 0.0, 0.0], [1.1, 0.0, 0.0], [0.0, 2.0, 2.0]]

第一個有3層嵌套,和原來一樣; 第二個只有 2 個。

===

您的代碼不可運行:

In [424]: vectors = np.array([[1, 0, 0], [1.1, 0, 0], [0, 2, 2]])
In [425]: groups = []
     ...: while not vectors.size == 0:
     ...:     vector = vectors[0]
     ...:     angles = (vectors @ vector)/(np.linalg.norm(vector, axis=1))
     ...:     angles = np.arccos(angles/np.linalg.norm(vector))
     ...:     group = vectors[angles <= angle]
     ...:     groups.append(group)
     ...:     vectors = vectors[angles > angle]
     ...: 
Traceback (most recent call last):
  File "<ipython-input-425-e50fafbda1c3>", line 4, in <module>
    angles = (vectors @ vector)/(np.linalg.norm(vector, axis=1))
  File "<__array_function__ internals>", line 5, in norm
  File "/usr/local/lib/python3.8/dist-packages/numpy/linalg/linalg.py", line 2561, in norm
    return sqrt(add.reduce(s, axis=axis, keepdims=keepdims))
AxisError: axis 1 is out of bounds for array of dimension 1

在您嘗試從中創建數組之前,我希望看到列表groups 我不想調試你的樣本。

簡而言之:可以! (但不是你想要的方式。)

圍繞此主題進行搜索會發現此問題: Numpy stack with unequal shapes

其中明確了以下內容: numpy arrays 必須是 rectangle ,因為上面的示例加在一起時不會產生矩形數組,這不起作用。

如果你嘗試用同樣大小的 arrays做同樣的事情,它會起作用 例如:

array = np.array([np.array([1, 2, 3]), np.array([1, 0, 1]))

將產生一個形狀為 (2, 3) 的數組。

但是,對於不同大小的 arrays , numpy 數組 dtype 默認為object ,而所有單獨的 ZA3CBC53F9D0CE1DE7D1 都被存儲6。

暫無
暫無

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

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