默认numpy会在某些情况触发科学计数法显示
scientific notation is used when absolute value of the smallest number is < 1e-4 or the ratio of the maximum absolute value to the minimum is > 1e3
这会导致0显示为非常小的浮点数,影响数据查看
关闭这一触发条件而始终使用定点表示法显示即可
>>> import numpy as np
>>> from numpy.linalg import inv
# 5x5正态分布数组
>>> x = np.random.randn(5,5)
# 与自己的逆矩阵求内积
>>> x.dot(inv(x))
array([[ 1.00000000e+00, -7.21523806e-16, 8.13703309e-17,
-2.98292313e-15, -6.07647342e-16],
[-1.97275625e-16, 1.00000000e+00, -8.19107942e-17,
-2.50921616e-15, 2.68372512e-17],
[-2.22533790e-16, 1.95190131e-15, 1.00000000e+00,
1.70492054e-15, -9.87309893e-16],
[-1.08790555e-15, -3.36377007e-15, -1.11605663e-16,
1.00000000e+00, -5.49392659e-15],
[-1.88292258e-16, -8.68506657e-17, -5.91007503e-17,
-2.03391053e-16, 1.00000000e+00]])
# 设置以定点表示法显示小数
>>> np.set_printoptions(suppress=True)
>>> x.dot(inv(x))
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.]])
参考:https://numpy.org/doc/stable/reference/generated/numpy.set_printoptions.html