【发布时间】:2017-06-04 05:55:58
【问题描述】:
首要问题:
我认为从TfidfVectorizer 在SparseVectors 上运行fit_transform 模型TruncatedSVD 会产生尺寸为(n_samples,n_components)的组件,如here 所述(跳转到fit_transform 部分)。
但是,我得到了一个形状矩阵 (n_components, n_words)。
这是一个重现问题的简单示例:
def build_tfidf_model(corpus):
transformer = TfidfVectorizer(analyzer='word')
matrix = transformer.fit_transform(corpus)
return matrix
def svd_tfidf_matrix(matrix):
svd = TruncatedSVD(n_components=3)
svd.fit_transform(matrix)
return svd.components_
corpus = ['sentence one', 'sentence two', 'another one', 'another sentence', 'two sentence', 'one sentence']
tfidf_model = build_tfidf_model(corpus)
reduced_vectors = svd_tfidf_matrix(matrix=tfidf_model)
所以,tfidf_model.shape 产生 (6, 4)。这对我来说很有意义。我有一个包含六个文档的语料库,总共包含 4 个不同的单词。
但是,reduced_vectors.shape 产生 (3,4)。我期待它的形状(6,3)。
我一定是误解了调用fit_transform 应该返回什么。我可以调用SVD 以使其返回一个矩阵,其中行是文档,列是缩减空间中的特征?
【问题讨论】:
标签: python scikit-learn tf-idf svd