[Numpy-discussion] Extending numpy statistics functions (like mean)

Keith Goodman kwgoodman at gmail.com
Mon Apr 11 18:03:16 EDT 2011


On Mon, Apr 11, 2011 at 2:36 PM, Sergio Pascual <sergio.pasra at gmail.com> wrote:
> Hi list.
>
> For mi application, I would like to implement some new statistics
> functions over numpy arrays, such as truncated mean. Ideally this new
> function should have the same arguments
> than numpy.mean: axis, dtype and out. Is there a way of writing this
> function that doesn't imply writing it in C from scratch?
>
> I have read the documentation, but as far a I see ufuncs convert a N
> dimensional array into another and generalized ufuncs require fixed
> dimensions. numpy mean converts a N dimensional array either in a
> number or a N - 1 dimensional array.

Here's a slow, brute force method:

>> a = np.arange(9).reshape(3,3)
>> a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>> idx = a > 6
>> b = a. copy()
>> b[idx] = 0
>> b
array([[0, 1, 2],
       [3, 4, 5],
       [6, 0, 0]])
>> 1.0 * b.sum(axis=0) / (~idx).sum(axis=0)
   array([ 3. ,  2.5,  3.5])



More information about the NumPy-Discussion mailing list