Skip to content

Instantly share code, notes, and snippets.

@Syncrossus
Last active September 5, 2019 08:41
Show Gist options
  • Save Syncrossus/1a69bdb78add4e83027f82f9c55180b4 to your computer and use it in GitHub Desktop.
Save Syncrossus/1a69bdb78add4e83027f82f9c55180b4 to your computer and use it in GitHub Desktop.
Function that transforms scores to ranks : takes a matrix with 2 columns with each row being of the form [object, score] and turns each row into the form [object, rank]. Highest score => rank 1
import numpy as np
def score2rank(m):
""" args:
- m, a numpy matrix with 2 columns the 2nd of which should be numeric
returns:
- m with the second column's numbers switched for ranks
example:
m = [[a,5.8], return = [[a,2],
[b,0.1], [b,5],
[c,0.3], [c,3],
[d,7.2], [d,1],
[e,0.2]] [e,4]]
"""
m[np.argsort(m[:, 1], axis=0), 1] = [[i] for i in range(m.shape[0], 0, -1)]
return m
@Syncrossus
Copy link
Author

Syncrossus commented Jun 8, 2018

This code is released under the WTFPL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment