movielens.py 4.4 KB
Newer Older
Y
Yu Yang 已提交
1
import zipfile
王益 已提交
2
from common import download
Y
Yu Yang 已提交
3 4 5 6
import re
import random
import functools

Y
Refine  
Yu Yang 已提交
7 8 9 10 11 12
__all__ = [
    'train', 'test', 'get_movie_title_dict', 'max_movie_id', 'max_user_id',
    'age_table'
]

age_table = [1, 18, 25, 35, 45, 50, 56]
Y
Yu Yang 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31


class MovieInfo(object):
    def __init__(self, index, categories, title):
        self.index = int(index)
        self.categories = categories
        self.title = title

    def value(self):
        return [
            self.index, [CATEGORIES_DICT[c] for c in self.categories],
            [MOVIE_TITLE_DICT[w.lower()] for w in self.title.split()]
        ]


class UserInfo(object):
    def __init__(self, index, gender, age, job_id):
        self.index = int(index)
        self.is_male = gender == 'M'
Y
Refine  
Yu Yang 已提交
32
        self.age = age_table.index(int(age))
Y
Yu Yang 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
        self.job_id = int(job_id)

    def value(self):
        return [self.index, 0 if self.is_male else 1, self.age, self.job_id]


MOVIE_INFO = None
MOVIE_TITLE_DICT = None
CATEGORIES_DICT = None
USER_INFO = None


def __initialize_meta_info__():
    fn = download(
        url='http://files.grouplens.org/datasets/movielens/ml-1m.zip',
Y
Yu Yang 已提交
48 49
        module_name='movielens',
        md5sum='c4d9eecfca2ab87c1945afe126590906')
Y
Yu Yang 已提交
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    global MOVIE_INFO
    if MOVIE_INFO is None:
        pattern = re.compile(r'^(.*)\((\d+)\)$')
        with zipfile.ZipFile(file=fn) as package:
            for info in package.infolist():
                assert isinstance(info, zipfile.ZipInfo)
                MOVIE_INFO = dict()
                title_word_set = set()
                categories_set = set()
                with package.open('ml-1m/movies.dat') as movie_file:
                    for i, line in enumerate(movie_file):
                        movie_id, title, categories = line.strip().split('::')
                        categories = categories.split('|')
                        for c in categories:
                            categories_set.add(c)
                        title = pattern.match(title).group(1)
                        MOVIE_INFO[int(movie_id)] = MovieInfo(
                            index=movie_id, categories=categories, title=title)
                        for w in title.split():
                            title_word_set.add(w.lower())

                global MOVIE_TITLE_DICT
                MOVIE_TITLE_DICT = dict()
                for i, w in enumerate(title_word_set):
                    MOVIE_TITLE_DICT[w] = i

                global CATEGORIES_DICT
                CATEGORIES_DICT = dict()
                for i, c in enumerate(categories_set):
                    CATEGORIES_DICT[c] = i

                global USER_INFO
                USER_INFO = dict()
                with package.open('ml-1m/users.dat') as user_file:
                    for line in user_file:
                        uid, gender, age, job, _ = line.strip().split("::")
                        USER_INFO[int(uid)] = UserInfo(
                            index=uid, gender=gender, age=age, job_id=job)
    return fn


def __reader__(rand_seed=0, test_ratio=0.1, is_test=False):
    fn = __initialize_meta_info__()
    rand = random.Random(x=rand_seed)
    with zipfile.ZipFile(file=fn) as package:
        with package.open('ml-1m/ratings.dat') as rating:
            for line in rating:
                if (rand.random() < test_ratio) == is_test:
                    uid, mov_id, rating, _ = line.strip().split("::")
                    uid = int(uid)
                    mov_id = int(mov_id)
                    rating = float(rating) * 2 - 5.0

                    mov = MOVIE_INFO[mov_id]
                    usr = USER_INFO[uid]
                    yield usr.value() + mov.value() + [[rating]]


def __reader_creator__(**kwargs):
    return lambda: __reader__(**kwargs)


Y
Refine  
Yu Yang 已提交
112 113
train = functools.partial(__reader_creator__, is_test=False)
test = functools.partial(__reader_creator__, is_test=True)
Y
Yu Yang 已提交
114 115


Y
Yu Yang 已提交
116 117 118 119 120
def get_movie_title_dict():
    __initialize_meta_info__()
    return MOVIE_TITLE_DICT


Y
Refine  
Yu Yang 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
def __max_index_info__(a, b):
    if a.index > b.index:
        return a
    else:
        return b


def max_movie_id():
    __initialize_meta_info__()
    return reduce(__max_index_info__, MOVIE_INFO.viewvalues()).index


def max_user_id():
    __initialize_meta_info__()
    return reduce(__max_index_info__, USER_INFO.viewvalues()).index


Y
Yu Yang 已提交
138
def unittest():
Y
Refine  
Yu Yang 已提交
139
    for train_count, _ in enumerate(train()()):
Y
Yu Yang 已提交
140
        pass
Y
Refine  
Yu Yang 已提交
141
    for test_count, _ in enumerate(test()()):
Y
Yu Yang 已提交
142 143 144 145 146 147 148
        pass

    print train_count, test_count


if __name__ == '__main__':
    unittest()