Reputation: 33
I have one input array with a shape of [1,500] in the form:
[[0][1][2] ... [499]]
To be combined with a random output array with a shape of [1,500] as:
[[22][16][11] ... [51]]
I have attempted using either concatenate or append like so (1 at a time):
inAndout = np.append(soloInput, outputMatrix, axis=1) #or:
inAndout = np.append(soloInput, outputMatrix, axis=1) #Commented 1 out each time
The output from both is fairly good with a form when print(inAndOut):
[[0 22][1 16][2 11] ... [499 51]] #Notice no ',' only spacing between added arrays
When I then use:
sortedInput = np.sort(inAndout, axis=0)
It results in:
[[0 11][1 16][2 22] ... [499 51]] #It sorts both inputs and outputs
Also when I use axis=1 it results in an unsorted matrix.
What I would like is for it to just sort based off outputs or just sort based off inputs like so:
[[0 22][1 16][2 22] ... [499 51]] #Sorted by inputs [[2 11][1 16][0 22] ... [499 51]] #Sorted by outputs
Any help is greatly appreciated!
Upvotes: 0
Views: 310
Reputation: 214977
You can use argsort
to return the indices that sorts either the input or the output and then reorder all columns with the indices:
Sort by input:
inAndout[inAndout[:,0].argsort(),:]
Sort by output:
inAndout[inAndout[:,1].argsort(),:]
Example:
inAndout = np.array([[0, 22],[1, 16],[2, 11],[499, 51]])
inAndout
#array([[ 0, 22],
# [ 1, 16],
# [ 2, 11],
# [499, 51]])
inAndout[inAndout[:,0].argsort(),:]
#array([[ 0, 22],
# [ 1, 16],
# [ 2, 11],
# [499, 51]])
inAndout[inAndout[:,1].argsort(),:]
#array([[ 2, 11],
# [ 1, 16],
# [ 0, 22],
# [499, 51]])
Upvotes: 1