Nik*_*kko 3 python arrays numpy matrix
我有两个来自文本文件的数组。通过观察,它看起来完全一样。然而,当我测试两个数组的等价性时,它们失败了 - 元素明智、形状明智等。我使用了此处回答的 numpy 测试。
这是两个矩阵。
import numpy as np
class TextMatrixAssertions(object):
def assertArrayEqual(self, dataX, dataY):
x = np.loadtxt(dataX)
y = np.loadtxt(dataY)
if not np.array_equal(x, y):
raise Exception("array_equal fail.")
if not np.array_equiv(x, y):
raise Exception("array_equiv fail.")
if not np.allclose(x, y):
raise Exception("allclose fail.")
dataX = "MyMatrix.txt"
dataY = "MyMatrix2.txt"
test = TextMatrixAssertions()
test.assertArrayEqual(dataX, dataY)
Run Code Online (Sandbox Code Playgroud)
我想知道两个阵列之间是否真的存在差异,如果没有,是什么导致了失败。
它们并不相等,它们有 54 个不同的元素。
np.sum(x!=y)
54
Run Code Online (Sandbox Code Playgroud)
要查找哪些元素不同,您可以执行以下操作:
np.where(x!=y)
(array([ 1, 5, 7, 11, 19, 24, 32, 48, 82, 92, 97, 111, 114,
119, 128, 137, 138, 146, 153, 154, 162, 165, 170, 186, 188, 204,
215, 246, 256, 276, 294, 300, 305, 316, 318, 333, 360, 361, 390,
419, 420, 421, 423, 428, 429, 429, 439, 448, 460, 465, 467, 471,
474, 487]),
array([18, 18, 18, 17, 17, 16, 15, 12, 8, 6, 5, 4, 3, 3, 2, 1, 1,
26, 0, 25, 24, 24, 24, 23, 22, 20, 20, 17, 16, 14, 11, 11, 11, 10,
10, 9, 7, 7, 5, 1, 1, 1, 26, 1, 0, 25, 23, 21, 19, 18, 18,
17, 17, 14]))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8797 次 |
| 最近记录: |