Regenerating an array based on tolerance value in Python
Question:
I have an array Sum
with some elements being very small. I want to print this array based on a tolerance value tol
such that if any element is less than this, it becomes zero.
import numpy as np
tol = 1e-12
Sum = np.array([ 7.37551766e-08, -4.99999992e-17, 0.00000000e+00, 5.00000028e-16,
-4.99999984e-17, 0.00000000e+00, -4.83360759e-07])
for i in range(0,len(Sum)):
if (abs(Sum[i]) < tol):
Sum[i]==0
print(Sum)
The expected output is
array([ 7.37551766e-08, 0, 0.00000000e+00, 0,
0, 0.00000000e+00, -4.83360759e-07])
Answers:
Two easy solutions:
Solution 1
You can apply a simple mask:
Sum[np.abs(Sum) < tol] = 0.0
This is a simple one-liner, ideal if you only want to zero-out some elements.
Solution 2
You could map over the array with a function that returns 0 when input is under threshold:
def f(x, threshold):
return x if x>threshold else 0.0
tol = 1e-12
Sum = np.vectorize(lambda x: f(x, tol))(Sum)
The advantage of this solution is that you can easily do more complex operations on your elements by modifying the function f(x, threshold)
. The drawback is that is a little bit longer.
Time efficiency
Using a simple boolean mask is way more efficient, see the figure and note the logarithmic axes.
How about a applying a simple boolean mask:
import numpy as np
np.random.seed(42)
arr = np.round(np.random.random(10), 3)
tol = 0.1
print(arr)
# [0.375 0.951 0.732 0.599 0.156 0.156 0.058 0.866 0.601 0.708]
# set to 0 values below `tol`
arr[np.abs(arr) < tol] = 0.0
print(arr)
# [0.375 0.951 0.732 0.599 0.156 0.156 0. 0.866 0.601 0.708]
I have an array Sum
with some elements being very small. I want to print this array based on a tolerance value tol
such that if any element is less than this, it becomes zero.
import numpy as np
tol = 1e-12
Sum = np.array([ 7.37551766e-08, -4.99999992e-17, 0.00000000e+00, 5.00000028e-16,
-4.99999984e-17, 0.00000000e+00, -4.83360759e-07])
for i in range(0,len(Sum)):
if (abs(Sum[i]) < tol):
Sum[i]==0
print(Sum)
The expected output is
array([ 7.37551766e-08, 0, 0.00000000e+00, 0,
0, 0.00000000e+00, -4.83360759e-07])
Two easy solutions:
Solution 1
You can apply a simple mask:
Sum[np.abs(Sum) < tol] = 0.0
This is a simple one-liner, ideal if you only want to zero-out some elements.
Solution 2
You could map over the array with a function that returns 0 when input is under threshold:
def f(x, threshold):
return x if x>threshold else 0.0
tol = 1e-12
Sum = np.vectorize(lambda x: f(x, tol))(Sum)
The advantage of this solution is that you can easily do more complex operations on your elements by modifying the function f(x, threshold)
. The drawback is that is a little bit longer.
Time efficiency
Using a simple boolean mask is way more efficient, see the figure and note the logarithmic axes.
How about a applying a simple boolean mask:
import numpy as np
np.random.seed(42)
arr = np.round(np.random.random(10), 3)
tol = 0.1
print(arr)
# [0.375 0.951 0.732 0.599 0.156 0.156 0.058 0.866 0.601 0.708]
# set to 0 values below `tol`
arr[np.abs(arr) < tol] = 0.0
print(arr)
# [0.375 0.951 0.732 0.599 0.156 0.156 0. 0.866 0.601 0.708]