Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

median of np.matrix is broken #4301

Open
argriffing opened this issue Feb 16, 2014 · 5 comments
Open

median of np.matrix is broken #4301

argriffing opened this issue Feb 16, 2014 · 5 comments

Comments

@argriffing
Copy link
Contributor

>>> import numpy as np                                                          
>>> a = np.matrix([[0, 1, 2]])                                                  
>>> np.mean(a)                                                                  
1.0
>>> np.median(a)                                                                
Traceback (most recent call last):
[...]
/numpy/core/fromnumeric.py", line 619, in partition
    a.partition(kth, axis=axis, kind=kind, order=order)
ValueError: kth(=1) out of bounds (1)
@empeeu
Copy link
Contributor

empeeu commented Feb 17, 2014

This actually seems to be a problem with partition.

>>> np.median(a, axis=0)
matrix([[ 0.,  1.,  2.]])
>>> np.median(a, axis=1)
matrix([[ 1.]])
>>> np.partition(a, 1, axis=1)
matrix([[0, 1, 2]])
>>> np.partition(a, 1, axis=None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 619, in partition
    a.partition(kth, axis=axis, kind=kind, order=order)
ValueError: kth(=1) out of bounds (1)
>>> np.partition(np.array(a), kth, axis=None)
array([0, 1, 2])

median just so happens to use partition as part of its calculation.

@charris
Copy link
Member

charris commented May 6, 2014

@juliantaylor I'm going to remove this as a blocker, but a fix would be nice. We can backport it later.

@charris charris modified the milestones: 1.10 blockers, 1.9 blockers May 6, 2014
@juliantaylor
Copy link
Contributor

I don't really know how to fix it, should matrix not override flatten to not flatten, or should squeeze be overriden to really flatten?

@charris charris modified the milestones: 1.11 blockers, 1.10 blockers Jun 21, 2015
@charris charris modified the milestones: 1.12.0 release, 1.11.0 blockers Jan 21, 2016
@charris
Copy link
Member

charris commented Jan 1, 2017

Heh, still present in 1.12.

@dipsivenkatesh
Copy link

Reproducing Bug:

This bug seems to exist when we compute median for NumPy matrix, and that too when we don't specify the axis.

a = np.matrix([[0,1,2],[3,4,5]])
>>> np.median(a)
matrix([[0., 1., 2., 3., 4., 5.]])
>>> np.median(a,axis = 1)
matrix([[1.],
        [4.]])
>>> np.median(a,axis = 0)
matrix([[1.5, 2.5, 3.5]])

for numpy array, it flattens the array and computes the median but for the matrix it's not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Matrix deprecation
  
Poor behaviour of np.matrix
Development

Successfully merging a pull request may close this issue.

8 participants