相关文章推荐
活泼的黄花菜  ·  MCP Server Boot ...·  4 月前    · 
不爱学习的番茄  ·  websocket ...·  2 年前    · 

Handling "RuntimeWarning: invalid value encountered in double_scalars" means addressing situations where operations like division or mean calculation involve invalid inputs such as NaN, infinite values or empty arrays. Example:

Python
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.

Using input validation

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")

Output
Please 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)

Output
4.5

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.