AI-026: 练习:Recommender System

本文是学习Andrew Ng的机器学习系列教程的学习笔记。教学视频地址:

https://study.163.com/course/introduction.htm?courseId=1004570029#/courseDetail?tab=1

通过用户对电影的评分,推荐用户喜爱的电影:

  1. 建立协同过滤的成本函数
  2. 建立协同过滤的梯度下降函数
  3. 同时最优化参数和特征
  4. 预测新用户对电影的评价

原始数据:

写函数时用矩阵方式计算效率高,公式也简洁。

function [J, grad] = cofiCostFunc(params, Y, R, num_users, num_movies, ...
                                  num_features, lambda)
%COFICOSTFUNC Collaborative filtering cost function
%   [J, grad] = COFICOSTFUNC(params, Y, R, num_users, num_movies, ...
%   num_features, lambda) returns the cost and gradient for the
%   collaborative filtering problem.
%

% Unfold the U and W matrices from params
X = reshape(params(1:num_movies*num_features), num_movies, num_features);
Theta = reshape(params(num_movies*num_features+1:end), ...
                num_users, num_features);

            
% You need to return the following values correctly
J = 0;
X_grad = zeros(size(X));
Theta_grad = zeros(size(Theta));

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost function and gradient for collaborative
%               filtering. Concretely, you should first implement the cost
%               function (without regularization) and make sure it is
%               matches our costs. After that, you should implement the 
%               gradient and use the checkCostFunction routine to check
%               that the gradient is correct. Finally, you should implement
%               regularization.
%
% Notes: X - num_movies  x num_features matrix of movie features
%        Theta - num_users  x num_features matrix of user features
%        Y - num_movies x num_users matrix of user ratings of movies
%        R - num_movies x num_users matrix, where R(i, j) = 1 if the 
%            i-th movie was rated by the j-th user
%
% You should set the following variables correctly:
%
%        X_grad - num_movies x num_features matrix, containing the 
%                 partial derivatives w.r.t. to each element of X
%        Theta_grad - num_users x num_features matrix, containing the 
%                     partial derivatives w.r.t. to each element of Theta
%

%J = 1/2.*sum(sum((X * Theta' .* R - Y .* R) .^ 2));%矩阵乘以R能保证R为1时参与计算
J = (1/2).*sum(sum(((X * Theta') .* R - Y .* R) .^ 2)) 
	+ ( lambda ./ 2 .* sum(sum(Theta .^ 2)))
	+ ( lambda ./ 2 .* sum(sum(X .^ 2)));

%X_grad = ((X * Theta') .* R - Y .* R) * Theta;
%Theta_grad = (X' * (((X * Theta') .* R) - (Y .* R)))';
X_grad = ((X * Theta') .* R * Theta - Y .* R * Theta) + lambda .* X;
Theta_grad = ((X' * ((X * Theta') .* R) - X' * (Y .* R)))' + lambda .* Theta;


% =============================================================

grad = [X_grad(:); Theta_grad(:)];

end

预测结果,推荐新用户的几部电影:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

铭记北宸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值