[SciPy-user] skew and kurtosis return different types

Stefan van der Walt stefan at sun.ac.za
Wed Oct 10 20:29:40 EDT 2007


On Wed, Oct 10, 2007 at 11:20:33AM -0700, Jarrod Millman wrote:
> We could fix this by either having the functions subtract zero or make
> a method call:
> >>> scipy.stats.skew(a)
> array(0.0)
> >>> scipy.stats.skew(a) - 0
> 0.0
> >>> scipy.stats.skew(a).tolist()
> 0.0
> >>> scipy.stats.skew(a).item()
> 0.0
> 
> Does it make any difference if we do it one way or another?  Are there
> any situations where returning an array with 1 element is importantly
> different than returning a float?  If we want all the moments to
> return values of the same type, my preference would be to use the
> .item method.

The .item method won't work in cases where more than one value is
returned, e.g.

scipy.stats.skew(numpy.random.random((3,3)))

.tolist() would work, but then we don't return numpy arrays any more.

Another alternative is to be explicit and write a helper function:

def unpack_if_scalar:
    if x.size == 1:
       return x.item()
    else:
       return x

This also accounts for the case where a 1-element array is returned.

Kurtosis exhibits the same problem, btw:

 scipy.stats.kurtosis(N.array([1,2,3]),fisher=False)

Regards
Stéfan



More information about the SciPy-User mailing list