K-means VS K-NN and 手肘法


1. The difference between classification and clustering. from here.

Classification: supervised learning with labels.

Clustering: unsupervised learning without labels. 

Classification and Clustering are the two types of learning methods which characterize objects into groups by one or more features. These processes appear to be similar, but there is a difference between them in the context of data mining. The prior difference between classification and clustering is that classification is used in supervised learning technique where predefined labels are assigned to instances by properties, on the contrary, clustering is used in unsupervised learning where similar instances are grouped, based on their features or properties.

2. The difference between k-means and k-NN. from here.

k-means: an unsupervised algorithm used for clustering.

k-NN: a supervised algorithm used for classification. 

3. K-NN algorithm

K-nearest neighbours needs labelled data to train on. With the given data, KNN can classify new, unlabelled data by analysis of the k number of the nearest data points. 

Steps

  1. 計算測試數據與各個訓練數據之間的距離;
  2. 按照距離的遞增關系進行排序;

  3. 選取距離最小的K個點;

  4. 確定前K個點所在類別的出現頻率;

  5. 返回前K個點中出現頻率最高的類別作為測試數據的預測分類。

影響KNN結果的兩個因素: i. k的選取, ii. 距離的測算

  • k過小,導致容易受噪聲影響,將噪聲學習到模型中,而忽略了數據的真實分布. k過大, 模型簡化,忽略訓練數據中的大量有用信息,導致無法學習. 如果k=N(N為訓練樣本的個數),那么無論輸入實例是什么,都將簡單地預測它屬於在訓練實例中最多的類。這時,模型是不是非常簡單,這相當於你壓根就沒有訓練模型!

4. K-means algorithm

Steps

  1. Initially, randomly pick k centroids/cluster centers. Try to make them near the data but different from one another.
  2. Then assign each data point to the closest centroid.
  3. Move the centroids to the average location of the data points assigned to it.
  4. Repeat the preceding two steps until the assignments don’t change, or change very little.

5. How to optimize K in k-means? 

SSE + elbow method; silhouette coefficient; Calinski-Harabaz index; 信息准則: AIC-Akaike information criterion; BIC-Bayesian information criterion.  

SSE,sum of the squared errors,誤差的平方和。在K-means 算法中,SSE 計算的是每類中心點與其同類成員距離的平方和。

 其中, ui 表示第i類的中心點.

手肘法的核心思想是:

隨着聚類數k的增大,樣本划分會更加精細,每個簇的聚合程度會逐漸提高,那么誤差平方和SSE自然會逐漸變小。

當k小於最佳聚類數時,k的增大會大幅增加每個簇的聚合程度,故SSE的下降幅度會很大;
當k到達最佳聚類數時,再增加k所得到的聚合程度,回報會迅速變小,所以SSE的下降幅度會驟減,然后隨着k值的繼續增大而趨於平緩。

也就是說SSE和 k 的關系圖是一個手肘的形狀,而這個肘部對應的k值就是數據的最佳聚類數。這也是該方法被稱為手肘法的原因。

from here.  

6. how to optimize KNN?

cross-validation; 

Supplementary knowledge:

1. cross-validation 交叉驗證

from here

在機器學習里,通常來說我們不能將全部用於數據訓練模型,否則我們將沒有數據集對該模型進行驗證,從而評估我們的模型的預測效果。

交叉驗證是在機器學習建立模型和驗證模型參數時常用的辦法。交叉驗證,顧名思義,就是重復的使用數據,把得到的樣本數據進行切分,組合為不同的訓練集和測試集,用訓練集來訓練模型,用測試集來評估模型預測的好壞。在此基礎上可以得到多組不同的訓練集和測試集,某次訓練集中的某樣本在下次可能成為測試集中的樣本,即所謂“交叉”。

根據切分的方法不同,交叉驗證分為下面三種:   

    第一種是簡單交叉驗證,所謂的簡單,是和其他交叉驗證方法相對而言的。首先,我們隨機的將樣本數據分為兩部分(比如: 70%的訓練集,30%的測試集),然后用訓練集來訓練模型,在測試集上驗證模型及參數。接着,我們再把樣本打亂,重新選擇訓練集和測試集,繼續訓練數據和檢驗模型。最后我們選擇損失函數評估最優的模型和參數。 

     第二種是S折交叉驗證(S-Folder Cross Validation)。和第一種方法不同,S折交叉驗證會把樣本數據隨機的分成S份,每次隨機的選擇S-1份作為訓練集,剩下的1份做測試集。當這一輪完成后,重新隨機選擇S-1份來訓練數據。若干輪(小於S)之后,選擇損失函數評估最優的模型和參數。

    第三種是留一交叉驗證(Leave-one-out Cross Validation),它是第二種情況的特例,此時S等於樣本數N,這樣對於N個樣本,每次選擇N-1個樣本來訓練數據,留一個樣本來驗證模型預測的好壞。此方法主要用於樣本量非常少的情況,比如對於普通適中問題,N小於50時,我一般采用留一交叉驗證。

Reference: 

  1. 交叉驗證(Cross Validation)原理小結

  2. 為什么要用交叉驗證


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM