- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
np.set_printoptions允许自定义 numpy 数组的 pretty-print 。但是,对于不同的用例,我希望有不同的打印选项。
理想情况下,无需每次都重新定义整个选项即可完成此操作。我正在考虑使用本地范围,例如:
with np.set_printoptions(precision=3):
print my_numpy_array
但是,set_printoptions
似乎不支持 with
语句,因为会抛出错误 (AttributeError: __exit__
)。有什么方法可以在不创建自己的 pretty-print 类的情况下完成这项工作吗?也就是说,我知道我可以创建自己的上下文管理器:
class PrettyPrint():
def __init__(self, **options):
self.options = options
def __enter__(self):
self.back = np.get_printoptions()
np.set_printoptions(**self.options)
def __exit__(self, *args):
np.set_printoptions(**self.back)
并将其用作:
>>> print A
[ 0.29276529 -0.01866612 0.89768998]
>>> with PrettyPrint(precision=3):
print A
[ 0.293 -0.019 0.898]
但是,是否有比创建新类更直接(最好已经内置)的方法?
最佳答案
所以根据@unutbu给出的链接,而不是使用
with np.set_printoptions(precision=3):
print (my_numpy_array)
我们应该使用:
with np.printoptions(precision=3):
print my_numpy_array
适用于我的情况。如果事情似乎没有改变,请尝试操纵打印选项的其他参数,例如linewidth = 125,edgeitems = 7,threshold = 1000
等等。
关于python - numpy set_printoptions 的本地范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38050643/
np.set_printoptions允许自定义 numpy 数组的 pretty-print 。但是,对于不同的用例,我希望有不同的打印选项。 理想情况下,无需每次都重新定义整个选项即可完成此操作。
我正在尝试使用 numpy 打印带有额外空格的眼矩阵,例如输入“3 3”,我得到 [[ 1. 0. 0.] [ 0. 1. 0.] [ 0. 0. 1.]] 我的这个程序的代码如下
在 numpy 中有一个函数可以使数组打印得更漂亮。 set_printoptions(suppress = True) 换句话说,而不是这样: array([[ 0.00000000e+00,
我正在尝试使用问题 How to pretty-printing a numpy.array without scientific notation and with given precision?
我是一名优秀的程序员,十分优秀!