np.set_printoptions()了解一下
需求1.需要以科学计数法输出:np.set_printoptions(suppress = True), 不需要以科学计数法输出:np.set_printoptions(suppress = False)
需求2.需要控制小数点后位数为N,np.set_printoptions(precision= N)
需求3.数组太长打印出来会太长,控制输出的元素的个数,np.set_printoptions(threshold = 1)
import numpy as np
a1 = np.array([0.0012345, 0.01234567891234, 111.1234567891111, 1])
np.set_printoptions(suppress=True)
print(a1)
np.set_printoptions(suppress=False)
print(a1)
np.set_printoptions(precision=3, suppress=True)
print(a1)
a1 = np.zeros(100)
np.set_printoptions(threshold = 1)
print(a1)
结果如下:
[ 0.001 0.012 111.123 1. ]
[1.234e-03 1.235e-02 1.111e+02 1.000e+00]
[ 0.001 0.012 111.123 1. ]
[0. 0. 0. ... 0. 0. 0.]
本文详细介绍了NumPy中np.set_printoptions()函数的使用方法,包括如何控制数组的科学计数法显示、设定小数点后保留的位数以及限制输出数组元素的数量,帮助读者更好地管理和展示大型数据集。

1358

被折叠的 条评论
为什么被折叠?



