Last active
April 8, 2020 23:39
-
-
Save zhezh/6708fdfb1fded27d1fcc9a947020b572 to your computer and use it in GitHub Desktop.
[numpy除零自动纠正] numpy np div 0 #numpy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 自动处理除零错误 | |
# 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