现在我正在使用nltk学习naivebayes分类器。
在文档(http://www.nltk.org/book/ch06.html) 1.3文档分类中,有一个功能重置示例。
featuresets = [(document_features(d), c) for (d,c) in documents]
train_set, test_set = featuresets[100:], featuresets[:100]
classifier = nltk.NaiveBayesClassifier.train(train_set)
all_words = nltk.FreqDist(w.lower() for w in movie_reviews.words())
word_features = list(all_words)[:2000] [1]
def document_features(document): [2]
document_words = set(document) [3]
features = {}
for word in word_features:
features['contains({})'.format(word)] = (word in document_words)
return features
因此,特征码集的形式示例是{(“包含(浪费)”:False,“example (Lot)”:False,.},‘neg’).}
但是我想将单词字典的形式从‘contains(废物)’:False改为‘contains(浪费)’:2。我认为这个表格(“包含(浪费)”:2)很好地解释了文档,因为它可以计算世界的频率。因此,功能重置将是{(“包含(废物)”:2,“包含(批次)”:5,.},‘neg’).}
但我担心的是,‘contains(废物)’:2和‘contains(废物):1与纯正分类器完全不同。然后,它不能解释‘can(废物)’:2和‘can(废物):1的相似性。
{“包含(批次)”:1和“包含(废物)”:1}和{“包含(废物)”:2和“包含(废物)”:1}可以与编程相同。
nltk.naivebayesclassifier能理解单词的频率吗?
这是我使用的代码
def split_and_count_word(data):
#belongs_to : Main
#Role : make featuresets from korean words using konlpy.
#Parameter : dictionary data(dict of contents ex.{'politic':{'parliament': [content,content]}..})
#Return : list featuresets([{'word':True',...},'politic'] == featureset + category)
featuresets = []
twitter = konlpy.tag.Twitter()#Korean word splitter
for big_cat in data:
for small_cat in data[big_cat]:
#save category name needed in featuresets
category = str(big_cat[0:3])+'/'+str(small_cat)
count = 0; print(small_cat)
for one_news in data[big_cat][small_cat]:
count+=1; if count%100==0: print(count,end=' ')
#one_news is list in list so open it!
doc = one_news
#split word as using konlpy
list_of_splited_word = twitter.morphs(doc[:-63])#delete useless sentences.
#get word length is higher than two and get list of splited words
list_of_up_two_word = [word for word in list_of_splited_word if len(word)>1]
dict_of_featuresets = make_featuresets(list_of_up_two_word)
#save
featuresets.append((dict_of_featuresets,category))
return featuresets
def make_featuresets(data):
#belongs_to : split_and_count_word
#Role : make featuresets
#Parameter : list list_of_up_two_word(ex.['비누','떨어','지다']
#Return : dictionary {word : True for word in data}
#PROBLEM :(
#cannot consider the freqency of word
return {word : True for word in data}
def naive_train(featuresets):
#belongs_to : Main
#Role : Learning by naive bayes rule
#Parameter : list featuresets([{'word':True',...},'pol/pal'])
#Return : object classifier(nltk naivebayesclassifier object),
# list test_set(the featuresets that are randomly selected)
random.shuffle(featuresets)
train_set, test_set = featuresets[1000:], featuresets[:1000]
classifier = naivebayes.NaiveBayesClassifier.train(train_set)
return classifier,test_set
featuresets = split_and_count_word(data)
classifier,test_set = naive_train(featuresets)
发布于 2016-11-13 12:21:43
nltk的朴素贝叶斯分类器将特征值视为逻辑上的不同。值不限于True
和False
,但它们从未被视为数量。如果您有功能f=2
和f=3
,它们可以算作不同的值。向这样一个模型添加数量的唯一方法是将它们排序为“桶”,例如f=1
、f="few"
(2-5)、f="several"
(6-10)、f="many"
(11或更多)。(注意:如果您选择这条路线,有一些算法可以为桶选择好的值范围。)即使这样,这种模式也不知道“少数”介于“一”和“数”之间。你需要一个不同的机器学习工具来直接处理数量。
https://stackoverflow.com/questions/40158447
复制相似问题