Re: [Numpy-discussion] advanced indexing question

2015-02-04 Thread Sebastian Berg
On Mi, 2015-02-04 at 07:22 +, David Kershaw wrote:
 The numpy reference manual, array objects/indexing/advance indexing, 
 says: 
 Advanced indexing always returns a copy of the data (contrast with 
 basic slicing that returns a view).
 
 If I run the following code:
  import numpy as np
  d=range[2]
  x=np.arange(36).reshape(3,2,3,2)
  y=x[:,d,:,d]
  y+=1
  print x
  x[:,d,:,d]+=1
  print x
 then the first print x shows that x is unchanged as it should be since y 
 was a copy, not a view, but the second print x shows that all the elements 
 of x with 1st index = 3rd index are now 1 bigger. Why did the left side of
  x[:,d,:,d]+=1
 act like a view and not a copy?
 

Python has a mechanism both for getting an item and for setting an item.
The latter will end up doing this (python already does this for us):
x[:,d,:,d] = x[:,d,:,d] + 1
so there is an item assignment going on (__setitem__ not __getitem__)

- Sebastian


 Thanks,
 David
 
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion
 



signature.asc
Description: This is a digitally signed message part
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] advanced indexing question

2015-02-04 Thread David Kershaw
Sebastian Berg sebastian at sipsolutions.net writes:
 
 Python has a mechanism both for getting an item and for setting an item.
 The latter will end up doing this (python already does this for us):
 x[:,d,:,d] = x[:,d,:,d] + 1
 so there is an item assignment going on (__setitem__ not __getitem__)
 
 - Sebastian
 
 
 
 
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion at scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion
 

Thanks for the prompt help Sebastian,

So can I use any legitimate ndarray indexing selection object, obj, in
 x.__setitem__(obj,y)
and as long as y's shape can be broadcast to x[obj]'s shape it will always 
set the appropriate elements of x to the corresponding elements of y?



___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] advanced indexing question

2015-02-03 Thread David Kershaw
The numpy reference manual, array objects/indexing/advance indexing, 
says: 
Advanced indexing always returns a copy of the data (contrast with 
basic slicing that returns a view).

If I run the following code:
 import numpy as np
 d=range[2]
 x=np.arange(36).reshape(3,2,3,2)
 y=x[:,d,:,d]
 y+=1
 print x
 x[:,d,:,d]+=1
 print x
then the first print x shows that x is unchanged as it should be since y 
was a copy, not a view, but the second print x shows that all the elements 
of x with 1st index = 3rd index are now 1 bigger. Why did the left side of
 x[:,d,:,d]+=1
act like a view and not a copy?

Thanks,
David

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Advanced indexing question - subset of data cube as a 2D array.

2008-10-29 Thread Adam Ginsburg
 In [85]: bi = (f.bolo_indices[np.newaxis,:]+

 ones([7751,1])).astype('int')

 In [86]: whc = (whscan[:,np.newaxis] + ones([1,107])).astype('int')

 In [87]: array2d[whc,bi] = temp2d

 I thought this had worked, but the values didn't seem to be going to the 
 right places when I re-examined them.

I think I answered my own question: if I'd used zeros() instead of
ones() it would have worked fine.  I don't know why I tried to use
ones().

Adam
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Advanced indexing question - subset of data cube as a 2D array.

2008-10-29 Thread Ravi
On Wednesday 29 October 2008 01:44:06 Adam wrote:
 In [62]: temp2d = reshape(array3d,[23*337,107])

 In [63]: temp2d2 = zeros([23*337,144])

 In [64]: temp2d2[:,f.bolo_indices] = temp2d

 In [65]: array2d[whscan,:] = temp2d2


 This works, but it feels wrong to me: I think there should be a way to do
 this by directly indexing array2d with two numpy arrays

Does numpy.ix_ not fit your use case?

In [2]: y = arange(15).reshape((3,5))

In [3]: y
Out[3]:
array([[ 0,  1,  2,  3,  4],
   [ 5,  6,  7,  8,  9],
   [10, 11, 12, 13, 14]])

In [4]: y[ ix_( array([1,2]), array([1,3,4]) ) ]
Out[4]:
array([[ 6,  8,  9],
   [11, 13, 14]])


Regards,
Ravi


___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] Advanced indexing question - subset of data cube as a 2D array.

2008-10-28 Thread Adam
Hi numpy group,
I have a problem I know there is an elegant solution to, but I can't
wrap my head around the right way to do the indexing.

The problem:
I have a 2D array that has been chopped up into 3 dimensions - it was [ time
X detectors ], it is now [ scans X time X detectors ].  During the chopping,
some of the time points and detector points have been removed, so the 3D
array contains only a subset of the data in the 2D array.  I'd like to
restore the 3D array back to the shape of the original 2D array b/c it's
being stored in a netCDF file that is not flexible.

My solution:
In [58]: array2d.shape
Out[58]: (11008, 144)

In [59]: array3d.shape
Out[59]: (23, 337, 107)

In [60]: whscan.shape
Out[60]: (7751,)

In [61]: 23*337
Out[61]: 7751

In [62]: temp2d = reshape(array3d,[23*337,107])

In [63]: temp2d2 = zeros([23*337,144])

In [64]: temp2d2[:,f.bolo_indices] = temp2d

In [65]: array2d[whscan,:] = temp2d2


This works, but it feels wrong to me: I think there should be a way to do
this by directly indexing array2d with two numpy arrays

In the process of asking this question, I might have come up with the answer
(courtesy Stefan at http://mentat.za.net/):

In [85]: bi = (f.bolo_indices[np.newaxis,:]+ones([7751,1])).astype('int')

In [86]: whc = (whscan[:,np.newaxis] + ones([1,107])).astype('int')

In [87]: array2d[whc,bi] = temp2d

I thought this had worked, but the values didn't seem to be going to the
right places when I re-examined them.

Thanks,
Adam
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion