Python 3.x 在循环中迭代时列表索引超出范围-Python
我正在进行遗漏模型验证过程。当我运行一个循环,留下列表中的一项进行测试时,当I=19时,它停止。但是当我用I=19手动一个接一个地运行时,它运行得很好。特征的长度是36Python 3.x 在循环中迭代时列表索引超出范围-Python,python-3.x,indexoutofboundsexception,Python 3.x,Indexoutofboundsexception,我正在进行遗漏模型验证过程。当我运行一个循环,留下列表中的一项进行测试时,当I=19时,它停止。但是当我用I=19手动一个接一个地运行时,它运行得很好。特征的长度是36 for i in range(len(features)): # i = 18 w_count = word_count[i] x_test_c = features[i][['count']].copy() x_test = features[i]
for i in range(len(features)):
# i = 18
w_count = word_count[i]
x_test_c = features[i][['count']].copy()
x_test = features[i]
x_test.drop('count', axis=1, inplace=True)
x_train_list = features
x_train_list.pop(i)
y_test = summaries[i]
y_train_list = summaries
y_train_list.pop(i)
x_train = merge_data(x_train_list)
x_train.drop('count', axis=1, inplace=True)
y_train = merge_data(y_train_list)
print(x_train.shape,"\t",y_train.shape)
print(x_test.shape,"\t",y_test.shape)
model = sm.OLS(y_train, x_train, missing='drop').fit()
predictions = model.predict(x_test)
predictions = predictions.sort_values(ascending=False)
print("\n\nLeave one out cross validation \nTest report:",i+1)
match(predictions, w_count, x_test_c, y_test)
示例输出如下所示
(sysenv) D:\pythonprojects\rec_proj>python main.py
Leave one out cross validation
Test report: 1
total word count of report: 509
summary word count: ~ 127.25
['2.4', '1.5', '3.2']
Precision= 1.0
Recall= 0.21428571428571427
F1= 0.35294117647058826
....
Leave one out cross validation
Test report: 18
total word count of report: 380
summary word count: ~ 95.0
['5.3', '12.2', '1.14', '5.2']
Precision= 0.75
Recall= 0.12
F1= 0.20689655172413793
Traceback (most recent call last):
File "main.py", line 49, in <module>
lou(df_len, df_summary, word_count)
File "D:\pythonprojects\rec_proj\model_eval.py", line 33, in lou
x_test_c = features[i][['count']].copy()
IndexError: list index out of range
它在这个迭代之后停止。
厄洛尔是这样的
(sysenv) D:\pythonprojects\rec_proj>python main.py
Leave one out cross validation
Test report: 1
total word count of report: 509
summary word count: ~ 127.25
['2.4', '1.5', '3.2']
Precision= 1.0
Recall= 0.21428571428571427
F1= 0.35294117647058826
....
Leave one out cross validation
Test report: 18
total word count of report: 380
summary word count: ~ 95.0
['5.3', '12.2', '1.14', '5.2']
Precision= 0.75
Recall= 0.12
F1= 0.20689655172413793
Traceback (most recent call last):
File "main.py", line 49, in <module>
lou(df_len, df_summary, word_count)
File "D:\pythonprojects\rec_proj\model_eval.py", line 33, in lou
x_test_c = features[i][['count']].copy()
IndexError: list index out of range
所以发现循环在18,27,30,33,35时失败。我无法调试它,因为它在手动插入这些值时工作正常。在Python中,range(n)
将生成从0
到n
的所有数字。为了帮助您将问题形象化,假设我们有一个简单的程序:
array = [0, 1, 2, 3, 4]
for m in range(len(array)): # len(array) evaluates to 5
print(m)
for n in array:
print(n)
输出(用空格替换换行符)将是:
0 1 2 3 4 5
0 1 2 3 4
如您所见,
range(len(array))
超出了数组的长度。这就是代码的作用。在第一行中,您启动一个for循环,该循环将在范围(len(features))
上循环,但在第四行中,您将访问features[i]
。因此,在循环的最后一次迭代中,range
超出了数组的长度,Python抛出了一个错误,因为代码试图访问features
中不存在的元素。我明白你的意思。但是如果你读了整篇文章,你会发现它停在i=17。特征的长度是36。由于在17时失败,我更改了范围(17,len(功能))
等等。这就是为什么我认为其他的I也会发生同样的错误。加上你对第一个循环的回答似乎不正确。我正在使用python 3.6。我刚刚测试了我的循环是否从0到36。不,35点停。我一定是想了些关于值域函数的不同的东西。我把它取下来。