'Python Numpy - Treat really small numbers as zero
I want to calculate the Determinant of a Singular Matrix (which has a 0 determinant) with Numpy and when I print the determinant it shows a really small number (which is nearly zero = -7.09974814699e-30) but not zero itself...
when I try to print the determinant either with %s, %d or %f, sometimes it's zero, sometimes -0 and sometimes -7.09974814699e-30 .
Here's the Code :
import numpy as np
array = np.arange(16)
array = array.reshape(4, -1)
determinant = np.linalg.det(array)
print("Determinant is %s" % determinant)
print("Determinant is %d" % determinant)
print("Determinant is %f" % determinant)
Determinant is -7.09974814699e-30
Determinant is 0
Determinant is -0.000000
How can I make Numpy treat really small numbers such as -7.09974814699e-30 as zero and show zero to me. I also asked this question before, if you take a look at the matrix you see that it's filled with really small numbers but not zero while it should be a diagonal matrix with numbers on the diagonal and zeros elsewhere...
Thank you...
Solution 1:[1]
You can troncate less significant digits with np.round
. But it's better do keep accuracy for float arithmetic. What you have to control is just the format of the final output, with the str.format
function.
In [7]: a=rand(12,12)
In [8]: deta=det(a)
#0.0063854296972496311
In [10]: detar=det(a.round(3))
# 0.0063817871557592153
In [12]: '{:.5f}'.format(deta)
Out[12]: '0.00639'
In [13]: '{:.5f}'.format(detar)
Out[13]: '0.00638'
Last line shows a false result due to earlier optimisation.
Solution 2:[2]
>>> if np.abs(determinant) < 0.000001:
... determinant=0
...
>>> print determinant
0
In case of array you can do it using a single operation (see my answer to your other question: https://stackoverflow.com/a/36395905/5088142)
To set array elements that are less than eps to zero:
array[np.abs(array) < eps] = 0
Solution 3:[3]
I am using Python 2.7.11 |Anaconda custom (x86_64)| (default, Dec 6 2015, 18:57:58) IPython 4.0.3 -- An enhanced Interactive Python. and I got results as below,
In [6]: print("Determinant is %s" % determinant)
Determinant is 0.0
In [7]: print("Determinant is %d" % determinant)
Determinant is 0
In [8]: print("Determinant is %f" % determinant)
Determinant is 0.000000
I think if you update numpy it might fit it for you else you use below approach
In [9]: sam = 0.000000121
In [10]: sam
Out[10]: 1.21e-07
In [11]: print sam if sam > 0.00001 else 1
1
This does not exactly answer your question "How can I make Numpy treat really small numbers such as -7.09974814699e-30 as zero and show zero to me.", I think you do work around for this.
In general as it comes to floats/exponents, calculations beyond certain point carry minor error,.. for example by your e power -30 case. So if you are using high level floats or exponents it better you expect some error.
Solution 4:[4]
Use numpy.set_printoptions
and enable the suppress
option:
>>> import numpy as np
>>> np.set_printoptions(precision=3, suppress=True)
>>> array = np.arange(16)
>>> array = array.reshape(4, -1)
>>> determinant = np.linalg.det(array)
>>> print("Determinant is:", determinant)
Determinant is: 0.0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | B. M. |
Solution 2 | rafaelc |
Solution 3 | sam |
Solution 4 | xFullArgon |