今天使用 numpy.true_divide 发现个有趣的事情,
下面的代码18、19行如果去掉,就会报下面的 RuntimeWarning
def multivalue_divide(timeseries): # Get the last one day
timeseries = [ item for item in timeseries if item[0]>(timeseries[-1][0]-86400) ] series = np.array([x[1] for x in timeseries]) window = 3
threshold = 0.5
remainder = series.size % window
if 0 != remainder:
series = series[remainder:]
series = series.reshape((series.size/window,window))
sum_arr = np.sum(series,axis=1) # The test statistic is infinite when the median is zero,
# so it becomes super sensitive. We play it safe and skip when this happens.
if 0 == sum_arr[-2]:
return False divide = np.true_divide(sum_arr[-1],sum_arr[-2])
return True if np.abs(divide-1) > threshold else False
RuntimeWarning
/data/skyline/src/analyzer/algorithms.py:320: RuntimeWarning: invalid value encountered in true_divide
divide = np.true_divide(sum_arr[-1],sum_arr[-2])
加上对除数是否为0的判断就可以了,是不是说明numpy有一个自动检查的过程?