Ex_treme's blog.

数据集介绍与公共信息抽取函数代码实战

2018/11/18 Share

数据集

下载地址:http://files.grouplens.org/datasets/movielens/ml-latest-small.zip
数据集1:movies.csv

1
2
3
4
5
6
7
8
movieId,title,genres
1,Toy Story (1995),Adventure|Animation|Children|Comedy|Fantasy
2,Jumanji (1995),Adventure|Children|Fantasy
3,Grumpier Old Men (1995),Comedy|Romance
4,Waiting to Exhale (1995),Comedy|Drama|Romance
5,Father of the Bride Part II (1995),Comedy
6,Heat (1995),Action|Crime|Thriller
7,Sabrina (1995),Comedy|Romance

物品ID,物品名称,物品分类

数据集2: ratings.csv

1
2
3
4
5
6
7
8
9
userId,movieId,rating,timestamp
1,1,4.0,964982703
1,3,4.0,964981247
1,6,4.0,964982224
1,47,5.0,964983815
1,50,5.0,964982931
1,70,3.0,964982400
1,101,5.0,964980868
1,110,4.0,964982176

用户ID,物品ID,行为得分,时间戳

reader.py

用于读取数据文件,将字段转化为预定格式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# -*- coding: utf-8 -*-
# @Time : 18-11-18 上午11:44
# @Author : Ex_treme
# @Email : pzsyjsgldd@163.com
# @File : reader.py
# @Software: PyCharm
import os


def get_user_click(rating_file):
"""get user click list
Args:
rating_file:input file
Return:
dict, key:userid, value:[itemid1,itemid2]
"""
if not os.path.exists(rating_file):
return {}

fp = open(rating_file)
num = 0
user_click = {}
for line in fp:
if num == 0:
num += 1
continue
item = line.strip().split(',')
if len(item) < 4:
continue
[userid, itemid, rating, timestamp] = item
if float(rating) < 3.0:
continue
if userid not in user_click:
user_click[userid] = []
user_click[userid].append(itemid)
fp.close()
return user_click


def get_item_info(item_file):
"""
get item info[title,genres]
:param item_info: input item
:return: a dict, key itemid, value:[title,genres]
"""
if not os.path.exists(item_file):
return {}

num = 0
item_info = {}
fp = open(item_file)
for line in fp:
if num == 0:
num += 1
continue
item = line.strip().split(',')
if len(item) < 3:
continue
else:
itemid = item[0]
genres = item[-1]
title = ','.join(item[1:-1])

if itemid not in item_info:
item_info[itemid] = [title,genres]
fp.close()

return item_info

if __name__ == "__main__":
user_click = get_user_click("/home/pzs741/PycharmProjects/cd/data/ratings.csv")
for i in user_click:
print(i,user_click[i])

print("--------------------------------------------------------------------")

item_info = get_item_info("/home/pzs741/PycharmProjects/cd/data/movies.csv")
for i in item_info:
print(i, item_info[i])

175 [‘531’, ‘2300’, ‘3594’, ‘3791’, ‘3969’, ‘7169’, ‘7259’, ‘8362’, ‘8643’, ‘8911’, ‘32289’, ‘33145’, ‘33880’, ‘38388’, ‘44613’, ‘45880’, ‘47382’, ‘56949’, ‘58107’]
6615 [‘Freddy vs. Jason (2003)’, ‘Action|Horror|Thriller’]

CATALOG
  1. 1. 数据集
  2. 2. reader.py