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

Sergio Pascual sergiopr at fis.ucm.es
Mon Apr 11 18:31:58 EDT 2011

```What I have is some C++ functions that implement statistic functions.
What I need is some kind of ufunc where I can "plug" my functions. But
I doesn't seem to exist an ufunc that operates on a N-d array and
turns it into a number.

2011/4/12 Keith Goodman <kwgoodman at gmail.com>:
> 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])
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>

--
Sergio Pascual                        http://guaix.fis.ucm.es/~spr
gpg fingerprint: 5203 B42D 86A0 5649 410A F4AC A35F D465 F263 BCCC