Kernel: Python 2 (SageMath)
Clustering
接下來將以 k-means 來當作我們討論的對象
In [1]:
/projects/sage/sage-6.10/local/lib/python2.7/site-packages/matplotlib-1.5.0-py2.7-linux-x86_64.egg/matplotlib/__init__.py:872: UserWarning: axes.color_cycle is deprecated and replaced with axes.prop_cycle; please use the latter.
warnings.warn(self.msg_depr % (key, alt_key))
K-means 簡介
K Means 為僅靠data來學習出底層pattern的方法,他的演算法相當簡潔,底下我們隨機產生一些資料點
In [2]:
由肉眼來分辨的話,可以看出大概有四群不同的物件
In [3]:
注意我們必須告訴kmeans要分成幾群,可以看出結果相當不錯~!
K-means 實做了 Expectation Maximization (EM) 演算法,這個演算法包含兩個部分:
起始猜出各cluster的中心
重複以下步驟直到收斂 A. 將資料點分到最近的cluster中心 B. 找出新分好的cluster mean指定為新的中心
In [9]:
None
Kmeans 作數字分類
用內建的數字辨識資料來測試一下kmeans的威力。
In [10]:
In [11]:
(10, 64)
在 64個維度上做 10個cluster的分類
In [12]:
即使沒有label也分得不錯!!
更進一步我們用PCA
做壓縮後來看看分類結果和真正label的比較圖。
In [8]:
那麼正確率是多少,先用內建的score來看
大多數sklearn的estimator都內建score來算performance,根據種類不同算法也不同
In [11]:
0.78853644963828606
再用前幾節用過的confusion matrix來看
In [12]:
[[177 0 0 0 1 0 0 0 0 0]
[ 0 154 24 1 0 1 2 0 0 0]
[ 1 10 147 13 0 0 0 4 0 2]
[ 0 7 0 155 0 2 0 7 0 12]
[ 0 9 0 0 162 0 0 10 0 0]
[ 0 0 0 1 2 136 1 0 0 42]
[ 1 3 0 0 0 0 177 0 0 0]
[ 0 4 0 0 0 5 0 170 0 0]
[ 0 105 3 2 0 7 2 3 0 52]
[ 0 20 0 6 0 7 0 8 0 139]]
In [ ]: