用Python做SVD文档聚类---奇异值分解----文档相似性----LSI(潜在语义分析)

时间:2023-03-09 05:18:07
用Python做SVD文档聚类---奇异值分解----文档相似性----LSI(潜在语义分析)

转载请注明出处:电子科技大学EClab——落叶花开http://www.cnblogs.com/nlp-yekai/p/3848528.html

SVD,即奇异值分解,在自然语言处理中,用来做潜在语义分析即LSI,或者LSA。最早见文章

An introduction to latent semantic analysis

SVD的有关资料,从很多大牛的博客中整理了一下,然后自己写了个python版本,放上来,跟大家分享~

关于SVD的讲解,参考博客

本文由LeftNotEasy发布于http://leftnoteasy.cnblogs.com, 本文可以被全部的转载或者部分使用,但请注明出处,如果有问题,请联系wheeleast@gmail.com

python的拓展包numpy,scipy都能求解SVD,基于numpy写了一个文档做svd的程序。首先将每篇文档向量化,然后对向量化后的文档集合做SVD,取计算后的矩阵U,进行分析。先上代码:

 #coding=utf-8
import re
import math
import numpy as np
import matplotlib.pylab as plt def f_file_open(trace_string):
"""open the document_set, save in the list called txt"""
f=open(trace_string,'r')
txt=f.readlines()
f.close()
return txt def f_vector_found(txt):
"""calculate all of the word in the document set---构造词空间"""
word_list=[]
for line in txt:
line_clean=line.split()
for word in line_clean:
if word not in word_list:
word_list.append(word)
else:
pass
return word_list def f_document_vector(document,word_list):
"""transform the document to vector---文档向量化"""
vector=[]
document_clean=document.split()
for word in word_list:
a=document_clean.count(word)
vector.append(a)
return vector def f_svd_calculate(document_array):
"""calculate the svd and return the three matrics"""
U,S,V=np.linalg.svd(document_array)
return (U,S,V) def f_process_matric_U(matric_U,Save_N_Singular_value):
"""according to the matric U, choose the words as the feature in each document,根据前N个奇异值对U进行切分,选择前N列"""
document_matric_U=[]
for line in matric_U:
line_new=line[:Save_N_Singular_value]
document_matric_U.append(line_new)
return document_matric_U def f_process_matric_S(matric_S,Save_information_value):
"""choose the items with large singular value,根据保留信息需求选择奇异值个数"""
matricS_new=[]
S_self=0
N_count=0
Threshold=sum(matric_S)*float(Save_information_value)
for value in matric_S:
if S_self<=Threshold:
matricS_new.append(value)
S_self+=value
N_count+=1
else:
break
print ("the %d largest singular values keep the %s information " %(N_count,Save_information_value))
return (N_count,matricS_new) def f_process_matric_V(matric_V,Save_N_Singular_value):
"""according to the matric V, choose the words as the feature in each document,根据前N个奇异值对U进行切分,选择前N行"""
document_matric_V=matric_V[:Save_N_Singular_value]
return document_matric_V def f_combine_U_S_V(matric_u,matric_s,matirc_v):
"""calculate the new document对奇异值筛选后重新计算文档矩阵""" new_document_matric=np.dot(np.dot(matric_u,np.diag(matric_s)),matirc_v)
return new_document_matric def f_matric_to_document(document_matric,word_list_self):
"""transform the matric to document,将矩阵转换为文档"""
new_document=[]
for line in document_matric:
count=0
for word in line:
if float(word)>=0.9: #转换后文档中词选择的阈值
new_document.append(word_list_self[count]+" ")
else:
pass
count+=1
new_document.append("\n")
return new_document def f_save_file(trace,document):
f=open(trace,'a')
for line in document:
for word in line:
f.write(word) trace_open="/home/alber/experiment/test.txt"
trace_save="/home/alber/experiment/20140715/svd_result1.txt"
txt=f_file_open(trace_open)
word_vector=f_vector_found(txt)
print (len(word_vector)) document=[]
Num_line=0
for line in txt: #transform the document set to matric
Num_line=Num_line+1
document_vector=f_document_vector(line,word_vector)
document.append(document_vector)
print (len(document))
U,S,V=f_svd_calculate(document)
print (sum(S))
N_count,document_matric_S=f_process_matric_S(S,0.9)
document_matric_U=f_process_matric_U(U,N_count)
document_matric_V=f_process_matric_V(V,N_count)
print (len(document_matric_U[1]))
print (len(document_matric_V))
new_document_matric=f_combine_U_S_V(document_matric_U,document_matric_S,document_matric_V)
print (sorted(new_document_matric[1],reverse=True))
new_document=f_matric_to_document(new_document_matric,word_vector)
f_save_file(trace_save,new_document)
print ("the new document has been saved in %s"%trace_save)

第一篇文档对应的向量的结果如下图(未列完,已排序):

[1.0557039715196566, 1.0302828340480468, 1.0177955652284856, 1.0059864028992798, 0.99050787479103541, 0.93109816291875147, 0.70360233131357808, 0.22614603502510683, 0.10577134907675778, 0.098346889985350489, 0.091221506093784849, 0.085227549911874326, 0.052355994530275715, 0.049805639460153352, 0.046430974364203001, 0.046430974364203001, 0.045655634442695908, 0.043471974743277547, 0.041953839699628029, 0.041483792741663243, 0.039635143169293147, 0.03681955156197822, 0.034893319065413916, 0.0331697465114036, 0.029874818442883051, 0.029874818442883051, 0.028506042937487715, 0.028506042937487715, 0.027724455461901349, 0.026160357130229708, 0.023821284531034687, 0.023821284531034687, 0.017212073571417009, 0.016793815602261938, 0.016793815602261938, 0.016726955476865021, 0.015012207148054771, 0.013657280765244915。。。。。

基于这样一种结果,要对分解后的矩阵进行分析,如上图,值越大,表明该位置的词对该文档贡献越大,而值越小则该词无意义,因而,下一步就是设定阈值,取每一篇文档的特征词,至于阈值的设定,有很多种方法,可以对所有值进行排序,取拐点。如图(不是上面的结果做出来的图):

用Python做SVD文档聚类---奇异值分解----文档相似性----LSI(潜在语义分析)                             用Python做SVD文档聚类---奇异值分解----文档相似性----LSI(潜在语义分析)

显然,只有拐点以后的值对文档的贡献较高,而拐点以后的值变为0,这样,一个文档--词矩阵就通过SVD分解而降低了维度。

这个过程中,有两个认为设定的参数,一个是奇异值的选择,如上图(右):奇异值下降较快,而其中前N个奇异值已经能够代替整个矩阵大部分的的信息。在我的程序中,通过设定需要保留的信息比率(保留90%或者95%或者其他等等)来控制奇异值个数。

另一个需要设定的就是在对上图(左),对于重新构造的矩阵,要用来代替原来的文档矩阵,需要对词进行选择,上面已经说过的,取拐点值是一种。

词--文档矩阵的SVD分解基本上就是这些内容。欢迎纠错和吐槽。