斯坦福机器学习实验之2-逻辑回归(Logistic Regression)

来源:互联网 发布:电路模拟软件 编辑:程序博客网 时间:2024/05/21 06:25

1.逻辑回归

目标:利用逻辑回归预测学生是否被录取。
数据集:一群学生的两次考试成绩作为输入和是否被录取作为输出。

1.1数据可视化(Visualizing the the data)
函数:find

% Find Indices of Positive and Negative Examplespos = find(y==1); neg = find(y == 0);% Plot Examplesplot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2, ...'MarkerSize', 7);plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y', ...'MarkerSize', 7);

1.2 执行(Implementation)
1.2.1 sigmoid函数(sigmoid function)
函数:exp

function t=sigmoid(z)  t=zeros(length(z));  t=1./(1+exp(-1*z)); end

1.2.2 代价函数和梯度(Cost function and gradient)
函数:log

function [J,grad] = costFunction(theta,X,y)%myFun - Description%% Syntax: [cost,grad] = costFunction(initial_theta,X,y)%% Long description   m=length(y);   J=0;   grad=zeros(size(theta));   J=-1/m*sum(y.*log(sigmoid(X*theta))+(1-y).*log(1-sigmoid(X*theta)));   grad=1/m*X'*(sigmoid(X*theta)-y);end

注:matlab里的自带的函数都是为处理向量/矩阵准备的!

1.2.3 利用fminunc学习参数
函数:optimset;fminunc

% Set options for fminuncoptions = 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);

1.2.4 评估逻辑回归(Evaluating logistic regression)

ex2.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 setp = predict(theta, X);fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100);fprintf('\nProgram paused. Press enter to continue.\n');pause;

predict.m

function p = predict(theta,X)   m=size(X,1);   p=zeros(m,1);   p(sigmoid(X*theta)>0.5)=1; end
阅读全文
0 0
原创粉丝点击