From: Mathew Y. <my...@jp...> - 2006-07-06 00:37:40
|
What is the typical way of doing the following starting with a 0 matrix, set all values to 1 when a certain condition is met, set to -1 when another condition is met, left alone if neither condition is met. Mathew |
From: Keith G. <kwg...@gm...> - 2006-07-06 00:45:13
|
On 7/5/06, Mathew Yeates <my...@jp...> wrote: > What is the typical way of doing the following > starting with a 0 matrix, set all values to 1 when a certain condition > is met, set to -1 when another condition is met, left alone if neither > condition is met. This works on recent versions of numpy: >> x = asmatrix(zeros((2,2))) >> x matrix([[0, 0], [0, 0]]) >> y = asmatrix(rand(2,2)) >> y matrix([[ 0.85219404, 0.48311427], [ 0.41026966, 0.2184193 ]]) >> x[y > 0.5] = 1 >> x[y < 0.5] = -1 >> x matrix([[ 1, -1], [-1, -1]]) |
From: Mathew Y. <my...@jp...> - 2006-07-06 20:24:00
|
Not working. A[row,all_dates == 10] = -1 where all_dates is a matrix with column length of 14 [[960111,..,.. and A is a matrix with same column length I get IndexError: arrays used as indices must be of integer type when I print out all_dates == 10 I get [True True True True True True True True True False False False True True]] I experimented with "<" instead of "==" but I still get boolean values as indices. Any help? Mathew Keith Goodman wrote: > On 7/5/06, Mathew Yeates <my...@jp...> wrote: >> What is the typical way of doing the following >> starting with a 0 matrix, set all values to 1 when a certain condition >> is met, set to -1 when another condition is met, left alone if neither >> condition is met. > > This works on recent versions of numpy: > >>> x = asmatrix(zeros((2,2))) > >>> x > > matrix([[0, 0], > [0, 0]]) > >>> y = asmatrix(rand(2,2)) > >>> y > > matrix([[ 0.85219404, 0.48311427], > [ 0.41026966, 0.2184193 ]]) > >>> x[y > 0.5] = 1 > >>> x[y < 0.5] = -1 > >>> x > > matrix([[ 1, -1], > [-1, -1]]) > |
From: Travis O. <oli...@ee...> - 2006-07-06 20:37:01
|
Mathew Yeates wrote: >Not working. >A[row,all_dates == 10] = -1 where all_dates is a matrix with column >length of 14 [[960111,..,.. >and A is a matrix with same column length > >I get >IndexError: arrays used as indices must be of integer type > >when I print out all_dates == 10 >I get >[True True True True True True True True True False False False True True]] > >I experimented with "<" instead of "==" but I still get boolean values >as indices. > >Any help? > > What version are you using? Can you give an example that shows the error. It's hard to guess the type of all the variables. The following works for me. import numpy print numpy.__version__ A = numpy.matrix(rand(3,14)) all_dates = array([10,10,1,10,1,10,0,10,0,10,0,1,10,1]) row = 2 A[row, all_dates == 10] -Trvis |
From: Keith G. <kwg...@gm...> - 2006-07-06 20:43:03
|
On 7/6/06, Travis Oliphant <oli...@ee...> wrote: > Mathew Yeates wrote: > > >Not working. > >A[row,all_dates == 10] = -1 where all_dates is a matrix with column > >length of 14 [[960111,..,.. > >and A is a matrix with same column length > > > >I get > >IndexError: arrays used as indices must be of integer type > > > >when I print out all_dates == 10 > >I get > >[True True True True True True True True True False False False True True]] > > > >I experimented with "<" instead of "==" but I still get boolean values > >as indices. > > > >Any help? > > > > > What version are you using? Can you give an example that shows the > error. It's hard to guess the type of all the variables. The following > works for me. > > import numpy > print numpy.__version__ > A = numpy.matrix(rand(3,14)) > all_dates = array([10,10,1,10,1,10,0,10,0,10,0,1,10,1]) > row = 2 > A[row, all_dates == 10] This is what NASA is doing (and what I would like to do): >> A[row, asmatrix(all_dates == 10)] --------------------------------------------------------------------------- exceptions.ValueError Traceback (most recent call last) /home/kwg/<ipython console> /usr/local/lib/python2.4/site-packages/numpy/core/defmatrix.py in __getitem__(self, index) 122 123 def __getitem__(self, index): --> 124 out = N.ndarray.__getitem__(self, index) 125 # Need to swap if slice is on first index 126 # or there is an integer on the second ValueError: too many indices for array |
From: Mathew Y. <my...@jp...> - 2006-07-06 21:20:59
|
okay, I went back to the binary windows distrib. Based on Keths code I wrote >> print numpy.asmatrix(all_dates == start_dates[row],dtype=int) [[0 0 0 0 0 0 0 0 0 0 0 1 0 0]] >> [row,numpy.asmatrix(all_dates == start_dates[row],dtype=int)] = -1 >> print A[row,:] [[-1. -1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]] huh? It set the first 2 elements and not the 12'th!! Mathew |
From: Keith G. <kwg...@gm...> - 2006-07-06 21:43:34
|
On 7/6/06, Mathew Yeates <my...@jp...> wrote: > okay, I went back to the binary windows distrib. Based on Keths code I wrote > > >> print numpy.asmatrix(all_dates == start_dates[row],dtype=int) > [[0 0 0 0 0 0 0 0 0 0 0 1 0 0]] > >> [row,numpy.asmatrix(all_dates == start_dates[row],dtype=int)] = -1 > >> print A[row,:] > [[-1. -1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]] > > huh? It set the first 2 elements and not the 12'th!! You are assigning -1 to column 0 thirteen times and assinging -1 to column 1 once. For now, until boolean indexing works with matrices, I would just use brute force: A[row,where(all_dates.A == 10)[0]] Or you can do all rows at once with A[all_dates== 10] where all_dates is the same size as A. |
From: Travis O. <oli...@ee...> - 2006-07-06 21:44:09
|
Mathew Yeates wrote: >okay, I went back to the binary windows distrib. Based on Keths code I wrote > > >> print numpy.asmatrix(all_dates == start_dates[row],dtype=int) >[[0 0 0 0 0 0 0 0 0 0 0 1 0 0]] > >> [row,numpy.asmatrix(all_dates == start_dates[row],dtype=int)] = -1 > >> print A[row,:] >[[-1. -1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]] > >huh? It set the first 2 elements and not the 12'th!! > > Indexing has changed in SVN python, but in general, index matrices are not what you want because the dimensionality of the index arrays means something and matrices have two dimensions. So use arrays for indexing. -Travis |
From: Mathew Y. <my...@jp...> - 2006-07-06 20:42:17
|
The very example you give produces IndexError: arrays used as indices must be of integer type this is with 0.9.8 Also .....while your example says "rand" I had to say numpy.rand This is on WindowsXP Mathew Travis Oliphant wrote: > Mathew Yeates wrote: > >> Not working. >> A[row,all_dates == 10] = -1 where all_dates is a matrix with column >> length of 14 [[960111,..,.. >> and A is a matrix with same column length >> >> I get >> IndexError: arrays used as indices must be of integer type >> >> when I print out all_dates == 10 >> I get >> [True True True True True True True True True False False False True >> True]] >> >> I experimented with "<" instead of "==" but I still get boolean >> values as indices. >> >> Any help? >> >> > What version are you using? Can you give an example that shows the > error. It's hard to guess the type of all the variables. The > following works for me. > > import numpy > print numpy.__version__ > A = numpy.matrix(rand(3,14)) > all_dates = array([10,10,1,10,1,10,0,10,0,10,0,1,10,1]) > row = 2 > A[row, all_dates == 10] > > > > > > -Trvis > > |
From: Mathew Y. <my...@jp...> - 2006-07-06 00:47:08
|
ohhhh. I was looking at using "where" Keith Goodman wrote: > On 7/5/06, Mathew Yeates <my...@jp...> wrote: >> What is the typical way of doing the following >> starting with a 0 matrix, set all values to 1 when a certain condition >> is met, set to -1 when another condition is met, left alone if neither >> condition is met. > > This works on recent versions of numpy: > >>> x = asmatrix(zeros((2,2))) > >>> x > > matrix([[0, 0], > [0, 0]]) > >>> y = asmatrix(rand(2,2)) > >>> y > > matrix([[ 0.85219404, 0.48311427], > [ 0.41026966, 0.2184193 ]]) > >>> x[y > 0.5] = 1 > >>> x[y < 0.5] = -1 > >>> x > > matrix([[ 1, -1], > [-1, -1]]) > |
From: Bill B. <wb...@gm...> - 2006-07-06 01:03:21
|
That's the kind of thing that should maybe be on the NumpyExample list: http://www.scipy.org/Numpy_Example_List But currently little syntactical tricks like that aren't listed there. I'm not sure how you'd list that even. There is an example like that ( a[a<0.5]=0 ) on the Numpy for Matlab Users page, though. http://www.scipy.org/NumPy_for_Matlab_Users It's supposed to be for matlab users, but if you just ignore the Matlab column, the main table there is still a pretty good list of examples of numpy syntax and basic functions. --bb On 7/6/06, Mathew Yeates <my...@jp...> wrote: > > ohhhh. I was looking at using "where" > > > > Keith Goodman wrote: > > On 7/5/06, Mathew Yeates <my...@jp...> wrote: > >> What is the typical way of doing the following > >> starting with a 0 matrix, set all values to 1 when a certain condition > >> is met, set to -1 when another condition is met, left alone if neither > >> condition is met. > > > > This works on recent versions of numpy: > > > >>> x = asmatrix(zeros((2,2))) > > > >>> x > > > > matrix([[0, 0], > > [0, 0]]) > > > >>> y = asmatrix(rand(2,2)) > > > >>> y > > > > matrix([[ 0.85219404, 0.48311427], > > [ 0.41026966, 0.2184193 ]]) > > > >>> x[y > 0.5] = 1 > > > >>> x[y < 0.5] = -1 > > > >>> x > > > > matrix([[ 1, -1], > > [-1, -1]]) > > > > > 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 > -- William V. Baxter III OLM Digital Kono Dens Building Rm 302 1-8-8 Wakabayashi Setagaya-ku Tokyo, Japan 154-0023 +81 (3) 3422-3380 |
From: Pau G. <pau...@gm...> - 2006-07-06 08:50:48
|
On 7/6/06, Bill Baxter <wb...@gm...> wrote: > That's the kind of thing that should maybe be on the NumpyExample list: > http://www.scipy.org/Numpy_Example_List > But currently little syntactical tricks like that aren't listed there. I'm > not sure how you'd list that even. > There is a (tentative) introduction to 'fancy' indexing in the tentative numpy tutorial. http://www.scipy.org/Tentative_NumPy_Tutorial#head-0dffc419afa7d77d51062d40d2d84143db8216c2 i'm going to add the '[ ]' operator to the Numpy example list. pau |
From: Christopher B. <Chr...@no...> - 2006-07-06 16:36:03
|
Mathew Yeates wrote: > ohhhh. I was looking at using "where" There's nothing wrong with where: >>> y = N.asmatrix(N.rand(3,3)) >>> y matrix([[ 0.29741635, 0.78756994, 0.641378 ], [ 0.0198837 , 0.71677631, 0.76068183], [ 0.84904382, 0.80169706, 0.23877389]]) >>> x = N.asmatrix(N.where(y > 0.5, 1, 0)) >>> x matrix([[0, 1, 1], [0, 1, 1], [1, 1, 0]]) And did you really mean matrix? or would 2-d arrays be fine? -CHB -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chr...@no... |