Skip to content

Instantly share code, notes, and snippets.

@zhezh
Last active April 8, 2020 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhezh/6708fdfb1fded27d1fcc9a947020b572 to your computer and use it in GitHub Desktop.
Save zhezh/6708fdfb1fded27d1fcc9a947020b572 to your computer and use it in GitHub Desktop.
[numpy除零自动纠正] numpy np div 0 #numpy
# 自动处理除零错误
# numpy >= 1.7
a = np.array([-1, 0, 1, 2, 3], dtype=float)
b = np.array([ 0, 0, 0, 2, 2], dtype=float)
# If you don't pass `out` the indices where (b == 0) will be uninitialized!
c = np.divide(a, b, out=np.zeros_like(a), where=b!=0)
print(c)
# [ 0. 0. 0. 1. 1.5]
# 如果需要除0输出正无穷, 可以 out=np.full_like(a, fill_value=np.inf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment