code: def downsample_with_averaging(array, factor):
      
      factor = tuple(factor)
      output_shape = tuple(int(math.ceil(s / f)) for s, f in zip(array.shape, factor))
      temp = np.zeros(output_shape, dtype=np.float32)
      counts = np.zeros(output_shape, np.int)
      for offset in np.ndindex(factor):
          part = array[tuple(np.s_[o::f] for o, f in zip(offset, factor))]
          indexing_expr = tuple(np.s_[:s] for s in part.shape)
          temp[indexing_expr] += part
          counts[indexing_expr] += 1
      return np.cast[array.dtype](temp / counts)
docs: Downsample x by factor using averaging.
  
      @return: The downsampled array, of the same type as x.