我知道对于k折交叉验证,我应该将语料库分成k个相等的部分。在这k个部分中,我应该使用k-1个部分进行训练,剩下的1个部分用于测试。这个过程要重复k次,以便每个部分都用作一次测试。
但我不明白训练具体指的是什么以及测试具体指的是什么。
我的理解是(如果我错了请纠正我):
1. 训练集(k个中的k-1个):这些集合用于构建标签转换概率和发射概率表。然后,使用这些概率表应用某种算法进行标注(例如,维特比算法)。
2. 测试集(1个集合):使用剩下的1个集合来验证第一步中的实现。也就是说,这个集合中的词是未标注的,我应该在这一集合上使用第一步的实现进行标注。
我的理解正确吗?如果不对,请解释一下。
谢谢。
回答:
希望这对你有帮助:
from nltk.corpus import brownfrom nltk import UnigramTagger as ut# 我们只取前1000个句子。sents = brown.tagged_sents()[:1000]num_sents = len(sents)k = 10foldsize = num_sents/10fold_accurracies = []for i in range(10): # 定位折中的测试集。 test = sents[i*foldsize:i*foldsize+foldsize] # 使用不在测试集中的其余句子进行训练。 train = sents[:i*foldsize] + sents[i*foldsize+foldsize:] # 使用训练数据训练一个单字标注器。 tagger = ut(train) # 使用测试数据评估准确率。 accuracy = tagger.evaluate(test) print "Fold", i print 'from sent', i*foldsize, 'to', i*foldsize+foldsize print 'accuracy =', accuracy print fold_accurracies.append(accuracy)print 'average accuracy =', sum(fold_accurracies)/k
[out]:
Fold 0from sent 0 to 100accuracy = 0.785714285714Fold 1from sent 100 to 200accuracy = 0.745431364216Fold 2from sent 200 to 300accuracy = 0.749628896586Fold 3from sent 300 to 400accuracy = 0.743798291989Fold 4from sent 400 to 500accuracy = 0.803448275862Fold 5from sent 500 to 600accuracy = 0.779836277467Fold 6from sent 600 to 700accuracy = 0.772676371781Fold 7from sent 700 to 800accuracy = 0.755679184052Fold 8from sent 800 to 900accuracy = 0.706402915148Fold 9from sent 900 to 1000accuracy = 0.774622079707average accuracy = 0.761723794252