【发布时间】: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')]
可能是什么问题?
【问题讨论】:
-
i是z的索引,所以你应该使用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)错误的事实仅仅意味着您的代码逻辑一开始就有缺陷。你必须自己弄清楚你想要追求的算法,因为我们对你的代码上下文一无所知。