在python中将数组的元素从科学计数法转换为十进制计数法 [英] convert elements of an array from scientific notation to decimal notation in python

查看:1459
本文介绍了在python中将数组的元素从科学计数法转换为十进制计数法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个numpy数组,其元素为科学格式,我想将它们转换为十进制格式.我的numpy数组看起来像这样:

I have a numpy array come of whose elements are in scientific format and I want to convert them into decimal format. My numpy array looks like this:

[array([ 93495052.96955582,  98555123.06146193])]
[array([  1.00097681e+09,   9.98276347e+08])]
[array([  6.86812785e+09,   6.90391125e+09])]
[array([  7.75127468e+08,   8.02369833e+08])]

这是在我的代码中使用以下行形成的:

and this is formed using this line in my code:

list1.append(np.array(regr.predict(data),dtype = np.float))

现在,我要将list1中的元素从科学格式转换为十进制格式.我四处寻找解决方案,发现print format(0.00001357, 'f')将数字从科学格式转换为十进制格式,但是如何使用它来转换数组的元素?

Now I want to convert elements in list1 from scientific format to decimal format. I looked around for some solution and found out that print format(0.00001357, 'f') converts numbers from scientific format to decimal format but how do I use it to convert elements of my array?

推荐答案

首先,正如一些人所指出的那样,数字的显示方式和存储方式之间存在很大差异.

First off, as several people have noted, there's a very large difference between how the numbers are displayed and how they're stored.

如果要将它们转换为字符串,请使用'{:f}'.format(x)(或等效的%).

If you want to convert them to strings, then use '{:f}'.format(x) (or the % equivalent).

但是,听起来好像您只是希望在进行交互式工作(或通过print语句)时以不同的方式显示数字.

However, it sounds like you're only wanting the numbers to be displayed differently when you're working interactively (or through a print statement).

交互显示numpy数组的方式由 numpy.set_printoptions .

The way that numpy arrays are displayed interactively is controlled by numpy.set_printoptions.

请注意,这不会将数字转换为字符串或以任何方式更改它们.

作为一个简单的例子:

In [1]: import numpy as np

In [2]: x = 1e9 * np.random.random(5)

In [3]: x
Out[3]:
array([  4.96602724e+08,   5.42486095e+08,   4.74495681e+08,
         7.37709684e+07,   9.75410927e+08])

In [4]: np.set_printoptions(formatter={'float_kind':'{:f}'.format})

In [5]: x
Out[5]:
array([496602723.824146, 542486095.316912, 474495680.688025,
       73770968.413642, 975410926.873148])

我们只更改了numpy显示数字的方式.它们仍在漂浮.

We've only changed how numpy will display the numbers. They're still floats.

我们可以对它们进行数学运算,它们的行为类似于数字:

We can operate on them mathematically, and they'll behave like numbers:

In [6]: x[0]
Out[6]: 496602723.82414573

In [7]: x[0] * 2
Out[7]: 993205447.64829147

转换为string s

现在让我们说我们已经将它们转换为字符串列表:

Converting to strings

Now let's say we had converted them to a list of strings:

In [1]: import numpy as np

In [2]: x = 1e9 * np.random.random(5)

In [3]: x
Out[3]:
array([  2.56619581e+08,   2.55721261e+08,   3.36984986e+08,
         2.67541556e+08,   9.01048842e+08])

In [4]: x = ['{:f}'.format(item) for item in x]

In [5]: x
Out[5]:
['256619580.697790',
 '255721261.271977',
 '336984986.430552',
 '267541556.373619',
 '901048842.193849']

现在它们是stringlist.如果我们对它们进行数学运算,它们的行为将类似于字符串,而不是数字:

Now they're a list of strings. If we operate on them mathematically, they'll behave like strings, not numbers:

In [6]: x[0] * 2
Out[6]: '256619580.697790256619580.697790'

控制如何使用savetxt

保存numpy数组

最后,如果您使用的是 numpy.savetxt ,并希望控制如何将数据输出到磁盘,请考虑使用fmt参数,而不是将数组的元素手动转换为字符串.

Controlling how numpy arrays are saved with savetxt

Finally, if you're using numpy.savetxt, and would like to control how the data is output to disk, consider using the fmt parameter instead of manually converting elements of the array to strings.

例如,如果我们要这样做:

For example, if we were to do:

np.savetxt('temp.txt', x)

默认情况下,如果数组的ascii表示形式更紧凑,则会使用科学计数法:

By default, the ascii representation of the array would use scientific notation if it is more compact:

8.702970453168644905e+08
9.991634082796489000e+08
5.032002956810175180e+08
2.382398232565869987e+08
1.868727085152311921e+08

但是,我们可以使用fmt进行控制.请注意,它期望使用旧式" %格式化字符串:

However, we can control that using fmt. Note that it expects the "old-style" % formatting strings:

np.savetxt('temp2.txt', x, fmt='%f')

我们将得到:

870297045.316864
999163408.279649
503200295.681018
238239823.256587
186872708.515231

这篇关于在python中将数组的元素从科学计数法转换为十进制计数法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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