逻辑回归预测学生是否会被大学录取. yo want to determine each applicant s chance of admission based on their res lts on two exams. yo have historical data from previo s applicants that yo can se as a training set for logistic regression. for each training example, yo have the applicant s scores on two exams and the admissions decision. ex2data1.txt(学生2次考试成绩,是否录取) 34.,78.0,0 30.,43.,0 35.,72.,0 60.,86.,1 79.0,75.,1 45.0,56.,0 ... part 1: plotting data %% == part 1: plotting data == fprintf([ plotting data with + indicating (y = 1) examples and o ... indicating (y = 0) examples.n ]); plotdata(x, y); fprintf( nprogram pa sed. press enter to contin e.n ); pa se; plotdata 函数 f nction plotdata(x, y) % create new fig re fig re; hold on; % find indices of positive and negative examples pos = find(y == 1); % 返回 y=1 的行号 组成 行号列向量 neg = find(y == 0); % plot examples plot(x(pos, 1), x(pos, 2), k+ , linewidth , 2, markersize , 7); % 2维上的点 plot(x(neg, 1), x(neg, 2), ko , markerfacecolor , y , markersize , 7); % labels and legend xlabel( exam 1 score ) ylabel( exam 2 score ) legend( admitted , not admitted ) % 图例 hold off; end 正负向类散点图 part 2: comp te cost and gradient %%  part 2: comp te cost and gradient  % in this part of the exercise, yo will implement the cost and gradient % for logistic regression. yo neeed to complete the code in % costf nction.m [m, n] = size(x); % add intercept term to x and x_test x = [ones(m, 1) x]; % initialize fitting parameters initial_theta = zeros(n + 1, 1); % theta n+1 维 % comp te and display initial cost and gradient [cost, grad] = costf nction(initial_theta, x, y); fprintf( cost at initial theta (zeros): %fn , cost); fprintf( gradient at initial theta (zeros): n ); fprintf( %f n , grad); fprintf( nprogram pa sed. press enter to contin e.n ); pa se; cost at initial theta (zeros): 0. gradient at initial theta (zeros): -0. -12.009217 -11. program pa sed. press enter to contin e. sigmoid 函数定义 f nction g = sigmoid(z) g = 1 . ( 1 + exp(-z) ); % 传入的z可能是向量,所以用 . end costf nction 求 j 和 grad 梯度算子 f nction [j, grad] = costf nction(theta, x, y) % initialize some sef l val es m = length(y); % h(x) = g(θ x) = sigmoid(x*theta) (g即sigmod函数) h = sigmoid(x*theta); % 预测为1概率 (m*1) (1-h:预测为0概率) % j = -1m * ∑ ( y*log(h(x)) + (1-y)*log(1-h(x)) ) j= -1m * s m(y .* log(h) + (1-y) .* log(1-h)); % .* 表示元素相乘,两边都是(m*1) grad = (x * (h - y))  m; % 调用公式 end 假设函数 损失函数 梯度链式求导,第 2 步用到了 sigmoid 函数求导 sigmoid 函数求导 part 3: optimizing sing fmin nc %% = part 3: optimizing sing fmin nc = % in this exercise, yo will se a b ilt-in f nction (fmin nc) to find the % optimal parameters theta. % set options for fmin nc % gradobj , on :告诉fmin nc在costf nction函数中定义了grad,minimize的时候可以利用grad % maxiter , 400 :至多循环400次 options = optimset( gradobj , on , maxiter , 400); % r n fmin nc to obtain the optimal theta % @(t)(costf nction(t, x, y)) 表示要最小化的函数 % initial_theta 从 initial_theta 开始得到最优的 theta [theta, cost] = fmin nc(@(t)(costf nction(t, x, y)), initial_theta, options); % print theta to screen fprintf( cost at theta fo nd by fmin nc: %fn , cost); fprintf( theta: n ); fprintf( %f n , theta); % plot bo ndary 做边界线,画直线 plotdecisionbo ndary(theta, x, y); % p t 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 pa sed. press enter to contin e.n ); pa se; gradobj , on : tells fmin nc that o r f nction ret rns both the cost and the gradient. this allows fmin nc to se the gradient when minimizing the f nction. 指定要最小化的函数 @(t) ( costf nction(t, x, y) ). this creates a f nction, with arg ment t, which calls yo r costf nction. this allows s to wrap the costf nction for se with fmin nc. if yo have completed the costf nction correctly, fmin nc will converge on the right optimization parameters and ret rn the final val es of the cost and θ. notice that by sing fmin nc, yo did not have to write any loops yo rself, or set a learning rate like yo did for gradient descent. this is all done by fmin nc, yo only needed to provide a f nction calc lating the cost and the gradient.