我正在使用 Python 编写一个迷你项目分类文本。
这个想法很简单:我们有一个句子语料库,分别属于 J. Chirac 和 Mitterrand(法兰西共和国的 2 位前总统(带有相关标签)。
目标是建立一个预测属于不同句子的模型。对于类(标签)它有“M”代表密特朗,“C”代表希拉克,在我的程序中我认为正确M == > -1
,并且C ==> 1
。
最后,我在我的数据集上应用了一个称为朴素贝叶斯的聚类算法,并对新数据进行了预测(测试)。
这里的问题是,在对我的系统进行性能评估后,我得到了一个非常低的分数,虽然我使用了几种方法来增加(停用词、双连词、平滑..)
如果有人对我有其他想法或建议来改进我的系统的性能,我会非常满意。
我将在下面附上我的一些代码。
在下面的代码中,我选择了我的停止列表,我删除了不是很重要的单词和拆分器来生成我的语料库,我使用了二元组:
stoplist = set('le la les de des à un une en au ne ce d l c s je tu il que qui mais quand'.split())
stoplist.add('')
splitters = u'; |, |\*|\. | |\'|'
liste = (re.split(splitters, doc.lower()) for doc in alltxts) # generator = pas de place en memoire
dictionary = corpora.Dictionary([u"{0}_{1}".format(l[i],l[i+1]) for i in xrange(len(l)-1)] for l in liste) # bigrams
print len(dictionary)
stop_ids = [dictionary.token2id[stopword] for stopword in stoplist if stopword in dictionary.token2id]
once_ids = [tokenid for tokenid, docfreq in dictionary.dfs.iteritems() if docfreq < 10 ]
dictionary.filter_tokens(stop_ids + once_ids) # remove stop words and words that appear only once
dictionary.compactify() # remove gaps in id sequence after words that were removed
print len(dictionary)
liste = (re.split(splitters, doc.lower()) for doc in alltxts) # ATTENTION: quand le générator a déjà servi, il ne se remet pas au début => le re-créer pour plus de sécurité
alltxtsBig = ([u"{0}_{1}".format(l[i],l[i+1]) for i in xrange(len(l)-1)] for l in liste)
corpusBig = [dictionary.doc2bow(text) for text in alltxtsBig]
在这里,我为我的测试数据集生成了一个语料库:
liste_test = (re.split(splitters, doc.lower()) for doc in alltxts_test)
alltxtsBig_test = ([u"{0}_{1}".format(l[i],l[i+1]) for i in xrange(len(l)-1)] for l in liste_test)
corpusBig_test = [dictionary.doc2bow(text) for text in alltxtsBig_test]
and here I am doing the processing of these data has a numpy matrix, and I apply the algorithm on data, and I make the prediction on test data:
dataSparse = gensim.matutils.corpus2csc(corpusBig)
dataSparse_test = gensim.matutils.corpus2csc(corpusBig_test)
import sklearn.feature_extraction.text as txtTools #.TfidfTransformer
t = txtTools.TfidfTransformer()
t.fit(dataSparse.T)
data2 = t.transform(dataSparse.T)
data_test = t.transform(dataSparse_test.T)
nb_classifier = MultinomialNB().fit(data2, labs)
y_nb_predicted = nb_classifier.predict(data_test)
编辑:
我系统的性能值为 0.28。通常,如果系统有效,它将给出超过 0.6。
我在一个文件 Millers 句子上工作,我声明了 gensim,我没有在这里粘贴所有代码,因为它很长,我的问题是是否有其他方法可以提高系统性能,我使用了二元组,平滑.. 仅此而已.