擅长:python、mysql、java
<p>你的问题并不是一个单一的答案,而是一些你可以用来构建模块的技巧。您可能会发现另一个有用的方法:</p>
<p>所有numpy ufunc都有一个<a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.ufunc.reduceat.html" rel="noreferrer">^{<cd1>}</a>方法,您可以利用它进行一些计算:</p>
<pre><code>>>> a = np.arange(100)
>>> breaks = np.linspace(0, 100, 11, dtype=np.intp)
>>> counts = np.diff(breaks)
>>> counts
array([10, 10, 10, 10, 10, 10, 10, 10, 10, 10])
>>> sums = np.add.reduceat(a, breaks[:-1], dtype=np.float)
>>> sums
array([ 45., 145., 245., 345., 445., 545., 645., 745., 845., 945.])
>>> sums / counts # i.e. the mean
array([ 4.5, 14.5, 24.5, 34.5, 44.5, 54.5, 64.5, 74.5, 84.5, 94.5])
</code></pre>