Programming Exercise 8: Anomaly Detection and Recommender Systems Machine Learning
2016-12-27 22:25
661 查看
大家好,今天总结Coursera网课上Andrew Ng MachineLearning 第八次作业,最后一次作业啦,哈哈
(1) estimateGaussian.m
(2)selectThreshold.m
(3)cofiCostFunc.m
(1) estimateGaussian.m
function [mu sigma2] = estimateGaussian(X) %ESTIMATEGAUSSIAN This function estimates the parameters of a %Gaussian distribution using the data in X % [mu sigma2] = estimateGaussian(X), % The input X is the dataset with each n-dimensional data point in one row % The output is an n-dimensional vector mu, the mean of the data set % and the variances sigma^2, an n x 1 vector % % Useful variables [m, n] = size(X); % You should return these values correctly mu = zeros(n, 1); sigma2 = zeros(n, 1); % ====================== YOUR CODE HERE ====================== % Instructions: Compute the mean of the data and the variances % In particular, mu(i) should contain the mean of % the data for the i-th feature and sigma2(i) % should contain variance of the i-th feature. % for i=1:n mu(i,1)=1/m*sum(X(:,i)); mui=mu(i,1).*ones(m,1); sigma2(i,1)=1/m*sum((X(:,i)-mui).^2); end % ============================================================= end
(2)selectThreshold.m
function [bestEpsilon bestF1] = selectThreshold(yval, pval) %SELECTTHRESHOLD Find the best threshold (epsilon) to use for selecting %outliers % [bestEpsilon bestF1] = SELECTTHRESHOLD(yval, pval) finds the best % threshold to use for selecting outliers based on the results from a % validation set (pval) and the ground truth (yval). % bestEpsilon = 0; bestF1 = 0; F1 = 0; stepsize = (max(pval) - min(pval)) / 1000; for epsilon = min(pval):stepsize:max(pval) % ====================== YOUR CODE HERE ====================== % Instructions: Compute the F1 score of choosing epsilon as the % threshold and place the value in F1. The code at the % end of the loop will compare the F1 score for this % choice of epsilon and set it to be the best epsilon if % it is better than the current choice of epsilon. % % Note: You can use predictions = (pval < epsilon) to get a binary vector % of 0's and 1's of the outlier predictions predictions = (pval < epsilon) %假设此预测为阳性,阳性则返回1 tp=sum((predictions==1)&(yval==1)); fp=sum((predictions==1)&(yval==0)); fn=sum((predictions==0)&(yval==1)); prec=tp/(tp+fp); rec=tp/(tp+fn); F1=2*(prec*rec)/(prec+rec); % ============================================================= if F1 > bestF1 bestF1 = F1; bestEpsilon = epsilon; end end end
(3)cofiCostFunc.m
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'-Y).^2.*R))+lambda/2*sum(sum(X.^2))+lambda/2*sum(sum(Theta.^2)); X_grad=(X*Theta'-Y).*R*Theta+lambda*X; Theta_grad=((X*Theta'-Y).*R)'*X+lambda*Theta % ============================================================= grad = [X_grad(:); Theta_grad(:)]; end
相关文章推荐
- Machine Learning week 9 quiz: programming assignment-Anomaly Detection and Recommender Systems
- Machine Learning by Andrew Ng --- Anomaly Detection and Recommender systems
- Coursera Machine Learning 第九周 quizProgramming Exercise 8: Anomaly Detection and Recommender Systems
- Programming Exercise 8:Anomaly Detection and Recommender Systems 第二部分
- Machine Learning 第八波编程作业(完)——Anomaly Detection and Recommender Systems
- Andrew NG 机器学习 练习8-Anomaly Detection and Recommender Systems
- Machine Learning week 8 programming exercise K-means Clustering and Principal Component Analysis
- Andrew NG 机器学习 笔记-week9-异常检测和推荐系统(Anomaly Detection and Recommender Systems)
- Anomaly Detection and Recommender Systems[异常检测&推荐系统]
- Machine Learning week 4 programming exercise One vs All and Neural network
- Programming Exercise 8:Anomaly Detection and Recommender Systems第一部分
- Machine Learning week 3 programming exercise logistic regression
- Coursera Machine Learning 第五周 quiz Programming Exercise 4: Neural Networks Learning
- 《Wide and Deep Learning for Recommender Systems》学习笔记
- Coursera Machine Learning 第三周 quiz Programming Exercise 2: Logistic Regression
- Machine Learning week 5 programming exercise Neural Network Learning
- MachineLearning Exercise 5 :Regularized Linear Regression and Bias vs Variance
- 机器学习中的神经网络Neural Networks for Machine Learning:Programming assignment 3: Optimization and generaliza
- MachineLearning Exercise 7 : K-means Clustering and Principle Component Analysis
- Machine Learning week 6 quiz: programming assignment-Regularized Linear Regression and Bias/Variance