我正在开展一个情感分析项目。 我需要计算召回率、精度和 f 度量,但我不知道数据集的语法,如下所示:
#The train data format ,contains text's words with their weights and the text's class label
train_set = [
({'adam': 0.05,'is': 0.0, 'a': 0.0, 'good': 0.02, 'man': 0.0}, 1),
({'eve': 0.0, 'is': 0.0, 'a': 0.0,'good': 0.02,'woman': 0.0}, 1),
({'adam': 0.05, 'is': 0.0, 'evil': 0.0}, 0)]
#0 or 1 for class label
#Test data the same as train data
这是我当前的代码
from nltk.classify import apply_features
def naivebyse(finaltfidfVector):
train_set = []
j = 0
for vector in finaltfidfVector:
if j < 2100: #take 70% of data for train
train_set.append(vector)
j += 1
else:
break
test_set = []
j = 0
for vector in finaltfidfVector:
if j < 3000 and j >= 2100: # 30% for test
test_set.append(vector)
if j>= 3000:
break
j += 1
classifier = nltk.NaiveBayesClassifier.train(train_set)
print("Accuracy of sarcasm classifier : ",
(nltk.classify.accuracy(classifier, test_set)*100))
refsets = collections.defaultdict(set)
testsets = collections.defaultdict(set)
for i, (feats, label) in enumerate(test_set):
refsets[label].add(i)
observed = classifier.classify(feats)
testsets[observed].add(i)
print("Precision percentage : " , nltk.metrics.precision(refsets['1'],
testsets['1'])*100)
print("Recall Percentage : ", nltk.metrics.recall(refsets['1'],
testsets['1'])*100)
异常
Exception in Tkinter callback
unable to realloc 20234 bytes
任何人都可以提供一些有关如何执行任务的提示吗?