如何计算召回率,精度和f值?

时间:2019-06-15 07:25:35

标签: python machine-learning nltk

我正在研究情感分析项目,并且是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

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

1 个答案:

答案 0 :(得分:1)

您可以使用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