Python 在 numpy 中将非常低的值设置为零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14419290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
set very low values to zero in numpy
提问by Eoin Murray
In numpy I have an array like
在 numpy 我有一个像
[0 + 0.5j, 0.25 + 1.2352444e-24j, 0.25+ 0j, 2.46519033e-32 + 0j]
what is the fastest and easiest way to set the super low value to zero to get
将超低值设置为零的最快和最简单的方法是什么
[0 + 0.5j, 0.25 + 0j, 0.25+ 0j, 0 + 0j]
efficiency is not the paramount.
效率不是最重要的。
采纳答案by DSM
Hmmm. I'm not super-happy with it, but this seems to work:
嗯。我对它不太满意,但这似乎有效:
>>> a = np.array([0 + 0.5j, 0.25 + 1.2352444e-24j, 0.25+ 0j, 2.46519033e-32 + 0j])
>>> a
array([ 0.00000000e+00 +5.00000000e-01j,
2.50000000e-01 +1.23524440e-24j,
2.50000000e-01 +0.00000000e+00j, 2.46519033e-32 +0.00000000e+00j])
>>> tol = 1e-16
>>> a.real[abs(a.real) < tol] = 0.0
>>> a.imag[abs(a.imag) < tol] = 0.0
>>> a
array([ 0.00+0.5j, 0.25+0.j , 0.25+0.j , 0.00+0.j ])
and you can choose your tolerance as your problem requires. I usually use an order of magnitude or so higher than
您可以根据您的问题选择您的容忍度。我通常使用一个数量级左右
>>> np.finfo(np.float).eps
2.2204460492503131e-16
but it's problem-dependent.
但它取决于问题。
回答by jfs
To set elements that are less than epsto zero:
将小于eps零的元素设置为:
a[np.abs(a) < eps] = 0
There could be a specialized function that is more efficient.
可能有一个更有效的专门功能。
If you want to suppress printing of small floats instead:
如果你想禁止打印小浮点数:
import numpy as np
a = np.array([1+1e-10j])
print a # -> [ 1. +1.00000000e-10j]
np.set_printoptions(suppress=True)
print a # -> [ 1.+0.j]
回答by Micka?l
You can also use the numpy.isclosemethod:
您还可以使用以下numpy.isclose方法:
>>> np.isclose([1e10,1e-7], [1.00001e10,1e-8])
array([True, False])
By asking if it is close to zero, it should work:
通过询问它是否接近于零,它应该可以工作:
>>> np.isclose([1e10,0], [1.00001e-10,0])
array([False, True])
You can customise the atol(absolute tolerance, defaults to 1e-08) and the rtol(relative tolerance, defaults to 1e-05) parameters. You can then set rtol=0to only use the absolute tolerance.
You can customise the atol(absolute tolerance, defaults to 1e-08) and the rtol(relative tolerance, defaults to 1e-05) parameters. You can then set rtol=0to only use the absolute tolerance.
回答by ttq
If all numbers have small imaginary parts, and you only want to suppress these then you can use
If all numbers have small imaginary parts, and you only want to suppress these then you can use
b=np.real_if_close(a)
Otherwise the suggestion by DSM is the way forward, i.e.
Otherwise the suggestion by DSM is the way forward, i.e.
a.real[abs(a.real)<1e-13]=0
a.imag[abs(a.imag)<1e-13]=0
回答by restrepo
By using the method round(n) of the array
By using the method round(n) of the array
np.array( [0 + 0.5j, 0.25 + 1.2352444e-24j,
0.25+ 0j, 2.46519033e-32 + 0j] ).round(23)
回答by user10941415
diff = x-y
diff[diff>1.e-16]
Out[93]:
array([], dtype=float64)
diff[diff>1.e-18]
array([1.73472348e-18, 1.73472348e-18, 1.73472348e-18, 1.73472348e-18,
1.73472348e-18, 1.73472348e-18, 1.73472348e-18, 1.73472348e-18,
1.73472348e-18, 1.73472348e-18, 1.73472348e-18, 1.73472348e-18,
1.73472348e-18, 1.73472348e-18, 1.73472348e-18, 1.73472348e-18,
1.73472348e-18])

