Rounding to significant figures in numpy

后端 未结 13 1474
无人及你
无人及你 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:26

    I got quite frustrated after scouring the internet and not finding an answer for this, so I wrote my own piece of code. Hope this is what you're looking for

    import numpy as np
    from numpy import ma
    
    exp = np.floor(ma.log10(abs(X)).filled(0))
    ans = np.round(X*10**-exp, sigfigs-1) * 10**exp
    

    Just plug in your np array X and the required number of significant figures. Cheers!

提交回复
热议问题