clear;
%x=load('./ex5data/ex5linx.dat');
%y=load('./ex5data/ex5liny.dat');
x=load('../linear regression/ex2data/ex2x.dat');
y=load('../linear regression/ex2data/ex2y.dat');
plot(x,y,'o','markeredgecolor','b','marke***cecolor','r');
x=[ones(length(x),1) ,x, x.^2, x.^3 ,x.^4,x.^5]; %這就是多項式模擬麼?之前找了半天,不知怎麼做的
[m, n]=size(x);
n=n-1;
%計算引數sidta,並且繪製出擬合曲線
rm=diag([0;ones(n,1)]); %lamda後面的矩陣
lamda=[0 1 10];
colortype=;
sida=zeros(n+1,3); %其實3個引數一起求了
%線性空間生成100個數
xrange=linspace(min(x(:,2)),max(x(:,2)))';
hold on;
for i=1:3
sida(:,i)=inv(x'*x+lamda(i).*rm)*x'*y;
norm_sida=norm(sida);
yrange=[ones(size(xrange)),xrange,xrange.^2,xrange.^3,...
xrange.^4,xrange.^5]*sida(:,i);
plot(xrange,yrange,char(colortype(i)));
hold on;
endlegend('traning data', '\lambda=0', '\lambda=1','\lambda=10')%注意轉義字元的使用方法
hold off;
logistic_regu.m
clear;
figure;
addpath('./ex5data');
x=load('./ex5data/ex5logx.dat');
y=load('./ex5data/ex5logy.dat');
%畫出資料的分布圖
plot(x(find(y),1),x(find(y),2),'o','marke***cecolor','b');
hold on;
plot(x(find(y==0),1),x(find(y==0),2),'r+');
legend('y=1','y=0');
x = map_feature(x(:,1), x(:,2)); %用高維,因為畫的不是直線而是彎的,像這裡就是圓
[m, n] = size(x);
theta = zeros(n, 1);
g = inline('1.0 ./ (1.0 + exp(-z))');
% setup for newton's method
max_itr = 15;
j = zeros(max_itr, 1);
lambda = 1;%lambda=0,1,10
%牛頓法
for i=1:max_itr
z=x*theta;
h=g(z);
j(i)=(1/m)*sum(-y.*log(h)-(1-y).*log(1-h))+...
(lambda/(2*m))*norm(theta)^2;
g = (lambda/m).*theta;g(1) = 0 ;%這樣不管什麼時候,都能保證首項是正確的
l = (lambda/m).*eye(n); l(1) = 0;
grad = ((1/m).*x' * (h-y)) + g;
h = ((1/m).*x' * diag(h) * diag(1-h) * x) + l;
theta = theta - h\grad;
%計算一階導和2階導
endu = linspace(-1, 1.5, 200);
v = linspace(-1, 1.5, 200);
z1 = zeros(length(u), length(v));
for i = 1:length(u)
for j = 1:length(v)
z1(i,j) = map_feature(u(i), v(j))*theta;%這裡繪製的並不是損失函式與迭代次數之間的曲線,而是線性變換後的值
endendz1 = z1';
%下面畫的是z1=0 的時候,也就是變數u v,取值的時候,z1為0,然後也就是等高線了。
contour(u, v, z1,[0,0], 'linewidth', 2)%在z上畫出為0值時的介面,因為為0時剛好概率為0.5,符合要求
legend('y = 1', 'y = 0', 'decision boundary')
title(sprintf('\\lambda = %g', lambda), 'fontsize', 14);
hold off;
模式識別與機器學習(2)
參考部落格 隨機梯度下降 clear all x load ex2data ex2x.dat y load ex2data ex2y.dat x 1.15 1.9 3.06 4.66 6.84 7.95 10,14,16 x power x,0.5 y power x,0.5 m length y ...
模式識別與機器學習(4)
講了推理,以及主要收穫為,原來是對損失函式求導。不過公式不是很對,因為 clear x load ex4data ex4x.dat y load ex4data ex4y.dat m,n size x x ones m,1 x figure pos find y neg find y 0 plot ...
PRML筆記 模式識別與機器學習
prml筆記 notes on pattern recognition and machine learning.pdf 知乎pattern recognition and machine learning這本書怎麼看?介紹了概率論,區分了經典概率論 frequentist 和 bayesian理論...