Stats

Popular Posts

Followers

Mathematica 教學 Kernel Fisher discriminant analysis

戴忠淵 於 2011年12月9日星期五 上午12:04 發表




Kernel Fisher discriminant analysis

2-Dimension

data=SplitBy[Import["http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"],Last];

X=Flatten[data[[{1,3}]],1][[All,{1,2}]];
L=Length[X];
L1=50;L2=50;

(* Kernel function *)
RBFKernel[x_,y_,\[Gamma]_]:=Exp[-\[Gamma](x-y).(x-y)]

(* Kernel matrix *)
ttX=Table[RBFKernel[X[[i]],X[[j]],2],{i,100},{j,100}];

(* Sum square of Total *)
SST=Covariance[ttX]*(L-1);

(* Sum square of within-class *)
SSW=(L1-1)*Covariance[ttX[[1;;50]]]+(L2-1)*Covariance[ttX[[51;;100]]];

(* Sum square of between-class *)
SSB=SST-SSW;

(* Eigen system *)
e1=Eigensystem[Inverse[SSW+IdentityMatrix[L]*L].SSB];

(* Kernel Fisher Discriminant function *)

kfd1=e1[[2,1]].Table[RBFKernel[X[[i]],{x1,x2},2],{i,L}];
P1=kfd1/.{x1->X[[#,1]],x2->X[[#,2]]}&/@Range[50];
P2=kfd1/.{x1->X[[#,1]],x2->X[[#,2]]}&/@Range[51,100,1];
kfd1=kfd1-(Mean[P1]StandardDeviation[P2]+
Mean[P2]StandardDeviation[P1])/(StandardDeviation[P2]+StandardDeviation[P1]);

(* Plot *)
Show[
ContourPlot[kfd1==0,{x1,4,8},{x2,2,5}],
ListPlot[{X[[1;;50]],X[[51;;100]]},PlotStyle->PointSize[0.02]]
]



3-Dimension

X=Flatten[data[[{1,3}]],1][[All,{1,2,3}]];
ttX=Table[RBFKernel[X[[i]],X[[j]],2],{i,100},{j,100}];

SST=Covariance[ttX]*(L-1);
SSW=(L1-1)*Covariance[ttX[[1;;50]]]+(L2-1)*Covariance[ttX[[51;;100]]];
SSB=SST-SSW;

e1=Eigensystem[Inverse[SSW+IdentityMatrix[L]*L].SSB];

kfd1=e1[[2,1]].Table[RBFKernel[X[[i]],{x1,x2,x3},2],{i,L}];
P1=kfd1/.{x1->X[[#,1]],x2->X[[#,2]],x3->X[[#,3]]}&/@Range[50];
P2=kfd1/.{x1->X[[#,1]],x2->X[[#,2]],x3->X[[#,3]]}&/@Range[51,100,1];

kfd1=kfd1-(Mean[P1]StandardDeviation[P2]+
Mean[P2]StandardDeviation[P1])/(StandardDeviation[P2]+
StandardDeviation[P1]);

kfdplot=ContourPlot3D[kfd1==0,{x1,2,8},{x2,0,6},{x3,0,6},
Mesh->None,ContourStyle->Opacity[0.3]];

Show[kfdplot,
ListPointPlot3D[{X[[1;;50]],X[[51;;100]]},
PlotStyle->PointSize[0.02],
PlotRange->{{2,8},{0,6},{0,6}}]
]




It's So Interesting! Tags:

讀者回應 ( 0 意見 )

發佈留言

Please leave your name and tell me what you thought about this site. Comments, suggestions and views are welcomed.

如果這篇文章對你有幫助,那請留個訊息給我~