首页 > 解决方案 > 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'

任何人都可以帮助我吗?

标签: python

解决方案


int() 函数一次只能处理一个数字。您正在尝试将“地图”对象转换为 int 类型。

numpy 转置函数返回一个映射对象 - 当您尝试访问“固定”中的值时

    r = fixated[np.transpose(indexer)]

您应该使用整数索引而不是地图。


推荐阅读