如何使 TF-IDF 矩阵密集? [英] How to make TF-IDF matrix dense?

查看:39
本文介绍了如何使 TF-IDF 矩阵密集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 TfidfVectorizer 将原始文档集合转换为 TF-IDF 特征矩阵,然后我计划将其输入到 k-means 算法(我将实施).在该算法中,我将不得不计算质心(文章类别)和数据点(文章)之间的距离.我将使用欧几里得距离,所以我需要这两个实体具有相同的维度,在我的例子中是 max_features.这是我所拥有的:

tfidf = TfidfVectorizer(max_features=10,strip_accents='unicode',analyzer='word',stop_words=stop_words.extra_stopwords,lowercase=True,use_idf=True)X = tfidf.fit_transform(data['Content']) # 矩阵文章 x max_features(=words)对于 i,enumerate(X) 中的行:打印 X[i]

然而 X 似乎是一个稀疏(?)矩阵,因为输出是:

 (0, 9) 0.723131915847(0, 8) 0.090245047798(0, 6) 0.117465276892(0, 4) 0.379981697363(0, 3) 0.235921470645(0, 2) 0.0968780456528(0, 1) 0.495689001273(0, 9) 0.624910843051(0, 8) 0.545911131362(0, 7) 0.160545991411(0, 5) 0.49900042174(0, 4) 0.191549050212...

我认为(0, col) 表示矩阵中的列索引,它实际上就像一个数组,其中每个单元格都指向一个列表.>

如何将此矩阵转换为密集矩阵(以便每一行具有相同的列数)?

<小时>

>打印类型(X)<类'scipy.sparse.csr.csr_matrix'>

解决方案

这应该很简单:

dense = X.toarray()

TfIdfVectorizer.fit_transform() 正在返回一个 SciPy csr_matrix()(压缩稀疏行矩阵),它有一个 toarray() 方法就是为了这个目的.SciPy 中有几种稀疏矩阵格式,但它们都有一个 .toarray() 方法.

请注意,对于大型矩阵,与稀疏矩阵相比,这将使用大量内存,因此通常尽可能长时间保持稀疏是一种好方法.

I am using TfidfVectorizer to convert a collection of raw documents to a matrix of TF-IDF features, which I then plan to input into a k-means algorithm (which I will implement). In that algorithm I will have to compute distances between centroids (categories of articles) and data points (articles). I am going to use Euclidean distance, so I need these two entities to be of same dimension, in my case max_features. Here is what I have:

tfidf = TfidfVectorizer(max_features=10, strip_accents='unicode', analyzer='word', stop_words=stop_words.extra_stopwords, lowercase=True, use_idf=True)
X = tfidf.fit_transform(data['Content']) # the matrix articles x max_features(=words)
for i, row in enumerate(X):
    print X[i]

However X seems to be a sparse(?) matrix, since the output is:

  (0, 9)    0.723131915847
  (0, 8)    0.090245047798
  (0, 6)    0.117465276892
  (0, 4)    0.379981697363
  (0, 3)    0.235921470645
  (0, 2)    0.0968780456528
  (0, 1)    0.495689001273

  (0, 9)    0.624910843051
  (0, 8)    0.545911131362
  (0, 7)    0.160545991411
  (0, 5)    0.49900042174
  (0, 4)    0.191549050212

  ...

Where I think the (0, col) states the column index in the matrix, which actually like an array, where every cell points to a list.

How do I convert this matrix to a dense one (so that every row has the same number of columns)?


>print type(X)
<class 'scipy.sparse.csr.csr_matrix'>

解决方案

This should be as simple as:

dense = X.toarray()

TfIdfVectorizer.fit_transform() is returning a SciPy csr_matrix() (Compressed Sparse Row Matrix), which has a toarray() method just for this purpose. There are several formats of sparse matrices in SciPy, but they all have a .toarray() method.

Note that for a large matrix, this will use a tremendous amount of memory compared to a sparse matrix, so generally it's a good approach to leave it sparse for as long as possible.

这篇关于如何使 TF-IDF 矩阵密集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆