![](/img/trans.png)
[英]Is there a Python function to find matrix index vector in a panda dataframe?
[英]Find index of a vector inside a matrix in Python
給定一個矩陣( numpy
數組) A
和一個向量v
。
A = np.array([[5,0],[1,-2],[0,2],[-1,3],[1,2]])
v = np.array([0,2])
獲得矩陣 A 中向量 v 的索引的最佳方法是什么(在這種情況下應該得到 2)。
這樣做:
np.argwhere((v == A).all(1))
Out[82]: array([[2]], dtype=int64)
如果最好的意思是最快的,經過徹底的實驗,用戶 hpaulj指出np.flatnonzero
是np.flatnonzero
的更快替代np.argwhere
。 你可以這樣使用它:
np.flatnonzero((v==A).all(1))[0]
輸出:
2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.