gpt4 book ai didi

python - 如何计算召回率、精度和f-measure?

转载 作者:行者123 更新时间:2023-11-30 09:42:49 27 4
gpt4 key购买 nike

我正在从事一个情感分析项目,而且我是 Python 初学者。我需要计算召回率、精度和 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

任何人都可以提供一些有关如何执行任务的提示吗?

最佳答案

您可以使用 scikit-learn 库来执行此操作,例如与

from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, classification_report, confusion_matrix

f1 = f1_score(y_test, y_pred)

prec = precision_score(y_test, y_pred)

recall = recall_score(y_test, y_pred) `

不确定这是否适用于您的数据集,但最佳实践是执行 cross validation以及。

关于python - 如何计算召回率、精度和f-measure?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56608300/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com