d=dc;
end
end
end
formatshort;
>>X=[11;12;63;82;80];
>>AnalyC(X)
聚类前的N个类:
第1次聚类,第1类和第2类合并:
212000
130000
140000
150000
第2次聚类,第3类和第4类合并:
212000
130000
245000
第3次聚类,第2类和第3类合并:
212000
334500
第4次聚类,第1类和第2类合并:
512345
利用系统聚类法对以下5个变量分类:
>>X=[12;2.54.5;22;41.5;42.5];
figure
(1);
plot(X(:
1),X(:
2),'*');
gridon;axis([0505]);gname;
>>X=[12;2.54.5;22;41.5;42.5];
>>Y=pdist(X)
Y=
Columns1through9
2.91551.00003.04143.04142.54953.35412.50002.06162.0616
Column10
1.0000
>>DisM=squareform(Y)
DisM=
02.91551.00003.04143.0414
2.915502.54953.35412.5000
1.00002.549502.06162.0616
3.04143.35412.061601.0000
3.04142.50002.06161.00000
>>Z=linkage(Y)
Z=
4.00005.00001.0000
1.00003.00001.0000
6.00007.00002.0616
2.00008.00002.5000
>>figure
(2);dendrogram(Z);
>>T1=cluster(Z,2)
T1=
2
1
2
2
2
>>T2=cluster(Z,3)
T2=
2
3
2
1
1
>>T3=cluster(Z,5)
T3=
1
2
3
4
5
用cluster函数创建聚类:
>>x=[3.02,131.5,101.8,2.18;3.44,173.8,118.0,2.69;5.12,277.0,136.0,4.25;5.41,321.0,138.0,4.78;2.58,242.0,103.0,2.63;1.87,210.8,102.0,2.11];
>>T=clusterdata(x,0.5)'
T=
213344
>>xx=zscore(x);
>>Y=pdist(xx)
Y=
Columns1through9
1.25393.74914.49881.68221.41052.58513.36351.45811.6335
Columns10through15
0.82703.04973.71383.62974.34710.8206
>>S=squareform(Y)
S=
01.25393.74914.49881.68221.4105
1.253902.58513.36351.45811.6335
3.74912.585100.82703.04973.7138
4.49883.36350.827003.62974.3471
1.68221.45813.04973.629700.8206
1.41051.63353.71384.34710.82060
>>L=linkage(Y)
L=
5.00006.00000.8206
3.00004.00000.8270
1.00002.00001.2539
7.00009.00001.4105
8.000010.00002.5851
>>C=cophenet(L,Y)'
C=
0.9355
>>dendrogram(L)
ans=
151.0052
153.0052
154.0052
155.0052
156.0052
>>I=inconsistent(L)
I=
0.820601.00000
0.827001.00000
1.253901.00000
1.16170.30563.00000.8144
1.60760.89543.00001.0917
>>c=cluster(L,0.8)
c=
3
3
2
2
1
1
将一个四维数据分成不同的类:
>>seed=931316785;
>>rand('seed',seed);
>>randn('seed',seed);
>>loadkmeansdata;
>>size(X);
>>k1=3;
>>idx3=kmeans(X,k1,'distance','city');
>>figure
(1);
>>[silh3,h]=silhouette(X,idx3,'city');
>>k2=4;
>>idx4=kmeans(X,k2,'dist','city','display','iter');
iterphasenumsum
115602897.56
21532736.67
31502476.78
411021779.68
5151771.1
6201771.1
6iterations,totalsumofdistances=1771.1
>>figure
(2);
>>[silh4,h]=silhouette(X,idx4,'city');
>>k3=5;
>>idx5=kmeans(X,k3,'dist','city','replicates',5);
>>figure(3);
>>[silh5,h]=silhouette(X,idx5,'city');
利用模糊C均值聚类法将一组随机数据分为两类:
>>data=rand(100,2);
>>[center,U,obj_fcn]=fcm(data,2);
Iterationcount=1,obj.fcn=9.620339
Iterationcount=2,obj.fcn=7.789095
Iterationcount=3,obj.fcn=7.374178
Iterationcount=4,obj.fcn=7.068896
Iterationcount=5,obj.fcn=6.986195
Iterationcount=6,obj.fcn=6.974324
Iterationcount=7,obj.fcn=6.971965
Iterationcount=8,obj.fcn=6.970658
Iterationcount=9,obj.fcn=6.969600
Iterationcount=10,obj.fcn=6.968702
Iterationcount=11,obj.fcn=6.967937
Iterationcount=12,obj.fcn=6.967284
Iterationcount=13,obj.fcn=6.966729
Iterationcount=14,obj.fcn=6.966256
Iterationcount=15,obj.fcn=6.965854
Iterationcount=16,obj.fcn=6.965513
Iterationcount=17,obj.fcn=6.965223
Iterationcount=18,obj.fcn=6.964977
Iterationcount=19,obj.fcn=6.964769
Iterationcount=20,obj.fcn=6.964592
Iterationcount=21,obj.fcn=6.964442
Iterationcount=22,obj.fcn=6.964315
Iterationcount=23,obj.fcn=6.964208
Iterationcount=24,obj.fcn=6.964117
Iterationcount=25,obj.fcn=6.964040
Iterationcount=26,obj.fcn=6.963975
Iterationcount=27,obj.fcn=6.963920
Iterationcount=28,obj.fcn=6.963873
Iterationcount=29,obj.fcn=6.963833
Iterationcount=30,obj.fcn=6.963800
Iterationcount=31,obj.fcn=6.963772
Iterationcount=32,obj.fcn=6.963748
Iterationcount=33,obj.fcn=6.963728
Iterationcount=34,obj.fcn=6.963711
Iterationcount=35,obj.fcn=6.963696
Iterationcount=36,obj.fcn=6.963684
Iterationcount=37,obj.fcn=6.963674
Iterationcount=38,obj.fcn=6.963665
>>plot(data(:
1),data(:
2),'o');
>>maxU=max(U);
>>index1=find(U(1,:
)==maxU);
>>index2=find(U(2,:
)==maxU);
>>line(data(index1,1),data(index1,2),'linestyle','none','marker','*','color','g');
>>line(data(index2,1),data(index2,2),'linestyle','none','marker','*','color','r');
将空间180个点用减法聚类方法分类,找到聚类中心及影响范围:
>>x=rand(180,3);
>>[C,S]=subclust(x,[0.50.50.5],[],[1.50.50.150])
C=
0.34750.57320.4631
0.75680.16730.4289
0.52380.86950.8692
0.69980.27660.9208
0.06540.57160.1488
0.15330.05770.6653
0.86120.80330.3495
0.45980.95240.2319
S=
0.17580.17170.1750
>>plot(0,0);
>>holdon;
>>plot3(C(:
1),C(:
2),C(:
3),'r+','markersize',16,'LineWidth',3);
>>holdon;
>>plot3(x(:
1),x(:
2),x(:
3),'g*');
>>view(3)
距离判别分析:
>>X=mvnrnd([0;0],[1.9;.91],100);
>>Y=[1,1;1,-1;-1,1;-1,-1];
>>d1=mahal(Y,X)
d1=
0.9907
17.8023
17.6900
1.3360
>>d2=sum((Y-repmat(mean(X),4,1)).^2,2)
d2=
1.7118
2.0196
2.0028
2.3106
>>scatter(X(:
1),X(:
2))
>>holdon;
>>scatter(Y(:
1),Y(:
2),100,d1,'r','LineWidth',2)
>>hb=colorbar;
单变量校正:
>>x=[0246810]';
>>y=[0.0090.1580.30190.50120.56310.7892]';
>>[b,bint,r,rint,ststa]=regress(y,x,0.2)
Warning:
R-squareandtheFstatisticarenotwell-definedunlessXhasacolumnofones.
Type"helpregress"formoreinformation.
>Inregressat158
b=
0.0769
bint=
0.07380.0800
r=
0.0090
0.0041
-0.0059
0.0395
-0.0524
0.0198
rint=
-0.04190.0599
-0.04670.0549
-0.05510.0434
0.00280.0763
-0.0717-0.0332
-0.01520.0548
ststa=
1.0149NaNNaN0.0010
>>x=[121112;141513;181711;171820;182019;181719;191818;231921;242225;252524];
>>x=[xones(10,1)];
>>y=[223223292267292301310315328340]';
>>[b1,bint,r,rint,stats]=regress(y,x)
b1=
8.9194
0.9744
-0.7236
116.8513
bint=
0.448917.3899
-8.414110.3629
-6.18454.7372
49.0484184.6543
r=
-2.9189
-23.9318
5.9946
-4.5477
8.8604
20.7837
19.1663
-10.3147
-6.2628
-6.8292
rint=
-33.631727.7939
-51.53643.6729
-14.479826.4690
-41.117232.0218
-25.289843.0106
-13.306454.8739
-18.069256.4017
-38.725318.0959
-41.797329.2717
-38.126524.4681
stats=
0.883015.09300.0033284.6561
>>b=zeros(4,100);
>>kk=0.01:
0.01:
1;
>>c=0;
>>fork=kk
c=c+1;
b(:
c)=ridge(y,x,k);
end
>>b(:
95:
100)
ans=
24.961424.894524.828224.762524.697424.6330
9.66409.68429.70419.72379.74299.7617
1.65091.68441.71751.75041.78311.8154
264.0183263.7774263.5369263.2969263.0573262.8182
>>a0=[34.10,42.95,33.55,11.90,14.78,7.89]';
>>a1=[36.45,81.15,45.33,31.21,26.63,22.74]';
>>n=[11;1020;55;201;102;151];
>>n=n*0.3;
>>a2=a1-a0
a2=
2.3500
38.2000
11.7800
19.3100
11.8500
14.8500
>>k=(n'*n)^-1*n'*a2
k=
2.9744
4.8794
>>c=a0*k'*(k*k')^-1
Warning:
Matrixisclosetosingularorbadlyscaled.
Resultsmaybeinaccurate.RCOND=3.511073e-017.
c=
80
84
80
20
02
21
非线性校正:
functiony1=li9_19a(beta0,x)
a=beta0
(1);
b=beta0
(2);
c=beta0(3);
y1=a.*exp(-(b.*x+c.*x.^2));
>>x=[0.28,0.56,0.75,0.85,1.1,1.2,1.4,1.5,1.6,1.8,2.0];
>>y=[0.14,0.33,0.031,0.025,0.03,0.003,0.003,0.003,0.003,0.003,0.003];
>>beta0=[111];
>>[beta,r,j]=nlinfit(x,y,'li9_19a',beta0)
beta=
0.0004-29.411731.3765
r=
Columns1through9
-0.00010.0003-0.00480.02050.03000.00300.00300.00300.0030
Columns10through11
0.00300.0030
j=
322.2272-0.0392-0.0110
758.0616-0.1846-0.1034
82.2214-0.0268-0.0201
10.2819-0.0038-0.0032
0.0037-0.0000-0.0000
0.0001-0.0000-0.0000
0.0000-0.0000-0.0000
0.0000-0.0000-0.0000
0.0000-0.0000-0.0000
0.0000-0.0000-0.0000
0.0000-0.0000-0.0000
因子分析:
>>X=randn(100,10);
>>LPC=princomp(X);
>>[L1,T]=rotatefactors(LPC(:
1:
3))
L1=
-0.0900-0.21480.7725
-0.49340.01510.0063
-0.09160.2191-0.0538
0.0987-0.07620.3812
0.0324-0.61440.0152
0.2437-0.2054-0.2918