Created
February 29, 2012 18:46
-
-
Save stober/1943451 to your computer and use it in GitHub Desktop.
Random Argmax in Python
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
#!/usr/bin/env python | |
""" | |
Author: Jeremy M. Stober | |
Program: RARGMAX.PY | |
Date: Wednesday, February 29 2012 | |
Description: Simple rargmax function. | |
""" | |
import numpy as np | |
import random as pr | |
def rargmax(vector): | |
""" Argmax that chooses randomly among eligible maximum indices. """ | |
m = np.amax(vector) | |
indices = np.nonzero(vector == m)[0] | |
return pr.choice(indices) | |
if __name__ == '__main__': | |
test = [0,1,2,2] | |
for i in range(10): | |
print rargmax(test) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment