
(*產生500組隨機變數*) data=Block[{t},{#,3+#*0.2+RandomReal[NormalDistribution[0,1]]}&/@ Table[RandomReal[NormalDistribution[0,2]],{500}]]; Manipulate[ Block[{p1=point1,p2=point2,m,v1,myline,pp,pcadata, pcaslope}, pcaslope=Divide@@Reverse@Eigensystem[Covariance[data]][[2,1]]; (*直線斜率*) m=(p2[[2]]-p1[[2]])/(p2[[1]]-p1[[1]]); (*計算投影座標*) v1=point2-point1; pcadata=p1+Projection[#-point1,v1]&/@data; Column[{ "Variance="<>ToString[Variance[{p1-p2}.#&/@data][[1]]], Plot[{m(x-p1[[1]])+p1[[2]], pcaslope(x-p1[[1]])+p1[[2]]},{x,-20,20}, PlotStyle->{{Green,Thickness[0.01]},Automatic}, PlotRange->{{-10,10},{-7.5,12.5}}, Axes->False,Frame->True,AspectRatio->1, Epilog->{Blue,PointSize[0.0125],Point[data], Red,PointSize[0.025],Point[pcadata]},ImageSize->500]}]], {{point1,{-1,-5}},Locator},{{point2,{2,1}},Locator}]
讀者回應 ( 0 意見 )
訂閱發佈留言 (Atom)
發佈留言
Please leave your name and tell me what you thought about this site. Comments, suggestions and views are welcomed.
如果這篇文章對你有幫助,那請留個訊息給我~