我在python中使用NLTK创建了一个性别分类模型。我正在使用前馈机制。如何将反向传播方法纳入其中?
import nltk
from nltk.corpus import names
import random
def gender_features(word):
return {'last_letter': word[-1]}
labeled_names=([(name1,'male') for name1 in names.words('male.txt')]+
(name1,'female') for name1 in names.words('female.txt')])
random.shuffle(labeled_names)
featuresets = [(gender_features(n), gender) for (n, gender) in
labeled_names]
train_set, test_set = featuresets[1500:], featuresets[:1500]
classifier = nltk.NaiveBayesClassifier.train(train_set)
#name input part
check=input('enter the name you wanna check: ')
print(classifier.classify(gender_features(check)))
print(nltk.classify.accuracy(classifier, test_set))
feedback=input('is my answer correct??? [Y/N]: ')
if feedback=='Y':
print('thanks')
elif feedback=='N':
def gender_features1(word):
return {'last_letter': word[-1],
'second_last_letter': word[-2]}
labeled_names=([(name1,'male') for name1 in names.words('male.txt')]+
[(name1,'female') for name1 in names.words('female.txt')])
random.shuffle(labeled_names)
featuresets = [(gender_features1(n), gender) for (n, gender) in labeled_names]
train_set= featuresets[:1000]
classifier = nltk.NaiveBayesClassifier.train(train_set)
check1=input('plz enter the name again: ')
print(classifier.classify(gender_features(check1)))
else:
print('i dnt understand this input')
NaiveBayesClassifier接受feautresets,但我希望每次用户输入名称时使用新的输入数据重新训练分类器。
我还尝试过第二次使用两个功能集,但我不知道如何继续使用这个部分。