根据《机器学习实战》中的phthon代码改编而来,详细说明参见原书,测试数据都是用的原书数据
主程序:lwlr_test.m
clc;
clear;
%高斯核波长
k=0.01;
%读取数据,
fid=fopen('ex0.txt','r');
dataset=zeros(1,3);
m=size(dataset,1);
while(~feof(fid))
lineinfo=fgetl(fid);
%将字符转换为数字组成的数组
c=str2num(lineinfo);
dataset(m,:)=c;
m=m+1;
end
y_p=zeros(1,m);
x=dataset(:,1:2);
y=dataset(:,3);
x_test=x;
m=size(x_test,1);
D=ones(m,1)/m;
for i=1:m
y_p(i)=lwlr(x_test(i,:),x,y,k);
end
y_p(length(y_p))=[];
xx=cat(2,x_test(:,2),y);
xx=sortrows(xx);
xxp=cat(2,x_test(:,2),y_p');
xxp=sortrows(xxp);
scatter(xx(:,1),xx(:,2),'.');
%plot(xx(:,1),xx(:,2));
hold on
plot(xxp(:,1),xxp(:,2),'r')
函数文件:lwlr.m
function result=lwlr(point,x,y,k)
%y转置
yT=y';
%获得x的行
m=size(x,1);
weights=eye(m);
for i=1:m
diff=point-x(i,:);
weights(i,i)=exp(diff*diff'/(-2.0*k^2));
end
xx=x'*(weights*x);
if det(xx)==0
display ('this matrix is singular, cannot do inverse');
return;
end
ws=xx^-1*(x'*(weights*y));
result=point*ws;
end