VLFeat教程之k-means

This tutorial shows how to use the K-means algorithm using the VlFeat implementation of Llloyd's algorithm as well as other faster variants.

Running K-means

KMeans is a clustering algorithm. Its purpose is to partition a set of vectors into K
groups that cluster around common mean vector. This can also be thought as approximating the input each of the input vector with one of the means, so the clustering process finds, in principle, the best dictionary or codebook to vector quantize the data.
Consider a dataset containing 1000 randomly sampled 2D points:
numData = 5000 ;dimension = 2 ;data = rand(dimension,numData) ;

The function vl_kmeans
can be used to cluster the data into, say, 30 groups:
numClusters = 30 ;[centers, assignments] = vl_kmeans(data, numClusters);

By default, this uses the the Lloyd algorithm, a method that alternates between optimizing the cluster centers and the data-to-center assignments. Once this process terminates, the matrix centers
contains the cluster centers and the vector assignments
the (hard) assignments of the input data to the clusters. The cluster centers are also called means because it can be shown that, when the clustering is optimal, the centers are the means of the corresponding data points. The cluster centers and assignments can be visualized as follows:

KMeans clustering of 5000 randomly sampled data points. The black dots are the cluster centers.

Given a new data point x
, this can be mapped to one of the clusters by looking for the closest center:
x = rand(dimension, 1) ;[~, k] = min(vl_alldist(x, centers)) ;

For larger datastes, this process may be significantly accelerated by using KDTrees or other approximate nearest neighbor procedures.
Choosing an initialization method
K-means uses local optimization algorithms and is therefore sensitive to initalization. By default, vl_kmeans
initializes the cluster centers by picks K
data points at random. Other initalization strategies can be selected as well. kmeans++ is a popular method that greedily pick K
data points that are maximally different, and can be use as follows:
[centers, assignments] = vl_kmeans(data, numClusters, 'Initialization', 'plusplus') ;

Choosing an optimization algorithm
In addition to the original KMeans algorithm proposed by Lloyd, vl_kmeans
supports two additional algorithms: Elkan's variant, an exact algorithm using an acceleration technique based the triangular inequality, and ANN, an approximated algorithm using approximate nearest neighbours.
These optimization methods can be enabled by setting the 'Algorithm'
parameter to 'Lloyd'
, 'Elkan'
or 'ANN'
respectively. When using the'ANN'
algorithm, the user can also specify the parameters 'MaxNumComparisons'
and 'NumTrees'
to configure the KD-tree used used as ANN. In particular, 'MaxNumComparisons'
controls the trade-off between approximation quality and speed.
vl_demo_kmeans_ann_speed
compares the speed of the three algorithms. Because of the random initialization, each of the KMeans calls converges to a different local minimum in a different amount of iterations. In order to measure more accurately the speed of each pass, a relatively small number of 'MaxNumIterations'
option) is selected. Note that the speedup factor are in general much more dramatic when truly large datasets are considered.

Comparisons of Elkan, Lloyd and ANN for different values of MaxNumComparison
, expressed as a fraction of the number of clusters. The figure reports the duration, final energy value, and speedup factor, using both the serial and parallel versions of the code. The figure was generated using vl_demo_kmeans_ann_speed.

from VLFeat

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末羞海,一起剝皮案震驚了整個(gè)濱河市绎秒,隨后出現(xiàn)的幾起案子调衰,更是在濱河造成了極大的恐慌,老刑警劉巖从铲,帶你破解...
    沈念sama閱讀 222,681評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡赶撰,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,205評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門柱彻,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)豪娜,“玉大人,你說(shuō)我怎么就攤上這事哟楷×鲈兀” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 169,421評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵卖擅,是天一觀的道長(zhǎng)鸣奔。 經(jīng)常有香客問(wèn)我,道長(zhǎng)惩阶,這世上最難降的妖魔是什么挎狸? 我笑而不...
    開(kāi)封第一講書人閱讀 60,114評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮断楷,結(jié)果婚禮上锨匆,老公的妹妹穿的比我還像新娘。我一直安慰自己脐嫂,他們只是感情好统刮,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,116評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著账千,像睡著了一般侥蒙。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上匀奏,一...
    開(kāi)封第一講書人閱讀 52,713評(píng)論 1 312
  • 那天鞭衩,我揣著相機(jī)與錄音,去河邊找鬼娃善。 笑死论衍,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的聚磺。 我是一名探鬼主播坯台,決...
    沈念sama閱讀 41,170評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼瘫寝!你這毒婦竟也來(lái)了蜒蕾?” 一聲冷哼從身側(cè)響起稠炬,我...
    開(kāi)封第一講書人閱讀 40,116評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎咪啡,沒(méi)想到半個(gè)月后首启,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,651評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡撤摸,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,714評(píng)論 3 342
  • 正文 我和宋清朗相戀三年毅桃,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片准夷。...
    茶點(diǎn)故事閱讀 40,865評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡钥飞,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出冕象,到底是詐尸還是另有隱情代承,我是刑警寧澤,帶...
    沈念sama閱讀 36,527評(píng)論 5 351
  • 正文 年R本政府宣布渐扮,位于F島的核電站,受9級(jí)特大地震影響掖棉,放射性物質(zhì)發(fā)生泄漏墓律。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,211評(píng)論 3 336
  • 文/蒙蒙 一幔亥、第九天 我趴在偏房一處隱蔽的房頂上張望耻讽。 院中可真熱鬧,春花似錦帕棉、人聲如沸针肥。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 32,699評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)慰枕。三九已至,卻和暖如春即纲,著一層夾襖步出監(jiān)牢的瞬間具帮,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,814評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工低斋, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蜂厅,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,299評(píng)論 3 379
  • 正文 我出身青樓膊畴,卻偏偏與公主長(zhǎng)得像掘猿,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子唇跨,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,870評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容

  • 你回頭吧 我在你后面 等你 2016.9.25
    TOJECO閱讀 137評(píng)論 0 0
  • 《蔓層生》目錄 1.如茉莉初見(jiàn) 林蔓生蜷縮在車子的座椅上稠通,車子在快速平穩(wěn)的前行衬衬,但,仍然有搖搖晃晃的感覺(jué)采记,車廂...
    六五七的趣能星球閱讀 222評(píng)論 0 1