LS - grupiranje u dva klastera

Podaci

SeedRandom[13]
c1=2;sig1=.5;m1=10;c2=5;sig2=.7;m2=10;
pi1=Round[Sort[RandomReal[NormalDistribution[c1,sig1],m1]],.01]
pi2=Round[Sort[RandomReal[NormalDistribution[c2,sig2],m2]],.01]
sl1=ListPlot[Table[{pi1[[i]],0},{i,Length[pi1]}],PlotStyle->{Blue,PointSize[.025]},AxesOrigin->{0,0}];
sl2=ListPlot[Table[{pi2[[i]],0},{i,Length[pi2]}],PlotStyle->{Red,PointSize[.025]},AxesOrigin->{0,0}];
sl=Show[sl1,sl2];
GraphicsGrid[{{sl1,sl2,sl}},ImageSize->700]

Particije

k1=Union[pi1,{3.4,3.7}];
k2={4.16,4.25,5.15,5.24,5.27,5.34,5.38,5.56};
(*k1=pi1; k2=pi2;*)
sl1=ListPlot[Table[{k1[[i]],0},{i,Length[k1]}],PlotStyle->{Blue,PointSize[.025]},AxesOrigin->{0,0}];
sl2=ListPlot[Table[{k2[[i]],0},{i,Length[k2]}],PlotStyle->{Red,PointSize[.025]},AxesOrigin->{0,0}];
sl=Show[sl1,sl2];
GraphicsGrid[{{sl1,sl2,sl}},ImageSize->1000]
(* Centri i vrijednosti funkcija F i G *)
c1=Total[k1]/Length[k1]; c2=Total[k2]/Length[k2]; A=Union[k1,k2];c=Total[A]/Length[A];
slc=ListPlot[{{c1,0},{c2,0},{c,0}},PlotStyle->{Green,PointSize[.04],Opacity[.5]}];
Show[sl,slc,ImageSize->400]
F=Total[(k1-c1)^2]+Total[(k2-c2)^2];G=Length[k1](c-c1)^2+Length[k2](c-c2)^2;
Print["F = ",F,";  G = ",G]

LAD - grupiranje u dva klastera

Podaci

SeedRandom[3]
a1=0;b1=10;m1=10;a2=7;b2=15;m2=10;
pi1=Sort[RandomInteger[{a1,b1},m1]]
pi2=Sort[RandomInteger[{a2,b2},m2]]
sl1=ListPlot[Table[{pi1[[i]],0},{i,Length[pi1]}],PlotStyle->{Blue,PointSize[.025]},AxesOrigin->{0,0}];
sl2=ListPlot[Table[{pi2[[i]],0},{i,Length[pi2]}],PlotStyle->{Red,PointSize[.025]},AxesOrigin->{0,0}];
sl=Show[sl1,sl2];
GraphicsGrid[{{sl1,sl2,sl}},ImageSize->700]

Particije

k1={0,1,2};
k2=Union[{7,8,8,9,9,10,10},pi2];
(*k1=pi1; k2=pi2;*)
sl1=ListPlot[Table[{k1[[i]],0},{i,Length[k1]}],PlotStyle->{Blue,PointSize[.025]},AxesOrigin->{0,0}];
sl2=ListPlot[Table[{k2[[i]],0},{i,Length[k2]}],PlotStyle->{Red,PointSize[.025]},AxesOrigin->{0,0}];
sl=Show[sl1,sl2];
GraphicsGrid[{{sl1,sl2,sl}},ImageSize->1000]
(* Centri i vrijednosti funkcija F i G *)
c1=Median[k1]; c2=Median[k2]; A=Union[k1,k2];c=Median[A];
slc=ListPlot[{{c1,0},{c2,0},{c,0}},PlotStyle->{Green,PointSize[.04],Opacity[.5]}];
Show[sl,slc,ImageSize->400]
F=Total[Abs[k1-c1]]+Total[Abs[k2-c2]];
Print["F = ",F]