[Numpy-discussion] Matrix and var method

Zachary Pincus zpincus at stanford.edu
Sat Apr 22 20:48:00 CDT 2006

Hi folks,

I just ran across an error with numpy.matrix types: the var() method  
does not seem to work! (I've tried all sorts of permutations on the  
matrix shape, and the axis parameter to var; nothing works.)

Perhaps this has already been fixed -- I haven't updated my numpy in  
a week or so. If so, sorry; if not, I hope this helps.


In [1]: import numpy
In [2]: numpy.__version__
Out[2]: ''

In [3]: numpy.matrix([[1,2,3], [1,2,3]]).var()
exceptions.ValueError                                Traceback (most  
recent call last)

/Users/zpincus/<ipython console>

packages/numpy/core/defmatrix.py in __mul__(self, other)
     147         if isinstance(other, N.ndarray) or N.isscalar(other)  
or \
     148                not hasattr(other, '__rmul__'):
--> 149             return N.dot(self, other)
     150         else:
     151             return NotImplemented

ValueError: matrices are not aligned

In [4]: numpy.array([[1,2,3], [1,2,3]]).var()
Out[4]: 0.80000000000000004

More information about the Numpy-discussion mailing list