【发布时间】:2018-11-27 22:47:42
【问题描述】:
我正在研究留一法模型验证过程。当我通过一个循环运行时,将列表中的一项进行测试,它会在 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]
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
但如果我插入 i = 18
Leave one out cross validation
Test report: 19
total word count of report: 741
summary word count: ~ 185.25
['3.10', '10.1', '2.2', '4.1', '5.3', '2.4']
Precision= 0.8333333333333334
Recall= 0.22727272727272727
F1= 0.35714285714285715
所以发现循环在 18、27、30、33、35 处失败。我无法调试它,因为手动插入这些值时它可以正常工作。
【问题讨论】:
标签: python-3.x indexoutofboundsexception