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

显然,只有拐点以后的值对文档的贡献较高,而拐点以后的值变为0,这样,一个文档--词矩阵就通过SVD分解而降低了维度。
这个过程中,有两个认为设定的参数,一个是奇异值的选择,如上图(右):奇异值下降较快,而其中前N个奇异值已经能够代替整个矩阵大部分的的信息。在我的程序中,通过设定需要保留的信息比率(保留90%或者95%或者其他等等)来控制奇异值个数。
另一个需要设定的就是在对上图(左),对于重新构造的矩阵,要用来代替原来的文档矩阵,需要对词进行选择,上面已经说过的,取拐点值是一种。
词--文档矩阵的SVD分解基本上就是这些内容。欢迎纠错和吐槽。
用Python做SVD文档聚类---奇异值分解----文档相似性----LSI(潜在语义分析)的更多相关文章
- [原创博文] 用Python做统计分析 (Scipy.stats的文档)
[转自] 用Python做统计分析 (Scipy.stats的文档) 对scipy.stats的详细介绍: 这个文档说了以下内容,对python如何做统计分析感兴趣的人可以看看,毕竟Python的库也 ...
- Kmeans文档聚类算法实现之python
实现文档聚类的总体思想: 将每个文档的关键词提取,形成一个关键词集合N: 将每个文档向量化,可以参看计算余弦相似度那一章: 给定K个聚类中心,使用Kmeans算法处理向量: 分析每个聚类中心的相关文档 ...
- Python爬虫、自动化常用库&帮助文档URL
一.Python下载地址 Windows终端Cmder.exe下载--->http://cmder.net/ Python下载(Windows) ---> https://w ...
- 孤荷凌寒自学python第五十四天使用python来删除Firebase数据库中的文档
孤荷凌寒自学python第五十四天使用python来删除Firebase数据库中的文档 (完整学习过程屏幕记录视频地址在文末) 今天继续研究Firebase数据库,利用google免费提供的这个数据库 ...
- Python之文件处理-批量修改md文档内容
目录 Python之文件处理-批量修改md文档内容 Python之文件处理-批量修改md文档内容 #!/usr/bin/env python # -*- coding:utf-8 -*- import ...
- 【技术博客】利用Python将markdown文档转为html文档
利用Python将markdown文档转为html文档 v1.0 作者:FZK 元素简单的md文件 Python中自带有一个markdown库,你可以直接这样使用 md_file = open(&qu ...
- docfx 做一个和微软一样的文档平台
开发中,有一句话叫 最不喜欢的是写文档,最不喜欢的是看别人家代码没有文档.那么世界上文档写最 la 好 ji 的就是微软了,那么微软的api文档是如何做的?难道请了很多人去写文档? 实际上微软有工具用 ...
- k-means+python︱scikit-learn中的KMeans聚类实现( + MiniBatchKMeans)
来源:, init='k-means++', n_init=10, max_iter=300, tol=0.0001, precompute_distances='auto', verbose=0, ...
- 使用python做科学计算
这里总结一个guide,主要针对刚开始做数据挖掘和数据分析的同学 说道统计分析工具你一定想到像excel,spss,sas,matlab以及R语言.R语言是这里面比较火的,它的强项是强大的绘图功能以及 ...
随机推荐
- Asp.net mvc 大文件上传 断点续传
Asp.net mvc 大文件上传 断点续传 进度条 概述 项目中需要一个上传200M-500M的文件大小的功能,需要断点续传.上传性能稳定.突破asp.net上传限制.一开始看到51CTO上的这 ...
- C#自动选择出系统中最合适的IP地址
写这个是因为很长时间以来,碰到过很多次这个问题,但都没当回事,这次又碰到了这个老问题,无奈百度了一圈儿未果,身边又没有大牛可以请教,就自己先“总结”了一套方法,一来给自己记录,二来如果碰巧能有朋友看到 ...
- 应用程序入口 WSGIHandler
Django 源码小剖: 应用程序入口 WSGIHandler WSGI 有三个部分, 分别为服务器(server), 应用程序(application) 和中间件(middleware). 已经知道 ...
- CKEditor4.x部署和配置
CKEditor4.x && JSP 官网下载CKEditor,可选Basic, Standard, Full 解压放置其WebRoot下 JSP中引入以下文件: <script ...
- 读写分离子系统 - C# SQL分发子系统(ADO.NET,不支持EF)
读写分离子系统 - C# SQL分发子系统(ADO.NET,不支持EF) 这次介绍的这个框架只适用于中小项目,并且各个读写数据库结构是一致的情况,还要并且是写入数据库只有1台情况. 我们来看看这个子系 ...
- hdu 1507
求能出售多少个1*2的矩形,,将原图染色,(i+j)%2==0的染白色,其余为黑色, 求白色跟黑色的最大匹配 #include<stdio.h> #include<string.h& ...
- poj 3897 Maze Stretching 二分+A*搜索
题意,给你一个l,和一个地图,让你从起点走到终点,使得路程刚好等于l. 你可以选择一个系数,把纵向的地图拉伸或收缩,比如你选择系数0.5,也就是说现在上下走一步消耗0.5的距离,如果选择系数3,也就是 ...
- Debian下的'aptitude update'失败处理
Hit http://ftp.us.debian.org squeeze/contrib amd64 Packages Hit http://download.proxmox.com squeeze/ ...
- 深入浅出MyBatis-Sqlsession
前面的章节主要讲mybatis如何解析配置文件,这些都是一次性的过程.从本章开始讲解动态的过程,它们跟应用程序对mybatis的调用密切相关.本章先从sqlsession开始. 创建 正如其名,Sql ...
- PRJ: Split a nodes-map into some triangles
PRJ: Split a nodes-map into some triangles Share the source codes of this algorithm to your guys... ...