斯坦福机器学习Coursera课程:第八次作业--推荐系统

根据已有的1682部电影和943用户及部分用户对电影的评分数据,对新用户作电影推荐或预测未评价的评分。


一. 准备工作

1. 加载ex8_movies.mat数据

Y (1682*943) 用户评份数据,由1-5组成;

R 标记矩阵,R(i,j)=1代表用户j评分了电影i,没评的为0;

目标是对用户没评分的电影作预测;同时把预测分最高的电影推荐给用户。

同时,为更好地理解矩阵Y,我们试着计算下用户对第一部电影Toy Story的平均评分,即 mean(Y(1,R(1,:)))=

Average rating for movie 1 (Toy Story): 3.878319 / 5

(这里的算式确实很精简,选对函数很重要,否则按正常顺序来计算也能得到结果,但麻烦很多,如

R1_num=length(find(R(1,:)))
R1_num =  452
>> avg=sum(Y(1,:))/R1_num
avg =  3.8783

另外,还使用imagesc(Y)对Y的数据进行可视化,对直接了解矩阵数据的稀疏性、数据分布的均匀性等有帮助。


2. 特征矩阵X和参数矩阵Theta

X中的第ix(i)表示第i部电影的特征向量,即,有多少个电影就有多少行,列数为特征数

Theta中的第j行表示第j个用户的参数,即行为用户数,列为用户参数的维度数

两者均为维度相同的向量,假如维度为100,刚X=Nm*100, Theta=Nu*100,相乘后为Nm*Nu,即评分矩阵。


二. 协同过滤算法

根据Y(i,j)=((Theta(j)T)*X(i),通过学习得到误差最小时的参数向量X(1), X(2), ...X(nm), Theta(1), Theat(2),...Theta(nu),即矩阵X,Theta.

下面分有无正则化两种情况进行成本函数,梯度等的说明,差别公在于有无lambda项。

1. 协同过滤的成本函数(无正则化)



对R(i,j)=1中所有的项依次累加得到J.

实现技巧:sum(sum(R.*M) 要得出R与M乘积后得到的矩阵中所有元素的和 (R.*M得到一矩阵,内层sum()计算出各列的和,是一向量,外层sum()计算出内层sum()中各向量的和。

2. 协同过滤梯度(无正则化)


这里得到的X梯度和Theta梯度分别是与X,Theat size一样的矩阵,即X, Theta中各元素对应位置上都将有梯度值。



汇总这两步中的实现代码为:cofiCostFunc.m

J=J+sum(sum((R.*(X*Theta'-Y).^2)))/2;

for i=1:size(X,1)  
    idx = find(R(i,:)==1);  
    Thetatemp = Theta(idx,:);  
    Ytemp = Y(i,idx);  
    X_grad(i,:)=(X(i,:)*Thetatemp'-Ytemp)*Thetatemp;  


for j=1:size(Theta,1)  
    idx = find(R(:,j)==1);  
    Xtemp = X(idx,:);  
    Ytemp = Y(idx,j);  
  Theta_grad(j,:)=(Xtemp*Theta(j,:)'-Ytemp)'*Xtemp; 


3. 正则化的成本函数和梯度




J=J+sum(sum((R.*(X*Theta'-Y).^2)))/2+lambda/2*sum(sum(Theta.^2))+...  
    lambda/2*sum(sum(X.^2));

for i=1:size(X,1)  
    idx = find(R(i,:)==1);  
    Thetatemp = Theta(idx,:);  
    Ytemp = Y(i,idx);  
    X_grad(i,:)=(X(i,:)*Thetatemp'-Ytemp)*Thetatemp+lambda*X(i,:);  
end  


for j=1:size(Theta,1)  
    idx = find(R(:,j)==1);  
    Xtemp = X(idx,:);  
    Ytemp = Y(idx,j);  
    Theta_grad(j,:)=(Xtemp*Theta(j,:)'-Ytemp)'*Xtemp+lambda*Theta(j,:);   

4. 电影评分学习和推荐

参照Y的形式,在Y中增加一列(即多一个用户的评分),即Y,R均变为1683*943的矩阵。

此次训练我们取特征数为10,对Y、R归一化后,随机初始化X,Theta,设定迭代100次,且lambda=10, 得到优化后的X, Theta,然后对新增的用户进行推荐。

Y = [my_ratings Y];
R = [(my_ratings ~= 0) R];

%  Normalize Ratings
[Ynorm, Ymean] = normalizeRatings(Y, R);

%  Useful Values
num_users = size(Y, 2);
num_movies = size(Y, 1);
num_features = 10;

% 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', 100);

% Set Regularization
lambda = 10;
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(1:num_movies*num_features), num_movies, num_features);
Theta = reshape(theta(num_movies*num_features+1:end), ...
                num_users, num_features);


%% ================== 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(:,1) + Ymean;

movieList = loadMovieList();

[r, ix] = sort(my_predictions, 'descend');
fprintf('\nTop recommendations for you:\n');
for i=1:10
    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 = 1:length(my_ratings)
    if my_ratings(i) > 0 
        fprintf('Rated %d for %s\n', my_ratings(i),movieList{i});
    end
end


New user ratings:
Rated 4 for Toy Story (1995)
Rated 3 for Twelve Monkeys (1995)
Rated 5 for Usual Suspects, The (1995)
Rated 4 for Outbreak (1995)
Rated 5 for Shawshank Redemption, The (1994)
Rated 3 for While You Were Sleeping (1995)
Rated 5 for Forrest Gump (1994)
Rated 2 for Silence of the Lambs, The (1991)
Rated 4 for Alien (1979)
Rated 5 for Die Hard 2 (1990)
Rated 5 for Sphere (1998)


lambda=10,  num_features=10, 迭代100次:

Predicting rating 5.0 for movie Aiqing wansui (1994)
Predicting rating 5.0 for movie Someone Else's America (1995)
Predicting rating 5.0 for movie Santa with Muscles (1996)
Predicting rating 5.0 for movie Saint of Fort Washington, The (1993)
Predicting rating 5.0 for movie Entertaining Angels: The Dorothy Day Story (1996)
Predicting rating 5.0 for movie They Made Me a Criminal (1939)
Predicting rating 5.0 for movie Marlene Dietrich: Shadow and Light (1996)
Predicting rating 5.0 for movie Prefontaine (1997)
Predicting rating 5.0 for movie Star Kid (1997)
Predicting rating 5.0 for movie Great Day in Harlem, A (1994)

相同参数情况下,迭代1000次及修改lambda=1.5时,分别迭代100,1000次,推荐的片名已差别不大,只是个别顺序略有差异而已。

lambda=10,  num_features=10, 迭代1000次

Predicting rating 5.0 for movie Star Kid (1997)
Predicting rating 5.0 for movie Entertaining Angels: The Dorothy Day Story (1996)
Predicting rating 5.0 for movie Saint of Fort Washington, The (1993)
Predicting rating 5.0 for movie Santa with Muscles (1996)
Predicting rating 5.0 for movie Prefontaine (1997)
Predicting rating 5.0 for movie Marlene Dietrich: Shadow and Light (1996)
Predicting rating 5.0 for movie They Made Me a Criminal (1939)
Predicting rating 5.0 for movie Great Day in Harlem, A (1994)
Predicting rating 5.0 for movie Someone Else's America (1995)
Predicting rating 5.0 for movie Aiqing wansui (1994)

lambda=1.5
Predicting rating 5.0 for movie Saint of Fort Washington, The (1993)
Predicting rating 5.0 for movie Someone Else's America (1995)
Predicting rating 5.0 for movie Star Kid (1997)
Predicting rating 5.0 for movie They Made Me a Criminal (1939)
Predicting rating 5.0 for movie Marlene Dietrich: Shadow and Light (1996)
Predicting rating 5.0 for movie Santa with Muscles (1996)
Predicting rating 5.0 for movie Aiqing wansui (1994)
Predicting rating 5.0 for movie Prefontaine (1997)
Predicting rating 5.0 for movie Great Day in Harlem, A (1994)
Predicting rating 5.0 for movie Entertaining Angels: The Dorothy Day Story (1996)

把num_features修改为20,不断调整lambda的大小,如10,3,1.5, 0.8,0.3等,分别迭代100次,将会发现,

成本函数随着lambda的减少,也在减少,但推荐列表中的电影评分已超出5,

lambda=3, Iteration   100 | Cost: 2.481120e+004
lambda=0.8, Iteration   100 | Cost: 1.827028e+004
ladbda=0.3, Iteration   100 | Cost: 1.644490e+004
lambda=0.1, Iteration   100 | Cost: 1.637439e+004
lambda=0.03, Iteration   100 | Cost: 1.625596e+004

lambda=0.1时的推荐列表为:

Top recommendations for you:
Predicting rating 12.3 for movie Don't Be a Menace to South Central While Drinking Your Juice in the Hood (1996)
Predicting rating 9.9 for movie Pather Panchali (1955)
Predicting rating 9.5 for movie Aristocats, The (1970)
Predicting rating 9.4 for movie Warriors of Virtue (1997)
Predicting rating 9.3 for movie Cérémonie, La (1995)
Predicting rating 9.2 for movie Joy Luck Club, The (1993)
Predicting rating 9.2 for movie Juror, The (1996)
Predicting rating 9.1 for movie Mouse Hunt (1997)
Predicting rating 9.0 for movie Substitute, The (1996)
Predicting rating 8.8 for movie Crossing Guard, The (1995)

lambda=0.8时的推荐列表为:

Predicting rating 6.1 for movie Happy Gilmore (1996)
Predicting rating 5.9 for movie Desperado (1995)
Predicting rating 5.8 for movie Gattaca (1997)
Predicting rating 5.7 for movie Four Rooms (1995)
Predicting rating 5.7 for movie Sleepers (1996)
Predicting rating 5.6 for movie Monty Python and the Holy Grail (1974)
Predicting rating 5.5 for movie Saint, The (1997)
Predicting rating 5.4 for movie Stargate (1994)
Predicting rating 5.4 for movie Career Girls (1997)
Predicting rating 5.3 for movie In Love and War (1996)

比较合适的可能还是lambda=3时,成本函数值较小,推荐列表评分也合适,电影片名与lambda=10时也出入不大。

lambda=3, feature=10, Iteration   100 | Cost: 3.070014e+004
Recommender system learning completed.

Program paused. Press enter to continue.

Top recommendations for you:
Predicting rating 5.0 for movie Prefontaine (1997)
Predicting rating 5.0 for movie Someone Else's America (1995)
Predicting rating 5.0 for movie Star Kid (1997)
Predicting rating 5.0 for movie Aiqing wansui (1994)
Predicting rating 5.0 for movie Marlene Dietrich: Shadow and Light (1996)
Predicting rating 5.0 for movie Great Day in Harlem, A (1994)
Predicting rating 5.0 for movie They Made Me a Criminal (1939)
Predicting rating 5.0 for movie Santa with Muscles (1996)
Predicting rating 5.0 for movie Entertaining Angels: The Dorothy Day Story (1996)
Predicting rating 5.0 for movie Saint of Fort Washington, The (1993)

简要总结一下:计算和推荐时,还是需要不断调整参数,根据结果选择较优的。这个不断调整参数的过程,确实是个时间和经验活。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值