MATLAB中“fitgmdist”的用法及其GMM聚類演算法 作者:凱魯嘎吉 - 博客園 http://www.cnblogs.com/kailugaji/ 高斯混合模型的基本原理:聚類——GMM,MATLAB官方文檔中有關於fitgmdist的介紹:fitgmdist。我之前寫過有關GMM聚類的算 ...
MATLAB中“fitgmdist”的用法及其GMM聚類演算法
作者:凱魯嘎吉 - 博客園 http://www.cnblogs.com/kailugaji/
高斯混合模型的基本原理:聚類——GMM,MATLAB官方文檔中有關於fitgmdist的介紹:fitgmdist。我之前寫過有關GMM聚類的演算法:GMM演算法的matlab程式。這篇文章主要應用MATLAB自帶的函數來進行聚類。
1. fitgmdist函數介紹
fitgmdist的使用形式:gmm = fitgmdist(X,k,Name,Value)
輸入
‘RegularizationValue’, 0。(取值:0, 0.1, 0.01,....,正則化繫數,防止協方差奇異)
'CovarianceType', 'full'。(取值: 'full',協方差矩陣是非對角陣,'diagonal',協方差矩陣為對角陣)
‘Start’, 'plus'。 (取值:‘randSample’,隨機初始化,‘plus’,k-means++初始化,‘S’,自定義初始化),其中S = struct('mu',init_Mu,'Sigma',init_Sigma,'ComponentProportion',init_Components);
‘Options’,statset('Display', 'final', 'MaxIter', MaxIter, 'TolFun', TolFun)。 ('Display'有三個取值:‘final’ 顯示最終的輸出結果、‘iter’ 顯示每次迭代的結果、‘off’ 不顯示優化參數信息;'MaxIter':預設100,最大迭代次數;'TolFun':預設1e-6,目標函數的終止誤差)
輸出
gmm.mu:更新完後的聚類中心(均值)
gmm.Sigma:更新完後的協方差矩陣
gmm.ComponentProportion:更新完後的混合比例
gmm.NegativeLogLikelihood:更新完後的負對數似然函數
gmm.NumIterations:實際迭代次數
gmm.BIC:貝葉斯信息準則,用於模型選擇
更多參數,請在命令行輸入properties(gmm)
2. 高斯混合模型聚類實例
generate.m
function data=generate() %生成數據 mu1 = [1 2]; Sigma1 = [2 0; 0 0.5]; mu2 = [-1 -2]; Sigma2 = [1 0;0 1]; data = [mvnrnd(mu1,Sigma1,400), ones(400,1);mvnrnd(mu2,Sigma2,600), 2*ones(600,1)]; X=[data(:, 1), data(:, 2)]; figure(1) plot(X(:,1), X(:,2),'bo') title('Scatter Plot') xlim([min(X(:)) max(X(:))]) % Make axes have the same scale ylim([min(X(:)) max(X(:))])
具體數據
-0.752713846442762 2.48140797998545 1 -0.798625575507672 2.14835001132099 1 2.82002920206994 1.97084621196340 1 0.913856576539988 2.24942313999122 1 1.57243525115195 2.68322568351427 1 0.241170005783610 1.89791938743627 1 2.10634746115858 2.20631449410867 1 1.61173455443266 2.69163655587553 1 1.39436249281445 1.28104472307183 1 1.65267727628557 1.85771163664832 1 0.0927741368750946 2.00698799954306 1 2.79887910552062 1.70868183551872 1 0.652907219449091 1.88702134773695 1 3.43327629572431 2.17571612839302 1 1.96527202098605 2.34069549818768 1 0.867813825335363 1.68534433959204 1 2.08252259376894 1.02114736190308 1 0.613863159235077 2.17081564512242 1 0.399552396654452 2.11342763957560 1 2.91186118166440 1.82639334702902 1 0.377524852838774 3.08573945644000 1 1.98881806018168 1.98460069084178 1 2.68223790603780 1.96134300697451 1 -0.622058850926772 3.30007822155342 1 0.275231442237888 1.59279139683080 1 3.34643957013717 1.68055674830698 1 1.03601109129843 2.70541249651070 1 0.752332013138056 2.49601620599903 1 0.508824726005269 2.57436514443626 1 1.73027562632102 2.19502286037081 1 -0.713230300478002 3.73262960861479 1 0.00814731258125789 1.52233440802986 1 4.86084534117603 2.64339300870922 1 2.82851799598685 1.79094004967764 1 0.725784406602703 1.32609248276320 1 0.147485590657029 1.49033710171378 1 0.601129894478657 3.58795933263211 1 1.32767044246750 1.73683867424831 1 1.27723837775742 2.62061783710240 1 0.240726647338080 1.43127112737987 1 0.387110343064517 2.39576239976627 1 -0.735171242460893 2.21884994425795 1 0.821208540380224 2.28230651108960 1 2.84910757299470 2.40958045875721 1 -0.00305938218238255 1.36758622169689 1 0.664256129846708 1.22550082896155 1 -0.178114240634728 2.30408704591301 1 2.00786623874121 2.03129722636611 1 1.20272622506596 2.22475373629028 1 2.50250207292945 2.98770457384530 1 -0.874927698714206 1.71654280942273 1 2.39750117925196 1.84040401876970 1 0.369966892118764 2.74170598529279 1 2.15279953669453 1.58546917422906 1 0.591444584760838 2.29854661364821 1 2.57828408797099 2.37460697014727 1 0.816618335677021 1.11574196519408 1 1.86043373148875 2.20777177264008 1 0.100606202686330 3.21045350847405 1 2.53445397100630 0.854302259627375 1 1.39168420124415 1.49609940831950 1 1.93929865340817 1.15181790512326 1 -0.727792104505467 1.79285231965211 1 0.646252982052485 1.03986431564848 1 1.19673169055003 1.93448155511090 1 1.12984878643876 2.22883744483371 1 4.02666426647567 1.86649614448613 1 1.27117265547696 2.03774514826000 1 1.00533765244964 1.17061188123361 1 -0.414741919400961 2.28828102447677 1 0.495317556413179 1.83288455064957 1 2.76823223857300 2.97934893222715 1 1.08058855707478 2.26487370266768 1 1.38546325331279 1.40278788558597 1 1.52167644900484 2.69183023331115 1 1.67505702182493 2.85222287490608 1 4.10199034200796 2.18231282334728 1 2.40054022033848 2.07878116194682 1 1.49836399169312 1.92754270979332 1 1.52768214037169 0.604144369658773 1 1.48106369736206 1.89716183115311 1 1.88582351601666 2.33794971444330 1 2.26705524676369 1.71381451846972 1 1.60866449353269 1.49135394247519 1 -0.115055715038331 1.12849347288791 1 3.57622395160265 1.10664344270162 1 2.26630255633806 1.22083515972890 1 1.33163191466841 1.44619768873473 1 -1.45842213709207 2.22949005602252 1 1.70411565663116 3.47971193248190 1 1.48213175474953 2.96603725956335 1 1.72101076041395 3.01672973472638 1 -1.35146166439553 2.85558221164039 1 0.380649241141038 2.56729542762187 1 1.88667156332277 2.85630884112937 1 1.97758943297860 1.70558017430484 1 3.24821546740318 1.82804963183289 1 0.843823793198429 2.64758378735128 1 0.0405183480549609 1.71670699774521 1 0.976813370248264 2.49595625636074 1 -0.962694637418230 2.58074576675867 1 1.82963248497625 1.90436660429995 1 -0.0311001170696965 2.55990131230256 1 1.78671558216327 1.30722487774421 1 -0.0804210798437657 1.50783793009072 1 0.592128283430208 0.755659320204709 1 1.95365332837982 2.16200348956491 1 1.36682968081700 1.73744055959892 1 0.979475411390431 2.42632494823554 1 1.39902046293800 1.73826197272758 1 -0.765939069959433 1.80573420327555 1 -0.635816070635523 2.54485465981086 1 0.430871671985252 2.95078063841104 1 1.07190555054187 2.60824897906429 1 0.190820867383636 1.56628799263572 1 -0.877375755503489 2.32098851030493 1 2.18596583418406 1.73505362502330 1 -0.858977422721553 1.67583237334513 1 -0.209010836036135 1.61369208932597 1 0.320019483215927 1.82234886961005 1 2.22606598804851 2.92553777460368 1 -1.34847806650935 1.73833614701202 1 -1.10055386975634 1.73836801433344 1 -0.742975839183661 2.56906224418957 1 0.417690253375689 2.64425612559673 1 2.21840601718359 1.86213274353155 1 -1.80189093986379 1.88663190741782 1 2.18214937211589 2.81206394431873 1 0.0239657716321194 2.37810261200744 1 1.48366333882258 1.88062228076692 1 -1.50485783728876 1.75467615653968 1 -0.156597929943039 2.92699154393160 1 1.00641966523359 1.82300801832677 1 0.229462398563604 0.218216163076399 1 3.08788315084484 2.44977421952063 1 -2.75317240661940 1.47607778387816 1 1.57689796637766 1.14148728120332 1 0.289637818858192 1.37410983329226 1 1.80546824398122 1.11123227831127 1 2.05342064550405 2.59331992761210 1 -0.908793900499392 1.78719275353105 1 0.366987870101896 0.201547399427021 1 0.449481220306850 3.37425663694842 1 1.77118120808653 1.75664778049838 1 -0.418491981240371 1.67286506452260 1 3.52677560506185 2.36946900535883 1 1.02163690286654 2.47962254560261 1 1.41689244102377 2.38217492483121 1 1.13691599064491 0.957013586442155 1 0.671020944057007 2.56279320248092 1 0.416289186862122 1.39483608724636 1 -0.0282160104049050 2.45786458707630 1 0.0521611900408511 2.82924447860124 1 1.24447082399573 3.12144145109146 1 2.55665751225502 2.59582682074224 1 -1.72738716416235 2.87567939582205 1 0.818034818369206 2.08879327658097 1 0.665710432632597 3.02252628314314 1 -0.830517107648893 1.19084685791838 1 -0.738848145854706 1.25759468066556 1 0.884558977595650 2.40820510111672 1 0.588696730035120 0.647436434761032 1 0.458454700841834 2.31765414523682 1 1.64997736187883 3.05662030735942 1 1.39057520552050 1.64617201786334 1 0.694081545245352 2.15199590331699 1 0.0806501948918665 1.80117182589078 1 -0.184406277418025 1.67502632215727 1 0.775990550072731 2.43159523244796 1 0.442197809595544 2.42563275519744 1 0.408665021617116 1.66059134779610 1 0.587376671955379 1.86069409237464 1 -0.342114019627880 1.33691809907463 1 -0.142841819015369 1.71852333719017 1 -0.260651941264746 1.99425566102537 1 1.09807519449644 2.47217799103807 1 2.62993953926983 0.646836252256355 1 3.36278068810442 3.41074385787854 1 1.80913370829291 3.17557239985633 1 -1.59335469603891 2.64454888986985 1 -0.735293470649240 1.69761457507026 1 1.46149445409731 2.51621553558738 1 0.0529118189982672 2.66819865723460 1 1.98005993371040 2.93220202949268 1 1.33800754576975 1.75196414069653 1 2.15416642093573 1.87958480581846 1 1.28247803384193 3.41980786237121 1 2.92917443686860 2.35403149513118 1 -0.470738428706770 0.823541040173880 1 2.78697845539703 2.47701324514116 1 0.518320239654389 2.40424725623016 1 3.02250396484622 2.55097729604176 1 -0.839762255830834 2.00164329123226 1 1.97089798972180 2.55221447381585 1 -0.289887426353096 2.07610884850028 1 -0.718531739571935 2.78476179702835 1 0.159548738233182 2.22484818846287 1 0.0714188829572822 2.17574623522435 1 0.464260706095287 2.07341615166054 1 2.02556814345805 1.45062673448325 1 0.737020824815466 3.63975837407915 1 2.14750623700563 2.23282234495443 1 2.53296991781638 2.22406578277892 1 0.240179086504137 1.76265939266772 1 2.36877906808012 1.99152631129811 1 -0.893624073060507 2.42679500507945 1 -0.723268011951694 2.32859127688179 1 -2.59545056360228 1.52181861224035 1 0.657633251364557 2.01724805594542 1 2.85299832181112 0.880567071545951 1 1.95540343663813 1.24583714921799 1 0.960390843211572 1.77723068955129 1 0.407588468887096 2.22032850805760 1 2.04391159921017 2.62263432946166 1 0.139422860698296 3.46606702350245 1 0.183723949354679 1.53015817186665 1 3.39797551359993 0.230231575623043 1 -0.497286845513600 3.06777504068115 1 2.70349699479679 2.60386143158266 1 0.185856815839956 2.77421368229388 1 -1.25352566567544 2.61455356095118 1 2.97698189327392 2.79489518362215 1 2.96340883068021 1.70435402189933 1 -2.93795294934390 1.69031063599255 1 3.20630707170919 1.90544165791216 1 -0.781101952149974 1.34505757509867 1 -0.110254964395879 1.88887924037098 1 -0.148493182169401 2.78931982306986 1 1.57894475487651 1.95012113995437 1 0.358692212589937 2.45874960327494 1 -0.552629537041592 1.95247795012209 1 1.32766937201009 1.23501275935944 1 1.34151713650691 2.68772457510701 1 -0.268674132203379 1.66058617107181 1 1.25359742611476 1.23987634100201 1 2.46812252319339 1.25605626114578 1 3.09198334328765 2.36801154533005 1 -0.566282976514046 1.25079006691005 1 1.85783758415852 2.81172759538976 1 0.875253856836606 1.99065650370977 1 0.535551500053441 1.93194332848913 1 2.07892805534457 2.03389849312396 1 1.10683822574576 2.60514294021722 1 -0.0297454662589745 1.33412963408874 1 -0.274875295549741 1.98869630893035 1 -0.611926311082764 2.95137346161668 1 -0.215504880369949 2.27174327492410 1 0.403380418836380 2.04217817886391 1 3.55063319312173 2.10491549317915 1 0.795219956773059 1.21562325860728 1 0.789938421848367 1.90695351738886 1 1.29465079260286 2.47202780980209 1 -1.12683343851593 3.06350234612551 1 -0.0799676654748724 1.69008029097531 1 0.487673784247081 1.50046684588507 1 3.91777486308584 1.52272565018330 1 -1.15152954154729 1.99965584856603 1 2.47432687749385 2.74948515633273 1 1.00678950632924 2.25465914519267 1 -0.227915979053637 2.45191696972133 1 -0.0543477750084895 1.12278291234785 1 1.51878847200521 1.85528303596989 1 -1.91498109886475 2.76529330031460 1 -1.78417806800794 1.60752691808620 1 -0.578763215473953 2.10444755619117 1 -2.39019616041897 1.28932825131823 1 -2.30764835245406 1.08991109739254 1 1.89064200659468 2.00785978957407 1 2.03976346376942 1.82002963509455 1 1.24263738709720 3.19095386811925 1 0.0285425144832171 2.31933541717718 1 -0.625828512267571 1.82791530314014 1 -1.29694211271572 1.59645498766398 1 0.0985414341762946 2.13809274804248 1 1.34787113097625 2.07350197353985 1 0.175544605294486 2.90718939546573 1 2.41317338702067 3.04616386009345 1 0.875591407386426 2.24008553295827 1 0.344523616420434 2.77887075834446 1 0.350088483934827 1.72226276593527 1 1.42188219517947 2.57859804737845 1 3.21381719211517 1.86799247354647 1 1.74589088462987 1.00573740899835 1 2.11557922702034 1.58199599894400 1 -2.04013802060973 2.60232796518928 1 3.20205165328965 2.60390719877483 1 1.56234192734599 2.30558982629769 1 2.08517734695935 1.31728822189134 1 -1.48620907555365 2.55332048785093 1 2.61148249074725 1.71621279484903 1 -0.905021070862110 2.12951279748967 1 3.16479611450917 1.41887494399374 1 0.154029113586113 2.28452711486875 1 1.91264842854147 2.50351640809194 1 -0.582913589306954 2.19705796223473 1 1.31322945294724 2.22036323126808 1 -0.228788488980173 3.05928224414262 1 -0.857386001496367 1.96205173656563 1 2.76983398450783 1.24742630849804 1 2.44015841669802 2.31394357034122 1 0.408432233213481 3.09948140659388 1 -0.114410818902137 2.88941697026244 1 0.407100252949123 3.41779243528360 1 0.712740414645104 1.70908969851738 1 0.593558606206566 1.35437115259034 1 -1.96654861654142 2.28811237465077 1 -0.0767496998679684 2.44666392158934 1 1.26057334891416 0.945631825339162 1 -0.351949202522450 2.25900916459579 1 -1.17945381425960 2.46633417748718 1 -0.467568620732904 2.40136460898594 1 1.23862083846545 2.22083791435337 1 0.420694076530883 1.86990301998729 1 -0.531859248789545 2.62637095213525 1 3.32802355934814 1.67779513521650 1 2.90746315820912 2.33166989338252 1 1.43223079754213 1.85780610453333 1 0.360886580813031 1.84795416252802 1 1.41836527275590 1.72693535248849 1 2.02833084383351 1.35904677193309 1 0.435180523147467 2.64715310736853 1 -0.698375319323621 1.43125428012617 1 1.19444497158568 1.57143006912886 1 3.45531952747569 0.961783186415359 1 0.298242242365380 1.46729618422928 1 -0.0391444929009328 1.63913735682960 1 -0.209124345388064 2.28569943039595 1 3.81252464400128 1.73130747390776 1 0.963772214614553 3.09667161402995 1 -0.904209679864521 2.43725922132225 1 0.548280045329855 1.02311177792852 1 2.98209960235769 2.87625017644987 1 2.46854911783815 1.09486636437231 1 0.967808350894169 2.26454239039685 1 2.31417169816689 2.26148586739507 1 1.52409461748456 2.65894850182436 1 -0.315502917866959 1.69566799280389 1 2.26672362611252 1.27184464297027 1 -1.02427251575416 1.59980957487435 1 2.85199714217155 1.93519657655905 1 1.86070097424808 1.51099712259492 1 1.81722939713941 1.87322152483130 1 -0.257315712745652 0.444085419761545 1 0.673818612834761 2.05544261993118 1 1.63867543397301 1.37007407166142 1 4.29548224168348 2.04087657428792 1 0.395997195725214 1.14587814316122 1 -0.367617690505528 1.80069451938889 1 2.71404805200950 2.33988704319061 1 -0.689040843335554 2.54395006354923 1 -1.60655257005459 1.11166719050019 1 2.15504096115465 1.49167698498253 1 0.818526998342130 2.29383839350294 1 0.823731392013672 1.70256188550563 1 1.98150639006393 1.91507074927960 1 -0.332870153654826 2.94474647908009 1 -2.85833589458332 1.66402610482239 1 2.84326036617204 1.72950443906094 1 1.37672155753240 2.82789021943827 1 1.97549066434863 1.15885128121590 1 2.63347366613122 1.08592402808597 1 1.13822825513100 2.02774948477578 1 0.670863129792378 3.02422037386870 1 -1.00491217945087 0.919457143051295 1 0.277243072529953 1.88491536694788 1 -0.435001760058096 2.72174438371371 1 -1.58089987878169 3.45903816033268 1 1.52973221864635 1.57105506398650 1 3.60218775876145 1.88011523273458 1 2.62158432487766 2.77855139329193 1 -1.75781025020838 1.84824410871244 1 3.37803057744834 2.21599028955715 1 -0.346098830363456 1.55398808498706 1 1.90236813756289 1.72879622465182 1 -0.105666124234816 1.83594061103424 1 2.34262323115159 2.05305174562525 1 1.09876921234816 1.81150277122060 1 0.966608028921029 2.33691241755861 1 0.823689860953087 1.94945325230174 1 0.419170804093286 1.86820804250038 1 0.149070681064704 1.65062764797219 1 0.615428281436471 3.16201035523892 1 1.71276028052706 0.826225385875133 1 -1.07854614441831 1.97061292997584 1 0.248665823531865 2.47021549757460 1 1.54544798803974 2.17303116878530 1 1.00188625822723 1.16349907676246 1 3.50697419187694 1.82850000343925 1 0.161203634132291 1.30647186284874 1 0.141564288672490 3.25713955619499 1 3.44485047085627 3.24647803770789 1 2.61135348875181 1.92727121676788 1 2.05487350292847 2.24731512195663 1 -1.30073459750214 1.58397980200452 1 0.551074578398585 2.34210567387313 1 1.68849008785765 1.53890381649265 1 2.26227822633084 2.97148694155939 1 0.455954545703309 1.78844599129861 1 1.37854458676860 3.22293763885626 1 1.71925044754000 3.26979910507886 1 -0.310387636175454 -2.23153168741952 2 -0.934089733648743 -1.54519135326412 2 -0.467063341039314 -2.69431693577286 2 -1.52725849938013 -4.06808090325163 2 -2.84861373495908 -0.968569243913297 2 -1.03315917202661 -0.836392717658259 2 -1.52945081330688 0.177925991170179 2 0.272277463024252 -1.73027958748459 2 -0.951527999228587 -1.84927294354937 2 -0.0243740341490367 -2.28862762294620 2 -0.615863160507723 -0.991725917827129 2 -0.940935570384387 -1.24732773069878 2 -3.86843684810980 -2.31417047925927 2 -2.57541255856745 -2.04725363847414 2 -0.634871202834112 -3.22533407947374 2 0.421500629873144 -2.89813841952592 2 -0.456791210518503 -1.46061556026566 2 -0.221449790774511 -3.65527623122871 2 -1.66584128372486 -1.97740661983487 2 -0.889659909110650 -0.129782237998816 2 -0.957191125028829 -1.13609174513829 2 -0.612655776722733 -1.62958614937591 2 -0.993471440517814 -1.43130323448552 2 -1.46738402646469 -1.66455407264236 2 -3.33940764839619 -2.28325731527929 2 -3.92625631928848 -3.65295906028593 2 -1.01431911662476 -2.20942288610598 2 -2.23266610481183 -0.733094002612328 2 0.614170279014691 -1.84404514209825 2 -1.59853740972766 -3.37335378250831 2 -1.18875486539556 -2.95592912966511 2 -1.90474004454223 -2.93986362364345 2 -0.461792254828884 0.659204872232193 2 -1.24652581552721 -2.51377131403041 2 -1.55263847786598 -0.659335919834218 2 -0.204965715890677 -3.07835427050231 2 0.505949048569452 -1.69539069152493 2 -1.25929071458034 -3.30167254366351 2 -1.45329084238931 -1.28119364062025 2 -1.59378208153597 -2.69967960555165 2 0.657876927467526 -2.75122951337310 2 -2.05969164865287 -0.777038119979204 2 -0.687303404972082 -2.39144914494798 2 -1.75617449175296 -3.20316354070852 2 -1.44428863272754 -1.88898910849531 2 0.516802020734900 -2.77289110491229 2 -3.21076628384575 -0.214652950150557 2 0.235656867720272 -2.24051440380587 2 -1.14911236694953 -2.07853558048726 2 -2.58236300436783 -3.39749417176423 2 -0.263713847148100 0.458512609215667 2 0.831682851530392 -2.08393284066220 2 -0.885375810711882 -2.82261961561946 2 -1.05237777281838 -4.41903132931144 2 -1.82602079657629 -2.81518110811275 2 -1.47450637332984 -1.11591227065293 2 -0.837695490924353 -1.99112891002352 2 0.503772962240550 -0.982491942689058 2 -1.27421476316112 -3.76916087911187 2 0.0622801415964360 -1.35996836839431 2 0.243234800019898 -2.91886304005307 2 -0.114965229811251 -1.84434516078637 2 -1.03478091646385 -0.472485719700510 2 0.222020356133710 -1.32086267515154 2 -3.04755746928280 -1.73397536103346 2 -1.41249857342550 -1.22307108058557 2 -1.73156502345195 -1.77569518325704 2 -0.742094417801203 -0.700017850171123 2 -1.24491275262119 -2.52862116083992 2 -1.80859407972665 -3.44903472811192 2 1.65126401905300 -1.88453932566484 2 -0.363787385977324 -2.96903265676122 2 -1.68394161548948 -2.51953284260788 2 0.231915878657905 -2.17557092867788 2 -0.623009572802111 -1.54073779499851 2 -1.18046364650384 -0.838619594274581 2 -1.97957122074801 -0.874507864298584 2 -2.08180656814877 -0.707787688204998 2 -2.29213171892552 -0.351559670403015 2 -0.287271532862648 -3.34342590146407 2 -1.91044099841237 -1.42413336314475 2 -1.32168223315596 -1.70986063364701 2 -0.0602885106603939 -4.27016064338500 2 -0.150160194208001 -0.791494432320046 2 -1.19917191161806 -2.68895112784025 2 -0.615647750316442 -2.34747907872228 2 1.14439584050046 -2.59281654733375 2 -0.610850531908339 -2.84891536112328 2 0.130871816112224 -2.20959543033864 2 -2.43512082757038 0.411329054129355 2 -0.554467809907876 -2.44039222410484 2 -0.0191711349105359 -1.89573198165047 2 -2.01188587294146 -1.26178284949416 2 -2.41523133203689 -1.91032605141725 2 -2.60714775073888 -2.39316877117065 2 -2.09238458059558 -2.04217127571499 2 0.445436252824795 -1.43181854274938 2 0.344520342317261 -0.795862492993640 2 -1.13247721986157 -1.37054401762598 2 -2.29160274499548 -0.223802358668605 2 -1.54753140183007 -1.76727782026563 2 -2.72853717504117 -2.93461716320278 2 -2.61691348239220 -1.65147172167033 2 -0.154841489547546 -1.45908135177144 2 -1.15080800841007 -2.11165026236726 2 -0.933229717848754 -1.57949446806009 2 -1.91825033242900 -0.936094286983232 2 -0.381822225829966 -3.52442301860361 2 -0.309208403056970 -2.23032013886121 2 -1.28292528979165 -1.86749695197344 2 -1.16965402984452 -1.37272157613288 2 -1.30226975258339 -3.23323074576827 2 -0.943098457701282 -2.00807907428116 2 -1.98085100225472 -3.14577976996913 2 -1.22033882804203 -1.27059490650837 2 -3.63193108279580 -3.11058334232267 2 0.102891612928375 -1.22160079536067 2 -1.40107196092072 -2.62656813971079 2 -0.119722341705324 -2.40468500363314 2 -1.69052410756657 -2.55362806958546 2 -2.54271964039796 -1.91589058634527 2 -0.343523108605652 -2.49149022102082 2 0.316883575333585 -2.52906321428936 2 -1.87573185497794 -1.20977920086606 2 -1.12291585554071 -2.95459637081458 2 -2.02712857136281 -0.961832058100867 2 -3.53522567663707 -0.984866486339600 2 -1.72315772679770 -2.05195287776772 2 1.42434912296780 -1.52067385879605 2 -1.13666037772266 -1.55037662787650 2 -1.66613214659963 -3.18296698080076 2 -0.457143150660647 0.509781116038181 2 -1.22098471988688 -0.882609121489547 2 -0.356343915708505 -1.67331661986542 2 -0.0786550935529085 -4.73343627490684 2 -0.184995446264432 -3.10213062754662 2 0.446478297792453 -3.26649328025486 2 0.0633851811636195 -1.84943984630028 2 -2.36855998994580 -0.994490576724791 2 -0.997130789427785 -3.04034037828003 2 -1.41856760217309 -2.28964625570538 2 0.350040150964700 -1.79746181028561 2 -0.755415279047432 -2.59781844643693 2 -0.352933669038259 -2.48624095000810 2 -2.68913988409750 -1.67386209746412 2 -0.651719917802835 -1.51374444462605 2 -1.26231084944852 0.779384007100693 2 -1.26296966895235 -0.810204720079530 2 -2.23682954171640 -0.835001138758917 2 -0.375248173785158 -1.55349331009271 2 1.51410020270461 -0.634119318427798 2 0.382174197104991 -3.35944655290700 2 -0.682865815806683 -1.41058114576690 2 -1.34694250851133 -1.55468097121420 2 -2.02544099148358 -2.65234877306471 2 -2.06016367113451 -1.35042861388722 2 -0.959277599912767 -1.03167390213503 2 -1.03762209668129 -2.52765134755163 2 -2.68754686476036 -0.129814097497158 2 -1.15977824869436 -1.79917602911918 2 -2.57950364225661 -2.52265489909263 2 0.00155330436570167 -1.71213265270818 2 -1.12978628406773 -3.39165516852278 2 0.0921150204253451 -2.63157279154719 2 -0.844093063839660 -2.36041467500693 2 -0.0396728868784710 -1.44548126171249 2 -0.621844714898918 -2.86019974123165 2 -1.07483069301477 -1.89741453350577 2 -0.738970626890969 -3.59759462929014 2 0.867969643742365 -0.722426725222320 2 -3.95131862925999 -0.854947488627092 2 -2.37808540058566 -1.35270964216778 2 -1.56079425806844 -3.09204770476217 2 -0.332624591684226 -0.600519433513413 2 -0.116811636576998 -2.05164198709530 2 -3.20068909585942 -3.26262554945542 2 -0.180169173681575 -1.61551669533017 2 -1.93214436712871 -0.669513630318290 2 -0.589120017172482 -2.51431952144892 2 -1.18785811174528 -0.966864765065473 2 -1.83453974391459 -1.45090440478646 2 -1.17576680832153 -3.85834206474533 2 0.123205389203243 -2.11877467377637 2 -1.85688894965492 -0.961077127314294 2 0.0149669270654607 -2.47650099648865 2 -0.781804260253942 -0.901474365264974 2 -0.803778819226794 -3.65271266033971 2 -0.679127200685969 0.0943513226193558 2 -1.52747702694411 -1.86880569947938 2 -1.04953817408260 -1.80483437010876 2 -1.22531806348295 -1.27051500091278 2 -1.78579658589578 -2.14062353116164 2 -2.08953176768977 -2.67662755112446 2 -0.667005897592690 -1.32960971159173 2 -0.463299687640113 -0.737577760861513 2 -1.68572522498320 -2.93892458223103 2 -1.63277217788072 -1.30354109857136 2 -1.02628214412421 -1.35851514446071 2 -1.52240929806981 -2.12350610427161 2 0.0415395073786338 -1.20926370539300 2 -1.04154854280782 -2.11018661331519 2 -2.18504517169308 -1.05966580312936 2 0.211116299213722 -3.11130041371660 2 -2.34157397591626 -4.19015927126862 2 -1.33882578169793 -1.88140790368051 2 -0.253558575619576 -2.38559587225172 2 -0.310698193653987 -3.00206431484621 2 -2.12026594227974 -2.39975266408945 2 -0.852296942624990 -0.592027407513589 2 -1.21935268674258 -1.03843590891724 2 -1.68690349414040 -0.503970846923828 2 -1.04513236837681 -2.95575991172932 2 -0.442190144781815 -0.636649177413588 2 0.730986622604762 -3.41955463009533 2 -1.71026520378744 -1.94819662165205 2 -0.527452199753948 -2.67616033113518 2 -1.30539456955551 -1.36182576504006 2 -2.72306179809562 -2.79053327524638 2 -2.07380215138405 -0.728705334357133 2 -0.965251006426627 -1.48375192282740 2 -0.774784394443729 -0.212376725139720 2 0.0521610467109230 -0.422846535259319 2 -0.0741762084430734 -4.27698495423505 2 -1.68659233829840 -2.71618224141462 2 -1.52269291096569 -2.72770042967216 2 -1.40460428851225 -2.77225359808668 2 -1.33772674821458 -0.817290927051766 2 -1.04440372646130 -1.42426376426828 2 -1.00354031447889 -1.82440013042544 2 -1.32034993578153 -1.76286299308342 2 -1.42095953142219 -1.87959448979845 2 -0.0627901950705300 -2.66773693416772 2 -0.985047819399222 -1.85205966160612 2 -0.0210797978149880 -3.00220778252617 2 -0.941985226070347 -1.87356488945152 2 -1.98849456979575 -2.97667369586016 2 -0.167818221055892 -2.18906056319472 2 -2.44979771530454 -2.75646421280144 2 1.13316295231929 -0.956232379716762 2 -1.86789123336239 -3.13793233496855 2 -0.429524488147674 -2.51908020547437 2 -0.934605542605394 -0.842877735049017 2 -0.346199946699204 -1.24787751720203 2 -1.00941739570003 -3.15816183686204 2 -1.83044422708701 -2.40283062514859 2 -1.22558892960916 -1.93708726149952 2 -1.65004805402606 -2.39993772732279 2 -1.60716377509085 -1.82767606924426 2 -0.746135061501171 -2.30907109469842 2 -0.342313627139470 -2.44605902181917 2 -0.634853759620572 -1.81633946307887 2 -2.75221123114020 -1.32416094860295 2 -2.61553372195090 -1.15820631630925 2 -2.19495662378768 -1.24407777110926 2 -0.222708581960948 -2.59082674730457 2 0.962592496386810 -1.66371529652988 2 -0.361768118909848 -3.03490714871514 2 -1.25015596463586 -2.11326572982229 2 -1.37134054895515 -3.51309224317994 2 -1.98912166042735 -0.794009637634717 2 -3.32001710609875 -1.94815819904904 2 -1.59860139494161 -0.256998955970272 2 0.353811135723513 -1.20398911333253 2 -0.166342176059333 -2.39552464481830 2 0.535202264184396 -1.70328356055745 2 1.14059728004187 -1.76081007169889 2 -0.243590196482061 -1.39745729962096 2 -2.51984328951400 -1.34142363213293 2 -2.64682305670103 -1.86699165311864 2 0.746637173542067 -2.41200805286518 2 -0.343878900693697 -0.440000696760032 2 -0.192261601778065 -1.53462542646109 2 0.138055375799523 -1.27530127164819 2 -0.880793613751649 -0.931376035774254 2 -0.00965893526194539 -1.56677278144837 2 0.224992954266040 -0.995965838331538 2 -1.58871843098242 -2.94774742751256 2 -1.95244476696295 -1.92114588240744 2 -1.28414235678403 -2.03403584957852 2 -1.64753516873300 -2.09345732261700 2 -1.64194894249586 -0.879951936300127 2 -3.61862462371681 -2.00243355559052 2 -1.01009617736081 -3.09020936806078 2 -1.60458808452461 -1.68831066156947 2 -1.87854194476564 -1.35738894317847 2 -0.205811391913226 -1.58767045846552 2 -1.55909865868355 -2.10395825438163 2 -1.76928725803331 -2.53433617280189 2 -0.360006204461635 -1.80799047405747 2 -1.05692587244651 -2.98986114338233 2 -0.462604573250955 -0.831093730135601 2 -1.04811085730829 -2.48549571827326 2 -2.71476305922275 -1.84452448809444 2 -0.959092352248364 -1.23124137079303 2 -0.959273262500398 -3.31096742037816 2 -0.904709408285316 -1.65788573729931 2 -1.09806808481574 -1.58946632686356 2 -0.533269000401694 -0.989356027925762 2 -1.39648346217912 -0.368539306851375 2 -1.65851405984130 -0.401948517273337 2 -2.83767386699204 0.290958501679957 2 -0.107685222210462 -3.15634190212422 2 -2.06901240338388 -2.71386535358664 2 -2.06859346263059 -1.61736196195404 2 -1.95925377694129 -3.42922428394369 2 -2.95488374018535 -3.18158259868045 2 0.0317347401402968 -0.942133541487370 2 -0.814323625903029 -2.22857298748150 2 0.00525088398120399 -1.59875881363231 2 -0.598831318634984 -2.13723898105117 2 -0.614672989850263 -3.13319420163449 2 -3.03806569181763 -3.07094269301106 2 -1.38262652366557 -1.13170101685462 2 -0.0922633763567908 -0.504561459028127 2 -3.72627383671239 -2.80041966023535 2 -1.77590029907800 -2.41550272897956 2 -1.40979134094414 -1.45851224873350 2 -2.82586417788894 -2.39228841442038 2 0.257680920545888 -1.97990980735224 2 -2.44635559878291 -4.79591814701568 2 -2.05624295490104 -3.68906524473623 2 -1.42270706811689 -1.39075258525401 2 -2.17840227436171 -1.21363375441867 2 -2.32914499142949 -0.881311803519092 2 0.598365926703067 -2.84544304298080 2 -0.643255670219147 -2.40803570375834 2 0.0843390852389934 -1.39378436497890 2 -1.66697693009880 -0.592773831507341 2 0.626745399948540 -0.926950247934740 2 -0.672414331967487 -3.53154556631934 2 0.352522637403434 -1.91797654912187 2 -1.82310534193298 -2.89450943912445 2 -0.597245086582475 -1.61359404985948 2 -1.14064233777260 -1.60271945361013 2 -1.70204836505010 -2.13961929003704 2 0.235878337869599 -2.38775241053189 2 -0.141735788903578 -2.59489245821041 2 -0.828502996088616 -1.23858904430036 2 -0.0978646928888770 -1.06281312750937 2 -1.93902400437420 -2.82819794443671 2 -1.39427299354969 0.249866344006670 2 -0.904924677326023 -1.89216268997124 2 -1.32085715610154 -0.990220924731499 2 -2.50193150440295 -0.281456247485279 2 -0.392391184147355 -0.546123544054292 2 -0.152929876732929 -2.48572749212263 2 -0.651820459529929 -1.88214926705837 2 -1.20422393497840 -1.21321276545477 2 -0.421613896630912 -3.25326083534703 2 -1.67320115671363 -2.67133610282419 2 -0.0852624479562102 -2.41703552582237 2 0.844602969463312 -2.99035060443023 2 0.903593972626629 -1.41354457280497 2 -1.18301884299683 -3.88352275972250 2 -2.06271253435047 -1.68740642193451 2 -2.19985135052367 -0.710004616120628 2 -0.347561513058305 -1.56644557331382 2 0.00771765065487395 -1.97354652691640 2 -0.419150422416612 -2.19813434187818 2 -1.87191613336216 -1.74821819929365 2 -1.93325806411695 -1.33480284571861 2 0.715082565012912 -1.77203690606901 2 -1.07327809054543 -1.58290015138732 2 -0.696446289521703 -3.55612029222875 2 1.01362804794963 -1.11417208800095 2 -0.491160775940306 -1.80373580945816 2 -2.17948683062784 -3.44259407437159 2 -2.28527398831899 -1.59616635540744 2 0.176292317925610 -2.85783893656436 2 -1.04846823034867 -1.72851746386366 2 -1.03833982297236 -2.89332954613358 2 -0.802503022927876 -1.08351554517757 2 0.819118229376311 -1.64172255911185 2 -1.08986028032381 -0.763440335594018 2 -3.47150050181481 -0.258151782557863 2 -0.768101308728363 -1.05568558215869 2 -1.07591282512733 -1.87764977308524 2 -2.26624463234343 -1.44137613788325 2 -2.61313183155456 -1.78632629093252 2 -2.30471698162323 -2.36512217921553 2 -1.76038184987878 -3.63521501833666 2 1.07551876753409 -3.30733959141121 2 -1.15230710676659 -3.33115350173336 2 -1.18338987511929 -2.56780890053918 2 -1.11088585618127 -3.39808425314404 2 -1.83227505417862 -1.40561210105766 2 0.565948658372208 -1.76506303386076 2 -0.706997101150841 -3.69905696402177 2 -0.773837557362752 -3.97479380075522 2 -2.10309516122273 -2.36908619866192 2 -1.37657318496037 -1.33643223891869 2 -1.07152828000462 -2.45731181102389 2 -1.20009544477679 -3.48124581883144 2 -1.48450148214270 -3.47471076726943 2 -0.259664499100813 -1.07269508254690 2 -0.367195930929824 -1.96105071506627 2 -2.34436375741424 -1.58626319295633 2 -0.0780443244569079 -2.69802832213466 2 -1.59849479699237 -2.20032548387826 2 -1.72725114932259 -0.774337767037896 2 -1.70816734873435 -2.32292394331809 2 0.0450597473316559 -2.06682151342142 2 -0.629411295485014 -3.35199325271732 2 -2.15541871235351 -2.38001387556443 2 -1.80731300921036 -0.488339245799372 2 0.0276122068900746 -2.88467940068988 2 -0.946246379889011 -1.40655817356052 2 -0.136237980780510 -2.30682222873736 2 0.131517668938548 -1.46442716981136 2 -0.865688924197129 -0.687128268850962 2 -1.01723666963235 -1.74323180990878 2 -1.31665222772616 -2.46531628443839 2 -1.03943896775983 -3.35638753155671 2 -0.689136519767400 -2.07877813926687 2 0.0874670532804673 -1.46543659549307 2 -0.968758938537593 -1.65324300260535 2 -1.72738518787498 -1.99312038073847 2 -1.01548588588349 -2.44191323311155 2 -4.08332149434866 -1.80043313039836 2 -4.11535059476019 -1.86374638848112 2 -0.708361139121137 -1.54600450356977 2 -1.34198370023637 -2.85843630856460 2 -0.565831366881962 -1.78124223009042 2 -2.32837728190169 -0.240275297094952 2 -1.78967032534176 -2.73255575389153 2 -0.0243394114020681 -0.421466846049968 2 -0.00229898622248015 -2.04068735283405 2 -2.73224080483576 -3.44865446495605 2 -2.45107957735752 -0.638230247746398 2 0.353664040226350 -0.801674766090938 2 0.277878772167532 -2.63848674407169 2 1.33252986378447 -2.33659741693032 2 -0.496475472940708 -0.922296360052885 2 -0.00557466539048146 -2.20041167969418 2 -1.03365947964672 -1.82050776525097 2 -0.705967647847428 -2.99523988876171 2 -0.614673593612801 -1.97916786516017 2 -2.49864760309159 -0.971147375807815 2 -0.0841116223796332 -0.881249676420870 2 -1.03953194607136 -2.07246533720685 2 -0.733681636034507 -2.03400035170586 2 -0.305374802458429 -2.07699395634030 2 -0.349833774992787 -2.10175512554054 2 -3.21603011143515 -2.59150777046022 2 -1.87792108750567 -0.222045315053595 2 -0.793298975343593 -2.14750158035805 2 -0.614061511469248 -2.41694436313888 2 -0.844267626692267 -2.24603138614179 2 -2.12572522458717 -3.04035740872403 2 -0.918234311524775 -2.38799359124106 2 -1.90994349006102 -1.96558348038381 2 0.619315945186005 -3.09408013231606 2 -0.312743268097232 -1.93453574643007 2 -1.10391894456359 -1.25241513481790 2 -0.184683702832998 -3.02894931450607 2 -0.897701216751985 -2.01812360001523 2 -1.34137251698927 -2.53746884545846 2 -1.70863179759171 -1.39496401207839 2 -2.08578251573531 -1.01003642515715 2 -1.52871376766741 -0.978064517675528 2 -0.202537021164779 -2.29600642474007 2 -2.88845100163391 -2.19988372536458 2 -0.429947857455468 -1.85671699626224 2 -3.32632170009248 -1.87255065221860 2 -1.03400567024739 -0.700009246256265 2 -1.37694159821308 -2.39130834034685 2 -0.0945661071617194 -1.21900096752797 2 -0.709789847152762 -2.38269803099081 2 1.27975736887045 -3.72891465881516 2 -0.660818182427366 -3.27600119839646 2 -0.844596830958682 -3.59275756143130 2 -0.984189656812919 -2.90754073379141 2 -0.584311254480856 -0.813538472102939 2 -2.14806469111188 -2.16923958835145 2 -1.04445784122100 -2.79581165576204 2 -1.35178025947604 -2.42062425249432 2 1.16090158161885 -3.65211856361873 2 0.258954033211960 -1.27578381224773 2 -1.37102791059223 -2.13257001231492 2 -0.279909950554544 -3.68030109742858 2 -3.01825425323906 -1.21917311828541 2 0.146546094365659 -0.262216138496850 2 -1.27762173786530 -2.77893048222840 2 -2.25097735140846 -1.32596788768795 2 0.587082509277191 -1.29646621321274 2 -0.980233966245535 -1.51153802891465 2 -1.76147789308117 -2.15616534168299 2 0.0679935548226136 -1.06459412168699 2 -0.0214022565933583 -2.72942457197311 2 -1.63309656229184 -1.94472385023418 2 -0.218734674703933 -2.42570558326272 2 -1.54795109628850 -1.90024128914679 2 -0.939874920634699 -1.68061994883721 2 -2.81300771301267 -2.31945116391850 2 -1.74575399976012 -3.24253340040246 2 -0.813018706658766 -1.46400119576122 2 -0.108209273587457 -2.49161534458891 2 -1.32921751050599 -1.92784532825640 2 -0.330066019151182 -1.59916643043317 2 -1.04922253957097 -1.85343486612750 2 -1.80247655424597 0.664250143226307 2 0.219337046296894 -3.92118700394802 2 0.245810395476468 -1.97572040322430 2 -0.641992110803202 -1.75236627317571 2 0.850595161464165 -1.03419918364220 2 -0.979522810563494 -2.39157521681757 2 0.462056628453292 -1.77040767112269 2 -0.796319839375389 -1.41637231894917 2 -0.126649092592207 -3.88732906465409 2 0.250656569085903 -1.41752144202359 2 -0.300689455900705 -2.39107316758738 2 -0.232311976406172 -1.07321239587254 2 0.761056751281351 -1.39439130328651 2 -2.04079218946409 -2.09321321916755 2 0.0130472959306258 -2.72775045770918 2 -0.783486702711137 -3.15915508854673 2 0.0875202075090629 -2.99095912231893 2 1.05058419190066 -2.48673765668898 2 -1.05094628104891 -2.46860292111285 2 0.135758702194356 -2.57659510632539 2 0.269632787023886 -2.12373546062669 2 -1.06013435510795 -0.637748278102319 2 -2.17712470143409 -1.81407076812359 2 -0.438842937339074 -1.98681964065650 2 -0.784266298150224 -2.72105969175406 2 -2.52441035898856 -1.77938097112198 2 -0.831643709133855 -2.56984989677121 2 -0.928267641983258 -4.81476418501057 2 0.505890916474055 -2.17959602358171 2 -2.16720737476316 -1.19244168426693 2 -0.700940721356467 -0.986127248645161 2 -2.63976359125509 -1.79847867416534 2 -0.574981863784512 -2.29540002373887 2 -0.742720166810230 -3.40044065166058 2 -1.55818044803369 -1.89982065599376 2 -2.08530289864125 -2.69614408848973 2 -2.27668246827953 -3.03166385728041 2 -0.928842867753549 -1.94813604188420 2 -3.02265903316559 -2.53321405989764 2 0.152454341677487 -1.21043788373565 2 -2.32574943116236 -0.949640745392916 2 -2.09064044178726 -3.12828760205670 2 0.0967202754673815 -2.11059463983785 2 0.134886977667473 -3.52136629978657 2 -1.89474855779492 -2.02192726086667 2 -0.318034104228869 -2.35306642253285 2 -1.61800574924745 -0.825524936471070 2 0.232274701537511 -0.510490091177981 2 -0.0936742645656620 -1.31823674467175 2 -1.79926641137595 -2.05147102529154 2 -0.777846784073033 -2.19294306164927 2 -0.747185845555017 -0.865097342773531 2 -0.0769862371374327 -1.75091909063040 2 -0.388773373440707 -1.67536345759349 2 -1.09941472513134 -2.71704690192925 2 0.289141358191950 -0.999020613999776 2 -0.755676079038423 -1.36263377313167 2 -1.84601940010296 -1.42132892959564 2 -2.05171794126506 -0.805237134419860 2 -2.68044484862042 -1.91632039544155 2 0.164356736046684 -3.21214779169833 2 -0.996608692945777 -2.07791925611247 2 -1.44213421076704 -1.80431573962877 2 -0.0778186103839036 -0.652391694340338 2 -0.677318908345893 -2.04659672596894 2 -2.59014848084703 -2.95994822892238 2 -0.343753351611487 -1.40446767547124 2 0.0136515736996878 -0.546580984222088 2 -0.855010778048427 -1.38958252424125 2 -1.00038700208592 -3.33789546454257 2 -1.91665282758399 -1.23681086631105 2 -0.183872727578518 -1.29070747986214 2 -2.49524182156958 -3.42768862270169 2 -1.34295133105490 -1.89929569418714 2 -2.36159542799308 -1.22859416624455 2 -3.62495313921221 -2.45809254186177 2 -0.740548874058931 -1.29966302603733 2 -2.67921920336005 -1.18542623160203 2 -0.847522322899486 -1.15560224742330 2 0.741840918731699 -0.496338051365575 2 -2.54221691809446 -3.97571910611358 2 -0.640870911917068 -3.98946416736222 2 -1.27601757941708 -2.19574158817790 2 -1.99518769488596 -2.50955319147105 2 -0.0971121023003621 -1.94763245438242 2 0.758929387865149 -3.34051699723595 2 -1.46945090437368 0.131656964194621 2 -0.774828435874664 -1.98917132945057 2 -1.44524731731601 -2.87227670173351 2 0.206600225332415 -1.61159479256529 2 -0.890612193740134 -3.25470953464458 2 -0.783526850981737 -1.46513244158462 2 0.229291351306705 -2.30369791576758 2 -0.0766703376419500 -2.93769333289539 2 -1.64705658546377 -2.19363222244332 2 -2.02564827857175 -2.23661197841419 2 0.580602656916380 -1.38403756935533 2 -1.27308082491469 -2.20514718975902 2 -2.92790668922674 -1.98439888443983 2 -1.62181912776895 -0.851506362212275 2
GMM_main.m
function [accuracy,NumIterations]=GMM_main(data, K) %主函數 [~, data_dim]=size(data); X=data(:, 1:data_dim-1); %數據 real_label=data(:, data_dim); [label, ~, NumIterations]=Matlab_gmm_2(X, K); accuracy=succeed(real_label,K,label);
Matlab_gmm.m
function [label, NegativeLogLikelihood, NumIterations]=Matlab_gmm(X, K) %協方差矩陣為對角陣,數據獨立同分佈 [X_num,X_dim]=size(X); para_sigma_inv=zeros(X_dim, X_dim, K); N_pdf=zeros(X_num, K); %單高斯分佈的概率密度函數 RegularizationValue=0.001; %正則化繫數,協方差矩陣求逆 MaxIter=100; %最大迭代次數 TolFun=1e-8; %終止條件 % 自己設置初始化參數 % init_Mu = [1 1; 2 2]; % init_Sigma(:,:,1) = [1 1; 1 2]; % init_Sigma(:,:,2) = 2*[1 1; 1 2]; % init_Components = [1/2,1/2]; % S = struct('mu',init_Mu,'Sigma',init_Sigma,'ComponentProportion',init_Components); % gmm=fitgmdist(X, K, 'RegularizationValue', RegularizationValue, 'CovarianceType', 'diagonal', 'Start', 'S', 'Options', statset('Display', 'final', 'MaxIter', MaxIter, 'TolFun', TolFun)); gmm=fitgmdist(X, K, 'RegularizationValue', RegularizationValue, 'CovarianceType', 'diagonal', 'Start', 'plus', 'Options', statset('Display', 'final', 'MaxIter', MaxIter, 'TolFun', TolFun)); NegativeLogLikelihood=gmm.NegativeLogLikelihood; NumIterations=gmm.NumIterations; %迭代次數 mu=gmm.mu; %均值 Sigma=gmm.Sigma; %協方差矩陣 ComponentProportion=gmm.ComponentProportion; %混合比例 for k=1:K sigma_inv=1./Sigma(:,:,k); %sigma的逆矩陣,(X_dim, X_dim)的矩陣 para_sigma_inv(:, :, k)=diag(sigma_inv); %sigma^(-1) end for k=1:K coefficient=(2*pi)^(-X_dim/2)*sqrt(det(para_sigma_inv(:, :, k))); %高斯分佈的概率密度函數e左邊的繫數 X_miu=X-repmat(mu(k,:), X_num, 1); %X-miu: (X_num, X_dim)的矩陣 exp_up=sum((X_miu*para_sigma_inv(:, :, k)).*X_miu,2); %指數的冪,(X-miu)'*sigma^(-1)*(X-miu) N_pdf(:,k)=coefficient*exp(-0.5*exp_up); end responsivity=N_pdf.*repmat(ComponentProportion,X_num,1); %響應度responsivity的分子,(X_num,K)的矩陣 responsivity=responsivity./repmat(sum(responsivity,2),1,K); %responsivity:在當前模型下第n個觀測數據來自第k個分模型的概率,即分模型k對觀測數據Xn的響應度 %聚類 [~,label]=max(responsivity,[],2); figure(2) scatter(X(:,1),X(:,2),10,'.') % Scatter plot with points of size 10 hold on gmPDF = @(x,y)reshape(pdf(gmm,[x(:) y(:)]),size(x)); fcontour(gmPDF,[-6 6])
Matlab_gmm_2.m
function [label, NegativeLogLikelihood, NumIterations]=Matlab_gmm_2(X, K) %協方差矩陣為非對角陣,數據不獨立 [X_num,X_dim]=size(X); N_pdf=zeros(X_num, K); %單高斯分佈的概率密度函數 RegularizationValue=0.001; %正則化繫數,協方差矩陣求逆 MaxIter=100; %最大迭代次數 TolFun=1e-8; %終止條件 % 自己設置初始化參數 % init_Mu = [1 1; 2 2]; % init_Sigma(:,:,1) = [1 1; 1 2]; % init_Sigma(:,:,2) = 2*[1 1; 1 2]; % init_Components = [1/2,1/2]; % S = struct('mu',init_Mu,'Sigma',init_Sigma,'ComponentProportion',init_Components); % gmm=fitgmdist(X, K, 'RegularizationValue', RegularizationValue, 'CovarianceType', 'diagonal', 'Start', 'S', 'Options', statset('Display', 'final', 'MaxIter', MaxIter, 'TolFun', TolFun)); gmm=fitgmdist(X, K, 'RegularizationValue', RegularizationValue, 'CovarianceType', 'full', 'Start', 'plus', 'Options', statset('Display', 'final', 'MaxIter', MaxIter, 'TolFun', TolFun)); NegativeLogLikelihood=gmm.NegativeLogLikelihood; NumIterations=gmm.NumIterations; %迭代次數 mu=gmm.mu; %均值 Sigma=gmm.Sigma; %協方差矩陣 ComponentProportion=gmm.ComponentProportion; %混合比例 for k=1:K X_miu=X-repmat(mu(k,:), X_num, 1); %X-miu: (X_num, X_dim)的矩陣 sigma_inv=inv(Sigma(:,:,k)); %sigma的逆矩陣,(X_dim, X_dim)的矩陣 exp_up=sum((X_miu*sigma_inv).*X_miu,2); %指數的冪,(X-miu)'*sigma^(-1)*(X-miu) coefficient=(2*pi)^(-X_dim/2)*sqrt(det(sigma_inv)); %高斯分佈的概率密度函數e左邊的繫數 N_pdf(:,k)=coefficient*exp(-0.5*exp_up); end responsivity=N_pdf.*repmat(ComponentProportion,X_num,1); %響應度responsivity的分子,(X_num,K)的矩陣 responsivity=responsivity./repmat(sum(responsivity,2),1,K); %responsivity:在當前模型下第n個觀測數據來自第k個分模型的概率,即分模型k對觀測數據Xn的響應度 %聚類 [~,label]=max(responsivity,[],2); figure(2) scatter(X(:,1),X(:,2),10,'.') % Scatter plot with points of size 10 hold on gmPDF = @(x,y)reshape(pdf(gmm,[x(:) y(:)]),size(x)); fcontour(gmPDF,[-6 6])
succeed.m
function accuracy=succeed(real_label,K,id) %輸入K:聚的類,id:訓練後的聚類結果,N*1的矩陣 N=size(id,1); %樣本個數 p=perms(1:K); %全排列矩陣 p_col=size(p,1); %全排列的行數 new_label=zeros(N,p_col); %聚類結果的所有可能取值,N*p_col num=zeros(1,p_col); %與真實聚類結果一樣的個數 %將訓練結果全排列為N*p_col的矩陣,每一列為一種可能性 for i=1:N for j=1:p_col for k=1:K if id(i)==k new_label(i,j)=p(j,k); %iris資料庫,1 2 3 end end end end %與真實結果比對,計算精確度 for j=1:p_col for i=1:N if new_label(i,j)==real_label(i) num(j)=num(j)+1; end end end accuracy=max(num)/N;
結果
以第二種情況為例,數據不獨立,協方差矩陣不是只在對角線上有元素。
>> [accuracy,NumIterations]=GMM_main(data, 2) 32 iterations, log-likelihood = -3449.42 accuracy = 0.995000000000000 NumIterations = 32