使用numpy的小驚喜

 

今天使用 numpy.true_divide 發現個有趣的事情,python

下面的代碼1八、19行若是去掉,就會報下面的  RuntimeWarningapp

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


RuntimeWarningide

/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有一個自動檢查的過程?this

相關文章
相關標籤/搜索