From: Keith G. <kwg...@gm...> - 2006-06-28 17:26:04
|
>> 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]]) |
From: Pau G. <pau...@gm...> - 2006-06-28 17:40:38
|
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 <kwg...@gm...> 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 > Num...@li... > https://lists.sourceforge.net/lists/listinfo/numpy-discussion > |
From: Keith G. <kwg...@gm...> - 2006-06-28 18:04:11
|
On 6/28/06, Pau Gargallo <pau...@gm...> 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. |
From: Pau G. <pau...@gm...> - 2006-06-28 18:25:18
|
On 6/28/06, Keith Goodman <kwg...@gm...> wrote: > On 6/28/06, Pau Gargallo <pau...@gm...> 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 |
From: Travis O. <oli...@ee...> - 2006-06-28 18:47:46
|
Keith Goodman wrote: >On 6/28/06, Pau Gargallo <pau...@gm...> 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 |
From: Keith G. <kwg...@gm...> - 2006-06-28 19:23:40
|
On 6/28/06, Travis Oliphant <oli...@ee...> wrote: > Keith Goodman wrote: > > >On 6/28/06, Pau Gargallo <pau...@gm...> 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 ]]) |
From: Keith G. <kwg...@gm...> - 2006-06-28 20:06:08
|
On 6/28/06, Travis Oliphant <oli...@ee...> wrote: > This should be better behaved now in SVN. Thanks for the reports. I'm impressed by how quickly features are added and bugs are fixed. And by how quick it is to install a new version of numpy. Thank you. |