注:本文为学习课程的作业,使用软件为matlab,学习网站为:https://www.coursera.org/learn/machine-learning/programming/ixFof/logistic-regression
1. Logistic Regression
在本部分练习中,建立一个逻辑回归模型,以预测学生是否被大学录取。根据每位申请人在两次考试中的成绩来确定他们的入学机会。 您具有以前申请人的历史数据,可以用作逻辑回归的训练集。 对于每个培训示例,您都有两次考试的申请人分数和入学决定。
任务是建立一个分类模型,根据这两次考试的分数估算申请人的录取概率。
加载数据:
% Load Data
% The first two columns contain the exam scores and the third column contains the label.
data = load('ex2data1.txt');
X = data(:, [1, 2]); y = data(:, 3);
1.1 Visualizing the data
可视化数据,在plotData.m中完成代码:
function plotData(X, y)
%PLOTDATA Plots the data points X and y into a new figure
% PLOTDATA(x,y) plots the data points with + for the positive examples
% and o for the negative examples. X is assumed to be a Mx2 matrix.
% Create New Figure
figure; hold on;
% ====================== YOUR CODE HERE ======================
% Instructions: Plot the positive and negative examples on a
% 2D plot, using the option 'k+' for the positive
% examples and 'ko' for the negative examples.
%
% Find Indices of Positive and Negative Examples
pos = find(y==1); neg = find(y == 0);
% Plot Examples
plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2, 'MarkerSize', 7);
plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y','MarkerSize', 7);
% =========================================================================
hold off;
end
调用函数:
% Plot the data with + indicating (y = 1) examples and o indicating (y = 0) examples.
plotData(X, y);
% Labels and Legend
xlabel('Exam 1 score')
ylabel('Exam 2 score')
% Specified in plot order
legend('Admitted', 'Not admitted')
结果如下:
1.2 Implementation
1.2.1 Warmup exercise: sigmoid function(S函数)
逻辑回归假设函数: