要解决的问题是,给出了具有2个特征的一堆训练数据集,从该数据的分布可以看出它们并不是非常线性可分的,因此很有必要用更高阶的特征来模拟。例如本程序中个就用到了特征值的6次方来求解。

Data

To begin, load the files 'ex5Logx.dat' and ex5Logy.dat' into your program. This dataset represents the training set of a logistic regression problem with two features. To avoid confusion later, we will refer to the two input features contained in 'ex5Logx.dat' as and . So in the 'ex5Logx.dat' file, the first column of numbers represents the feature , which you will plot on the horizontal axis, and the second feature represents , which you will plot on the vertical axis.

After loading the data, plot the points using different markers to distinguish between the two classifications. The commands in Matlab/Octave will be:

x = load('ex5Logx.dat');
y = load('ex5Logy.dat'); figure % Find the indices for the 2 classes
pos = find(y); neg = find(y == 0); plot(x(pos, 1), x(pos, 2), '+')
hold on
plot(x(neg, 1), x(neg, 2), 'o')

After plotting your image, it should look something like this:

Model

the hypothesis function is

 

Let's look at the parameter in the sigmoid function .

In this exercise, we will assign to be all monomials (meaning polynomial terms) of and up to the sixth power:

To clarify this notation: we have made a 28-feature vector where

此时加入了规则项后的系统的损失函数为:

Newton’s method

Recall that the Newton's Method update rule is

1. is your feature vector, which is a 28x1 vector in this exercise.

2. is a 28x1 vector.

3. and are 28x28 matrices.

4. and are scalars.

5. The matrix following in the Hessian formula is a 28x28 diagonal matrix with a zero in the upper left and ones on every other diagonal entry.

After convergence, use your values of theta to find the decision boundary in the classification problem. The decision boundary is defined as the line where

Code

%载入数据
clc,clear,close all;
x = load('ex5Logx.dat');
y = load('ex5Logy.dat'); %画出数据的分布图
plot(x(find(y),),x(find(y),),'o','MarkerFaceColor','b')
hold on;
plot(x(find(y==),),x(find(y==),),'r+')
legend('y=1','y=0') % Add polynomial features to x by
% calling the feature mapping function
% provided in separate m-file
x = map_feature(x(:,), x(:,)); %投影到高维特征空间 [m, n] = size(x); % Initialize fitting parameters
theta = zeros(n, ); % Define the sigmoid function
g = inline('1.0 ./ (1.0 + exp(-z))'); % setup for Newton's method
MAX_ITR = ;
J = zeros(MAX_ITR, ); % Lambda is the regularization parameter
lambda = ;%lambda=,,,修改这个地方,运行3次可以得到3种结果。 % Newton's Method
for i = :MAX_ITR
% Calculate the hypothesis function
z = x * theta;
h = g(z); % Calculate J (for testing convergence) -- 损失函数
J(i) =(/m)*sum(-y.*log(h) - (-y).*log(-h))+ ...
(lambda/(*m))*norm(theta([:end]))^; % Calculate gradient and hessian.
G = (lambda/m).*theta; G() = ; % extra term for gradient
L = (lambda/m).*eye(n); L() = ;% extra term for Hessian
grad = ((/m).*x' * (h-y)) + G;
H = ((/m).*x' * diag(h) * diag(1-h) * x) + L; % Here is the actual update
theta = theta - H\grad; end % Plot the results
% We will evaluate theta*x over a
% grid of features and plot the contour
% where theta*x equals zero % Here is the grid range
u = linspace(-, 1.5, );
v = linspace(-, 1.5, ); z = zeros(length(u), length(v));
% Evaluate z = theta*x over the grid
for i = :length(u)
for j = :length(v)
z(i,j) = map_feature(u(i), v(j))*theta;%这里绘制的并不是损失函数与迭代次数之间的曲线,而是线性变换后的值
end
end
z = z'; % important to transpose z before calling contour % Plot z =
% Notice you need to specify the range [, ]
contour(u, v, z, [, ], 'LineWidth', )%在z上画出为0值时的界面,因为为0时刚好概率为0.,符合要求
legend('y = 1', 'y = 0', 'Decision boundary')
title(sprintf('\\lambda = %g', lambda), 'FontSize', ) hold off % Uncomment to plot J
% figure
% plot(:MAX_ITR-, J, 'o--', 'MarkerFaceColor', 'r', 'MarkerSize', )
% xlabel('Iteration'); ylabel('J')

Result

最新文章

  1. Python: open和codecs.open
  2. Python 随机数生成总结
  3. python成长之路-----day1----笔记(1)
  4. [ASP.NET MVC] ASP.NET Identity学习笔记 - 原始码下载、ID型别差异
  5. 20150906VS小知识
  6. mongodb 最佳实践
  7. GitHub 基本操作流程
  8. Web- HTML网页颜色大全
  9. hdu 1403 Longest Common Substring(最长公共子字符串)(后缀数组)
  10. easyUI的datagrid控件日期列不能正确显示Json格式数据的解决方案
  11. 使用ToolRunner运行Hadoop程序基本原理分析
  12. find: missing argument to `-exec'
  13. Angular中 build的时候遇到的错误--There are multiple modules with names that only differ in casing
  14. spring-cloud-gateway负载普通web项目
  15. 【Python 17】B分R计算器1.0(数值类型)
  16. vue drag 对表格的列进行拖动排序
  17. Docker 容器内存限制 - 八
  18. 公共的service接口
  19. 【TPM】tpm搭建基础指南
  20. HDU.5819.Knights(概率DP)

热门文章

  1. Ubuntu18.06 Mate桌面环境下VirtuslBox打开虚拟机“全局菜单”异常退出解决办法
  2. CF981C(菊花图)
  3. git error: object file .git/objects/b9/e269f50db2a3415cc8ad5ba40b82b9b6a13d45 is empty
  4. 紫书 例题 10-25 UVa 1363(找规律)
  5. HDU 4976 A simple greedy problem. 贪心+DP
  6. Spring拦截器 /* 和 /** 的区别
  7. CSU 1249 竞争性酶抑制剂和同工酶
  8. HTML学习----------DAY1 第二节
  9. bug14052601
  10. error:assign attribute must be unsafeunretained