-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arguments.py
62 lines (45 loc) · 2.04 KB
/
Arguments.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import numpy as np
def multi_argmax(values, n_instances: int = 1):
"""
Selects the indeices of the n_instances highest values
:param values: contains the values to be selected from
:param n_instances: specifies how many indices to return
:return: the indices of the n_instances largest values
"""
assert n_instances <= values.shape[0], 'n_instances must be less or equal than the size'
###Making Changes - Need to speak to Jeremie ###TODO Speak to Jeremie\
"""
import pandas as pd
test_max_1 = np.argpartition(-values, n_instances - 1, axis=0)[:n_instances]
test_df_1 = pd.DataFrame(test_max_1)
test_max = np.argsort(values, kind='mergesort')[len(values) - n_instances:]
test_df = pd.DataFrame(test_max)
"""
if values.shape[0] == n_instances:
max_idx = np.argsort(values, kind='mergesort')[len(values) - n_instances:]
else:
max_idx = np.argpartition(-values, n_instances - 1, axis=0)[:n_instances]
return max_idx
def shuffled_argmax(values, n_instances: int = 1):
"""
Shuffles the values and sorts them afterwards. This can be used to break the tie when the highest score is not
unique. The shuffle randomizes orderm which is preserved by the mergersort algorithm
:param values: Contains the values to be selected from
:param n_instances: specifies how many indices to return
:return: the indices of the n_instances largest values
"""
assert n_instances <= values.shape[0], 'n_instances must be less than or equal'
shuffled_idx = np.random.permutation(len(values))
shuffled_values = values[shuffled_idx]
sorted_query_idx = np.argsort(shuffled_values, kind='mergesort')[len(shuffled_values) - n_instances:]
query_idx = shuffled_idx[sorted_query_idx]
return query_idx
def _is_arraylike(x):
return (hasattr(x, '__len__') or
hasattr(x, 'shape') or
hasattr(x, '__array__'))
def nlargestarg(a, n):
assert (_is_arraylike(a))
assert (n > 0)
argret = np.argsort(a)
return argret[argret.size - n:]