[Scipy-tickets] [SciPy] #1777: ttest_ind not working for masked arrays with ndim>1

SciPy Trac scipy-tickets@scipy....
Tue Nov 20 15:24:04 CST 2012


#1777: ttest_ind not working for masked arrays with ndim>1
-------------------------+--------------------------------------------------
 Reporter:  bulli92      |       Owner:  somebody   
     Type:  defect       |      Status:  new        
 Priority:  normal       |   Milestone:  Unscheduled
Component:  scipy.stats  |     Version:  0.11.0     
 Keywords:               |  
-------------------------+--------------------------------------------------
 The ttest_ind function does not work with any arrays with a dimension > 1.
 If using for instance a 3-D masked array, the following error occurs:

     744     (n1, n2) = (a.count(axis), b.count(axis))
     745     df = n1+n2-2
 --> 746     svar = ((n1-1)*v1+(n2-1)*v2) / float(df)
     747     svar == 0
     748     t = (x1-x2)/ma.sqrt(svar*(1.0/n1 + 1.0/n2))  # N-D COMPUTATION
 HERE!!!!!!

 TypeError: only length-1 arrays can be converted to Python scalars


 which is from
 https://github.com/scipy/scipy/blob/master/scipy/stats/mstats_basic.py#L746

 The problem is the float function, used in L746 and L750

 I propose a fix, that is simply multiplying the degree of freedom (df)
 with 1.


 *** FIXED CODE:


 {{{
     def ttest_ind(a, b, axis=0):
         a, b, axis = _chk2_asarray(a, b, axis)
         (x1, x2) = (a.mean(axis), b.mean(axis))
         (v1, v2) = (a.var(axis=axis, ddof=1), b.var(axis=axis, ddof=1))
         (n1, n2) = (a.count(axis), b.count(axis))
         df = n1+n2-2
         svar = ((n1-1)*v1+(n2-1)*v2) / (df*1.)
         svar == 0
         t = (x1-x2)/ma.sqrt(svar*(1.0/n1 + 1.0/n2))  # N-D COMPUTATION
 HERE!!!!!!
         t = ma.filled(t, 1)           # replace NaN t-values with 1.0
         probs = betai(0.5*df,0.5,(df*1.)/(df+t*t)).reshape(t.shape)
         return t, probs.squeeze()
 }}}




 Cheers,
    Alex

-- 
Ticket URL: <http://projects.scipy.org/scipy/ticket/1777>
SciPy <http://www.scipy.org>
SciPy is open-source software for mathematics, science, and engineering.


More information about the Scipy-tickets mailing list