Skip to content

Instantly share code, notes, and snippets.

@acadien
Last active August 29, 2015 13:56
Show Gist options
  • Save acadien/9313820 to your computer and use it in GitHub Desktop.
Save acadien/9313820 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from numpy import *
x = random.random([10,3])
x[range(10), fabs(x-0.5).argsort()[:,0]]
#Break it down niao
x1 = fabs(x-0.5) # numbers closest to 0.5 will be closest to 0 now.
x2 = x1.argsort() # returns the indexes of each sorted *row*, numbers closest to 0 are first
x3 = x2[:,0] # give me just the index of the smallest number
x4 = x[range(10),x3] # index back into the original array using the row information you've gathered
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment