python - AUC_shuffled 分数:IndexError:用作索引的数组必须是整数(或布尔)类型
问题描述
我需要使用学术论文中的代码计算一种 AUC 分数。但我不断收到同样的错误。根据回溯,似乎是因为索引不是整数。所以我int()
给它添加了一个功能。但是后来出现问题是因为它被用作地图对象。
def AUC_shuffled(saliency_map, fixation_map, other_map, n_rep=100, step_size=0.1):
'''
Parameters
----------
saliency_map : real-valued matrix
fixation_map : binary matrix
Human fixation map.
other_map : binary matrix, same shape as fixation_map
A binary fixation map (like fixation_map) by taking the union of fixations from M other random images
(Borji uses M=10).
n_rep : int, optional
Number of repeats for random sampling of non-fixated locations.
step_size : int, optional
Step size for sweeping through saliency map.
Returns
-------
AUC : float, between [0,1]
'''
other_map = np.array(other_map, copy=False) > 0.5
if other_map.shape != fixation_map.shape:
raise ValueError('other_map.shape != fixation_map.shape')
# For each fixation, sample n_rep values (from fixated locations on other_map) on the saliency map
def sample_other(other, S, F, n_rep, n_fix):
fixated = np.nonzero(other)[0]
indexer = map(lambda x: random.permutation(x)[:n_fix], np.tile(range(len(fixated)), [n_rep, 1]))
r = fixated[np.transpose(indexer)]
S_rand = S[r] # Saliency map values at random locations (including fixated locations!? underestimated)
return S_rand
return AUC_Borji(saliency_map, fixation_map, n_rep, step_size, partial(sample_other, other_map.ravel()))
完整的追溯在这里:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-121-ffb2b08fa4b6> in <module>()
----> 1 AUC_shuffled(b, c, o)
2 frames
<ipython-input-106-305a871c6ad5> in AUC_shuffled(saliency_map, fixation_map, other_map, n_rep, step_size)
258 S_rand = S[r] # Saliency map values at random locations (including fixated locations!? underestimated)
259 return S_rand
--> 260 return AUC_Borji(saliency_map, fixation_map, n_rep, step_size, partial(sample_other, other_map.ravel()))
261
262
<ipython-input-106-305a871c6ad5> in AUC_Borji(saliency_map, fixation_map, n_rep, step_size, rand_sampler)
205 S_rand = S[r] # Saliency map values at random locations (including fixated locations!? underestimated)
206 else:
--> 207 S_rand = rand_sampler(S, F, n_rep, n_fix)
208 # Calculate AUC per random split (set of random locations)
209 auc = np.zeros(n_rep) * np.nan
<ipython-input-106-305a871c6ad5> in sample_other(other, S, F, n_rep, n_fix)
255 fixated = np.nonzero(other)[0]
256 indexer = map(lambda x: random.permutation(x)[:n_fix], np.tile(range(len(fixated)), [n_rep, 1]))
--> 257 r = fixated[np.transpose(indexer)]
258 S_rand = S[r] # Saliency map values at random locations (including fixated locations!? underestimated)
259 return S_rand
IndexError: arrays used as indices must be of integer (or boolean) type
我尝试添加函数,但我得到indexer = int(indexer)
了sample_other
TypeError: int() argument must be a string, a bytes-like object or a number, not 'map'
任何人都可以帮助我吗?
解决方案
int() 函数一次只能处理一个数字。您正在尝试将“地图”对象转换为 int 类型。
numpy 转置函数返回一个映射对象 - 当您尝试访问“固定”中的值时
r = fixated[np.transpose(indexer)]
您应该使用整数索引而不是地图。
推荐阅读
- python - 交换数据框中列的内容
- sql - 为什么这个 TSQL LIKE 语句总是找不到逗号?
- react-native - react native - 改变导航方向(即从右到左)
- python - 如何从 R 中的 xlsx 文件中检测“删除线”样式
- python - Python3 帮助确定动态创建列表的大多数 Pythonic 方法
- hide - 在大文件上运行宏时 Excel 冻结(隐藏粗体行并转置到新工作表)
- html - html 文件中缺少图像
- c# - .Net 中的数据保护
- c++ - 如何解决 Devc++ 找不到 afxwin.h 文件?
- java - EditText,在 Listview 内,调用 setLayoutParams() 时光标移到前面;