1、Logistic Regression
MATLAB代码:
% load the data
dataset = load("C:\Users\LENOVO\OneDrive - 东南大学\桌面\机器学习\吴恩达作业\matlab\ex2\data\ex2data1.txt");
[m, n] = size(dataset);
x1 = dataset(:, 1);
x2 = dataset(:, 2);
y = dataset(:, 3);
X = [ones(size(y)), x1, x2];
X = X';
% scatter
pos = find(y==1);
neg = find(y==0);
hold on;
plot(x1(pos), x2(pos), 'k+', 'LineWidth', 2, 'MarkerSize', 7);
plot(x1(neg), x2(neg), 'ko', 'MarkerFaceColor', 'y', 'MarkerSize', 7);
% gradient descend
init_theta = [0, 0, 0];
alpha = 0.001;
iter_nums = 150000;
theta = init_theta';
for i = 1 : iter_nums
hyp = zeros(1, m);
for j = 1 : m
hyp(1, j) = 1 / (1 + exp(-theta' * X(:,j))) - y(j, 1);
end
hyp = hyp * X';
hyp = hyp';
theta = theta - hyp * alpha /m;
end
% classfied
theta1 = ones(m, 1) .* theta(1, 1);
theta2 = ones(m, 1) .* theta(2, 1);
fitted_x2 = (theta1 + theta2.*x1)/theta(3,1) *(-1);
plot(x1, fitted_x2, 'b-');
hold off;

2、Regularized Logistic Regression
待更…
本文介绍了使用MATLAB实现的基础逻辑回归算法,并扩展探讨了正则化逻辑回归在数据分类中的应用。通过实例展示了如何通过梯度下降法求解参数,以及如何在高维数据集上防止过拟合。

2018

被折叠的 条评论
为什么被折叠?



