gensin.lda/lsi + sklearn 文本分类

使用gensim内LDA与LSA训练文本,将其中的docment_topc矩阵信息作为原始矩阵进行分类。

我自己选的数据是知网的几千篇摘要,都是计算机大类别下的文档,类别比较模糊,只做了个分词处理。以俩空格‘  '作为分隔符保存。最终分类准确率肯定没有那些类别明显的高(sogo)

import os
import re
from gensim import models,corpora
stop_=[]
for x in open('stopwords.txt').readlines():
    x=x.encode('utf-8').decode('utf-8-sig').strip('\n')
    stop_.append(x)

documents=[]
labels=[]
class_dir=os.listdir('/home/com/kqx/DATA/')
 
#读取语料库
ii=0
for i in class_dir:
    currentpath='/home/com/kqx/DATA/'+i
    print(currentpath)
    file=open(currentpath)
    for f in file.readlines():
        labels.append(ii)
        f=f.encode('utf-8').decode('utf-8-sig').strip('\n')//有时候文本会多出来个诡异的/ufeff, 处理后会去除这个鬼东西
        f=f.split('  ')
        tmp_=[]
        for x in f:
            if x not in stop_:
                tmp_.append(x)
        documents.append(tmp_)
        file.close()
    ii+=1     


             
训练个TFIDF作为稠密矩阵存储

dictionary=corpora.Dictionary(documents)
corpus=[dictionary.doc2bow(doc) for doc in documents]#generate the corpus
tf_idf=models.TfidfModel(corpus)#the constructor
corpus_tfidf=tf_idf[corpus]


 
#训练LSA,并进行分类
 

lsi=models.LsiModel(corpus_tfidf,id2word=dictionary,num_topics=50)

#建立LSA对应的文档主题矩阵
d_l=[]
labelss=labels
for x in range(len(documents)):
    tmp=[]
    a1=dictionary.doc2bow(documents[x])
    for xx in lsi[a1]:
        #print(xx)
        tmp.append(xx[1])
    if len(tmp)!=lsi.num_topics:
        del labelss[x]
        continue
    d_l.append(tmp)

#利用SVM进行分类

from sklearn.svm  import  SVC
from sklearn.cross_validation import train_test_split
X_train, X_test, Y_train, Y_test =train_test_split(d_l,labelss,test_size=0.3)
sv=SVC(C=1, kernel='linear')
print('训练')
sv.fit(X_train,Y_train)
sv.score(X_test, Y_test)

基于LSA的SVM分类准确率是 85%

下面试LDA的。

#训练模型
LDA=models.LdaModel(corpus_tfidf,id2word=dictionary,num_topics=150)
#LDA  利用inference 建立模型
import numpy as np
d_l=[]
de=[]
labelss=labels
for x in range(len(documents)):
    tmp=[]
    a1=dictionary.doc2bow(documents[x])
    for xx in list(LDA.inference([a1])[0][0]):
        tmp.append(xx)
    if len(tmp)!=LDA.num_topics:
        print (x)
        de.append(x) 
        continue
    d_l.append(tmp) 
for x in de:
    print('del',x)
    del labelss[x]

由于是使用LDA.inference做的矩阵模型,数据未做归一化,同样利用LSA总的SVM分类器,分类结果仅有65%+的样子

利用LDA[],建立文档主题矩阵,建立个文档主题矩阵。

#LDA  归一化的矩阵  
import numpy as np
d_l=[]
de=[]
for x in range(len(documents)):
    tmp=np.zeros(LDA.num_topics)
    a1=dictionary.doc2bow(documents[x])
    for xx in list(LDA[a1]):
        tmp[xx[0]]=xx[1]
    d_l.append(tmp) 
for x in de:
    print('dede')
    del labelss[x]

这个效果也不行。仅有63%+的样子。用gensim.lda效果不咋地。

用源码的LDA效果能在80%+。

等再改改,看看这么提高。占个位置。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值