[SciPy-dev] scipy.stats._chk_asarray

Robert Kern robert.kern@gmail....
Tue Jun 2 16:03:29 CDT 2009


On Tue, Jun 2, 2009 at 15:56, ctw <lists.20.chth@xoxy.net> wrote:
> I just wanted to give you a heads up that I came across a bug in
> scipy.stats._chk_asarray.
> I have updated the corresponding review page (
> http://projects.scipy.org/scipy/ticket/44 ) with the following
> comment:
>
> _chk_asarray does not respect the class of the input. If the input is
> a subclass of numpy.ndarray _chk_asarray will transform it into an
> ndarray. This can lead to undesirable consequences in functions that
> call _chk_asarray (e.g., scipy.stats.nanmean).
>
> Simple example:
>>>> tst = matrix(range(4))
>>>> scipy.stats.nanmean(tst)
>  array([0., 1., 2., 3.])
>>>> np.mean(tst,0)
>  matrix([[0., 1., 2., 3.]])
>
> The problem is the call to np.asarray() which should only be made for
> input that is not an ndarray instance. I just committed a
> corresponding fix via SVN.

Please revert that. Some of those functions rely on the result of
_chk_asarray() being a real ndarray. For example, nanstd() uses an
exponent, which will be misinterpreted by a matrix object:

In [1]: from scipy.stats import nanstd

In [2]: m = matrix(range(4))

In [3]: nanstd(m)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/Users/rkern/<ipython console> in <module>()

/Users/rkern/svn/scipy/scipy/stats/stats.py in nanstd(x, axis, bias)
    312         d = d.transpose(shape)
    313     else:
--> 314         d = (x-m1)**2.0
    315     m2 = np.sum(d,axis)-(m1*m1)*Nnan
    316     if bias:

/Users/rkern/svn/numpy/numpy/core/defmatrix.pyc in __pow__(self, other)
    301
    302     def __pow__(self, other):
--> 303         return matrix_power(self, other)
    304
    305     def __ipow__(self, other):

/Users/rkern/svn/numpy/numpy/core/defmatrix.pyc in matrix_power(M, n)
    117     """
    118     if len(M.shape) != 2 or M.shape[0] != M.shape[1]:
--> 119         raise ValueError("input must be a square array")
    120     if not issubdtype(type(n),int):
    121         raise TypeError("exponent must be an integer")

ValueError: input must be a square array

In [4]: nanstd(range(4))
Out[4]: 1.2909944487358056


-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the Scipy-dev mailing list