Andrew Ng coursera上的《机器学习》ex8
来源:互联网 发布:老克勒淘宝店是真是假 编辑:程序博客网 时间:2024/05/22 02:55
Andrew Ng coursera上的《机器学习》ex8
按照课程所给的ex8的文档要求,ex8要求完成以下几个计算过程的代码编写:
一、estimateGuasssian.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 correctlymu = 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.%mu=mean(X);sigma2=var(X,1);% =============================================================end
可以直接调用函数库里面的函数进行求值。
二、selectThresold.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 predictionspredictions = (pval < epsilon);fp = sum((predictions == 1) &(yval == 0));tp = sum((predictions == 1) &(yval == 1));fn = sum((predictions == 0) &(yval == 1));pr=tp/(tp+fp);re=tp/(tp+fn);F1=2*pr*re/(pr+re); % ============================================================= if F1 > bestF1 bestF1 = F1; bestEpsilon = epsilon; endendend
三、coficostFunction.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 paramsX = 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 correctlyJ = 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%M=(X*Theta'-Y).^2;J=1/2*sum(sum(R.*M));J=J+lambda/2*sum(sum(Theta.^2))+lambda/2*sum(sum(X.^2));t=(X*Theta'-Y).*R;for k=1:num_features for i=1:num_movies for j=1:num_users X_grad(i,k)=X_grad(i,k)+(X(i,:)*Theta(j,:)'-Y(i,j))*Theta(j,k).*R(i,j); end endendX_grad=X_grad+lambda*X;for k=1:num_features for j=1:num_users for i=1:num_movies Theta_grad(j,k)=Theta_grad(j,k)+(X(i,:)*Theta(j,:)'-Y(i,j))*X(i,k).*R(i,j); end endendTheta_grad=Theta_grad+lambda*Theta;% =============================================================grad = [X_grad(:); Theta_grad(:)];end
0 0
- Andrew Ng coursera上的《机器学习》ex8
- Coursera上的Andrew Ng《机器学习》学习笔记Week1
- Coursera上的Andrew Ng《机器学习》学习笔记Week2
- Andrew Ng coursera上的《机器学习》ex1
- Andrew Ng coursera上的《机器学习》ex2
- Andrew Ng coursera上的《机器学习》ex3
- Andrew Ng coursera上的《机器学习》ex4
- Andrew Ng coursera上的《机器学习》ex5
- Andrew Ng coursera上的《机器学习》ex6
- Andrew Ng coursera上的《机器学习》ex7
- Coursera上Andrew Ng机器学习课程总结(一)
- Coursera上Andrew Ng机器学习课程总结(二)
- Coursera 的机器学习 (Andrew Ng) 课程 视频百度云
- coursera斯坦福Andrew Ng的机器学习编程作业答案
- Coursera机器学习(Andrew Ng)笔记1
- Coursera机器学习(Andrew Ng)笔记:神经网络
- Coursera机器学习(Andrew Ng)笔记:大规模机器学习
- Andrew Ng机器学习笔记ex8 异常检测和推荐系统
- doTag方法程序的测试
- 开发时Activity生命周期异常记录
- Thorlabs APT activeX 开发应用
- MySQL中mysql.sock找不到的解决方法
- Intent详解
- Andrew Ng coursera上的《机器学习》ex8
- android studio升级后的各种错误解决
- 生成随机验证码
- 左侧菜单,点击右侧弹出内容
- C++从某个目录中找出某一类型的所有文件
- Servlet文件的下载
- PL/SQL【32位】连接Oracle 11g【64位】终于成功了!!!
- XCODE 添加调试真机
- 数据结构基本术语及其关系图