【问题标题】:Can't replace value upon condition? [closed]不能按条件替换值? [关闭]
【发布时间】:2020-09-18 16:05:20
【问题描述】:

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

以下是数据:

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

可能是什么问题?

【问题讨论】:

  • iz 的索引,所以你应该使用xa=-np.sort(-z[i]) 或者干脆xa=-np.sort(-p),如果这确实是你所追求的逻辑。
  • @blhsing 感谢您的回答。当我输入 xa=-np.sort(-z[i])xa=-np.sort(-p) 时,我得到:TypeError: bad operand type for unary -: 'str'
  • 我只是指出了一个显而易见的问题,即 i 不应用作 proba_ex. 的索引,因为它是从 z 的枚举派生的。您收到xa=-np.sort(-p) 错误的事实仅仅意味着您的代码逻辑一开始就有缺陷。你必须自己弄清楚你想要追求的算法,因为我们对你的代码上下文一无所知。

标签: python list for-loop


【解决方案1】:

经过一些调试我发现它,你改变了内部循环内的变量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')]

只要第一个数组满足条件,而第二个不满足,这就有意义

【讨论】:

  • 那么需要进行哪些更改才能生效?
  • 你能告诉我怎么解决吗?
  • 已编辑,现在没有错误了。请注意,我不完全理解您的程序的目的。但是我通过丢弃已经替换的索引的循环情况来避免错误(它们的类型为str
  • 好的,我会研究它,如果可以的话,我会告诉你并标记你的答案。thnks
  • 我已经对我的问题进行了编辑,以进一步帮助您了解代码的逻辑。也许您已经接近解决方案,但我不知道发生了什么。请检查我所做的编辑,看看您是否知道需要什么。
猜你喜欢
  • 2023-03-15
  • 2015-03-31
  • 2013-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多