-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtop_k_array.py
55 lines (51 loc) · 1.43 KB
/
top_k_array.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
import numpy as np
def get_top_k_index(arr, k_rate):
"""
Get top k element index in 1-D or 2-D ndarray
"""
arr = np.absolute(arr)
shape = list(arr.shape)
if len(shape) == 1:
if k_rate > 1:
k = k_rate
else:
k = int(shape[0] * k_rate)
if k < 1:
k = 1
index = np.argpartition(arr, -k)[-k:]
elif len(shape) == 2:
"""
flatten to get global top-k
"""
if k_rate > 1:
k = k_rate
else:
k = int(shape[0] * shape[1] * k_rate)
if k < 1:
k = 1
flatten_arr = arr.flatten()
flatten_idx = np.argpartition(flatten_arr, -k)[-k:]
row_len = shape[0]
column_len = shape[1]
index = [divmod(i, column_len) for i in flatten_idx]
"""
To get local top-k each column
"""
# if k_rate > 1:
# k = k_rate
# else:
# k = int(shape[0] * k_rate)
# re_arr = arr.T
# index = []
# for i, column_data in enumerate(re_arr):
# local_index = [(j, i) for j in np.argpartition(column_data, -k)[-k:]]
# index.extend(local_index)
else:
raise "Not Implemented."
return index
def get_top_k_array(arr, k_rate):
top_k_array = np.zeros_like(arr)
index = get_top_k_index(arr, k_rate)
for i in index:
top_k_array[i] = arr[i]
return top_k_array