[SciPy-User] scipy.stats.nanmedian

Keith Goodman kwgoodman@gmail....
Fri Jan 22 16:19:10 CST 2010


On Fri, Jan 22, 2010 at 1:55 PM,  <josef.pktd@gmail.com> wrote:
> On Fri, Jan 22, 2010 at 4:52 PM, Keith Goodman <kwgoodman@gmail.com> wrote:
>> On Fri, Jan 22, 2010 at 1:44 PM, Keith Goodman <kwgoodman@gmail.com> wrote:
>>> On Fri, Jan 22, 2010 at 1:08 PM,  <josef.pktd@gmail.com> wrote:
>>>> .item() returns a python number not a numpy number
>>>>
>>>>>>> np.array([[[1]]]).item().__class__
>>>> <type 'int'>
>>>>>>> np.array([[[1]]]).flat[0].__class__
>>>> <type 'numpy.int32'>
>>>
>>> Good catch. Thanks. I'll update my local copy of nanmedian.
>>
>> Looks like we don't even need .tolist(), .item(), or [()]. This should
>> do the trick:
>>
>>>> np.float64(np.array(1))
>>   1.0
>>>> type(np.float64(np.array(1)))
>>   <type 'numpy.float64'>
>>
>> And what if the input is float32? Well, numpy turns that into float64
>> so nothing to worry about:
>>
>>>> x = np.array(1, dtype=np.float32)
>>>> m = np.median(x)
>>>> type(m)
>>   <type 'numpy.float64'>
>
> but it breaks complex
>>>> np.float64(np.array(1.j))
> 0.0
>
> I did one more shape correction in my version

Crap. So many corner cases.

After collecting all the input cases (all without NaNs) we could
extend your automated test by adding an outer loop over [nanmean,
nanmedian, nanstd] and make sure it gives the same results as the
numpy versions. It might be good to check for dtype too since 1 ==
1.0.


More information about the SciPy-User mailing list