[Numpy-discussion] array sort questions

CL WU anewgene at hotpop.com
Wed Sep 17 15:28:06 CDT 2003


Great! It works much more efficiently. Thank you so much.

Best,

Chunlei

Tim Hochberg wrote:

> CL WU wrote:
>
>> Thank you, Tim.  argsort() and take() does provide a easy way to sort 
>> an array based on any col or row. But for the second question, it 
>> doesn't return the result I want.
>> As below, softrank or softrank1  are functions I am currently using 
>> for get the rank of a vector(first is more efficient). It returns the 
>> index of each value from original array/list in sorted array/list.
>
>
> Hmmm. It seems that argsort and sortrank are inverses of a sort, so it 
> should be possible to do what you want efficiently, but I'm not sure how.
>
> <think>
>
> Ah, it appears to be quite simple. I believe:
>
> argsort(argsort(a))
>
> is equivalent to your sortrank and should be much faster.
>
> regards,
>
> -tim
>
>
>> I hope there is an efficient function in array level to do the same 
>> work.
>> >>> from Numeric import *
>> >>> a=array([5,2,3])
>> >>> argsort(a)
>> array([1, 2, 0])
>> >>> def sortrank(list):
>> ...     n=len(list)
>> ...     li_a=[(i,list[i]) for i in range(n)]
>> ...     li_a.sort(lambda a,b:cmp(a[1],b[1]))
>> ...     li_b=[(i,li_a[i]) for i in range(n)]
>> ...     li_b.sort(lambda a,b:cmp(a[1][0],b[1][0]))
>> ...     return [x[0] for x in li_b]
>> ...    >>> sortrank(a)
>> [2, 0, 1]
>> >>> def sortrank2(li):
>> ...     li_sorted=li[:]
>> ...     li_sorted.sort()
>> ...     return [li_sorted.index(x) for x in li]
>> >>> sortrank1(list(a))
>> [2, 0, 1]
>>
>>
>> Thanks again.
>>
>> Chunlei
>>
>> Tim Hochberg wrote:
>>
>>> CL WU wrote:
>>>
>>>> Hi, group,
>>>>          I am new to numpy. I have 2 questions for array sort.
>>>>
>>>> 1. How to sort an array by its one column or one row?
>>>>    I know python build-in sort() can do it for list by passing own 
>>>> cmp function. but array function sort() will sort each column or 
>>>> row seperately,as I know. I don't want to convert array to list to 
>>>> sort and then convert back to array.
>>>
>>>
>>>
>>>
>>> I think you want argsort plus take. For example, the following sorts 
>>> on the second column of a:
>>>
>>>     a = array([[4,5,6], [1,2,3], [7,8,9]])
>>>     arg = argsort(a[:,1])
>>>     take(a, arg, 0)
>>>
>>>> 2. How to get the rank of a rank-0 array? The first "rank" means 
>>>> the order of each element after sorting, instead of the "dimension" 
>>>> meaning in numpy.     Just like "rank()" function in splus.
>>>
>>>
>>>
>>>
>>> If  I understand you correctly, you want argsort as mentioned above.
>>>
>>> Regards,
>>>
>>> -tim
>>>
>>>
>>>>
>>>> Thank you
>>>>
>>>> Chunlei
>>>>
>>>>
>>>>
>>>>
>>>> -------------------------------------------------------
>>>> This sf.net email is sponsored by:ThinkGeek
>>>> Welcome to geek heaven.
>>>> http://thinkgeek.com/sf
>>>> _______________________________________________
>>>> Numpy-discussion mailing list
>>>> Numpy-discussion at lists.sourceforge.net
>>>> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>>>>
>>>
>>>
>>>
>>>
>>>
>>> -------------------------------------------------------
>>> This sf.net email is sponsored by:ThinkGeek
>>> Welcome to geek heaven.
>>> http://thinkgeek.com/sf
>>> _______________________________________________
>>> Numpy-discussion mailing list
>>> Numpy-discussion at lists.sourceforge.net
>>> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>>>
>>
>>
>>
>>
>> -------------------------------------------------------
>> This sf.net email is sponsored by:ThinkGeek
>> Welcome to geek heaven.
>> http://thinkgeek.com/sf
>> _______________________________________________
>> Numpy-discussion mailing list
>> Numpy-discussion at lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>>
>
>
>
>






More information about the Numpy-discussion mailing list