首页 > 解决方案 > 不能按条件替换值?

问题描述

我收到一个错误“列表索引超出范围”,但在检查代码后我不明白出了什么问题。

以下是数据:

import numpy as np
proba_ex=[np.array([[0.00639649, 0.00251385, 0.00729689, 0.007919488, 0.00368546,
         0.00068663],
        [0.08320138, 0.04170561, 0.04755181, 0.42204733, 0.15220323,
         0.10432409]]),np.array([[0.14774831, 0.09566049, 0.30208335, 0.05015277, 0.10008666,
         0.06229035],
        [0.0573518 , 0.095365  , 0.11942169, 0.12236523, 0.21965485,
         0.11497026]])]


pred_data_ex=[np.array(['class3','class4']),np.array(['class1','class7'])]

下面是代码,它的作用是:如果带有下面注释的条件给出的结果小于 0.1,它应该用pred_data_ex单词“UNK”替换。

for x in proba_ex:
    pt=0.1
    for z in pred_data_ex:
        sproba=[]
    
        for i,p in enumerate(z):
            xa=-np.sort(-proba_ex[i])
            if xa[0][0]-xa[0][1]<pt:   #here is the condition
                pred_data_ex[i]=u'<UNK>'

当我运行它时,我得到:

  IndexError                                Traceback (most recent call last)
    <ipython-input-21-fb2d5516c0db> in <module>
          6 
          7         for i,p in enumerate(z):
    ----> 8             xa=-np.sort(-proba_ex[i])
          9             print(xa)
         10           #  sproba.append(xa)
    
    IndexError: list index out of range

编辑: 最终结果应如下所示:

pred_data_ex 的输出应该类似于:

[np.array(['UNK','class4']),np.array(['class1','UNK'])]

如您所见,它会根据所描述的条件替换“UNK”一词。

编辑2 解释

@Yossi 来自您的代码,结果如下:

xa: [[0.00791949 0.00729689 0.00639649 0.00368546 0.00251385 0.00068663]
 [0.42204733 0.15220323 0.10432409 0.08320138 0.04755181 0.04170561]]
xa: [[0.30208335 0.14774831 0.10008666 0.09566049 0.06229035 0.05015277]
 [0.21965485 0.12236523 0.11942169 0.11497026 0.095365   0.0573518 ]]
xa: [[0.00791949 0.00729689 0.00639649 0.00368546 0.00251385 0.00068663]
 [0.42204733 0.15220323 0.10432409 0.08320138 0.04755181 0.04170561]]
xa: [[0.30208335 0.14774831 0.10008666 0.09566049 0.06229035 0.05015277]
 [0.21965485 0.12236523 0.11942169 0.11497026 0.095365   0.0573518 ]]
xa: [[0.00791949 0.00729689 0.00639649 0.00368546 0.00251385 0.00068663]
 [0.42204733 0.15220323 0.10432409 0.08320138 0.04755181 0.04170561]]
xa: [[0.30208335 0.14774831 0.10008666 0.09566049 0.06229035 0.05015277]
 [0.21965485 0.12236523 0.11942169 0.11497026 0.095365   0.0573518 ]]
xa: [[0.00791949 0.00729689 0.00639649 0.00368546 0.00251385 0.00068663]
 [0.42204733 0.15220323 0.10432409 0.08320138 0.04755181 0.04170561]]
xa: [[0.30208335 0.14774831 0.10008666 0.09566049 0.06229035 0.05015277]
 [0.21965485 0.12236523 0.11942169 0.11497026 0.095365   0.0573518 ]]
[array(['<UNK>', 'class4'], dtype='<U6'), array(['<UNK>', 'class7'], dtype='<U6')]

看起来结果不正确,因为如果您检查计算,它应该给出:

[np.array(['UNK','class4']),np.array(['class1','UNK'])]

因为:

0.00791949 -0.00729689<0.1
True (which makes it take the 'UNK' value)

0.42204733 -0.15220323<0.1
False

0.30208335 -0.14774831<0.1
False

0.21965485- 0.12236523<0.1
True (which makes it take the 'UNK' value)

因此应该是:

[np.array(['UNK','class4']),np.array(['class1','UNK'])]

你的结果给出:

[array(['<UNK>', 'class4'], dtype='<U6'), array(['<UNK>', 'class7'], dtype='<U6')]

可能是什么问题?

标签: pythonlistfor-loop

解决方案


经过一些调试后我发现它,您更改了pred_data_ex内部循环内的变量,使得在某些时候z大于 2 个元素(您实际上循环了一个超过 2 个字符的字符串,看看p)。这就是您通过列表大小访问 proba_ex 的原因。

这是使我弄清楚的添加内容(几次打印),看看 的值zpi沿着循环的迭代:

import numpy as np

proba_ex=[np.array([[0.00639649, 0.00251385, 0.00729689, 0.007919488, 0.00368546,
         0.00068663],
        [0.08320138, 0.04170561, 0.04755181, 0.42204733, 0.15220323,
         0.10432409]]),np.array([[0.14774831, 0.09566049, 0.30208335, 0.05015277, 0.10008666,
         0.06229035],
        [0.0573518 , 0.095365  , 0.11942169, 0.12236523, 0.21965485,
         0.11497026]])]


pred_data_ex=[np.array(['class3','class4']),np.array(['class1','class7'])]

for x in proba_ex:
    pt=0.1
    for j,z in enumerate(pred_data_ex):
        sproba=[]
        if not isinstance(z,str):
            for i,p in enumerate(z):
                xa=-np.sort(-proba_ex[i])
                print("xa: " + str(xa))
                if xa[0][0]-xa[0][1]<pt:   #here is the condition
                    pred_data_ex[j][i]=u'<UNK>'

print(pred_data_ex)

对 proba_ex 排序后的第一个元素和第二个元素之间的差异对于第二个数组大于 0.1,而对于第一个数组则较小。这与您的代码相对应意味着 pred-data_ex 中每个双重元素的第一个元素将根据您的意愿替换为 '' ,而不是您所说的预期。

我的输出:

[array(['<UNK>', 'class4'], dtype='<U6'), array(['<UNK>', 'class7'], dtype='<U6')]

只要第一个数组满足条件而第二个不满足条件,这是有道理的


推荐阅读