将元组与其中的数组进行比较
compare tuple with array in them
我正在学习 python 和 numpy。在尝试预测结果以检查我的理解时,我遇到了这个:
import numpy as np
x = np.arange(1,10).reshape(3,3)
np.where(x>5) == (np.array([1, 2, 2, 2]), np.array([2, 0, 1, 2]))
以及相关的错误我现在明白为什么了。
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我想到了这个:
all(map(np.array_equal,
np.where(x>5), (np.array([1, 2, 2, 2]), np.array([2, 0, 1, 2])) ))
all([np.array_equal(v,t) for (v,t) in zip(
np.where(x>5), (np.array([1, 2, 2, 2]), np.array([2, 0, 1, 2]))) ])
all([(v==t).all() for (v,t) in zip(
np.where(x>5), (np.array([1, 2, 2, 2]), np.array([2, 0, 1, 2]))) ])
哪个有用,但在我看来有点乏味且难以阅读。是否有更多 pythonic 或 numpy 方法来测试元组中的数组?
你们非常接近。以下作品:
np.array_equal(np.where(x>5), (np.array([1, 2, 2, 2]), np.array([2, 0, 1, 2])))
np.array_equal
具有通过元组进行广播的能力。它还将数组和序列视为等效的,因此您可以根据需要使用:
np.array_equal(np.where(x>5), ([1, 2, 2, 2], [2, 0, 1, 2]))
还有一个testing
子模块(虽然我用的不多)
In [54]: import numpy.testing
In [59]: x = np.arange(1,10).reshape(3,3)
...: idx = np.where(x>5); test=(np.array([1, 2, 2, 2]), np.array([2, 0, 1, 2]))
通过:
In [60]: np.testing.assert_equal(idx,test)
一次失败:
In [61]: idx = np.where(x>4)
In [62]: np.testing.assert_equal(idx,test)
Traceback (most recent call last):
File "<ipython-input-62-a0326017ecb7>", line 1, in <module>
np.testing.assert_equal(idx,test)
File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 338, in assert_equal
assert_equal(actual[k], desired[k], f'item={k!r}\n{err_msg}',
File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 344, in assert_equal
return assert_array_equal(actual, desired, err_msg, verbose)
File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 932, in assert_array_equal
assert_array_compare(operator.__eq__, x, y, err_msg=err_msg,
File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 761, in assert_array_compare
raise AssertionError(msg)
AssertionError:
Arrays are not equal
item=0
(shapes (5,), (4,) mismatch)
x: array([1, 1, 2, 2, 2])
y: array([1, 2, 2, 2])
它的文档:
Raises an AssertionError if two objects are not equal.
Given two objects (scalars, lists, tuples, dictionaries or numpy arrays),
check that all elements of these objects are equal. An exception is raised
at the first conflicting values.
transpose
可以把两个元组都变成二维数组:
In [64]: np.argwhere(x>5)
Out[64]:
array([[1, 2],
[2, 0],
[2, 1],
[2, 2]])
In [66]: np.transpose(test)
Out[66]:
array([[1, 2],
[2, 0],
[2, 1],
[2, 2]])
形状匹配的可以比较:
In [68]: np.argwhere(x>5)==np.transpose(test)
Out[68]:
array([[ True, True],
[ True, True],
[ True, True],
[ True, True]])
但是像这样比较数组时你必须非常小心。形状必须匹配,否则你会出错(就像你试图添加它们一样)。
我正在学习 python 和 numpy。在尝试预测结果以检查我的理解时,我遇到了这个:
import numpy as np
x = np.arange(1,10).reshape(3,3)
np.where(x>5) == (np.array([1, 2, 2, 2]), np.array([2, 0, 1, 2]))
以及相关的错误我现在明白为什么了。
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我想到了这个:
all(map(np.array_equal,
np.where(x>5), (np.array([1, 2, 2, 2]), np.array([2, 0, 1, 2])) ))
all([np.array_equal(v,t) for (v,t) in zip(
np.where(x>5), (np.array([1, 2, 2, 2]), np.array([2, 0, 1, 2]))) ])
all([(v==t).all() for (v,t) in zip(
np.where(x>5), (np.array([1, 2, 2, 2]), np.array([2, 0, 1, 2]))) ])
哪个有用,但在我看来有点乏味且难以阅读。是否有更多 pythonic 或 numpy 方法来测试元组中的数组?
你们非常接近。以下作品:
np.array_equal(np.where(x>5), (np.array([1, 2, 2, 2]), np.array([2, 0, 1, 2])))
np.array_equal
具有通过元组进行广播的能力。它还将数组和序列视为等效的,因此您可以根据需要使用:
np.array_equal(np.where(x>5), ([1, 2, 2, 2], [2, 0, 1, 2]))
还有一个testing
子模块(虽然我用的不多)
In [54]: import numpy.testing
In [59]: x = np.arange(1,10).reshape(3,3)
...: idx = np.where(x>5); test=(np.array([1, 2, 2, 2]), np.array([2, 0, 1, 2]))
通过:
In [60]: np.testing.assert_equal(idx,test)
一次失败:
In [61]: idx = np.where(x>4)
In [62]: np.testing.assert_equal(idx,test)
Traceback (most recent call last):
File "<ipython-input-62-a0326017ecb7>", line 1, in <module>
np.testing.assert_equal(idx,test)
File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 338, in assert_equal
assert_equal(actual[k], desired[k], f'item={k!r}\n{err_msg}',
File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 344, in assert_equal
return assert_array_equal(actual, desired, err_msg, verbose)
File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 932, in assert_array_equal
assert_array_compare(operator.__eq__, x, y, err_msg=err_msg,
File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 761, in assert_array_compare
raise AssertionError(msg)
AssertionError:
Arrays are not equal
item=0
(shapes (5,), (4,) mismatch)
x: array([1, 1, 2, 2, 2])
y: array([1, 2, 2, 2])
它的文档:
Raises an AssertionError if two objects are not equal.
Given two objects (scalars, lists, tuples, dictionaries or numpy arrays),
check that all elements of these objects are equal. An exception is raised
at the first conflicting values.
transpose
可以把两个元组都变成二维数组:
In [64]: np.argwhere(x>5)
Out[64]:
array([[1, 2],
[2, 0],
[2, 1],
[2, 2]])
In [66]: np.transpose(test)
Out[66]:
array([[1, 2],
[2, 0],
[2, 1],
[2, 2]])
形状匹配的可以比较:
In [68]: np.argwhere(x>5)==np.transpose(test)
Out[68]:
array([[ True, True],
[ True, True],
[ True, True],
[ True, True]])
但是像这样比较数组时你必须非常小心。形状必须匹配,否则你会出错(就像你试图添加它们一样)。