octave实现协同过滤推荐算法
标签:推荐算法
这是对关于电影评分的数据集使用协同过滤算法,实现推荐系统。
数据来源为:电影数据
- 先从本地导入数据(代码如下):
% 导入数据
load ('ex8_movies.mat');
- 现在对矩阵可视化看看:
-
我们可以看出,该图为Y的输出,横轴为用户,纵轴为电影,所以 Y Y 矩阵是
nummovies∗numusersnummovies∗numusers
另外对于 R R 矩阵,其Rij=1意思是电影i被用户j评分过Rij=1意思是电影i被用户j评分过
另外代码中常会看到两个矩阵:
X大小为电影数*特征数,第i行代表第i部电影的特征,Theta大小为用户数*特征数,第j行代表第j个用户对应的参数。
4.现在开始求代价函数
代码如下:
J = / * (sum(sum(R .* (((X * Theta') - Y).^) ))) ;
%正则化
J = J + lambda/ * (sum(sum(X.^))) + lambda/ * (sum(sum(Theta.^))) ;
%梯度下降
X_grad = (R .* (X * Theta' - Y)) * Theta ;
X_grad = X_grad + lambda * X ;
Theta_grad = (R .* (X * Theta' - Y))' * X ;
Theta_grad = Theta_grad + lambda * Theta ;
其中,经过正则化的公式为:
我们更新参数公式中,损失函数梯度(这里没打出正则化,代码里正则化了)为:
调用为:
%% ========= Part : Collaborative Filtering Cost Regularization ========
% Now, you should implement regularization for the cost function for
% collaborative filtering. You can implement it by adding the cost of
% regularization to the original cost computation.
%
% Evaluate cost function
J = cofiCostFunc([X(:) ; Theta(:)], Y, R, num_users, num_movies, ...
num_features, );
fprintf(['Cost at loaded parameters (lambda = 1.5): %f '...
'\n(this value should be about 31.34)\n'], J);
fprintf('\nProgram paused. Press enter to continue.\n');
pause;
好,有了这些,再加上Octave中的无约束最小化优化函数,就可以直接训练了(下面是这个优化函数调用的代码):
theta = fmincg (@(t)(cofiCostFunc(t, Y, R, num_users, num_movies, ...
num_features, lambda)), ...
initial_parameters, options);
现在可以看看对于一个用户它的效果了:
这里来了一个用户,且有该用户对几个电影的评分,代码如下:
%% ============== Part : Entering ratings for a new user ===============
% 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 = loadMovieList();
% Initialize my ratings
my_ratings = zeros(, );
% Check the file movie_idx.txt for id of each movie in our dataset
% For example, Toy Story () has ID , so to rate it "4", you can set
my_ratings() = ;
% Or suppose did not enjoy Silence of the Lambs (), you can set
my_ratings() = ;
% We have selected a few movies we liked / did not like and the ratings we
% gave are as follows:
my_ratings() = ;
my_ratings()= ;
my_ratings() = ;
my_ratings()= ;
my_ratings()= ;
my_ratings() = ;
my_ratings() = ;
my_ratings() = ;
my_ratings()= ;
fprintf('\n\nNew user ratings:\n');
for i = :length(my_ratings)
if my_ratings(i) >
fprintf('Rated %d for %s\n', my_ratings(i), ...
movieList{i});
end
end
fprintf('\nProgram paused. Press enter to continue.\n');
pause;
其中LoadmovieList()导入了如下的电影(其实是我选了几个,另外几个随便选的)
New user ratings:
Rated for Toy Story ()
Rated for Twelve Monkeys ()
Rated for Usual Suspects, The ()
Rated for Outbreak ()
Rated for Shawshank Redemption, The ()
Rated for While You Were Sleeping ()
Rated for Forrest Gump ()
Rated for Silence of the Lambs, The ()
Rated for Alien ()
Rated for Die Hard ()
Rated for Sphere ()
现在开始训练参数了:
%% ================== Part : Learning Movie Ratings ====================
% Now, you will train the collaborative filtering model on a movie rating
% dataset of movies and users
%
fprintf('\nTraining collaborative filtering...\n');
% Load data
load('ex8_movies.mat');
% Y is a 1682x943 matrix, containing ratings (-) of movies by
% users
%
% R is a 1682x943 matrix, where R(i,j) = if and only if user j gave a
% rating to movie i
% Add our own ratings to the data matrix
Y = [my_ratings Y];
R = [(my_ratings ~= ) R];
% Normalize Ratings
[Ynorm, Ymean] = normalizeRatings(Y, R);
% Useful Values
num_users = size(Y, );
num_movies = size(Y, );
num_features = ;
% Set Initial Parameters (Theta, X)
X = randn(num_movies, num_features);
Theta = randn(num_users, num_features);
initial_parameters = [X(:); Theta(:)];
% Set options for fmincg
options = optimset('GradObj', 'on', 'MaxIter', );
% Set Regularization
lambda = ;
theta = fmincg (@(t)(cofiCostFunc(t, Ynorm, R, num_users, num_movies, ...
num_features, lambda)), ...
initial_parameters, options);
% Unfold the returned theta back into U and W
X = reshape(theta(:num_movies*num_features), num_movies, num_features);
Theta = reshape(theta(num_movies*num_features+:end), ...
num_users, num_features);
fprintf('Recommender system learning completed.\n');
fprintf('\nProgram paused. Press enter to continue.\n');
pause;
然后,开始推荐:
%% ================== Part 8: Recommendation for you ====================
% After training the model, you can now make recommendations by computing
% the predictions matrix.
%
p = X * Theta';
my_predictions = p(:,) + Ymean;
movieList = loadMovieList();
[r, ix] = sort(my_predictions, 'descend');
fprintf('\nTop recommendations for you:\n');
for i=:
j = ix(i);
fprintf('Predicting rating %.1f for movie %s\n', my_predictions(j), ...
movieList{j});
end
fprintf('\n\nOriginal ratings provided:\n');
for i = :length(my_ratings)
if my_ratings(i) >
fprintf('Rated %d for %s\n', my_ratings(i), ...
movieList{i});
end
end
结果推荐了这几部电影:
Top recommendations for you:
Predicting rating for movie Forrest Gump ()
Predicting rating for movie Return of the Jedi ()
Predicting rating for movie Star Wars ()
Predicting rating for movie Raiders of the Lost Ark ()
Predicting rating for movie Shawshank Redemption, The ()
Predicting rating for movie Empire Strikes Back, The ()
Predicting rating for movie Braveheart ()
Predicting rating for movie Titanic ()
Predicting rating for movie Back to the Future ()
Predicting rating for movie Game, The ()
好吧,我也没看过,都是很老的电影。。。我也不知道推荐的准不准。。。