机器学习文本分类

2024-06-01 00:30:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python编写一个小型项目分类文本。
想法很简单:我们有一组分别属于希拉克和密特朗(两位法兰西共和国前总统)的句子集。
目标是建立一个预测属于不同句子的模型。对于类(标签),它有“M”代表密特朗,“C”代表希拉克,在我的程序中,我认为M == > -1,和{}。
最后,我在我的数据集上应用了一个叫做naivebayes的聚类算法,并对新数据进行了预测(Test)。
这里的问题是,在对我的系统性能进行评估后,我得到了非常低的分数,尽管我使用了几种方法来提高(stopwords,bigrams,smoothing…)

如果有人对我的系统性能有别的想法或建议,我会很满意的。在

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

在下面的代码中,我选择了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]

在这里,我为我的测试数据集生成了一个语料库:

^{pr2}$

编辑:
我的系统性能值为0.28。正常情况下,如果系统有效,它将给出超过0.6。
我写了一个文件Millers的句子,我声明gensim,我没有把所有代码粘贴在这里,因为它很长,我的问题是,如果有其他方法可以提高系统性能,我已经使用了bigrams,smoothing。。这就是全部。在


Tags: 代码inleidsfordocdictionarylen
1条回答
网友
1楼 · 发布于 2024-06-01 00:30:16

欢迎来到stackoverflow。首先,你确定你的表现很差吗?你甚至没有说你的表现如何,但是如果(就像你所说的那样)你试图根据一个句子来确定作者,我不认为这是可能的。作者识别通常是在更长的文本上完成的。在

恐怕您的代码都不完整(在哪里定义了gensim?所有这些库函数都做什么?)太长了,不容易跟上。但是,您是否使用文本中所有(非停止字)双元组的存在作为分类器的特征?这有很多特性,而且它们都是相同的(bigrams)。您可以尝试向混合中添加一些不同种类的特性,和/或更有选择地使用bigram特性,以避免过度训练。你应该多读一读,找出什么样的东西才有可能起作用——作家鉴定并不是一项新的任务。在

你的问题有点过于宽泛,无法有效地回答,因为可能的答案太多了。但当你在这方面做得更多的时候,要坚持住,问一些更具体的问题。祝你好运!在

相关问题 更多 >