Re: [Numpy-discussion] Can numpy catch this error for me?
On Tue, Apr 7, 2009 at 14:19, Nathaniel Peterson nathanielpeterso...@gmail.com wrote: import numpy as np import operator np.seterr(all='raise') a=np.arange(1)+1 print(a.dtype) # int32 for num in range(1,17): a=np.arange(num)+1 b=np.multiply.reduce(a) print('%s! = %s'%(num,b)) # c=reduce(operator.mul,range(1,num+1)) # assert(b==c) The code above outputs int32 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 362880 10! = 3628800 11! = 39916800 12! = 479001600 13! = 1932053504 14! = 1278945280 15! = 2004310016 16! = 2004189184 The results for 14! and above are wrong due to overflow of the int32 data type. Is there a way to setup numpy so it will raise an error when this occurs? No, sorry. Of course one can get the int behaviour by specifying dtype=object. (This way you get the longer int behaviour and the *lack of speed* to go with it.) for num in range(1,17): a=np.arange(num, dtype=object)+1 b=np.multiply.reduce(a) print('%s! = %s'%(num,b)) 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 362880 10! = 3628800 11! = 39916800 12! = 479001600 13! = 6227020800 14! = 87178291200 15! = 1307674368000 16! = 20922789888000 matt -- This message and any attachments are confidential, proprietary, and may be privileged. If this message was misdirected, Barclays Global Investors (BGI) does not waive any confidentiality or privilege. If you are not the intended recipient, please notify us immediately and destroy the message without disclosing its contents to anyone. Any distribution, use or copying of this e-mail or the information it contains by other than an intended recipient is unauthorized. The views and opinions expressed in this e-mail message are the author's own and may not reflect the views and opinions of BGI, unless the author is authorized by BGI to express such views or opinions on its behalf. All email sent to or from this address is subject to electronic storage and review by BGI. Although BGI operates anti-virus programs, it does not accept responsibility for any damage whatsoever caused by viruses being passed. ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Numpy Positional Array
The array follows a pattern: each array of length 2 represents the x,y index of that array within the larger array. Is this what you are after? numpy.array(list(numpy.ndindex(n,n))).reshape(n,n,2) -- This message and any attachments are confidential, proprietary, and may be privileged. If this message was misdirected, Barclays Global Investors (BGI) does not waive any confidentiality or privilege. If you are not the intended recipient, please notify us immediately and destroy the message without disclosing its contents to anyone. Any distribution, use or copying of this e-mail or the information it contains by other than an intended recipient is unauthorized. The views and opinions expressed in this e-mail message are the author's own and may not reflect the views and opinions of BGI, unless the author is authorized by BGI to express such views or opinions on its behalf. All email sent to or from this address is subject to electronic storage and review by BGI. Although BGI operates anti-virus programs, it does not accept responsibility for any damage whatsoever caused by viruses being passed. ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
[Numpy-discussion] lost with slicing
I apologise if I'm asking an obvious question or one that has already been addressed. I've tried to understand the documentation in the numpy manual on slicing, but I'm a bit lost. I'm trying to do indexing using both slices and index lists. I have a problem when I do something like: x[0, :, [0,1,2]] Here are a couple of examples: a = numpy.arange(6).reshape(2,3) print a [[0 1 2] [3 4 5]] print a[:, [0,1,2]] # example 1 - this works as I expected [[0 1 2] [3 4 5]] b = numpy.arange(6).reshape(1,2,3) print b [[[0 1 2] [3 4 5]]] print b[0, :, [0,1,2]] # example 2 - this seems to be the transpose of what I was expecting [[0 3] [1 4] [2 5]] print b[0, [[0],[1]], [[0,1,2]]] # example 3 - this is what I expected [[0 1 2] [3 4 5]] Am I doing something wrong? Why do we get different behaviour in example 2 compared with example 1 or example 3? (I'm using numpy 1.0.3.1 on python 2.4.1 for windows, but I've tried some more recent versions of numpy as well.) mattp -- This message and any attachments are confidential, proprietary, and may be privileged. If this message was misdirected, Barclays Global Investors (BGI) does not waive any confidentiality or privilege. If you are not the intended recipient, please notify us immediately and destroy the message without disclosing its contents to anyone. Any distribution, use or copying of this e-mail or the information it contains by other than an intended recipient is unauthorized. The views and opinions expressed in this e-mail message are the author's own and may not reflect the views and opinions of BGI, unless the author is authorized by BGI to express such views or opinions on its behalf. All email sent to or from this address is subject to electronic storage and review by BGI. Although BGI operates anti-virus programs, it does not accept responsibility for any damage whatsoever caused by viruses being passed. ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] lost with slicing
I apologise if I'm asking an obvious question or one that has already been addressed. I've tried to understand the documentation in the numpy manual on slicing, but I'm a bit lost. I'm trying to do indexing using both slices and index lists. I have a problem when I do something like: x[0, :, [0,1,2]] Here are a couple of examples: a = numpy.arange(6).reshape(2,3) print a [[0 1 2] [3 4 5]] print a[:, [0,1,2]] # example 1 - this works as I expected [[0 1 2] [3 4 5]] b = numpy.arange(6).reshape(1,2,3) print b [[[0 1 2] [3 4 5]]] print b[0, :, [0,1,2]] # example 2 - this seems to be the transpose of what I was expecting [[0 3] [1 4] [2 5]] print b[0, [[0],[1]], [[0,1,2]]] # example 3 - this is what I expected [[0 1 2] [3 4 5]] Am I doing something wrong? Why do we get different behaviour in example 2 compared with example 1 or example 3? (I'm using numpy 1.0.3.1 on python 2.4.1 for windows, but I've tried some more recent versions of numpy as well.) mattp that's how it works, whether we like it or not. see thread with title is it a bug? starting march 11 Josef Thanks Josef, I've looked over is it a bug thread, and realise that it is very relevant! But I'm still lost. Robert Kern wrote: It's certainly weird, but it's working as designed. Fancy indexing via arrays is a separate subsystem from indexing via slices. Basically, fancy indexing decides the outermost shape of the result (e.g. the leftmost items in the shape tuple). If there are any sliced axes, they are *appended* to the end of that shape tuple. I see that's the case in example 2, but not in example 1 (above). Josef, I also see your example doesn't fit this explanation: x = np.arange(30).reshape(3,5,2) idx = np.array([0,1]); e = x[:,[0,1],0]; e.shape (3, 2) idx = np.array([0,1]); e = x[:,:2,0]; e.shape (3, 2) Travis Oliphant wrote: Referencing my previous post on this topic. In this case, it is unambiguous to replace dimensions 1 and 2 with the result of broadcasting idx and idx together. Thus the (5,6) dimensions is replaced by the (2,) result of indexing leaving the outer dimensions in-tact, thus (4,2,7) is the result. I'm unclear on when something is regarded as unambiguous; I don't really get how the rules work. I'm trying to build something where I can do (for a having a shape (n1,n2,n3,...)): a[i1, i2, i3, ...] where i1, i2, i3 can be * a single index: eg a[3] * a slice: eg a[:3] * a list of keys: eg a[[1,2,3]] and the interpretation of this should yield: * no corresponding dimension if a single index is used * a dimension of length of the slice if a slice is used * a dimension of length of the list if a list is used I currently apply the following logic: * look through the index coordinates that are being applied * if there are multiple list-of-key indices, then reshape them so that they will broadcast to agree: a[[1,2,3], [4,5]] -- a[[[1],[2],[3]], [[4,5]]] * note if there are any slices. If so, I assume (as per Robert Kern's remark) that the dimensions corresponding to the slices are going to be appended to the end. So I make sure that I transpose my result at the end to correct for this. When I do all this, I get example 2 behaving like example 3, but example 1 then doesn't work. I'm not trying to get the discussion list to do my work for me, but I'm pretty confused as to when dimensions get swapped and when they don't; when something is ambiguous and when it is unambiguous. Any help appreciated, thanks, matt -- This message and any attachments are confidential, proprietary, and may be privileged. If this message was misdirected, Barclays Global Investors (BGI) does not waive any confidentiality or privilege. If you are not the intended recipient, please notify us immediately and destroy the message without disclosing its contents to anyone. Any distribution, use or copying of this e-mail or the information it contains by other than an intended recipient is unauthorized. The views and opinions expressed in this e-mail message are the author's own and may not reflect the views and opinions of BGI, unless the author is authorized by BGI to express such views or opinions on its behalf. All email sent to or from this address is subject to electronic storage and review by BGI. Although BGI operates anti-virus programs, it does not accept responsibility for any damage whatsoever caused by viruses being passed. ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] lost with slicing
Sorry group. I found Travis Oliphant's earlier 12 March post (that didn't show up in the same thread), and found the answer to my question. matt I apologise if I'm asking an obvious question or one that has already been addressed. I've tried to understand the documentation in the numpy manual on slicing, but I'm a bit lost. I'm trying to do indexing using both slices and index lists. I have a problem when I do something like: x[0, :, [0,1,2]] Here are a couple of examples: a = numpy.arange(6).reshape(2,3) print a [[0 1 2] [3 4 5]] print a[:, [0,1,2]] # example 1 - this works as I expected [[0 1 2] [3 4 5]] b = numpy.arange(6).reshape(1,2,3) print b [[[0 1 2] [3 4 5]]] print b[0, :, [0,1,2]] # example 2 - this seems to be the transpose of what I was expecting [[0 3] [1 4] [2 5]] print b[0, [[0],[1]], [[0,1,2]]] # example 3 - this is what I expected [[0 1 2] [3 4 5]] Am I doing something wrong? Why do we get different behaviour in example 2 compared with example 1 or example 3? (I'm using numpy 1.0.3.1 on python 2.4.1 for windows, but I've tried some more recent versions of numpy as well.) mattp that's how it works, whether we like it or not. see thread with title is it a bug? starting march 11 Josef Thanks Josef, I've looked over is it a bug thread, and realise that it is very relevant! But I'm still lost. Robert Kern wrote: It's certainly weird, but it's working as designed. Fancy indexing via arrays is a separate subsystem from indexing via slices. Basically, fancy indexing decides the outermost shape of the result (e.g. the leftmost items in the shape tuple). If there are any sliced axes, they are *appended* to the end of that shape tuple. I see that's the case in example 2, but not in example 1 (above). Josef, I also see your example doesn't fit this explanation: x = np.arange(30).reshape(3,5,2) idx = np.array([0,1]); e = x[:,[0,1],0]; e.shape (3, 2) idx = np.array([0,1]); e = x[:,:2,0]; e.shape (3, 2) Travis Oliphant wrote: Referencing my previous post on this topic. In this case, it is unambiguous to replace dimensions 1 and 2 with the result of broadcasting idx and idx together. Thus the (5,6) dimensions is replaced by the (2,) result of indexing leaving the outer dimensions in-tact, thus (4,2,7) is the result. I'm unclear on when something is regarded as unambiguous; I don't really get how the rules work. I'm trying to build something where I can do (for a having a shape (n1,n2,n3,...)): a[i1, i2, i3, ...] where i1, i2, i3 can be * a single index: eg a[3] * a slice: eg a[:3] * a list of keys: eg a[[1,2,3]] and the interpretation of this should yield: * no corresponding dimension if a single index is used * a dimension of length of the slice if a slice is used * a dimension of length of the list if a list is used I currently apply the following logic: * look through the index coordinates that are being applied * if there are multiple list-of-key indices, then reshape them so that they will broadcast to agree: a[[1,2,3], [4,5]] -- a[[[1],[2],[3]], [[4,5]]] * note if there are any slices. If so, I assume (as per Robert Kern's remark) that the dimensions corresponding to the slices are going to be appended to the end. So I make sure that I transpose my result at the end to correct for this. When I do all this, I get example 2 behaving like example 3, but example 1 then doesn't work. I'm not trying to get the discussion list to do my work for me, but I'm pretty confused as to when dimensions get swapped and when they don't; when something is ambiguous and when it is unambiguous. Any help appreciated, thanks, matt -- This message and any attachments are confidential, proprietary, and may be privileged. If this message was misdirected, Barclays Global Investors (BGI) does not waive any confidentiality or privilege. If you are not the intended recipient, please notify us immediately and destroy the message without disclosing its contents to anyone. Any distribution, use or copying of this e-mail or the information it contains by other than an intended recipient is unauthorized. The views and opinions expressed in this e-mail message are the author's own and may not reflect the views and opinions of BGI, unless the author is authorized by BGI to express such views or opinions on its behalf. All email sent to or from this address is subject to electronic storage and review by BGI. Although BGI operates anti-virus programs, it does not accept responsibility for any damage whatsoever caused by viruses being passed. ___ Numpy-discussion mailing list