Created
May 16, 2017 12:55
-
-
Save zhouhoo/5b1c7a9e86ad52b26e3a0adbdc5aaab9 to your computer and use it in GitHub Desktop.
cool and efficient code in numpy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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