import numpy as np
a = [1, 2, 4, 7, 8]
b = []
print(np.mean(a))
print(np.mean(b))
Output
4.4
nan
/usr/local/lib/python3.11/dist-packages/numpy/_core/fromnumeric.py:3596: RuntimeWarning: Mean of empty slice.
return _methods._mean(a, axis=axis, dtype=dtype,
/usr/local/lib/python3.11/dist-packages/numpy/_core/_methods.py:138: RuntimeWarning: invalid value encountered in scalar divide
ret = ret.dtype.type(ret / rcount)
Explanation:
- np.mean(a) computes the average of valid numeric values in a.
- np.mean(b) triggers a RuntimeWarning and returns nan as b is empty.
- No input validation is present, so the warning occurs for b.
Before computing the mean, ensure input arrays are not empty. Calculating the mean of an empty list causes a runtime warning. A simple if check prevents this and ensures valid results.
Python
import numpy as np
a = [1, 2, 4, 7, 8]
b = []
if a and b:
print(np.mean(a))
print(np.mean(b))
else:
print("Please enter valid arrays")
OutputPlease enter valid arrays
Explanation: This code checks if both a and b are non-empty before computing their means. Since b is empty, it prints a message to avoid invalid operations and runtime warnings.
Use numpy.nanmean()
When data contains NaN values, mean() may produce invalid results. numpy.nanmean() ignores NaNs and computes the mean of valid elements, making it a safe choice for handling incomplete data.
Python
import numpy as np
a = [1, 2, np.nan, 7, 8]
res = np.nanmean(a)
print(res)
Explanation: np.nanmean() compute the mean of a, ignoring NaN values. This ensures valid results even with incomplete data. Here, the mean is calculated from non-NaN elements only.
Use scipy.special.logsumexp
scipy.special.logsumexp() computes the log of the sum of exponentials of input elements in a numerically stable way. logsumexp([x]) - logsumexp([y]) effectively calculates log(exp(x)/exp(y)), which simplifies to x - y, avoiding overflow issues with large exponentials.
Python
import numpy as np
from scipy.special import logsumexp
x = 900
y = 711
res = logsumexp([x]) - logsumexp([y])
print(res)
Output
189.0
Explanation: logsumexp() compute log(exp(x)) - log(exp(y)), which simplifies to x - y. It ensures numerical stability when dealing with large exponentials.
Using np.clip()
When calculations yield extreme values, np.clip() restricts them within a defined range (min, max). This ensures numerical stability and prevents warnings or errors from overflow or underflow, making computations more reliable.
Python
import numpy as np
x = np.array([1e300, 1e308, 1e309])
res = np.clip(x, -1e308, 1e308)
print(res)
Output[1.e+300 1.e+308 1.e+308]
Explanation: np.clip() to constrain values of x within the range [-1e308, 1e308]. This prevents overflow by capping values that exceed this range, ensuring numerical stability.