forked from PRML/PRMLT
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
335 additions
and
232 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
% demos for ch03 | ||
clear; close all; | ||
d = 1; | ||
n = 200; | ||
[x,t] = linRnd(d,n); | ||
%% Empirical Bayesian linear regression via EM | ||
[model,llh] = linRegEm(x,t); | ||
plot(llh); | ||
[y,sigma] = linRegPred(model,x,t); | ||
figure | ||
plotCurveBar(x,y,sigma); | ||
hold on; | ||
plot(x,t,'o'); | ||
hold off; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
% demos for ch03 | ||
clear; close all; | ||
d = 1; | ||
n = 200; | ||
[x,t] = linRnd(d,n); | ||
%% Empirical Bayesian linear regression via Mackay fix point iteration method | ||
[model,llh] = linRegFp(x,t); | ||
plot(llh); | ||
[y,sigma] = linRegPred(model,x,t); | ||
figure | ||
plotCurveBar(x,y,sigma); | ||
hold on; | ||
plot(x,t,'o'); | ||
hold off; | ||
%% | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
% demos for ch03 | ||
clear; close all; | ||
d = 1; | ||
n = 200; | ||
[x,t] = linRnd(d,n); | ||
%% Linear regression | ||
model = linReg(x,t); | ||
[y,sigma] = linRegPred(model,x,t); | ||
plotCurveBar( x, y, sigma ); | ||
hold on; | ||
plot(x,t,'o'); | ||
hold off; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
% demos for ch04 | ||
|
||
%% Logistic logistic regression for binary classification | ||
clear; close all; | ||
k = 2; | ||
n = 1000; | ||
[X,t] = kmeansRnd(2,k,n); | ||
[model, llh] = logitBin(X,t-1); | ||
plot(llh); | ||
y = logitBinPred(model,X)+1; | ||
binPlot(model,X,y) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
%% Logistic logistic regression for multiclass classification | ||
clear | ||
k = 3; | ||
n = 1000; | ||
[X,t] = kmeansRnd(2,k,n); | ||
[model, llh] = logitMn(X,t); | ||
y = logitMnPred(model,X); | ||
plotClass(X,y) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
%% demo for knCenter | ||
clear; close all; | ||
kn = @knGauss; | ||
X=rand(2,100); | ||
X1=rand(2,10); | ||
X2=rand(2,5); | ||
|
||
maxdiff(knCenter(kn,X,X1),diag(knCenter(kn,X,X1,X1))') | ||
maxdiff(knCenter(kn,X),knCenter(kn,X,X,X)) |
141 changes: 61 additions & 80 deletions
141
chapter06/demo.m → demo/ch06/knLin_demo.m
100755 → 100644
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,61 @@ | ||
demos for ch06 | ||
|
||
|
||
%% Kernel regression with gaussian kernel | ||
clear; close all; | ||
n = 100; | ||
x = linspace(0,2*pi,n); % test data | ||
t = sin(x)+rand(1,n)/2; | ||
model = knReg(x,t,1e-4,@knGauss); | ||
[y,s] = knRegPred(model,x); | ||
plotCurveBar(x,y,s); | ||
hold on; | ||
plot(x,t,'o'); | ||
hold off; | ||
%% Kernel regression with linear kernel is EQUIVALENT to linear regression | ||
lambda = 1e-4; | ||
model_kn = knReg(x,t,lambda,@knLin); | ||
model_lin = linReg(x,t,lambda); | ||
|
||
idx = 1:2:n; | ||
xt = x(:,idx); | ||
tt = t(idx); | ||
|
||
[y_kn, sigma_kn,p_kn] = knRegPred(model_kn,xt,tt); | ||
[y_lin, sigma_lin,p_lin] = linRegPred(model_lin,xt,tt); | ||
|
||
maxdiff(y_kn,y_lin) | ||
maxdiff(sigma_kn,sigma_lin) | ||
maxdiff(p_kn,p_lin) | ||
%% Kernel kmeans with linear kernel is EQUIVALENT to kmeans | ||
clear; close all; | ||
d = 2; | ||
k = 3; | ||
n = 500; | ||
[X,y] = kmeansRnd(d,k,n); | ||
init = ceil(k*rand(1,n)); | ||
[y_kn,en_kn,model_kn] = knKmeans(X,init,@knLin); | ||
[y_lin,en_lin,model_lin] = kmeans(X,init); | ||
|
||
idx = 1:2:n; | ||
Xt = X(:,idx); | ||
|
||
[t_kn,ent_kn] = knKmeansPred(model_kn, Xt); | ||
[t_lin,ent_lin] = kmeansPred(model_lin, Xt); | ||
|
||
maxdiff(y_kn,y_lin) | ||
maxdiff(en_kn,en_lin) | ||
|
||
maxdiff(t_kn,t_lin) | ||
maxdiff(ent_kn,ent_lin) | ||
%% Kernel PCA with linear kernel is EQUIVALENT TO PCA | ||
clear; close all; | ||
d = 10; | ||
q = 2; | ||
n = 500; | ||
X = randn(d,n); | ||
|
||
|
||
model_kn = knPca(X,q,@knLin); | ||
idx = 1:2:n; | ||
Xt = X(:,idx); | ||
|
||
Y_kn = knPcaPred(model_kn,Xt); | ||
|
||
[U,L,mu,mse] = pca(X,q); | ||
Y_lin = U'*bsxfun(@minus,Xt,mu); % projection | ||
|
||
|
||
R = Y_lin/Y_kn; % the results are equivalent up to a rotation. | ||
maxdiff(R*R', eye(q)) | ||
|
||
%% demo for knCenter | ||
clear; close all; | ||
kn = @knGauss; | ||
X=rand(2,100); | ||
X1=rand(2,10); | ||
X2=rand(2,5); | ||
|
||
maxdiff(knCenter(kn,X,X1),diag(knCenter(kn,X,X1,X1))') | ||
maxdiff(knCenter(kn,X),knCenter(kn,X,X,X)) | ||
%% Kernel regression with linear kernel is EQUIVALENT to linear regression | ||
clear; close all; | ||
n = 100; | ||
x = linspace(0,2*pi,n); % test data | ||
t = sin(x)+rand(1,n)/2; | ||
|
||
lambda = 1e-4; | ||
model_kn = knReg(x,t,lambda,@knLin); | ||
model_lin = linReg(x,t,lambda); | ||
|
||
idx = 1:2:n; | ||
xt = x(:,idx); | ||
tt = t(idx); | ||
|
||
[y_kn, sigma_kn,p_kn] = knRegPred(model_kn,xt,tt); | ||
[y_lin, sigma_lin,p_lin] = linRegPred(model_lin,xt,tt); | ||
|
||
maxdiff(y_kn,y_lin) | ||
maxdiff(sigma_kn,sigma_lin) | ||
maxdiff(p_kn,p_lin) | ||
%% Kernel kmeans with linear kernel is EQUIVALENT to kmeans | ||
clear; close all; | ||
d = 2; | ||
k = 3; | ||
n = 500; | ||
[X,y] = kmeansRnd(d,k,n); | ||
init = ceil(k*rand(1,n)); | ||
[y_kn,en_kn,model_kn] = knKmeans(X,init,@knLin); | ||
[y_lin,en_lin,model_lin] = kmeans(X,init); | ||
|
||
idx = 1:2:n; | ||
Xt = X(:,idx); | ||
|
||
[t_kn,ent_kn] = knKmeansPred(model_kn, Xt); | ||
[t_lin,ent_lin] = kmeansPred(model_lin, Xt); | ||
|
||
maxdiff(y_kn,y_lin) | ||
maxdiff(en_kn,en_lin) | ||
|
||
maxdiff(t_kn,t_lin) | ||
maxdiff(ent_kn,ent_lin) | ||
%% Kernel PCA with linear kernel is EQUIVALENT TO PCA | ||
clear; close all; | ||
d = 10; | ||
q = 2; | ||
n = 500; | ||
X = randn(d,n); | ||
|
||
|
||
model_kn = knPca(X,q,@knLin); | ||
idx = 1:2:n; | ||
Xt = X(:,idx); | ||
|
||
Y_kn = knPcaPred(model_kn,Xt); | ||
|
||
[U,L,mu,mse] = pca(X,q); | ||
Y_lin = U'*bsxfun(@minus,Xt,mu); % projection | ||
|
||
|
||
R = Y_lin/Y_kn; % the results are equivalent up to a rotation. | ||
maxdiff(R*R', eye(q)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
% demos for ch06 | ||
|
||
|
||
%% Kernel regression with gaussian kernel | ||
clear; close all; | ||
n = 100; | ||
x = linspace(0,2*pi,n); % test data | ||
t = sin(x)+rand(1,n)/2; | ||
model = knReg(x,t,1e-4,@knGauss); | ||
[y,s] = knRegPred(model,x); | ||
plotCurveBar(x,y,s); | ||
hold on; | ||
plot(x,t,'o'); | ||
hold off; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
%% RVM for classification | ||
clear; close all | ||
k = 2; | ||
d = 2; | ||
n = 1000; | ||
[X,t] = kmeansRnd(d,k,n); | ||
|
||
[model, llh] = rvmBinEm(X,t-1); | ||
plot(llh); | ||
y = rvmBinPred(model,X)+1; | ||
figure; | ||
binPlot(model,X,y); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
%% regression | ||
d = 100; | ||
beta = 1e-1; | ||
X = rand(1,d); | ||
w = randn; | ||
b = randn; | ||
t = w'*X+b+beta*randn(1,d); | ||
x = linspace(min(X),max(X),d); % test data | ||
|
||
%% RVM regression by EM | ||
[model,llh] = rvmRegEm(X,t); | ||
plot(llh); | ||
[y, sigma] = linRegPred(model,x,t); | ||
figure | ||
plotCurveBar(x,y,sigma); | ||
hold on; | ||
plot(X,t,'o'); | ||
hold off |
Oops, something went wrong.