机器学习文本分类

时间:2014-10-10 23:31:40

标签: python algorithm machine-learning text-classification

我正在使用Python制作迷你项目分类文本 这个想法很简单:我们有一组句子,分别属于J. Chirac和Mitterrand(法国共和国的两位前总统(附有相关标签)。) 目标是建立一个预测属于不同句子的模型。对于课程(标签),它有" M"对于Mitterand和" C"对于希拉克来说,正确地在我的计划中我考虑过M == > -1C ==> 1 最后,我在我的数据集上应用了一个名为朴素贝叶斯的聚类算法,并对新数据进行了预测(测试)。 这里的问题是,在评估我的系统性能之后,我得到了一个非常低的分数,虽然我已经使用了几种方法来增加(停用词,双字母组,平滑...)

如果有人对我有其他想法或建议来提高我的系统性能,我会非常满意。

我将在下面附上我的一些代码。

在下面的代码中,我选择了我的stopliste并删除了不太重要的单词和splitters来生成我的语料库,并使用bigrams:

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,我没有粘贴所有代码,因为它很长,我的问题是,如果有其他方法可以提高系统性能,我使用了bigrams,平滑那就是全部。

1 个答案:

答案 0 :(得分:0)

欢迎使用stackoverflow。首先,你是否确定你的表现不佳?你甚至没有说出你获得了什么样的表现,但是如果(正如你似乎在说的那样)你试图根据单个句子来识别作者,我不会指望它具有任何可靠性。作者身份识别通常在更长的文本上完成。

我担心你的代码是不完整的(gensim定义在哪里?所有这些库函数都做了什么?)并且太长而无法轻松跟踪。但是你是否使用文本中所有(非禁止词)双字母组合作为分类器的功能?这是很多功能,而且它们都是同一类型(bigrams)。您可以尝试添加一些不同类型的功能,和/或更有选择地使用bigram功能,以避免过度训练。你应该阅读以找出可能起作用的东西 - 作者识别并不是一项新任务。

你的问题有点过于宽泛而无法有效回答,因为答案太多了。但是当你更多地研究这个问题时,请坚持下去并提出更具体的问题。祝你好运!