Re: [Numpy-discussion] indexing question
I think that the only speedup you will get is defining an index only once and reusing it. 2010/11/22 Ernest Adrogué : > 22/11/10 @ 14:04 (-0600), thus spake Robert Kern: >> > This way, I get the elements (0,1) and (1,1) which is what >> > I wanted. The question is: is it possible to omit the [0,1] >> > in the index? >> >> No, but you can write generic code for it: >> >> t[np.arange(t.shape[0]), x, y] > > Thank you. This is what I wanted to know. > > -- > Ernest > ___ > NumPy-Discussion mailing list > NumPy-Discussion@scipy.org > http://mail.scipy.org/mailman/listinfo/numpy-discussion > ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
22/11/10 @ 14:04 (-0600), thus spake Robert Kern: > > This way, I get the elements (0,1) and (1,1) which is what > > I wanted. The question is: is it possible to omit the [0,1] > > in the index? > > No, but you can write generic code for it: > > t[np.arange(t.shape[0]), x, y] Thank you. This is what I wanted to know. -- Ernest ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
22/11/10 @ 11:20 (-0800), thus spake John Salvatier: > I didn't realize the x's and y's were varying the first time around. > There's probably a way to omit it, but I think the conceptually > simplest way is probably what you had to begin with. Build an index by > saying i = numpy.arange(0, t.shape[0]) > > then you can do t[i, x,y] Exactly. I was just wondering if I can speed this up by omitting building the "arange array". This is inside a function that gets called a lot, so I suppose it would make a difference if I can get rid of it. -- Ernest ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
2010/11/21 Ernest Adrogué : > Hi, > > Suppose an array of shape (N,2,2), that is N arrays of > shape (2,2). I want to select an element (x,y) from each one > of the subarrays, so I get a 1-dimensional array of length > N. For instance: > > In [228]: t=np.arange(8).reshape(2,2,2) > > In [229]: t > Out[229]: > array([[[0, 1], > [2, 3]], > > [[4, 5], > [6, 7]]]) > > In [230]: x=[0,1] > > In [231]: y=[1,1] > > In [232]: t[[0,1],x,y] > Out[232]: array([1, 7]) > > This way, I get the elements (0,1) and (1,1) which is what > I wanted. The question is: is it possible to omit the [0,1] > in the index? No, but you can write generic code for it: t[np.arange(t.shape[0]), x, y] -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
22/11/10 @ 11:08 (-0800), thus spake Christopher Barker: > On 11/21/10 11:37 AM, Ernest Adrogué wrote: > >>so you want > >> > >>t[:,x,y] > > > >I tried that, but it's not the same: > > > >In [307]: t[[0,1],x,y] > >Out[307]: array([1, 7]) > > > >In [308]: t[:,x,y] > >Out[308]: > >array([[1, 3], > >[5, 7]]) > > what is your t? Here's my example, which I think matches what you asked for: > > In [1]: import numpy as np > > In [2]: a = np.arange(12) > > In [3]: a.shape = (3,2,2) > > In [4]: a > Out[4]: > array([[[ 0, 1], > [ 2, 3]], > >[[ 4, 5], > [ 6, 7]], > >[[ 8, 9], > [10, 11]]]) > > In [5]: a[:,1,0] > Out[5]: array([ 2, 6, 10]) This works with scalar indices, but not with arrays. The problem is that I don't want always the same element from each subarray, but an arbitrary element, say the (1,0) from the first, the (0,0) from the second, and so on, so I have to use arrays. -- Ernest ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
I didn't realize the x's and y's were varying the first time around. There's probably a way to omit it, but I think the conceptually simplest way is probably what you had to begin with. Build an index by saying i = numpy.arange(0, t.shape[0]) then you can do t[i, x,y] On Mon, Nov 22, 2010 at 11:08 AM, Christopher Barker wrote: > On 11/21/10 11:37 AM, Ernest Adrogué wrote: >>> so you want >>> >>> t[:,x,y] >> >> I tried that, but it's not the same: >> >> In [307]: t[[0,1],x,y] >> Out[307]: array([1, 7]) >> >> In [308]: t[:,x,y] >> Out[308]: >> array([[1, 3], >> [5, 7]]) > > what is your t? Here's my example, which I think matches what you asked for: > > In [1]: import numpy as np > > In [2]: a = np.arange(12) > > In [3]: a.shape = (3,2,2) > > In [4]: a > Out[4]: > array([[[ 0, 1], > [ 2, 3]], > > [[ 4, 5], > [ 6, 7]], > > [[ 8, 9], > [10, 11]]]) > > In [5]: a[:,1,0] > Out[5]: array([ 2, 6, 10]) > > > > > -- > Christopher Barker, Ph.D. > Oceanographer > > Emergency Response Division > NOAA/NOS/OR&R (206) 526-6959 voice > 7600 Sand Point Way NE (206) 526-6329 fax > Seattle, WA 98115 (206) 526-6317 main reception > > chris.bar...@noaa.gov > ___ > NumPy-Discussion mailing list > NumPy-Discussion@scipy.org > http://mail.scipy.org/mailman/listinfo/numpy-discussion > ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
On 11/21/10 11:37 AM, Ernest Adrogué wrote: >> so you want >> >> t[:,x,y] > > I tried that, but it's not the same: > > In [307]: t[[0,1],x,y] > Out[307]: array([1, 7]) > > In [308]: t[:,x,y] > Out[308]: > array([[1, 3], > [5, 7]]) what is your t? Here's my example, which I think matches what you asked for: In [1]: import numpy as np In [2]: a = np.arange(12) In [3]: a.shape = (3,2,2) In [4]: a Out[4]: array([[[ 0, 1], [ 2, 3]], [[ 4, 5], [ 6, 7]], [[ 8, 9], [10, 11]]]) In [5]: a[:,1,0] Out[5]: array([ 2, 6, 10]) -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R(206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception chris.bar...@noaa.gov ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
Hi, 21/11/10 @ 11:28 (-0800), thus spake John Salvatier: > yes use the symbol ':' > > so you want > > t[:,x,y] I tried that, but it's not the same: In [307]: t[[0,1],x,y] Out[307]: array([1, 7]) In [308]: t[:,x,y] Out[308]: array([[1, 3], [5, 7]]) No? -- Ernest ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
yes use the symbol ':' so you want t[:,x,y] 2010/11/21 Ernest Adrogué : > Hi, > > Suppose an array of shape (N,2,2), that is N arrays of > shape (2,2). I want to select an element (x,y) from each one > of the subarrays, so I get a 1-dimensional array of length > N. For instance: > > In [228]: t=np.arange(8).reshape(2,2,2) > > In [229]: t > Out[229]: > array([[[0, 1], > [2, 3]], > > [[4, 5], > [6, 7]]]) > > In [230]: x=[0,1] > > In [231]: y=[1,1] > > In [232]: t[[0,1],x,y] > Out[232]: array([1, 7]) > > This way, I get the elements (0,1) and (1,1) which is what > I wanted. The question is: is it possible to omit the [0,1] > in the index? > > Thanks in advance. > > -- > Ernest > ___ > NumPy-Discussion mailing list > NumPy-Discussion@scipy.org > http://mail.scipy.org/mailman/listinfo/numpy-discussion > ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
read about basic slicing : http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html On Sun, Nov 21, 2010 at 11:28 AM, John Salvatier wrote: > yes use the symbol ':' > > so you want > > t[:,x,y] > > 2010/11/21 Ernest Adrogué : >> Hi, >> >> Suppose an array of shape (N,2,2), that is N arrays of >> shape (2,2). I want to select an element (x,y) from each one >> of the subarrays, so I get a 1-dimensional array of length >> N. For instance: >> >> In [228]: t=np.arange(8).reshape(2,2,2) >> >> In [229]: t >> Out[229]: >> array([[[0, 1], >> [2, 3]], >> >> [[4, 5], >> [6, 7]]]) >> >> In [230]: x=[0,1] >> >> In [231]: y=[1,1] >> >> In [232]: t[[0,1],x,y] >> Out[232]: array([1, 7]) >> >> This way, I get the elements (0,1) and (1,1) which is what >> I wanted. The question is: is it possible to omit the [0,1] >> in the index? >> >> Thanks in advance. >> >> -- >> Ernest >> ___ >> NumPy-Discussion mailing list >> NumPy-Discussion@scipy.org >> http://mail.scipy.org/mailman/listinfo/numpy-discussion >> > ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
On Mon, Jun 21, 2010 at 7:10 PM, Robert Kern wrote: > On Mon, Jun 21, 2010 at 17:42, Neal Becker wrote: >> Robert Kern wrote: >> >>> On Mon, Jun 21, 2010 at 14:01, Neal Becker wrote: Can I find an efficient way to do this? I have a 2d array, A, 80 rows by 880 columns. I have a vector, B, of length 80, with scalar indexes. >>> >>> I assume you mean 880. >>> I want a vector output C where C[i] = A[b[i],i] (i=0,879) >>> >>> C = A[b, np.arange(880)] >>> >> >> Thanks! Just what I needed. >> >> I wouldn't have guessed this. Do we have a wiki to save useful examples >> like this? > > This kind of indexing is documented here: > > http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#integer > > Various examples are on the wiki here: > > http://www.scipy.org/Cookbook/Indexing > > -- > Robert Kern > > "I have come to believe that the whole world is an enigma, a harmless > enigma that is made terrible by our own mad attempt to interpret it as > though it had an underlying truth." > -- Umberto Eco > ___ > NumPy-Discussion mailing list > NumPy-Discussion@scipy.org > http://mail.scipy.org/mailman/listinfo/numpy-discussion > And to be completely pedantic one could use ndarray.take to do this about 3-4x faster (but less intuitively): A.take(b * 880 + np.arange(880)) ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
On Mon, Jun 21, 2010 at 17:42, Neal Becker wrote: > Robert Kern wrote: > >> On Mon, Jun 21, 2010 at 14:01, Neal Becker wrote: >>> Can I find an efficient way to do this? >>> >>> I have a 2d array, A, 80 rows by 880 columns. >>> >>> I have a vector, B, of length 80, with scalar indexes. >> >> I assume you mean 880. >> >>> I want a vector output C where >>> C[i] = A[b[i],i] (i=0,879) >> >> C = A[b, np.arange(880)] >> > > Thanks! Just what I needed. > > I wouldn't have guessed this. Do we have a wiki to save useful examples > like this? This kind of indexing is documented here: http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#integer Various examples are on the wiki here: http://www.scipy.org/Cookbook/Indexing -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
Robert Kern wrote: > On Mon, Jun 21, 2010 at 14:01, Neal Becker wrote: >> Can I find an efficient way to do this? >> >> I have a 2d array, A, 80 rows by 880 columns. >> >> I have a vector, B, of length 80, with scalar indexes. > > I assume you mean 880. > >> I want a vector output C where >> C[i] = A[b[i],i] (i=0,879) > > C = A[b, np.arange(880)] > Thanks! Just what I needed. I wouldn't have guessed this. Do we have a wiki to save useful examples like this? ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
On Mon, Jun 21, 2010 at 14:01, Neal Becker wrote: > Can I find an efficient way to do this? > > I have a 2d array, A, 80 rows by 880 columns. > > I have a vector, B, of length 80, with scalar indexes. I assume you mean 880. > I want a vector output C where > C[i] = A[b[i],i] (i=0,879) C = A[b, np.arange(880)] -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
On Tue, Mar 30, 2010 at 10:13 AM, Tom K. wrote: > > This one bit me again, and I am trying to understand it better so I can > anticipate when it will happen. > > What I want to do is get rid of singleton dimensions, and index into the > last dimension with an array. > > In [1]: import numpy as np > > In [2]: x=np.zeros((10,1,1,1,14,1024)) > > In [3]: x[:,0,0,0,:,[1,2,3]].shape > Out[3]: (3, 10, 14) > > Whoa! Trimming my array to a desired number ends up moving the last > dimension to the first! > > In [4]: np.__version__ > Out[4]: '1.3.0' > > ... > In [7]: x[:,:,:,:,:,[1,2,3]].shape > Out[7]: (10, 1, 1, 1, 14, 3) > > This looks right... > > In [8]: x[...,[1,2,3]].shape > Out[8]: (10, 1, 1, 1, 14, 3) > > and this... > > In [9]: x[...,[1,2,3]][:,0,0,0].shape > Out[9]: (10, 14, 3) > > ... > In [11]: x[:,0,0,0][...,[1,2,3]].shape > Out[11]: (10, 14, 3) > > Either of the last 2 attempts above results in what I want, so I can do > that... I just need some help deciphering when and why the first thing > happens. An explanation about the surprising behavior when slicing and fancy indexing is mixed with more than 2 dimensions is in this thread http://www.mail-archive.com/numpy-discussion@scipy.org/msg16299.html More examples show up every once in a while on the mailing list. Josef > > -- > View this message in context: > http://old.nabble.com/indexing-question-tp28083162p28083162.html > Sent from the Numpy-discussion mailing list archive at Nabble.com. > > ___ > NumPy-Discussion mailing list > NumPy-Discussion@scipy.org > http://mail.scipy.org/mailman/listinfo/numpy-discussion > ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
On Tue, Mar 30, 2010 at 09:46, Charles R Harris wrote: > > > On Tue, Mar 30, 2010 at 8:13 AM, Tom K. wrote: >> >> This one bit me again, and I am trying to understand it better so I can >> anticipate when it will happen. >> >> What I want to do is get rid of singleton dimensions, and index into the >> last dimension with an array. >> >> In [1]: import numpy as np >> >> In [2]: x=np.zeros((10,1,1,1,14,1024)) >> >> In [3]: x[:,0,0,0,:,[1,2,3]].shape >> Out[3]: (3, 10, 14) >> > > Hmm... That doesn't look right. It's a known feature. Slicing and list indexing are separate subsystems. The list indexing takes priority so the list-indexed axes end up first in the result. The sliced axes follow them. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
On Tue, Mar 30, 2010 at 8:13 AM, Tom K. wrote: > > This one bit me again, and I am trying to understand it better so I can > anticipate when it will happen. > > What I want to do is get rid of singleton dimensions, and index into the > last dimension with an array. > > In [1]: import numpy as np > > In [2]: x=np.zeros((10,1,1,1,14,1024)) > > In [3]: x[:,0,0,0,:,[1,2,3]].shape > Out[3]: (3, 10, 14) > > Hmm... That doesn't look right. Chuck ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
On 3/30/2010 10:13 AM, Tom K. wrote: > What I want to do is get rid of singleton dimensions, and index into the > last dimension with an array. >>> x=np.zeros((10,1,1,1,14,1024)) >>> np.squeeze(x).shape (10, 14, 1024) hth, Alan Isaac ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
On Sun, Dec 20, 2009 at 8:58 PM, Alan G Isaac wrote: > Why is s3 F_CONTIGUOUS, and perhaps equivalently, > why is its C_CONTIGUOUS data in s3.base (below)? > Thanks, > Alan Isaac > a3 > array([[ 0, 1, 2, 3, 4, 5], > [ 6, 7, 8, 9, 10, 11]]) a3.flags > C_CONTIGUOUS : True > F_CONTIGUOUS : False > OWNDATA : True > WRITEABLE : True > ALIGNED : True > UPDATEIFCOPY : False ind > array([3, 1, 2, 4, 5, 0]) s3 = a3[:,ind] s3.flags > C_CONTIGUOUS : False > F_CONTIGUOUS : True > OWNDATA : False > WRITEABLE : True > ALIGNED : True > UPDATEIFCOPY : False s3.base > array([[ 3, 9], > [ 1, 7], > [ 2, 8], > [ 4, 10], > [ 5, 11], > [ 0, 6]]) s3 > array([[ 3, 1, 2, 4, 5, 0], > [ 9, 7, 8, 10, 11, 6]]) Maybe another consequence of the different internal treatment of fancy and non-fancy slicing. I would infer from a comment by Travis in response to the question about the change in axis for 3d arrays with mixed fancy and non-fancy slicing. >>> ind = np.array([3, 1, 2, 4, 5, 0]) >>> s3 = a[:,ind] >>> s3.flags C_CONTIGUOUS : False F_CONTIGUOUS : True OWNDATA : False WRITEABLE : True ALIGNED : True UPDATEIFCOPY : False >>> s4 = a[np.arange(2)[:,None],ind] >>> s4.flags C_CONTIGUOUS : True F_CONTIGUOUS : False OWNDATA : True WRITEABLE : True ALIGNED : True UPDATEIFCOPY : False Josef > ___ > NumPy-Discussion mailing list > NumPy-Discussion@scipy.org > http://mail.scipy.org/mailman/listinfo/numpy-discussion > ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
On Thu, Mar 5, 2009 at 9:15 PM, Stéfan van der Walt wrote: > Hi Robin > > 2009/3/5 Robin : >> On Thu, Mar 5, 2009 at 10:57 AM, Robin wrote: >>> On Thu, Mar 5, 2009 at 10:40 AM, Robin wrote: Hi, I have an indexing problem, and I know it's a bit lazy to ask the list, sometime when people do interesting tricks come up so I hope no one minds! I have a 2D array X.shape = (a,b) and I want to change it into new array which is shape (2,(a*b)) which has the following form: [ X[0,0], X[0,1] X[1,0], X[1,1] X[2,0], X[2,1] X[a,0], X[a,1] X[0,1], X[0,2] X[1,1], X[1,2] ... ] > > >From the array you wrote down above, I assume you meant ((a*b-1), 2): > > In [23]: x = np.arange(16).reshape((4,4)) > > In [24]: x > Out[24]: > array([[ 0, 1, 2, 3], > [ 4, 5, 6, 7], > [ 8, 9, 10, 11], > [12, 13, 14, 15]]) > > In [25]: x.strides > Out[25]: (16, 4) > > In [26]: np.lib.stride_tricks.as_strided(x, shape=(3, 4, 2), strides=(4, 16, > 4)) > Out[26]: > array([[[ 0, 1], > [ 4, 5], > [ 8, 9], > [12, 13]], > > [[ 1, 2], > [ 5, 6], > [ 9, 10], > [13, 14]], > > [[ 2, 3], > [ 6, 7], > [10, 11], > [14, 15]]]) > > In [27]: np.lib.stride_tricks.as_strided(x, shape=(3, 4, 2), > strides=(4, 16, 4)).reshape((12, 2)) > Out[27]: > array([[ 0, 1], > [ 4, 5], > [ 8, 9], > [12, 13], > [ 1, 2], > [ 5, 6], > [ 9, 10], > [13, 14], > [ 2, 3], > [ 6, 7], > [10, 11], > [14, 15]]) > > Does that help? Ah thats great thanks... I had realised it could be done with as_strided and a reshape from your excellent slides - but I had trouble figure out the new strides so I settled on making a list with _ix and the hstack'ing the list. This is much neater though. Thanks, Robin ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
Hi Robin 2009/3/5 Robin : > On Thu, Mar 5, 2009 at 10:57 AM, Robin wrote: >> On Thu, Mar 5, 2009 at 10:40 AM, Robin wrote: >>> Hi, >>> >>> I have an indexing problem, and I know it's a bit lazy to ask the >>> list, sometime when people do interesting tricks come up so I hope no >>> one minds! >>> >>> I have a 2D array X.shape = (a,b) >>> >>> and I want to change it into new array which is shape (2,(a*b)) which >>> has the following form: >>> [ X[0,0], X[0,1] >>> X[1,0], X[1,1] >>> X[2,0], X[2,1] >>> >>> X[a,0], X[a,1] >>> X[0,1], X[0,2] >>> X[1,1], X[1,2] >>> ... >>> ] >>> >From the array you wrote down above, I assume you meant ((a*b-1), 2): In [23]: x = np.arange(16).reshape((4,4)) In [24]: x Out[24]: array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]]) In [25]: x.strides Out[25]: (16, 4) In [26]: np.lib.stride_tricks.as_strided(x, shape=(3, 4, 2), strides=(4, 16, 4)) Out[26]: array([[[ 0, 1], [ 4, 5], [ 8, 9], [12, 13]], [[ 1, 2], [ 5, 6], [ 9, 10], [13, 14]], [[ 2, 3], [ 6, 7], [10, 11], [14, 15]]]) In [27]: np.lib.stride_tricks.as_strided(x, shape=(3, 4, 2), strides=(4, 16, 4)).reshape((12, 2)) Out[27]: array([[ 0, 1], [ 4, 5], [ 8, 9], [12, 13], [ 1, 2], [ 5, 6], [ 9, 10], [13, 14], [ 2, 3], [ 6, 7], [10, 11], [14, 15]]) Does that help? Regards Stéfan ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
On Thu, Mar 5, 2009 at 10:57 AM, Robin wrote: > On Thu, Mar 5, 2009 at 10:40 AM, Robin wrote: >> Hi, >> >> I have an indexing problem, and I know it's a bit lazy to ask the >> list, sometime when people do interesting tricks come up so I hope no >> one minds! >> >> I have a 2D array X.shape = (a,b) >> >> and I want to change it into new array which is shape (2,(a*b)) which >> has the following form: >> [ X[0,0], X[0,1] >> X[1,0], X[1,1] >> X[2,0], X[2,1] >> >> X[a,0], X[a,1] >> X[0,1], X[0,2] >> X[1,1], X[1,2] >> ... >> ] >> > > Ah, so it's a bit easier than I thought at first glance: > > X[ ix_( (b-1)*range(a), [0,1]) ] > does the trick I think Not doing well this morning - that's wrong of course... I need to stack lots of such blocks for [0,1], [1,2], [2,3] etc.. up to [b-1,b]. So I guess the question still stands... Robin ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
On Thu, Mar 5, 2009 at 10:40 AM, Robin wrote: > Hi, > > I have an indexing problem, and I know it's a bit lazy to ask the > list, sometime when people do interesting tricks come up so I hope no > one minds! > > I have a 2D array X.shape = (a,b) > > and I want to change it into new array which is shape (2,(a*b)) which > has the following form: > [ X[0,0], X[0,1] > X[1,0], X[1,1] > X[2,0], X[2,1] > > X[a,0], X[a,1] > X[0,1], X[0,2] > X[1,1], X[1,2] > ... > ] > Ah, so it's a bit easier than I thought at first glance: X[ ix_( (b-1)*range(a), [0,1]) ] does the trick I think Sorry for the noise. Robin ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] indexing question
On Thu, Mar 5, 2009 at 10:40 AM, Robin wrote: > Hi, > > I have an indexing problem, and I know it's a bit lazy to ask the > list, sometime when people do interesting tricks come up so I hope no > one minds! > > I have a 2D array X.shape = (a,b) > > and I want to change it into new array which is shape (2,(a*b)) which > has the following form: > actually the new array would have dimensions (2,(a*(b-1) ) as the last bin wouldn't have the second point. Cheers Robin ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion