numpy.where : how to delay evaluating parameters?
问题 I am using numpy.where , and I was wondering if there was a simply way to avoid calling the unused parameter. Example: import numpy as np z = np.array([-2, -1, 0, 1, -2]) np.where(z!=0, 1/z, 1) returns: array([-0.5, -1. , 1. , 1. , -0.5]) but I get a divide by zero warning because when z was 0, the code still evaluates 1/z even though it doesn't use it. 回答1: You can also turn off the warning and turn it back on after you are done using the context manager errstate: with np.errstate(divide=