[Numpy-discussion] strange behavior of variable
Hi, I have defined a small function to find the n maximum values of an array as below. With in it I assign the input array to a second array and temporarily make the array location after first iteration as nan. I expected this temporary change to be limited to the second variable. However my initial variable gets modified. Can any one through some light to what is happening here?. In case of matlab this logic works. ## #FUNCTION maxn ## import numpy as np def max_n(a,n): b=a result=[] for i in np.arange(1,n+1): mxidx=np.where(b==max(b)) result.append(mxidx) b[mxidx]=np.nan result=np.ravel(result) return(result) ### TEST In [8]: x=np.arange(float(0),10) In [9]: max max max_n In [9]: max_n(x,2) Out[9]: array([9, 8]) In [10]: x Out[10]: array([ 0., 1., 2., 3., 4., 5., 6., 7., nan, nan]) *** Sudheer Joseph Indian National Centre for Ocean Information Services Ministry of Earth Sciences, Govt. of India POST BOX NO: 21, IDA Jeedeemetla P.O. Via Pragathi Nagar,Kukatpally, Hyderabad; Pin:5000 55 Tel:+91-40-23886047(O),Fax:+91-40-23895011(O), Tel:+91-40-23044600(R),Tel:+91-40-9440832534(Mobile) E-mail:sjo.in...@gmail.com;sudheer.jos...@yahoo.com Web- http://oppamthadathil.tripod.com ***___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] strange behavior of variable
On 2013/08/17 9:49 PM, Sudheer Joseph wrote: Hi, I have defined a small function to find the n maximum values of an array as below. With in it I assign the input array to a second array and temporarily make the array location after first iteration as nan. I expected this temporary change to be limited to the second variable. However my initial variable gets modified. Can any one through some light to what is happening here?. In case of matlab this logic works. ## #FUNCTION maxn ## import numpy as np def max_n(a,n): b=a This is not making b a copy of a, it is simply making it an alias for it. To make it a copy you could use b = a[:], or b = a.copy() It sounds like you don't really need a function, however. Try this: # test data: a = np.random.randn(10) n = 2 # One-line solution: biggest_n = np.sort(a)[-n:] print a print biggest_n If you want them ordered from largest to smallest, just reverse the list: biggest_n = biggest_n[::-1] Eric result=[] for i in np.arange(1,n+1): mxidx=np.where(b==max(b)) result.append(mxidx) b[mxidx]=np.nan result=np.ravel(result) return(result) ### TEST In [8]: x=np.arange(float(0),10) In [9]: max maxmax_n In [9]: max_n(x,2) Out[9]: array([9, 8]) In [10]: x Out[10]: array([ 0., 1., 2., 3., 4., 5., 6., 7., nan, nan]) *** Sudheer Joseph Indian National Centre for Ocean Information Services Ministry of Earth Sciences, Govt. of India POST BOX NO: 21, IDA Jeedeemetla P.O. Via Pragathi Nagar,Kukatpally, Hyderabad; Pin:5000 55 Tel:+91-40-23886047(O),Fax:+91-40-23895011(O), Tel:+91-40-23044600(R),Tel:+91-40-9440832534(Mobile) E-mail:sjo.in...@gmail.com;sudheer.jos...@yahoo.com Web- http://oppamthadathil.tripod.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] strange behavior of variable
Thanks a lot Eric, Here comes the smartness of python!!, I am just climbing the bottom steps... with best regards, Sudheer *** Sudheer Joseph Indian National Centre for Ocean Information Services Ministry of Earth Sciences, Govt. of India POST BOX NO: 21, IDA Jeedeemetla P.O. Via Pragathi Nagar,Kukatpally, Hyderabad; Pin:5000 55 Tel:+91-40-23886047(O),Fax:+91-40-23895011(O), Tel:+91-40-23044600(R),Tel:+91-40-9440832534(Mobile) E-mail:sjo.in...@gmail.com;sudheer.jos...@yahoo.com Web- http://oppamthadathil.tripod.com *** From: Eric Firing efir...@hawaii.edu To: numpy-discussion@scipy.org Sent: Sunday, 18 August 2013 1:57 PM Subject: Re: [Numpy-discussion] strange behavior of variable On 2013/08/17 9:49 PM, Sudheer Joseph wrote: Hi, I have defined a small function to find the n maximum values of an array as below. With in it I assign the input array to a second array and temporarily make the array location after first iteration as nan. I expected this temporary change to be limited to the second variable. However my initial variable gets modified. Can any one through some light to what is happening here?. In case of matlab this logic works. ## #FUNCTION maxn ## import numpy as np def max_n(a,n): b=a This is not making b a copy of a, it is simply making it an alias for it. To make it a copy you could use b = a[:], or b = a.copy() It sounds like you don't really need a function, however. Try this: # test data: a = np.random.randn(10) n = 2 # One-line solution: biggest_n = np.sort(a)[-n:] print a print biggest_n If you want them ordered from largest to smallest, just reverse the list: biggest_n = biggest_n[::-1] Eric result=[] for i in np.arange(1,n+1): mxidx=np.where(b==max(b)) result.append(mxidx) b[mxidx]=np.nan result=np.ravel(result) return(result) ### TEST In [8]: x=np.arange(float(0),10) In [9]: max max max_n In [9]: max_n(x,2) Out[9]: array([9, 8]) In [10]: x Out[10]: array([ 0., 1., 2., 3., 4., 5., 6., 7., nan, nan]) *** Sudheer Joseph Indian National Centre for Ocean Information Services Ministry of Earth Sciences, Govt. of India POST BOX NO: 21, IDA Jeedeemetla P.O. Via Pragathi Nagar,Kukatpally, Hyderabad; Pin:5000 55 Tel:+91-40-23886047(O),Fax:+91-40-23895011(O), Tel:+91-40-23044600(R),Tel:+91-40-9440832534(Mobile) E-mail:sjo.in...@gmail.com;sudheer.jos...@yahoo.com Web- http://oppamthadathil.tripod.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 ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] strange behavior of variable
However, I was looking for the indices of the biggest 3 numbers which can be used to find corresponding values in another vector, which made me to write the function. Is there a smarter way for getting indices? with best regards, Sudheer Thanks a lot Eric, Here comes the smartness of python!!, I am just climbing the bottom steps... with best regards, Sudheer *** Sudheer Joseph Indian National Centre for Ocean Information Services Ministry of Earth Sciences, Govt. of India POST BOX NO: 21, IDA Jeedeemetla P.O. Via Pragathi Nagar,Kukatpally, Hyderabad; Pin:5000 55 Tel:+91-40-23886047(O),Fax:+91-40-23895011(O), Tel:+91-40-23044600(R),Tel:+91-40-9440832534(Mobile) E-mail:sjo.in...@gmail.com;sudheer.jos...@yahoo.com Web- http://oppamthadathil.tripod.com *** From: Eric Firing efir...@hawaii.edu To: numpy-discussion@scipy.org Sent: Sunday, 18 August 2013 1:57 PM Subject: Re: [Numpy-discussion] strange behavior of variable On 2013/08/17 9:49 PM, Sudheer Joseph wrote: Hi, I have defined a small function to find the n maximum values of an array as below. With in it I assign the input array to a second array and temporarily make the array location after first iteration as nan. I expected this temporary change to be limited to the second variable. However my initial variable gets modified. Can any one through some light to what is happening here?. In case of matlab this logic works. ## #FUNCTION maxn ## import numpy as np def max_n(a,n): b=a This is not making b a copy of a, it is simply making it an alias for it. To make it a copy you could use b = a[:], or b = a.copy() It sounds like you don't really need a function, however. Try this: # test data: a = np.random.randn(10) n = 2 # One-line solution: biggest_n = np.sort(a)[-n:] print a print biggest_n If you want them ordered from largest to smallest, just reverse the list: biggest_n = biggest_n[::-1] Eric result=[] for i in np.arange(1,n+1): mxidx=np.where(b==max(b)) result.append(mxidx) b[mxidx]=np.nan result=np.ravel(result) return(result) ### TEST In [8]: x=np.arange(float(0),10) In [9]: max max max_n In [9]: max_n(x,2) Out[9]: array([9, 8]) In [10]: x Out[10]: array([ 0., 1., 2., 3., 4., 5., 6., 7., nan, nan]) *** Sudheer Joseph Indian National Centre for Ocean Information Services Ministry of Earth Sciences, Govt. of India POST BOX NO: 21, IDA Jeedeemetla P.O. Via Pragathi Nagar,Kukatpally, Hyderabad; Pin:5000 55 Tel:+91-40-23886047(O),Fax:+91-40-23895011(O), Tel:+91-40-23044600(R),Tel:+91-40-9440832534(Mobile) E-mail:sjo.in...@gmail.com;sudheer.jos...@yahoo.com Web- http://oppamthadathil.tripod.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 ___ 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] strange behavior of variable
Try: np.argsort(a)[-n:] -n On 18 Aug 2013 10:10, Sudheer Joseph sudheer.jos...@yahoo.com wrote: However, I was looking for the indices of the biggest 3 numbers which can be used to find corresponding values in another vector, which made me to write the function. Is there a smarter way for getting indices? with best regards, Sudheer Thanks a lot Eric, Here comes the smartness of python!!, I am just climbing the bottom steps... with best regards, Sudheer *** Sudheer Joseph Indian National Centre for Ocean Information Services Ministry of Earth Sciences, Govt. of India POST BOX NO: 21, IDA Jeedeemetla P.O. Via Pragathi Nagar,Kukatpally, Hyderabad; Pin:5000 55 Tel:+91-40-23886047(O),Fax:+91-40-23895011(O), Tel:+91-40-23044600(R),Tel:+91-40-9440832534(Mobile) E-mail:sjo.in...@gmail.com;sudheer.jos...@yahoo.com Web- http://oppamthadathil.tripod.com *** -- *From:* Eric Firing efir...@hawaii.edu *To:* numpy-discussion@scipy.org *Sent:* Sunday, 18 August 2013 1:57 PM *Subject:* Re: [Numpy-discussion] strange behavior of variable On 2013/08/17 9:49 PM, Sudheer Joseph wrote: Hi, I have defined a small function to find the n maximum values of an array as below. With in it I assign the input array to a second array and temporarily make the array location after first iteration as nan. I expected this temporary change to be limited to the second variable. However my initial variable gets modified. Can any one through some light to what is happening here?. In case of matlab this logic works. ## #FUNCTION maxn ## import numpy as np def max_n(a,n): b=a This is not making b a copy of a, it is simply making it an alias for it. To make it a copy you could use b = a[:], or b = a.copy() It sounds like you don't really need a function, however. Try this: # test data: a = np.random.randn(10) n = 2 # One-line solution: biggest_n = np.sort(a)[-n:] print a print biggest_n If you want them ordered from largest to smallest, just reverse the list: biggest_n = biggest_n[::-1] Eric result=[] for i in np.arange(1,n+1): mxidx=np.where(b==max(b)) result.append(mxidx) b[mxidx]=np.nan result=np.ravel(result) return(result) ### TEST In [8]: x=np.arange(float(0),10) In [9]: max maxmax_n In [9]: max_n(x,2) Out[9]: array([9, 8]) In [10]: x Out[10]: array([ 0., 1., 2., 3., 4., 5., 6., 7., nan, nan]) *** Sudheer Joseph Indian National Centre for Ocean Information Services Ministry of Earth Sciences, Govt. of India POST BOX NO: 21, IDA Jeedeemetla P.O. Via Pragathi Nagar,Kukatpally, Hyderabad; Pin:5000 55 Tel:+91-40-23886047(O),Fax:+91-40-23895011(O), Tel:+91-40-23044600(R),Tel:+91-40-9440832534(Mobile) E-mail:sjo.in...@gmail.com;sudheer.jos...@yahoo.com Web- http://oppamthadathil.tripod.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 ___ 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 ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] strange behavior of variable
Tank you, It worked fine, with best regards, Sudheer From: Nathaniel Smith n...@pobox.com To: Discussion of Numerical Python numpy-discussion@scipy.org Sent: Sunday, 18 August 2013 3:01 PM Subject: Re: [Numpy-discussion] strange behavior of variable Try: np.argsort(a)[-n:] -n On 18 Aug 2013 10:10, Sudheer Joseph sudheer.jos...@yahoo.com wrote: However, I was looking for the indices of the biggest 3 numbers which can be used to find corresponding values in another vector, which made me to write the function. Is there a smarter way for getting indices? with best regards, Sudheer Thanks a lot Eric, Here comes the smartness of python!!, I am just climbing the bottom steps... with best regards, Sudheer *** Sudheer Joseph Indian National Centre for Ocean Information Services Ministry of Earth Sciences, Govt. of India POST BOX NO: 21, IDA Jeedeemetla P.O. Via Pragathi Nagar,Kukatpally, Hyderabad; Pin:5000 55 Tel:+91-40-23886047(O),Fax:+91-40-23895011(O), Tel:+91-40-23044600(R),Tel:+91-40-9440832534(Mobile) E-mail:sjo.in...@gmail.com;sudheer.jos...@yahoo.com Web- http://oppamthadathil.tripod.com *** From: Eric Firing efir...@hawaii.edu To: numpy-discussion@scipy.org Sent: Sunday, 18 August 2013 1:57 PM Subject: Re: [Numpy-discussion] strange behavior of variable On 2013/08/17 9:49 PM, Sudheer Joseph wrote: Hi, I have defined a small function to find the n maximum values of an array as below. With in it I assign the input array to a second array and temporarily make the array location after first iteration as nan. I expected this temporary change to be limited to the second variable. However my initial variable gets modified. Can any one through some light to what is happening here?. In case of matlab this logic works. ## #FUNCTION maxn ## import numpy as np def max_n(a,n): b=a This is not making b a copy of a, it is simply making it an alias for it. To make it a copy you could use b = a[:], or b = a.copy() It sounds like you don't really need a function, however. Try this: # test data: a = np.random.randn(10) n = 2 # One-line solution: biggest_n = np.sort(a)[-n:] print a print biggest_n If you want them ordered from largest to smallest, just reverse the list: biggest_n = biggest_n[::-1] Eric result=[] for i in np.arange(1,n+1): mxidx=np.where(b==max(b)) result.append(mxidx) b[mxidx]=np.nan result=np.ravel(result) return(result) ### TEST In [8]: x=np.arange(float(0),10) In [9]: max max max_n In [9]: max_n(x,2) Out[9]: array([9, 8]) In [10]: x Out[10]: array([ 0., 1., 2., 3., 4., 5., 6., 7., nan, nan]) *** Sudheer Joseph Indian National Centre for Ocean Information Services Ministry of Earth Sciences, Govt. of India POST BOX NO: 21, IDA Jeedeemetla P.O. Via Pragathi Nagar,Kukatpally, Hyderabad; Pin:5000 55 Tel:+91-40-23886047(O),Fax:+91-40-23895011(O), Tel:+91-40-23044600(R),Tel:+91-40-9440832534(Mobile) E-mail:sjo.in...@gmail.com;sudheer.jos...@yahoo.com Web- http://oppamthadathil.tripod.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 ___ 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 ___ 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
[Numpy-discussion] 1.8.0 branch reminder
Just a reminder that 1.8.0 will be branched tonight. I've put up a big STY: PR https://github.com/numpy/numpy/pull/3635 that removes trailing whitespace and fixes spacing after commas. I would like to apply before the branch, but it may cause merge difficulties down the line. I'd like feedback on that option. Thoughts? Chuck ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] 1.8.0 branch reminder
On Sun, Aug 18, 2013 at 12:17 PM, Charles R Harris charlesr.har...@gmail.com wrote: Just a reminder that 1.8.0 will be branched tonight. I've put up a big STY: PR https://github.com/numpy/numpy/pull/3635 that removes trailing whitespace and fixes spacing after commas. I would like to apply before the branch, but it may cause merge difficulties down the line. I'd like feedback on that option. I've also run autopep8 on the code and it does a nice job of cleaning up things. It gets a little lost in deeply nested lists, but there aren't too many of those. By default it doesn't fix spaces about operator (it seems). I can apply that also if there is interest in doing so. Chuck ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
[Numpy-discussion] Adding an axis argument to numpy.unique
Hi everyone, I've recently put together a pull request that adds an `axis` kwarg to `numpy.unique` so that `unique`can easily be used to find unique rows/columns/sub-arrays/etc of a larger array. https://github.com/numpy/numpy/pull/3584 Currently, this works as a warpper around `unique`. If `axis` is specified, it reshapes the input to a 2D contiguous array, views each row as a single item, then passes it on to `unique`. For int and string dtypes, each row is viewed as a void dtype and therefore bitwise-equality is used for comparisons. For all other dtypes, the each row is viewed as a structured array. The current implementation has two main drawbacks: 1. For anything other than ints and strings, it's relatively slow. 2. It doesn't work with object arrays of any sort. I'd appreciate any thoughts/feedback folks might have on both the general idea and this specific implementation. It think it's a worthwhile addition, but I'm biased. Thanks! -Joe ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
[Numpy-discussion] maintenance/1.8.x has been branched.
Master is open for 1.9. Chuck ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
[Numpy-discussion] Removal of numarray and oldnumeric modules.
Hi All, I've put up a PR https://github.com/numpy/numpy/pull/3638 that removes the numarray and oldnumeric modules from Numpy 1.9. The hope is that 1.9 will be released in five to six months. If you have trouble with removal on that schedule, now is the time to complain. Chuck ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion