Skip to content

Instantly share code, notes, and snippets.

@Robofied
Created February 15, 2019 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Robofied/6a7021cc7c0869e064d3359968658a73 to your computer and use it in GitHub Desktop.
Save Robofied/6a7021cc7c0869e064d3359968658a73 to your computer and use it in GitHub Desktop.
Numpy
import numpy as np
## 1. sort()
## Sorting along flattened array
a = np.array([[5,4],[3,1]])
np.sort(a)
#[Output]:
#array([[4, 5],
# [1, 3]])
## Sorting along first axis i.e, columns(down)
np.sort(a,axis=0)
#[Output]:
#array([[3, 1],
# [5, 4]])
## 2. argsort()
np.argsort(a.flatten())
#[Output]:
#array([3, 2, 1, 0], dtype=int64)
## Sorting along first axis
np.argsort(a,axis=0)
#[Output]:
#array([[1, 1],
# [0, 0]], dtype=int64)
## Sorting along last axis
np.argsort(a,axis=1)
#[Output]:
#array([[1, 0],
# [1, 0]], dtype=int64)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment