如何强制 ndarray 以正常方式显示而不是科学记数法?

发布于 2024-10-03 07:30:34 字数 1450 浏览 4 评论 0原文

我正在尝试在屏幕上打印 ndarray 。但python总是以科学记数法显示,我不喜欢。对于标量,我们可以使用

>>> print '%2.4f' %(7.47212470e-01)
0.7472

但是如何对 numpy.ndarray 执行此操作,如下所示:

[[  7.47212470e-01   3.71730070e-01   1.16736538e-01   1.22172891e-02]
 [  2.79279640e+00   1.31147152e+00   7.43946656e-02   3.08162255e-02]
 [  6.93657970e+00   3.14008688e+00   1.02851599e-01   3.96611266e-02]
 [  8.49295040e+00   3.94730094e+00   8.99398479e-02   7.60969188e-02]
 [  2.01849250e+01   8.62584092e+00   8.75722302e-02   6.17109672e-02]
 [  2.22570710e+01   1.00291292e+01   1.20918359e-01   1.07250131e-01]
 [  2.82496660e+01   1.27882133e+01   1.08438172e-01   1.58723714e-01]
 [  5.89170270e+01   2.55268510e+01   1.31990966e-01   1.61599514e-01]]

方法 .astype(float) 不会更改结果,并且 .round(4) 返回:

[[  7.47200000e-01   3.71700000e-01   1.16700000e-01   1.22000000e-02]
 [  2.79280000e+00   1.31150000e+00   7.44000000e-02   3.08000000e-02]
 [  6.93660000e+00   3.14010000e+00   1.02900000e-01   3.97000000e-02]
 [  8.49300000e+00   3.94730000e+00   8.99000000e-02   7.61000000e-02]
 [  2.01849000e+01   8.62580000e+00   8.76000000e-02   6.17000000e-02]
 [  2.22571000e+01   1.00291000e+01   1.20900000e-01   1.07300000e-01]
 [  2.82497000e+01   1.27882000e+01   1.08400000e-01   1.58700000e-01]
 [  5.89170000e+01   2.55269000e+01   1.32000000e-01   1.61600000e-01]]

我只想要 0.7472 0.3717 等。

I'm trying to print a ndarray on the screen. But python always shows it in scientific notation, which I don't like. For a scalar we can use

>>> print '%2.4f' %(7.47212470e-01)
0.7472

But how to do that for a numpy.ndarray like this :

[[  7.47212470e-01   3.71730070e-01   1.16736538e-01   1.22172891e-02]
 [  2.79279640e+00   1.31147152e+00   7.43946656e-02   3.08162255e-02]
 [  6.93657970e+00   3.14008688e+00   1.02851599e-01   3.96611266e-02]
 [  8.49295040e+00   3.94730094e+00   8.99398479e-02   7.60969188e-02]
 [  2.01849250e+01   8.62584092e+00   8.75722302e-02   6.17109672e-02]
 [  2.22570710e+01   1.00291292e+01   1.20918359e-01   1.07250131e-01]
 [  2.82496660e+01   1.27882133e+01   1.08438172e-01   1.58723714e-01]
 [  5.89170270e+01   2.55268510e+01   1.31990966e-01   1.61599514e-01]]

The method .astype(float) does not change the result, and .round(4) returns:

[[  7.47200000e-01   3.71700000e-01   1.16700000e-01   1.22000000e-02]
 [  2.79280000e+00   1.31150000e+00   7.44000000e-02   3.08000000e-02]
 [  6.93660000e+00   3.14010000e+00   1.02900000e-01   3.97000000e-02]
 [  8.49300000e+00   3.94730000e+00   8.99000000e-02   7.61000000e-02]
 [  2.01849000e+01   8.62580000e+00   8.76000000e-02   6.17000000e-02]
 [  2.22571000e+01   1.00291000e+01   1.20900000e-01   1.07300000e-01]
 [  2.82497000e+01   1.27882000e+01   1.08400000e-01   1.58700000e-01]
 [  5.89170000e+01   2.55269000e+01   1.32000000e-01   1.61600000e-01]]

I just simply want the 0.7472 0.3717 etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花期渐远 2024-10-10 07:30:34

numpy.set_string_function函数可用于更改数组的字符串表示形式。

您还可以使用 numpy.set_print_options< /a> 更改默认使用的精度并关闭以科学记数法表示的小数字的报告。

set_print_options 的示例中:

>>> np.set_printoptions(precision=4)
>>> print np.array([1.123456789])
[ 1.1235]

The numpy.set_string_function function can be used to change the string representation of arrays.

You can also use numpy.set_print_options to change the precision used by default and turn off reporting of small numbers in scientific notation.

From the examples for set_print_options:

>>> np.set_printoptions(precision=4)
>>> print np.array([1.123456789])
[ 1.1235]
究竟谁懂我的在乎 2024-10-10 07:30:34

我不知道 numpy 数组,但我在用 Python 做一个项目时遇到了同样的问题。

看一下提供的 Decimal 类 http://docs.python.org/library/decimal.html

我不知道 numpy 是否提供了它。

I don't know about the numpy arrays but I was simply facing the same problem while doing a project in Python.

Take a look at the Decimal class provided http://docs.python.org/library/decimal.html .

I don't know if it's provided in numpy though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文