Python 机器学习文本分类

Python 机器学习文本分类,python,algorithm,machine-learning,text-classification,Python,Algorithm,Machine Learning,Text Classification,我正在用Python编写一个小型项目分类文本。 想法很简单:我们有一组句子,分别属于J.希拉克和密特朗(法兰西共和国的两位前总统)(有相关标签)。 我们的目标是建立一个模型,预测属于不同的句子。对于类(标签),它有“M”代表密特朗,“C”代表希拉克,在我的程序中,我正确地认为M=>-1,和C==>1 最后,我在我的数据集上应用了一种称为Naive Bayes的聚类算法,并对新数据进行了预测(测试)。 这里的问题是,在对我的系统的性能进行评估之后,我得到了一个非常低的分数,尽管我使用了几种方法来提

我正在用Python编写一个小型项目分类文本。
想法很简单:我们有一组句子,分别属于J.希拉克和密特朗(法兰西共和国的两位前总统)(有相关标签)。
我们的目标是建立一个模型,预测属于不同的句子。对于类(标签),它有“M”代表密特朗,“C”代表希拉克,在我的程序中,我正确地认为
M=>-1
,和
C==>1

最后,我在我的数据集上应用了一种称为Naive Bayes的聚类算法,并对新数据进行了预测(测试)。
这里的问题是,在对我的系统的性能进行评估之后,我得到了一个非常低的分数,尽管我使用了几种方法来提高分数(stopwords、bigrams、smoothing…)

如果有人有其他想法或建议让我改进我的系统性能,我会非常满意

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

在下面的代码中,我选择了stopliste,删除了不太重要的单词和拆分器来生成语料库,并使用了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]
编辑:
我的系统的性能值为0.28。正常情况下,如果系统有效,它的性能值将超过0.6。

我在处理文件Millers语句,我声明gensim,我没有在这里粘贴所有代码,因为它很长,我的问题是,是否有其他方法可以提高系统性能,我使用了bigrams、平滑…仅此而已。

欢迎使用stackoverflow。首先,你确定你的性能很差吗?你甚至不知道说你的表现如何,但如果(正如你所说的)你试图根据一句话来识别作者,我不认为这是可能的。作者识别通常是在更长的文本上进行的

恐怕您的代码既不完整(gensim定义在哪里?所有这些库函数都做了什么?)又太长,很难理解。但是您是否使用文本中所有(非stopword)bigram作为分类器的功能?这是很多功能,它们都是相同的(bigram)。您可以尝试在组合中添加一些不同类型的功能,和/或更有选择地使用双字符功能,以避免过度训练。您应该仔细阅读,了解哪些功能可能会起作用—作者识别不是一项新任务

你的问题有点过于宽泛,无法有效回答,因为可能的答案太多了。但在你更多地研究这个问题时,请留下来问更具体的问题。祝你好运

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)