Python 使用 Sklearn 的 LSA
Python LSA with Sklearn
我目前正在尝试使用 Sklearn 实现 LSA 以在多个文档中查找同义词。
这是我的代码:
#import the essential tools for lsa
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.decomposition import TruncatedSVD
from sklearn.metrics.pairwise import cosine_similarity
#other imports
from os import listdir
#load data
datafolder = 'data/'
filenames = []
for file in listdir(datafolder):
if file.endswith(".txt"):
filenames.append(datafolder+file)
#Document-Term Matrix
cv = CountVectorizer(input='filename',strip_accents='ascii')
dtMatrix = cv.fit_transform(filenames).toarray()
print dtMatrix.shape
featurenames = cv.get_feature_names()
print featurenames
#Tf-idf Transformation
tfidf = TfidfTransformer()
tfidfMatrix = tfidf.fit_transform(dtMatrix).toarray()
print tfidfMatrix.shape
#SVD
#n_components is recommended to be 100 by Sklearn Documentation for LSA
#http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.TruncatedSVD.html
svd = TruncatedSVD(n_components = 100)
svdMatrix = svd.fit_transform(tfidfMatrix)
print svdMatrix
#Cosine-Similarity
#cosine = cosine_similarity(svdMatrix[1], svdMatrix)
现在这是我的问题:
Term-DOcument Matrix和tf-idf Matrix的Shape是一样的,都是(27,3099)。 27 篇文献,3099 字。
单值分解后矩阵的形状为 (27,27)。
我知道您可以计算 2 行的余弦相似度以获得相似度,但我认为我无法通过使用 SVD 矩阵来计算文档中 2 个单词的相似度。
有人可以向我解释 SVD 矩阵代表什么以及我可以使用它在我的文档中查找同义词的任何方式吗?
提前致谢。
SVD 是一种降维工具,这意味着它将特征的顺序(数量)减少到更多 representative
集合。
来自 github 上的源代码:
def fit_transform(self, X, y=None):
"""Fit LSI model to X and perform dimensionality reduction on X.
Parameters
----------
X : {array-like, sparse matrix}, shape (n_samples, n_features)
Training data.
Returns
-------
X_new : array, shape (n_samples, n_components)
Reduced version of X. This will always be a dense array.
"""
我们可以看到返回的矩阵包含的样本数量减少了。然后,您可以使用距离计算方法来确定任意两行的相似度。
Here 还给出了一个通过 SVD 的 LSA 的简单示例。
我目前正在尝试使用 Sklearn 实现 LSA 以在多个文档中查找同义词。 这是我的代码:
#import the essential tools for lsa
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.decomposition import TruncatedSVD
from sklearn.metrics.pairwise import cosine_similarity
#other imports
from os import listdir
#load data
datafolder = 'data/'
filenames = []
for file in listdir(datafolder):
if file.endswith(".txt"):
filenames.append(datafolder+file)
#Document-Term Matrix
cv = CountVectorizer(input='filename',strip_accents='ascii')
dtMatrix = cv.fit_transform(filenames).toarray()
print dtMatrix.shape
featurenames = cv.get_feature_names()
print featurenames
#Tf-idf Transformation
tfidf = TfidfTransformer()
tfidfMatrix = tfidf.fit_transform(dtMatrix).toarray()
print tfidfMatrix.shape
#SVD
#n_components is recommended to be 100 by Sklearn Documentation for LSA
#http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.TruncatedSVD.html
svd = TruncatedSVD(n_components = 100)
svdMatrix = svd.fit_transform(tfidfMatrix)
print svdMatrix
#Cosine-Similarity
#cosine = cosine_similarity(svdMatrix[1], svdMatrix)
现在这是我的问题: Term-DOcument Matrix和tf-idf Matrix的Shape是一样的,都是(27,3099)。 27 篇文献,3099 字。 单值分解后矩阵的形状为 (27,27)。 我知道您可以计算 2 行的余弦相似度以获得相似度,但我认为我无法通过使用 SVD 矩阵来计算文档中 2 个单词的相似度。
有人可以向我解释 SVD 矩阵代表什么以及我可以使用它在我的文档中查找同义词的任何方式吗?
提前致谢。
SVD 是一种降维工具,这意味着它将特征的顺序(数量)减少到更多 representative
集合。
来自 github 上的源代码:
def fit_transform(self, X, y=None):
"""Fit LSI model to X and perform dimensionality reduction on X.
Parameters
----------
X : {array-like, sparse matrix}, shape (n_samples, n_features)
Training data.
Returns
-------
X_new : array, shape (n_samples, n_components)
Reduced version of X. This will always be a dense array.
"""
我们可以看到返回的矩阵包含的样本数量减少了。然后,您可以使用距离计算方法来确定任意两行的相似度。
Here 还给出了一个通过 SVD 的 LSA 的简单示例。