From 7ed6c4ac3ea5c409c073f1db3e62d989ffe5f351 Mon Sep 17 00:00:00 2001 From: Gertjan van den Burg Date: Tue, 12 Dec 2017 20:18:28 -0500 Subject: added gridsearch and extended gensvm class --- gensvm/util.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 gensvm/util.py (limited to 'gensvm/util.py') diff --git a/gensvm/util.py b/gensvm/util.py new file mode 100644 index 0000000..1e79d75 --- /dev/null +++ b/gensvm/util.py @@ -0,0 +1,32 @@ +""" +Utility functions for GenSVM + +""" + + +import numpy as np + + +def get_ranks(x): + """ + Rank data in an array. Low values get a small rank number. Ties are broken + by assigning the lowest value. + + Examples + -------- + >>> x = [7, 0.1, 0.5, 0.1, 10, 100, 200] + >>> get_ranks(x) + [4, 1, 3, 1, 5, 6, 7] + + """ + x = np.ravel(np.asarray(x)) + l = len(x) + r = 1 + ranks = np.zeros((l, )) + while not all([k is None for k in x]): + m = min([k for k in x if not k is None]) + idx = [1 if k == m else 0 for k in x] + ranks = [r if idx[k] else ranks[k] for k in range(l)] + r += sum(idx) + x = [None if idx[k] else x[k] for k in range(l)] + return ranks -- cgit v1.2.3