- 来自:https://mianbaoduo.com/o/bread/YZmakpZp
- 具体见:https://mianbaoduo.com/o/bread/YZmakpZp
- 部分代码如下:
% A=importdata('data.txt',' ',200);%????????A.data
%以下代码仅供参考 为多元线性回归方程及数据预处理的程序案例
a = load('data.txt');
x1=a(:,[2]) ;
x2=a(:,[3]) ;
x3=a(:,[4]) ;
y=a(:,[5]);
X=[ones(length(y),1), x1,x2,x3];
[b,bint,r,rint,stats]=regress(y,X);
b;bint;stats;
rcoplot(r,rint)
tx=[230.1,37.8,69.2];
b2=[b(2),b(3),b(4)];
ty=b(1)+b2*tx';
ty;
% 简单的得到一个变换的公式
% y=b(1)+b(2)*x1+b(3)*x2+b(3)*x3;
%下面的程序是在回归前对数据进行预处理
function [ w ] = ridgeRegression( x, y, lam )
xTx = x'*x;
[m,n] = size(xTx);
temp = xTx + eye(m,n)*lam;
if det(temp) == 0
disp('This matrix is singular, cannot do inverse');
end
w = temp^(-1)*x'*y;
end
%% ???(Ridge Regression)
clc;
%????
data = load('data.txt');
[m,n] = size(data);
dataX = data(:,2:4);%??
dataY = data(:,5);%??
%???
yMeans = mean(dataY);
for i = 1:m
yMat(i,:) = dataY(i,:)-yMeans;
end
xMeans = mean(dataX);
xVars = var(dataX);
for i = 1:m
xMat(i,:) = (dataX(i,:) - xMeans)./xVars;
end
% ??30?
testNum = 30;
weights = zeros(testNum, n-2);
for i = 1:testNum
w = ridgeRegression(xMat, yMat, exp(i-10));
weights(i,:) = w';
end
% ??????lam
hold on
axis([-9 20 -1.0 2.5]);
xlabel log(lam);
ylabel weights;
for i = 1:n-2
x = -9:20;
y(1,:) = weights(:,i)';
plot(x,y);
end
%plot出来的图像显示,k=5的时候,出现了拟合,因此取k=5时的w值,
% resualt output ,i=5
% w = ridgeRegression(xMat, yMat, exp(5-10));
%另一种程序
function [b,bint,r,rint,stats] = ridge1(Y,X,k)
[n,p] = size(X);
mx = mean (X);
my = mean (Y);
stdx = std(X);
stdy=std(Y);
idx = find(abs(stdx) < sqrt(eps));
MX = mx(ones(n,1),:);
STDX = stdx(ones(n,1),:);
Z = (X - MX) ./ STDX;Y=(Y-my)./stdy;
pseudo = sqrt(k*(n-1)) * eye(p);
Zplus = [Z;pseudo];
Yplus = [Y;zeros(p,1)];
[b,bint,r,rint,stats] = regress(Yplus,Zplus);
end
x=[71.35 22.90 3.76 1158.18 12.20 55.87;
67.92 34048 17.11 1494.38 19.82 56.60;
79.38 24.91 33.60 691.56 16.17 92.78;
87.97 10.18 0.73 923.04 12.15 24.66;
59.03 7.71 3.58 696.92 13.50 61.81;
55.23 22.94 1.34 1083.84 10.76 49.79;
58.30 12.78 5.25 1180.36 9.58 57.02;
67.43 9.59 2.92 797.72 16.82 38.29;
76.63 15.12 2.55 919.49 17.79 32.07];
y=[28.46;27.76;26.02;33.29;40.84;44.50;28.09;46.24; 45.21];
x'*x;
count=0;
kvec=0.1:0.1:1;
for k=0.1:0.1:1
count=count+1;
[b,bint,r,rint,stats]=ridge1(y,x,k);
bb(:,count)=b;
stats1(count,:)=stats;
end
bb',stats1
plot(kvec',bb),xlabel('k'),ylabel('b','FontName','Symbo l')
% 从运行结果及图1可见,k≥0.7时每个变量相应
% 的岭回归系数变化较为稳定,因而可选k=0.7,建立回归方程
% y=-0.219 5x1-0.120 2x2-0.237 8x3- 0.244 6x4+0.203 6x5-0.249 4x6
X=[83 131.5
83 111.5
83 91.5
83 61.5
1.5 51.5
83 51.5
83 41.5
164.5 41.5
1.5 31.5
83 31.5
83 21.5
164.5 21.5
1.5 11.5
83 11.5
1.5 1.5];%附件1道路节点
Y='ABCDEFGHIJKLMNO';%编号
figure
hold on
for i=1:size(X,1)
plot(X(i,1),X(i,2),'k*')
text(X(i,1)+1,X(i,2)+3,Y(i))
end
X1=[1.5 61.5
164.5 61.5
83 1.5
164.5 1.5];%补充道路节点
Y1='PQRS';
for j=1:size(X1)
plot(X1(j,1),X1(j,2),'k*')
text(X1(j,1)+1,X1(j,2)+3,Y1(j))
end
%补充仓库点
%划分等分
a1=linspace(1.5,83,6);%Ⅰ区划分
a=a1(2:5);
b1=linspace(83,164.5,6);%Ⅱ区划分
b=b1(2:5);
X2=[[a';b';a';b';a';b'],[131.5.*ones(8,1);111.5.*ones(8,1);91.5.*ones(8,1)]];
for i=1:size(X2,1)
plot(X2(i,1),X2(i,2),'r*')
text(X2(i,1)-2,X2(i,2)+4,num2str(i))
end
%Ⅰ区补充工位点
X3=[[a';a';a';a';a';a'],[61.5.*ones(4,1);51.5.*ones(4,1);41.5.*ones(4,1);31.5.*ones(4,1);21.5.*ones(4,1);11.5.*ones(4,1)]];
%Ⅱ区补充工位点
X4=[[b';b';b';b';b';b'],[61.5.*ones(4,1);51.5.*ones(4,1);41.5.*ones(4,1);31.5.*ones(4,1);21.5.*ones(4,1);11.5.*ones(4,1)]];
for i=1:size(X3)
plot(X3(i,1),X3(i,2),'b*')
text(X3(i,1)-2,X3(i,2)+4,num2str(i))
plot(X4(i,1),X4(i,2),'b*')
text(X4(i,1)-2,X4(i,2)+4,num2str(i))
end
XX=[X;X1;X2;X3;X4];%依次将附件1道路节点、补充道路节点、补充仓库点、Ⅰ区补充工位点、Ⅱ区补充工位点组合
%构建有向矩阵,确定连接点
Z=fun(XX,a1,b1);
%绘制有向路径
for i=1:size(XX,1)
for j=1:i
if Z(i,j)==1
plot(XX([i,j],1),XX([i,j],2))
end
end
end
%路段距离矩阵,点编号依次为附件1道路节点、补充道路节点、补充仓库点、Ⅰ区补充工位点、Ⅱ区补充工位点
%求出距离矩阵,备用求路径长度
D=zeros(size(XX,1),size(XX,1));
for i=1:size(XX,1)
for j=1:size(XX,1)
if Z(i,j)==1
D(i,j)=sqrt(sum((XX(i,:)-XX(j,:)).^2));
end
end
end
disp('有向距离矩阵见工作区D矩阵')
```