无法根据条件替换值?

2024-06-02 11:31:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到一个错误“列表索引超出范围”,但在检查代码后,我无法理解出了什么问题

以下是数据:

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')]

可能是什么问题


Tags: 代码infordatanparrayexdtype
1条回答
网友
1楼 · 发布于 2024-06-02 11:31:06

经过一些调试,我找到了它,您更改了内部循环中的变量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,对于第一个数组小于0.1。这与您的代码相对应,意味着pred-data_ex中每个双元素的第一个元素将按照您的意愿替换为“”,而不是按照您的预期

我的输出:

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

只要第一个数组符合条件,而第二个数组不符合条件,这是有意义的

相关问题 更多 >