问题
I was wondering if anyone could help me through a code snippet that demonstrates how to train Naive Bayes classifier using a feature frequency method as opposed to feature presence.
I presume the below as shown in Chap 6 link text refers to creating a featureset using Feature Presence (FP) -
def document_features(document):
document_words = set(document)
features = {}
for word in word_features:
features['contains(%s)' % word] = (word in document_words)
return features
Please advice
回答1:
In the link you sent it says this function is feature extractor that simply checks whether each of these words is present in a given document.
Here is the whole code with numbers for each line:
1 all_words = nltk.FreqDist(w.lower() for w in movie_reviews.words())
2 word_features = all_words.keys()[:2000]
3 def document_features(document):
4 document_words = set(document)
5 features = {}
6 for word in word_features:
7 features['contains(%s)' % word] = (word in document_words)
8 return features
In line 1 it created a list of all words.
In line 2 it takes the most frequent 2000 words.
3 the definition of the function
4 converts the document list (I think it must be a list) and converts the list to a set.
5 declares a dictionary
6 iterates over all of the most frequent 2000 words
7 creates a dictionary where the key is 'contains(theword)' and the value is either true or false. True if the word is present in the document, false otherwise
8 returns the dictionary which is shows whether the document contains the most frequent 2000 words or not.
Does this answer your question?
回答2:
For training, create appropriate FreqDists that you can use to create ProbDists, than can then be passed in to the NaiveBayesClassifier. But the classification actually works on feature sets, which use boolean values, not frequencies. So if you want to classify based on a FreqDist, you'll have to implement your own classifier, that does not use the NLTK feature sets.
回答3:
Here's a method which will help you :
''' Returns the frequency of letters '''
def get_freq_letters(words):
fdist = nltk.FreqDist([char.lower() for word in words for char in word if char.isalpha()])
freq_letters = {}
for key,value in fdist.iteritems():
freq_letters[key] = value
return freq_letters
来源:https://stackoverflow.com/questions/2162718/python-nltk-code-snippet-to-train-a-classifier-naive-bayes-using-feature-frequ