2

I have a list in python and the first numbers are [[29.046875, 1], [33.65625, 1], [18.359375, 1], [11.296875, 1], [36.671875, 1], [23.578125, 1],.........,[34.5625, 1]]

The above list is given an id of listNumber. I'm trying to use numpy.argsort to sort it based on the float elements:

listNumber = np.array(listNumber)
print(np.argsort(listNumber))

But this gives me the following but not sure why:

[[1 0]
 [1 0]
 [1 0]
 ...
 [1 0]
 [1 0]
 [1 0]]

Why is this returning this? and is there another way to approach this?

2

4 Answers 4

3

Ok so i think there's two things going on here:

1- Your list is a list of lists

2- The 'argsort' function:

returns the indices that would sort an array.

According to the documentation.

So what is happening is the function reads through each item of the list, which in itself is a list, say index 0 is:

[29.046875, 1]

Then it is saying, okay this is another list so let me sort it and then return a number based on where it would go if it was the new index:

[29.046875, 1] -> [1, 0]

Because 1 would come before 29 if it was sorted in ascending order. It does this for every nested list then gives you a final list containing all these 1's and 0's.

This answers the first question. Another user was able to answer the second :)

Sign up to request clarification or add additional context in comments.

Comments

2

You must set axis like:

import numpy as np

l = [[29.046875, 1], [33.65625, 1], [18.359375, 1], [11.296875, 1], [36.671875, 1], [23.578125, 1],[34.5625, 1]]
l = np.argsort(l, axis=0)  # sorts along first axis (down)
print(l)

output:

[[3 0]
 [2 1]
 [5 2]
 [0 3]
 [1 4]
 [6 5]
 [4 6]]

Comments

0

Try this;

sortedList = listNumber[listNumber[:,0].argsort(axis=0)]
print(sortedList)

Comments

0

I don't know why people like using predone functions instead of using their own algorithm. Anyway, you are using argsort in a bad way. argsort returns an array containing the INDEXES of your elements, thos are 2 examples :

Code 1:


import numpy as geek 

# input array 

in_arr = geek.array([ 2, 0,  1, 5, 4, 1, 9]) 

print ("Input unsorted array : ", in_arr)  

out_arr = geek.argsort(in_arr) 

print ("Output sorted array indices : ", out_arr) 

print("Output sorted array : ", in_arr[out_arr]) 

Output :

Input unsorted array :  [2 0 1 5 4 1 9]
Output sorted array indices :  [1 2 5 0 4 3 6]
Output sorted array :  [0 1 1 2 4 5 9]

Code 2:


# Python program explaining 
# argpartition() function 

import numpy as geek 

# input 2d array 

in_arr = geek.array([[ 2, 0, 1], [ 5, 4, 3]]) 

print ("Input array : ", in_arr)  


# output sorted array indices 

out_arr1 = geek.argsort(in_arr, kind ='mergesort', axis = 0) 

print ("Output sorteded array indices along axis 0: ", out_arr1) 

out_arr2 = geek.argsort(in_arr, kind ='heapsort', axis = 1) 

print ("Output sorteded array indices along axis 1: ", out_arr2) 

Output:

Input array :  [[2 0 1]
 [5 4 3]]
Output sorteded array indices along axis 0:  [[0 0 0]
 [1 1 1]]
Output sorteded array indices along axis 1:  [[1 2 0]
 [2 1 0]]

I am supposing that your data is stored in listnumber

import numpy as np
new_listnumber = listnumber[:, 0]
index_array = np.argsort(new_listnumber , axis=0)
New_val = listnumber[index_array] 
print(New_val) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.