Пример #1
0
    def predict(self, itemList, Y, R, n=10, normalize=True):
        assert self.X is not None
        assert self.Theta is not None

        # make predictions by multiplying X and Theta
        self.predictions = self.X.dot(self.Theta.T)[:, 0]

        if normalize:
            # normalize ratings
            Ynorm, Ymean = utils.normalizeRatings(Y, R)
            self.predictions = self.predictions + Ymean

        assert n < len(self.predictions)

        # get the indices ix that would sort predictions in a descending order
        # by predictions[ix]
        self.ix = np.argsort(self.predictions)[::-1]

        self.top = []
        i = 0
        while i < n:
            j = self.ix[i]
            if self.ratings[j] == 0:  # only recommend unrated items
                self.top.append((np.rint(self.predictions[j]), itemList[j]))
                i += 1
Пример #2
0
    def train(self,
              Y,
              R,
              lambd=10,
              normalize=True,
              num_features=10,
              maxiter=100):
        print("\nTraining model...")

        # reshape ratings into a column vector
        ratings = np.reshape(self.ratings, (self.ratings.size, 1))

        # add the new user's ratings to Y and R
        Y = np.concatenate((ratings, Y), axis=1)
        R = np.concatenate(((ratings != 0), R), axis=1)

        Y_in = Y
        if normalize:
            # normalize ratings
            Ynorm, Ymean = utils.normalizeRatings(Y, R)
            Y_in = Ynorm

        # useful values
        num_users = Y.shape[1]
        num_items = Y.shape[0]

        # set initial parameters (Theta, X)
        X = np.random.randn(num_items, num_features)
        Theta = np.random.randn(num_users, num_features)
        initial_parameters = np.concatenate((X, Theta))
        initial_parameters = np.reshape(initial_parameters,
                                        initial_parameters.size)

        # set functions for the cost and the gradient
        f = lambda t: utils.costFunc(t, Y_in, R, num_users, num_items,
                                     num_features, lambd)[0]
        fprime = lambda t: utils.costFunc(t, Y_in, R, num_users, num_items,
                                          num_features, lambd)[1]

        # minimize gradient with fmincg
        def callback(x):
            if self.count % 10 == 0:
                print("iteration {}\tloss {}".format(self.count, f(x)))
            self.count += 1

        theta = fmin_cg(f=f,
                        x0=initial_parameters,
                        fprime=fprime,
                        maxiter=maxiter,
                        callback=callback)

        # unfold theta back into X and Theta
        self.X = np.reshape(theta[0:num_items * num_features],
                            (num_items, num_features))
        self.Theta = np.reshape(theta[num_items * num_features:],
                                (num_users, num_features))
Пример #3
0
def predict(new_ratings, _id):
    Y, R = loadData()
    Y = np.hstack([Y, new_ratings[:, None]])
    R = np.hstack([R, (new_ratings[:, None] > 0)])
    print("Ratings added!")
    #  Normalize Ratings
    Ynorm, Ymean = utils.normalizeRatings(Y, R)

    #  Useful Values
    num_movies, num_users = Y.shape
    num_features = 10

    # Set Initial Parameters (Theta, X)
    X = np.random.randn(num_movies, num_features)
    Theta = np.random.randn(num_users, num_features)

    initial_parameters = np.concatenate([X.ravel(), Theta.ravel()])

    # Set options for scipy.optimize.minimize
    options = {'maxiter': 100}

    # Set Regularization
    lambda_ = 10
    res = optimize.minimize(lambda x: cofiCostFunc(
        x, Ynorm, R, num_users, num_movies, num_features, lambda_),
                            initial_parameters,
                            method='TNC',
                            jac=True,
                            options=options)
    theta = res.x

    # Unfold the returned theta back into U and W
    X = theta[:num_movies * num_features].reshape(num_movies, num_features)
    Theta = theta[num_movies * num_features:].reshape(num_users, num_features)

    print('Recommender system learning completed.')

    p = np.dot(X, Theta.T)
    print(p.shape)
    my_predictions = p[:, _id] + Ymean

    movieList = utils.loadMovieList()

    ix = np.argsort(my_predictions)[::-1]
    return ix[20:], my_predictions
Пример #4
0
#  Load data
data = loadmat(os.path.join('Data', 'ex8_movies.mat'))
Y, R = data['Y'], data['R']

#  Y is a 1682x943 matrix, containing ratings (1-5) of 1682 movies by
#  943 users

#  R is a 1682x943 matrix, where R(i,j) = 1 if and only if user j gave a
#  rating to movie i

#  Add our own ratings to the data matrix
Y = np.hstack([my_ratings[:, None], Y])
R = np.hstack([(my_ratings > 0)[:, None], R])

#  Normalize Ratings
Ynorm, Ymean = utils.normalizeRatings(Y, R)

#  Useful Values
num_movies, num_users = Y.shape
num_features = 10

# Set Initial Parameters (Theta, X)
X = np.random.randn(num_movies, num_features)
Theta = np.random.randn(num_users, num_features)

initial_parameters = np.concatenate([X.ravel(), Theta.ravel()])

# Set options for scipy.optimize.minimize
options = {'maxiter': 100}

# Set Regularization