簡體   English   中英

numpy:如何獲得2個矩陣不同的項?

[英]Numpy: how to get the items that are different of 2 matrices?

我知道如何使用Numpy比較2個整數矩陣。 但是Numpy是否提供一種獲取它們之間不同元素列表的方法?

像這樣嗎

>>> import numpy as np
>>> a = np.zeros(3)
>>> b = np.zeros(3)
>>> a[1] = 1
>>> a == b
array([ True, False,  True], dtype=bool)

對於浮點數: http : //docs.scipy.org/doc/numpy-dev/reference/generated/numpy.isclose.html

>>> np.isclose(a,b)
array([ True, False,  True], dtype=bool)

不同元素的索引

 >>>[i for i,val in enumerate(np.isclose(a,b)) if val == False]

(改為使用numpy)

>>> np.where(np.isclose(a,b) == False)

查找不同元素的值:

 >>> d = [i for i,val in enumerate(np.isclose(a,b)) if val == False]
 >>> a[d]
 array([ 1.])
 >>> b[d]
 array([ 0.])

暫無
暫無

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

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