易百教程人工智能python修正-人工智能NLTK性别发现器
在这个问题陈述中,将通过提供名字来训练分类器以找到性别(男性或女性)。 我们需要使用启发式构造特征向量并训练分类器。这里使用scikit-learn软件包中的标签数据。 以下是构建性别查找器的Python代码 -
导入必要的软件包 -
import random from nltk import NaiveBayesClassifier from nltk.classify import accuracy as nltk_accuracy from nltk.corpus import names
def extract_features(word, N = 2):
last_n_letters = word[-N:]
return {'feature': last_n_letters.lower()}
if __name__=='__main__':
使用NLTK中提供的标签名称(男性和女性)创建培训数据 -
male_list = [(name, 'male') for name in names.words('male.txt')]
female_list = [(name, 'female') for name in names.words('female.txt')]
data = (male_list + female_list)
random.seed(5)
random.shuffle(data)
现在,测试数据将被创建如下 -
namesInput = ['Rajesh', 'Gaurav', 'Swati', 'Shubha']
使用以下代码定义用于列车和测试的样本数 -
train_sample = int(0.8 * len(data))
现在,需要迭代不同的长度,以便可以比较精度 -
for i in range(1, 6):
print('\nNumber of end letters:', i)
features = [(extract_features(n, i), gender) for (n, gender) in data]
train_data, test_data = features[:train_sample],
features[train_sample:]
classifier = NaiveBayesClassifier.train(train_data)
分类器的准确度可以计算如下 -
accuracy_classifier = round(100 * nltk_accuracy(classifier, test_data), 2)
print('Accuracy = ' + str(accuracy_classifier) + '%')
现在,可以预测输出结果 -
for name in namesInput:
print(name, '==>', classifier.classify(extract_features(name, i))
上述程序将生成以下输出 -
Number of end letters: 1
Accuracy = 74.7%
Rajesh -> female
Gaurav -> male
Swati -> female
Shubha -> female
Number of end letters: 2
Accuracy = 78.79%
Rajesh -> male
Gaurav -> male
Swati -> female
Shubha -> female
Number of end letters: 3
Accuracy = 77.22%
Rajesh -> male
Gaurav -> female
Swati -> female
Shubha -> female
Number of end letters: 4
Accuracy = 69.98%
Rajesh -> female
Gaurav -> female
Swati -> female
Shubha -> female
Number of end letters: 5
Accuracy = 64.63%
Rajesh -> female
Gaurav -> female
Swati -> female
Shubha -> female
完整代码
import random from nltk import NaiveBayesClassifier from nltk.classify import accuracy as nltk_accuracy from nltk.corpus import names def extract_features(word, N=2): last_n_letters = word[-N:] return {'feature': last_n_letters.lower()} if __name__ == '__main__': male_list = [(name, 'male') for name in names.words('male.txt')] female_list = [(name, 'female') for name in names.words('female.txt')] data = (male_list + female_list) random.seed(5) random.shuffle(data) namesInput = ['Rajesh', 'Gaurav', 'Swati', 'Shubha'] train_sample = int(0.8 * len(data)) for i in range(1, 6): print('\nNumber of end letters:', i) features = [(extract_features(n, i), gender) for (n, gender) in data] train_data, test_data = features[:train_sample], features[train_sample:] classifier = NaiveBayesClassifier.train(train_data) accuracy_classifier = round(100 * nltk_accuracy(classifier, test_data), 2) print('Accuracy = ' + str(accuracy_classifier) + '%') for name in namesInput: print(name, '==>', classifier.classify(extract_features(name, i)))