将两个字典与numpy矩阵作为值进行比较 [英] Comparing two dictionaries with numpy matrices as values

查看:909
本文介绍了将两个字典与numpy矩阵作为值进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想断言两个Python字典是相等的(这意味着:等量的键,从键到值的每个映射是相等的;顺序并不重要)。一个简单的方法是断言A == B ,但是,如果字典的值为 numpy数组。如果两个字典相同,我如何编写一个函数来检查?

 >>>导入numpy为np 
>>>> A = {1:np.identity(5)}
>>> B = {1:np.identity(5)+ np.ones([5,5])}
>>> A == B
ValueError:具有多个元素的数组的真值是不明确的。使用a.any()或a.all()

编辑我知道应该检查numpy矩阵是否与 .all()相等。我正在寻找的是检查这个的一般方法,而不必检查 isinstance(np.ndarray)。这是可能的吗?



没有numpy数组的相关主题:




解决方案

考虑这个代码

 >>>导入numpy为np 
>>>> np.identity(5)
array([[1.,0.,0.,0.,0.],
[0.,1.,0.,0.,0] ,
[0.,0.,1.,0.,0.],
[0.,0.,0.,1.,0],
[ ,0.,0.,0.,1.]])
>>>> np.identity(5)+ np.ones([5,5])
array([[2.,1.,1.,1.,1.],
[1.,2 ,1.,1.,1.],
[1.,1.,2.,1.,1.],
[1.,1.,1., 1.],
[1.,1.,1.,1.,2.]])
>>> np.identity(5)== np.identity(5)+ np.ones([5,5])
数组([[False,False,False,False,False],
[False ,False,False,False],
[False,False,False,False,False],
[False,False,False,False,False],
[False,False ,False,False,False]],dtype = bool)
>>>

注意比较的结果是一个矩阵,而不是一个布尔值。字幕比较将使用值 cmp 方法比较值,这意味着当比较矩阵值时,dict比较将获得复合结果。你想做的是使用
numpy.all 将复合数组结果折叠为标量布尔值结果

 >>> np.all(np.identity(5)== np.identity(5)+ np.ones([5,5]))
False
>>> np.all(np.identity(5)== np.identity(5))
True
>>>

您需要编写自己的函数来比较这些字典,测试值类型,以查看它们是否matricies,然后使用 numpy.all 进行比较,否则使用 == 。当然,如果你也愿意,你可以随时看看,开始继承dict并重载 cmp


I want to assert that two Python dictionaries are equal (that means: equal amount of keys, and each mapping from key to value is equal; order is not important). A simple way would be assert A==B, however, this does not work if the values of the dictionaries are numpy arrays. How can I write a function to check in general if two dictionaries are equal?

>>> import numpy as np
>>> A = {1: np.identity(5)}
>>> B = {1: np.identity(5) + np.ones([5,5])}
>>> A == B
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

EDIT I am aware that numpy matrices shall be checked for equality with .all(). What I am looking for is a general way to check for this, without having to check isinstance(np.ndarray). Would this be possible?

Related topics without numpy arrays:

解决方案

Consider this code

>>> import numpy as np
>>> np.identity(5)
array([[ 1.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  0.,  1.]])
>>> np.identity(5)+np.ones([5,5])
array([[ 2.,  1.,  1.,  1.,  1.],
       [ 1.,  2.,  1.,  1.,  1.],
       [ 1.,  1.,  2.,  1.,  1.],
       [ 1.,  1.,  1.,  2.,  1.],
       [ 1.,  1.,  1.,  1.,  2.]])
>>> np.identity(5) == np.identity(5)+np.ones([5,5])
array([[False, False, False, False, False],
       [False, False, False, False, False],
       [False, False, False, False, False],
       [False, False, False, False, False],
       [False, False, False, False, False]], dtype=bool)
>>> 

Note the the result of the comparison is a matrix, not a boolean value. Dict comparisons will compare values using the values cmp methods, which means that when comparing matrix values, the dict comparison will get a composite result. What you want to do is use numpy.all to collapse the composite array result into a scalar boolean result

>>> np.all(np.identity(5) == np.identity(5)+np.ones([5,5]))
False
>>> np.all(np.identity(5) == np.identity(5))
True
>>> 

You would need to write your own function to compare these dictionaries, testing value types to see if they are matricies, and then comparing using numpy.all, otherwise using ==. Of course, you can always get fancy and start subclassing dict and overloading cmp if you want too.

这篇关于将两个字典与numpy矩阵作为值进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆