如何从nltk分类器中获得正预测和负预测的精确性、召回率和F度量?

2024-04-26 03:08:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图获得准确度,召回率和F-措施的积极和消极预测情绪分析。 我使用的python 3.6如下:

import nltk
from nltk.tokenize import word_tokenize 
# Trainng data    
train = [('I love this sandwich.', 'pos'),
         ('This is an amazing place!', 'pos'),
         ('I feel very good about these beers.', 'pos'),
         ('This is my best work.', 'pos'),
         ("What an awesome view", 'pos'),
         ('I do not like this restaurant', 'neg'),
         ('I am tired of this stuff.', 'neg'),
         ("I can't deal with this", 'neg'),
         ('He is my sworn enemy!', 'neg'),
         ('My boss is horrible.', 'neg') ]

# Test data   
test = [('The beer was good.', 'pos'),
        ('I do not enjoy my job', 'neg'),
        ("I ain't feeling dandy today.", 'neg'),
        ("I feel amazing!", 'pos'),
        ('Gary is a friend of mine.', 'pos'),
        ("I can't believe I'm doing this.", 'neg') ]


# Tokenize Training words
Training_words = set(word.lower() for passage in train for word in word_tokenize(passage[0]))

# Training feature sets
training_set = [({word: (word in word_tokenize(x[0])) for word in Training_words}, x[1]) for x in train]

# Tokenize Test words
Test_words = set(word.lower() for passage in test for word in word_tokenize(passage[0]))

# Test feature sets
test_set= [({word: (word in word_tokenize(x[0])) for word in Test_words}, x[1]) for x in test]

# Naive Bayes classifier
classifier = nltk.NaiveBayesClassifier.train(training_set)

# Informative Features
classifier.show_most_informative_features()

# print the accuracy 
print("accuracy %",(nltk.classify.accuracy(classifier, test_set))*100)

上面的代码显示了naivebayes分类器的信息特性和准确性。 我尝试了下面的代码来获得它的精确性、召回率以及对正面和负面预测的F度量。在

^{pr2}$

我需要你的hep。。在


Tags: inpostestforistrainingtrainthis