我是否需要为K折交叉验证中的每一折创建一个新的分类器?

时间:2019-08-13 03:33:35

标签: python nltk cross-validation k-fold

我正在尝试训练一个分类器来检测命令。 我的数据中有2000个命令项和2000个非命令项。 我使用4000(400)中的10%作为测试集,其余3600个句子用作分类器的训练集。 我尝试应用K折交叉验证的概念。 我的部分代码如下:

featuresets = [(document_features(d, word_features), c) for (d, c) in train]
train_set, test_set = featuresets[360:], featuresets[:360] 
#first 360 (first 10% of the data)sentences be the first test_set 

classifier = nltk.NaiveBayesClassifier.train(train_set)
a=nltk.classify.accuracy(classifier, test_set)

train_set2, test_set2= featuresets[:360]+featuresets[720:], 
featuresets[360:720] #second 10% of the sentences to be the second test_set 
classifier2 = classifier.train(train_set2)
b=nltk.classify.accuracy(classifier2, test_set2)

train_set3, test_set3 = featuresets[:720]+featuresets[1080:], 
featuresets[720:1080]
#Third 10% of the data be the third test_set 
classifier3 = classifier2.train(train_set3)
c=nltk.classify.accuracy(classifier3, test_set3)

train_set4, test_set4 = featuresets[:1080]+featuresets[1440:], 
featuresets[1080:1440]
#Fourth 10% of the data be the Fourth test_set 
classifier4 = classifier3.train(train_set4)
d=nltk.classify.accuracy(classifier4, test_set4)

我重复了相同的训练动作10次(我在代码中只显示了4次),因为10个不同部分的数据至少需要进行一次验证数据才能进行K折交叉验证。

我在这里的问题是我不知道每次是否应该创建一个新的分类器   (classifier = nltk.NaiveBayesClassifier.train(train_set)),对其进行训练,并根据每个单独的分类器计算平均准确性得分,以得出准确性得分。还是我应该用新数据训练先前训练过的分类器(就像我现在所做的那样),以便最后一个分类器将是经过训练的10次?

0 个答案:

没有答案