-1

I have built a Sequential model to predict dataset. The output is an array of values something like below:

[[25.672846 ]
 [35.517834 ]
 [16.25976  ]
 [27.06339  ]
 [15.176579 ]
 [20.261982 ]
 [17.346558 ]
 [14.3797455]
 [23.327494 ]
 [17.967245 ]
 [17.85637  ]
 [16.672417 ]
 [ 6.2621603]
 [20.35404  ]]

I want to sort the array in ascending order.

I've tried

pred = np.sort(model.predict(data))

and

pred = np.argsort(model.predict(data))

and I've failed.

1
  • This is a Numpy array sorting question, and it has nothing to do with machine-learning or Keras (tags edited). Commented May 26, 2021 at 8:16

3 Answers 3

2

You should use np.sort as follows:

pred = np.sort(model.predict(data), axis=0)

Default behaviour is axis=-1 which is equivalent to axis=1 in your case.

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

Comments

0

If you want to sort the array in the number you can use the below

pred = model.predit(data)
pred.sort() 
print(p)

This will show sorted preds. By default, the sort is in ascending order

if you want the index of the sorted

pred = model.predict(data)
sorted_index = pred.argsort(axis=0)
print(sorted_index)

Comments

0

Apply np.reshape(x, -1) before sorting. This way you will have a flat array which is something you might want to do eventually after sorting anyway. This is why I would prefer this over specifying the axis which is otherwise a good solution too.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.