首頁  >  文章  >  後端開發  >  如何存取 NumPy 多維數組中的特定列?

如何存取 NumPy 多維數組中的特定列?

Patricia Arquette
Patricia Arquette原創
2024-10-27 11:53:01505瀏覽

How do you access specific columns in a NumPy multidimensional array?

存取 NumPy 多維數組中的列

給定 NumPy 多維數組,可以使用索引技術有效地檢索特定列。要存取第i 列,請使用以下語法:

<code class="python">array[:, i]</code>

例如:

<code class="python">test = np.array([[1, 2], [3, 4], [5, 6]])
test[:, 0]  # Accesses the first column</code>

其輸出:

array([1, 3, 5])

相反,要存取第i 個列行,使用:

<code class="python">array[i, :]</code>

例如:

<code class="python">test[0, :]  # Accesses the first row</code>

輸出:

array([1, 2])

有關更多詳細信息,請參閱NumPy 參考文獻的索引部分。此操作通常很有效,特別是與循環單一元素相比。

以上是如何存取 NumPy 多維數組中的特定列?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn