def __init__(self, mask, n): ''' Constructor ''' self.__wordmask = mask self.__lreg = LinearRegression(n) self.__training_set = [] self.__ratings = []
class RecommenderSystem(): ''' System to rate new pages and estimate the relevance for the user ''' def __init__(self, mask, n): ''' Constructor ''' self.__wordmask = mask self.__lreg = LinearRegression(n) self.__training_set = [] self.__ratings = [] def rate(self, document): ''' rates the specified document @return: a value between 0 and 1 that specifies how well this document suits to the user ''' f = gen_feature_vector(self.__wordmask, document) h = self.__lreg.estimate(array([f])) return float(h[0][0]) def set_rate(self, document, rating): ''' Lets the user rate a particular document @param rating: The user rating, between 0 (no interest) and 1 (great interest) ''' f = gen_feature_vector(self.__wordmask, document) self.__training_set.append(f) self.__ratings.append([rating]) def train(self, iterations, learnrate): ''' Trains the classifier @param iterations: the number of iterations @param learnrate: the learning rate ''' [X, Y] = self.__gen_matrix() self.__lreg.train(iterations, learnrate, X, Y) def __gen_matrix(self): return [ array(self.__training_set), array(self.__ratings) ]