Example #1
0
def addRatings():
    if request.method == 'POST':
        user_ratings = np.zeros(len(loadMovieList()))
        ratings = request.get_json()
        for i in ratings:
            user_ratings[int(i)] = ratings[i]
        recs, acc = predict(user_ratings, 943)
        return ({"r": recs.tolist()})
Example #2
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
Example #3
0
# In[7]:

#  Check gradients by running checkCostFunction
utils.checkCostFunction(cofiCostFunc, 1.5)

# ### 2.3 Learning movie recommendations
#
# After you have finished implementing the collaborative filtering cost function and gradient, you can now start training your algorithm to make movie recommendations for yourself. In the next cell, you can enter your own movie preferences, so that later when the algorithm runs, you can get your own movie recommendations! We have filled out some values according to our own preferences, but you should change this according to your own tastes. The list of all movies and their number in the dataset can be found listed in the file `Data/movie_idx.txt`.

# In[8]:

#  Before we will train the collaborative filtering model, we will first
#  add ratings that correspond to a new user that we just observed. This
#  part of the code will also allow you to put in your own ratings for the
#  movies in our dataset!
movieList = utils.loadMovieList()
n_m = len(movieList)

#  Initialize my ratings
my_ratings = np.zeros(n_m)

# Check the file movie_idx.txt for id of each movie in our dataset
# For example, Toy Story (1995) has ID 1, so to rate it "4", you can set
# Note that the index here is ID-1, since we start index from 0.
my_ratings[0] = 4

# Or suppose did not enjoy Silence of the Lambs (1991), you can set
my_ratings[97] = 2

# We have selected a few movies we liked / did not like and the ratings we
# gave are as follows:
Example #4
0
def getList():
    if request.method == 'GET':
        return{"a": loadMovieList()}