indexing bug in numpy r2694

x = asmatrix(rand(3,2))
y = asmatrix(rand(3,1))
y
matrix([[ 0.77952062], [ 0.97110465], [ 0.77450218]])
idx = where(y > 0.5)[0]
idx matrix([[0, 1, 2]])
x[idx,:]
matrix([[ 0.24837887, 0.52988253], [ 0.28661085, 0.43053076], [ 0.05360893, 0.22668509]]) So far everything works as it should. Now the problem:
y[idx,:]
exceptions.ValueError Traceback (most recent call last) /usr/local/lib/python2.4/site-packages/numpy/core/defmatrix.py in __getitem__(self, index) 120 121 def __getitem__(self, index): --> 122 out = N.ndarray.__getitem__(self, index) 123 # Need to swap if slice is on first index 124 retscal = False /usr/local/lib/python2.4/site-packages/numpy/core/defmatrix.py in __array_finalize__(self, obj) 116 self.shape = (1,1) 117 elif ndim == 1: --> 118 self.shape = (1,self.shape[0]) 119 return 120 ValueError: total size of new array must be unchanged And, on a related note, shouldn't this be a column vector?
x[idx,0] matrix([[ 0.24837887, 0.28661085, 0.05360893]])

i don't know why 'where' is returning matrices. if you use:
idx = where(y.A > 0.5)[0]
everything will work fine (I guess) pau On 6/28/06, Keith Goodman <kwgoodman@gmail.com> wrote:
x = asmatrix(rand(3,2))
y = asmatrix(rand(3,1))
y
matrix([[ 0.77952062], [ 0.97110465], [ 0.77450218]])
idx = where(y > 0.5)[0]
idx matrix([[0, 1, 2]])
x[idx,:]
matrix([[ 0.24837887, 0.52988253], [ 0.28661085, 0.43053076], [ 0.05360893, 0.22668509]])
So far everything works as it should. Now the problem:
y[idx,:]
exceptions.ValueError Traceback (most recent call last)
/usr/local/lib/python2.4/site-packages/numpy/core/defmatrix.py in __getitem__(self, index) 120 121 def __getitem__(self, index): --> 122 out = N.ndarray.__getitem__(self, index) 123 # Need to swap if slice is on first index 124 retscal = False
/usr/local/lib/python2.4/site-packages/numpy/core/defmatrix.py in __array_finalize__(self, obj) 116 self.shape = (1,1) 117 elif ndim == 1: --> 118 self.shape = (1,self.shape[0]) 119 return 120
ValueError: total size of new array must be unchanged
And, on a related note, shouldn't this be a column vector?
x[idx,0] matrix([[ 0.24837887, 0.28661085, 0.05360893]])
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 _______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion

On 6/28/06, Pau Gargallo <pau.gargallo@gmail.com> wrote:
i don't know why 'where' is returning matrices. if you use:
idx = where(y.A > 0.5)[0]
everything will work fine (I guess)
What about the second issue? Is this expected behavior?
idx array([0, 1, 2])
y
matrix([[ 0.63731308], [ 0.34282663], [ 0.53366791]])
y[idx]
matrix([[ 0.63731308], [ 0.34282663], [ 0.53366791]])
y[idx,0] matrix([[ 0.63731308, 0.34282663, 0.53366791]])
I was expecting a column vector.

On 6/28/06, Keith Goodman <kwgoodman@gmail.com> wrote:
On 6/28/06, Pau Gargallo <pau.gargallo@gmail.com> wrote:
i don't know why 'where' is returning matrices. if you use:
idx = where(y.A > 0.5)[0]
everything will work fine (I guess)
What about the second issue? Is this expected behavior?
idx array([0, 1, 2])
y
matrix([[ 0.63731308], [ 0.34282663], [ 0.53366791]])
y[idx]
matrix([[ 0.63731308], [ 0.34282663], [ 0.53366791]])
y[idx,0] matrix([[ 0.63731308, 0.34282663, 0.53366791]])
I was expecting a column vector.
I have never played with matrices, but if y was an array, y[idx,0] will be an array of the same shape of idx. That is a 1d array. I guess that when y is a matrix, this 1d array is converted to a matrix and become a row vector. I don't know if this behaviour is wanted :-( cheers, pau

Keith Goodman wrote:
On 6/28/06, Pau Gargallo <pau.gargallo@gmail.com> wrote:
i don't know why 'where' is returning matrices. if you use:
idx = where(y.A > 0.5)[0]
everything will work fine (I guess)
What about the second issue? Is this expected behavior?
idx
array([0, 1, 2])
y
matrix([[ 0.63731308], [ 0.34282663], [ 0.53366791]])
y[idx]
matrix([[ 0.63731308], [ 0.34282663], [ 0.53366791]])
y[idx,0]
matrix([[ 0.63731308, 0.34282663, 0.53366791]])
I was expecting a column vector.
This should be better behaved now in SVN. Thanks for the reports. -Travis

On 6/28/06, Travis Oliphant <oliphant@ee.byu.edu> wrote:
Keith Goodman wrote:
On 6/28/06, Pau Gargallo <pau.gargallo@gmail.com> wrote:
i don't know why 'where' is returning matrices. if you use:
idx = where(y.A > 0.5)[0]
everything will work fine (I guess)
What about the second issue? Is this expected behavior?
idx
array([0, 1, 2])
y
matrix([[ 0.63731308], [ 0.34282663], [ 0.53366791]])
y[idx]
matrix([[ 0.63731308], [ 0.34282663], [ 0.53366791]])
y[idx,0]
matrix([[ 0.63731308, 0.34282663, 0.53366791]])
I was expecting a column vector.
This should be better behaved now in SVN. Thanks for the reports.
Now numpy can do y[y > 0.5] instead of y[where(y.A > 0.5)[0]] where, for example, y = asmatrix(rand(3,1)). I know I'm pushing my luck here. But one more feature would make this perfect. Currently y[y>0.5,:] returns the first column even if y has more than one column. Returning all columns would make it perfect. Example:
y
matrix([[ 0.38828902, 0.91649964], [ 0.41074001, 0.7105919 ], [ 0.15460833, 0.16746956]])
y[y[:,1]>0.5,:]
matrix([[ 0.38828902], [ 0.41074001]]) A better answer for matrix users would be:
y[(0,1),:]
matrix([[ 0.38828902, 0.91649964], [ 0.41074001, 0.7105919 ]])
participants (3)
-
Keith Goodman
-
Pau Gargallo
-
Travis Oliphant