【问题标题】:TFIDF representation for ML dataset in coo format pythoncoo格式python的ML数据集的TFIDF表示
【发布时间】:2016-11-28 21:08:54
【问题描述】:

我想获得 MovieLens 标签数据集的 tf-idf 表示。标签采用“coo”格式:

import pandas as pd

ratings = pd.read_csv('data/ratings.csv',sep=',')
movies = pd.read_csv('data/movies.csv',sep=',')
tags = pd.read_csv('data/tags.csv',sep=',')
print(tags)

       userId  movieId                                      tag  \
0         15      339                            sandra 'boring' bullock   
1         15     1955                                            dentist   
2         15     7478                                           Cambodia   
3         15    32892                                            Russian   
4         15    34162                                        forgettable   
5         15    35957                                              short   
6         15    37729                                         dull story   
7         15    45950                                         powerpoint   
8         15   100365                                           activist   
9         15   100365                                        documentary   
10        15   100365                                             uganda   
11        23      150                                         Ron Howard 
...

我的 tf-idf 代码的第一个版本如下所示:

 vectorizer = TfidfVectorizer(use_idf=True, norm= 'l2')
 X = vectorizer.fit_transform(tags['tag'])

 print(X)

 (0, 89)    0.603928505945
 (0, 80)    0.52013528953
 (0, 577)   0.603928505945
 (1, 160)   1.0
 (2, 94)    1.0
 (3, 573)   1.0
 (4, 255)   1.0
 (5, 604)   1.0
  ...

虽然这看起来不错,但这并不是我想要的确切表示。主要有两个问题:

  1. 我认为“标签”矩阵中的每一行都被视为一个不正确的文档。许多电影被不同的用户标记为单独的条目。
  2. “X”中的 id 是矩阵索引。我怎样才能知道相应的 ML-id?假设我想知道 MLid: 150 的电影的 tf-idf 表示。我怎么知道?

如果你能告诉我如何解决上述情况,我认为这是一件很容易的事,那就太好了。

【问题讨论】:

  • 您将什么定义为文档?电影所有标签的串联?
  • 每部电影都是一个文档。在这种情况下,在标签矩阵中,每个文档都有一个唯一的movieId。因此,是的,给定电影的所有标签的串联定义了该文档(电影)。

标签: python tf-idf


【解决方案1】:

输入

userId  movieId tag
15  339 sandra 'boring' bullock
15  1955    dentist
15  7478    Cambodia
15  32892   Russian
15  34162   forgettable
15  35957   short
15  37729   dull story
15  45950   powerpoint
15  100365  activist
15  100365  documentary
15  100365  uganda
23  150 Ron Howard

代码

import pandas as pd

# consolidated dataset
tags = pd.read_csv('tfidf_input1.csv')
concatenated_tags = tags.groupby('movieId')['tag'].apply(lambda x: "%s" % ' '.join(x)).reset_index()
#print concatenated_tags

# TfidfVectorization
from sklearn.feature_extraction.text import TfidfVectorizer
vec = TfidfVectorizer()
X = vec.fit_transform(concatenated_tags['tag'])
#print X

# knowing IDs in tftdf matrix
# you have to convert to dense [NOT AT ALL advised for large matrices]
# the output is a compressed sparse matrix for the memory reason
X_dense = X.todense()
print vec.get_feature_names()
print X_dense[0,:] # output for the first movieId

【讨论】:

  • 答案看起来像我需要的,谢谢。仍然存在一个问题,如下所示:代码中concatenated_tags 的输出返回给我一个大小为(689, 764) 的矩阵。 [689电影(文档),764标签(文字)]。然而,实际上电影的数量是9125。这意味着很多电影没有被标记。现在,在您的代码中,我怎么知道X 的行对应于哪个movieId?谢谢
  • X_dense 应该返回矩阵的正确维度...... X 仍然是一个稀疏压缩的......对于通过电影 ID 检索,您需要从原始数据集中获取该电影 ID 的索引。 .. X_dense 中的相应索引应该给你正确的向量!
  • 非常感谢 Vivek。答案很准确,解决了我的问题。我将您的回复标记为真实答案。
猜你喜欢
  • 2014-09-28
  • 2017-06-13
  • 1970-01-01
  • 2017-04-26
  • 1970-01-01
  • 2017-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多