Rounding to significant figures in numpy

后端 未结 13 1462
无人及你
无人及你 2020-12-09 16:00

I\'ve tried searching this and can\'t find a satisfactory answer.

I want to take a list/array of numbers and round them all to n significant figures. I have written

13条回答
  •  时光取名叫无心
    2020-12-09 16:17

    Is numpy.set_printoptions what you're looking for?

    import numpy as np
    np.set_printoptions(precision=2)
    print np.array([  0.0, -1.2366e22, 1.2544444e-15, 0.001222 ])
    

    Gives:

    [  0.00e+00  -1.24e+22   1.25e-15   1.22e-03]
    

    Edit:

    numpy.around appears to solve aspects of this problem if you're trying to transform the data. However, it doesn't do what you want in cases where the exponent is negative.

提交回复
热议问题