如何打印具有 3 个小数位的 numpy 数组? [英] How to printing numpy array with 3 decimal places?

查看:36
本文介绍了如何打印具有 3 个小数位的 numpy 数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何打印小数点后 3 位的 numpy 数组?我试过 array.round(3) 但它一直像这样打印 6.000e-01.有没有一个选项可以让它像这样打印:6.000?

How can I print numpy array with 3 decimal places? I tried array.round(3) but it keeps printing like this 6.000e-01. Is there an option to make it print like this: 6.000?

我得到了一个解决方案 print ("%0.3f" % arr),但我想要一个全局解决方案,即每次我想检查数组内容时都不要这样做.

I got one solution as print ("%0.3f" % arr), but I want a global solution i.e. not doing that every time I want to check the array contents.

推荐答案

 np.set_printoptions(formatter={'float': lambda x: "{0:0.3f}".format(x)})

这将设置 numpy 使用这个 lambda 函数来格式化它打印出的每个浮点数.

This will set numpy to use this lambda function for formatting every float it prints out.

您可以定义格式的其他类型(来自函数的文档字符串)

other types you can define formatting for (from the docstring of the function)

    - 'bool'
    - 'int'
    - 'timedelta' : a `numpy.timedelta64`
    - 'datetime' : a `numpy.datetime64`
    - 'float'
    - 'longfloat' : 128-bit floats
    - 'complexfloat'
    - 'longcomplexfloat' : composed of two 128-bit floats
    - 'numpy_str' : types `numpy.string_` and `numpy.unicode_`
    - 'str' : all other strings

Other keys that can be used to set a group of types at once are::

    - 'all' : sets all types
    - 'int_kind' : sets 'int'
    - 'float_kind' : sets 'float' and 'longfloat'
    - 'complex_kind' : sets 'complexfloat' and 'longcomplexfloat'
    - 'str_kind' : sets 'str' and 'numpystr'

这篇关于如何打印具有 3 个小数位的 numpy 数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆