Skip to content

Instantly share code, notes, and snippets.

@zhouhoo
Created May 16, 2017 12:55
Show Gist options
  • Save zhouhoo/5b1c7a9e86ad52b26e3a0adbdc5aaab9 to your computer and use it in GitHub Desktop.
Save zhouhoo/5b1c7a9e86ad52b26e3a0adbdc5aaab9 to your computer and use it in GitHub Desktop.
cool and efficient code in numpy.
1. sort an array by the nth column
Z = np.random.randint(0,10,(3,3))
print(Z)
print(Z[Z[:,1].argsort()])
2. Find the nearest value from a given value in an array
m = Z.flat[np.abs(Z - z).argmin()]
3. accumulate elements of a vector (X) to an array (F) based on an index list (I)
X = [1,2,3,4,5,6]
I = [1,3,9,3,4,1]
F = np.bincount(I,X)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment