一、ex2.m: the main .m file to call other function files

% matlab%% Machine Learning Online Class - Exercise 2: Logistic Regression
%
%  Instructions
%  ------------
%
%  This file contains code that helps you get started on the logistic
%  regression exercise. You will need to complete the following functions
%  in this exericse:
%
%     sigmoid.m
%     costFunction.m
%     predict.m
%     costFunctionReg.m
%
%  For this exercise, you will not need to change any code in this file,
%  or any other files other than those mentioned above.
%%% Initialization
clear ; close all; clc%% Load Data
%  The first two columns contains the exam scores and the third column
%  contains the label.data = load('ex2data1.txt');
X = data(:, [1, 2]); y = data(:, 3);%% ==================== Part 1: Plotting ====================
%  We start the exercise by first plotting the data to understand the
%  the problem we are working with.fprintf(['Plotting data with + indicating (y = 1) examples and o ' ...'indicating (y = 0) examples.\n']);plotData(X, y);% Put some labels
hold on;
% Labels and Legend
xlabel('Exam 1 score')
ylabel('Exam 2 score')% Specified in plot order
legend('Admitted', 'Not admitted')
hold off;fprintf('\nProgram paused. Press enter to continue.\n');
pause;%% ============ Part 2: Compute Cost and Gradient ============
%  In this part of the exercise, you will implement the cost and gradient
%  for logistic regression. You neeed to complete the code in
%  costFunction.m%  Setup the data matrix appropriately, and add ones for the intercept term
[m, n] = size(X);% Add intercept term to x and X_test
X = [ones(m, 1) X]; %m*(n+1)% Initialize fitting parameters
initial_theta = zeros(n + 1, 1); %(n+1)*1% Compute and display initial cost and gradient
[cost, grad] = costFunction(initial_theta, X, y);fprintf('Cost at initial theta (zeros): %f\n', cost);
fprintf('Gradient at initial theta (zeros): \n');
fprintf(' %f \n', grad);fprintf('\nProgram paused. Press enter to continue.\n');
pause;%% ============= Part 3: Optimizing using fminunc  =============
%  In this exercise, you will use a built-in function (fminunc) to find the
%  optimal parameters theta.%  Set options for fminunc
options = optimset('GradObj', 'on', 'MaxIter', 400);%  Run fminunc to obtain the optimal theta
%  This function will return theta and the cost
[theta, cost] = ...fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);% Print theta to screen
fprintf('Cost at theta found by fminunc: %f\n', cost);
fprintf('theta: \n');
fprintf(' %f \n', theta);% Plot Boundary
plotDecisionBoundary(theta, X, y);% Put some labels
hold on;
% Labels and Legend
xlabel('Exam 1 score')
ylabel('Exam 2 score')% Specified in plot order
legend('Admitted', 'Not admitted')
hold off;fprintf('\nProgram paused. Press enter to continue.\n');
pause;%% ============== Part 4: Predict and Accuracies ==============
%  After learning the parameters, you'll like to use it to predict the outcomes
%  on unseen data. In this part, you will use the logistic regression model
%  to predict the probability that a student with score 45 on exam 1 and
%  score 85 on exam 2 will be admitted.
%
%  Furthermore, you will compute the training and test set accuracies of
%  our model.
%
%  Your task is to complete the code in predict.m%  Predict probability for a student with score 45 on exam 1
%  and score 85 on exam 2 prob = sigmoid([1 45 85] * theta);
fprintf(['For a student with scores 45 and 85, we predict an admission ' ...'probability of %f\n\n'], prob);% Compute accuracy on our training set
p = predict(theta, X);fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100);fprintf('\nProgram paused. Press enter to continue.\n');
pause;

二、costFunction.m

function [J, grad] = costFunction(theta, X, y)
%COSTFUNCTION Compute cost and gradient for logistic regression
%   J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the
%   parameter for logistic regression and the gradient of the cost
%   w.r.t. to the parameters.% Initialize some useful values
m = length(y); % number of training examples % You need to return the following variables correctly
J = 0; %1*1
grad = zeros(size(theta)); %(n+1)*1% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
%               You should set J to the cost.
%               Compute the partial derivatives and set grad to the partial
%               derivatives of the cost w.r.t. each parameter in theta
%
% Note: grad should have the same dimensions as theta
%h = sigmoid(X*theta); %m*1
part1 = y.*(log(h)); %m*1
part2 = (1-y).*(log(1-h)); %m*1J = sum(-part1 - part2) / m; %1*1diff = h - y; %m*1
temp = X' * diff; % (n+1)*m × m*1 -> (n+1)*1
temp = temp / m; % (n+1)*1;grad = temp;% =============================================================end

三、predict.m

function p = predict(theta, X)
%PREDICT Predict whether the label is 0 or 1 using learned logistic
%regression parameters theta
%   p = PREDICT(theta, X) computes the predictions for X using a
%   threshold at 0.5 (i.e., if sigmoid(theta'*x) >= 0.5, predict 1)m = size(X, 1); % Number of training examples %m% You need to return the following variables correctly
p = zeros(m, 1); %m*1% ====================== YOUR CODE HERE ======================
% Instructions: Complete the following code to make predictions using
%               your learned logistic regression parameters.
%               You should set p to a vector of 0's and 1's
%h = X * theta; % m*(n+1) × (n+1)*1 -> m*1
g = sigmoid(h); % m*1
p = g >= 0.5;% =========================================================================end

四、costFunctionReg.m

function [J, grad] = costFunctionReg(theta, X, y, lambda)
%COSTFUNCTIONREG Compute cost and gradient for logistic regression with regularization
%   J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using
%   theta as the parameter for regularized logistic regression and the
%   gradient of the cost w.r.t. to the parameters. % Initialize some useful values
m = length(y); % number of training examples % m% You need to return the following variables correctly
J = 0; % 1*1
grad = zeros(size(theta)); % (n+1)*1% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
%               You should set J to the cost.
%               Compute the partial derivatives and set grad to the partial
%               derivatives of the cost w.r.t. each parameter in theta[J_ori, grad_ori] = costFunction(theta, X, y);
sz_theta = size(theta, 1);
theta_temp = theta(2:sz_theta);
punish_J = sum(theta_temp.^2)*lambda/2/m;
J = J_ori + punish_J;%--- grad
punish_theta = theta_temp*lambda/m;
punish_theta = [0; punish_theta];
grad = grad_ori + punish_theta;% =============================================================end

五、submit results

Machine Learning week 3 quiz: programming assignment-Logistic Regression相关推荐

  1. Machine Learning week 6 quiz: programming assignment-Regularized Linear Regression and Bias/Variance

    一.ex5.m %% Machine Learning Online Class % Exercise 5 | Regularized Linear Regression and Bias-Varia ...

  2. Machine Learning week 4 quiz: programming assignment-Multi-class Classification and Neural Networks

    一.ex3.m %% Machine Learning Online Class - Exercise 3 | Part 1: One-vs-all% Instructions % --------- ...

  3. Machine Learning week 5 quiz: programming assignment-Multi-Neural Network Learning

    一.ex4.m %% Machine Learning Online Class - Exercise 4 Neural Network Learning% Instructions % ------ ...

  4. Machine Learning week 9 quiz: programming assignment-Anomaly Detection and Recommender Systems

    一.ex8.m %% Machine Learning Online Class % Exercise 8 | Anomaly Detection and Collaborative Filterin ...

  5. Machine Learning week 8 quiz: programming assignment-K-Means Clustering and PCA

    一.ex7.m %% Machine Learning Online Class % Exercise 7 | Principle Component Analysis and K-Means Clu ...

  6. Machine Learning week 7 quiz: programming assignment-Support Vector Machines

    一.ex6.m %% Machine Learning Online Class % Exercise 6 | Support Vector Machines % % Instructions % - ...

  7. Machine Learning week 9 quiz: Recommender Systems

    Recommender Systems 5 试题 1. Suppose you run a bookstore, and have ratings (1 to 5 stars) of books. Y ...

  8. Machine Learning week 3 quiz : Logistic Regression

    Logistic Regression 5 试题 1. Suppose that you have trained a logistic regression classifier, and it o ...

  9. Machine Learning week 11 quiz: Application: Photo OCR

    Application: Photo OCR 5 试题 1. Suppose you are running a sliding window detector to find text in ima ...

最新文章

  1. 最全面的缓存架构设计
  2. leetCode:twoSum 两数之和 【JAVA实现】
  3. angularjs教程网址
  4. 统计个人已完成的工作量_已注销注册人数统计(勘察设计类)2019.4.11
  5. Centos7 ubuntu 安装Telnet服务
  6. 团队行为守则—如果你们由我来领导
  7. Wireshark和TcpDump抓包分析心得
  8. myEclipse中的web项目直接引入到eclipse中运行
  9. 照片浏览器_照片审核不通过!这可能是史上最亏的中级失败理由!
  10. PHPThumb处理图片
  11. paip.c#图片裁剪
  12. SAPUI5教程——URLHelper的使用技巧
  13. python 残差图_为啥一定要用残差图检查你的回归分析?
  14. 常吃大蒜对人有什么好处与坏处?
  15. 利用python选股的思路
  16. python回溯法解9*9数独
  17. Prolog系列学习-1
  18. 五十八、Sqoop的常用参数及命令
  19. 我不想关注你了,饭否
  20. 《挑战程序设计竞赛》推荐及算法相关书籍吐槽

热门文章

  1. 供应链金融与区块链技术-可以研读
  2. 深入理解分布式技术 - ServiceMesh 服务网格
  3. 高并发编程-重新认识Java内存模型(JMM)
  4. java bigdecimal乘法_Java BigDecimal类型的 加减乘除运算
  5. 后端程序员工作经验总结
  6. Error:java: Annotation processing is not supported for module cycles. Please ensure that all modules
  7. wpf 修改label值_WPF 获取动态添加控件的值
  8. 2021-01-27 计算机-进程与线程区别
  9. 面试java回答优缺点_阿里Java开发面经分享,面试题(附回答)
  10. mfc vs2013 项目怎么更改类名_VS2010 更改MFC标题及标题栏图标和exe图标