1. 程式人生 > >(相似度、鄰近及聚類)Similarity, Neighbors, and Clusters

(相似度、鄰近及聚類)Similarity, Neighbors, and Clusters

主要內容:

  1. 相似度(Similarity) (can be used for classification and regression)
  2. 距離函式(Distance Function)
  3. Nearest - Neighbor
  4. Hierarchical Clustering
  5. K-Mean

——————————————————————————————————

(一)相似度

相似度是很多資料科學方法和商業解決方案的基礎。如果物件之間相似,那麼它們共享著很多其他的屬性。利用相似度我們可以對事物進行歸類,並在此基礎上做出各種決策。以下是幾個例子:

  • 尋找與優質客戶相似的客戶
  • 對使用者進行分類
  • 推薦系統
  • 醫學和法律中,依據相似案例來解決問題

將分析的物件資料化之後,我們便可以引進距離函式來衡量物件之間的相似程度。利用物件之間的距離我們便可以對物件空間進行劃分,進而得到不同的組別。

Note:

物件屬性資料化時會有Heterogeneous Attributes

主要是 Numeric  和 Categorical  兩大類

其中Numeric 型資料需要注意的是資料的Scale 和 Range

(二)距離函式

  • L1-norm(曼哈頓距離)

  • L2-norm(尤拉距離)

  • Jaccard distance(將兩個物件看成集合,運用集合運算、集合中元素數量來構造)

  • Cosine distance  (餘弦函式距離,可以忽略向量大小)[ignore differences in scale
    ]

  • edit distance

用於度量兩個字串之間的差異

例如:【1113 Bleaker St. 113 Bleecker St.

最主要思想是:記錄將一個字串轉變為另一個字串所需要的最小改變步數

(counts the minimum number of edit operations required to convert one string into the other)

(三)Nearest-Neighbor  Method

思想:

給定一個樣本後,我們遍歷所有訓練集,尋找出幾個和新樣本最為接近的訓練樣本,然後基於選出的這些樣本來預測新樣本的屬性

  1. Classification(nearest - neighbor 的直觀解釋)

如圖‘?’所示樣本點是我們需要預測的點,尋找到離它最近的三個點得到兩個‘+’一個‘·’的結果,基於這個結果,如果我們採用投票(majority vote)的原則,那麼新樣本點應當屬於‘+’

PS:進一步我們需要考慮我們應該取多少個最近點(k),每個點是否應該有不同的權重

上面的例子我們只是給出了屬於哪個類別的結論,現實運用中我們往往需要樣本屬於各個類別的概率值,所以接下來我們討論概率估計

  1. Probability Estimation

在上面的例子中,我們取k=3,屬於‘+’的有兩個,屬於‘·’的有一個,所以我們可以估計(假定權重一致)

  1. Regression

Nearest - neighbor Method 不僅僅可以用來分類,其實還可以用來預測物件的數值(類似迴歸預測)

例如新樣本的最近三個訓練樣本的A屬性值(numeric)分別為1,2,3 (假設設定相同的權重),那麼我們可以預測新樣本的A屬性值為(1+2+3)/3 = 2

(Note: 與傳統迴歸不同的是,我們在選取新樣本的鄰近樣本是並未用到屬性A)

關於 K 值選取的討論

權重計分方式可以較好地權衡K值選取的問題,距離較遠的點其權重降低,自動將不重要的點去除(約等於)【Weighted scoring has a nice consequence in that it reduces the importance of deciding how many neighbors to use. Because the contribution of each neighbor is moderated by its distance, the influence of neighbors naturally drops off the farther they are from the instance. Consequently, when using weighted scoring the exact value of k is much less critical than with majority voting or unweighted averaging.】

兩個特例:

  • 1-NN 過擬合
  • n-NN 每個新樣本的預測值都是訓練集的平均值

Nearest-Neighbor Method 的公式化表示:(Combination Function)

Majority vote classification

Here neighborsk(x) returns thek nearest neighbors of instancex

Majority scoring function

Similarity-moderated classification

the distance is commonly used:

Similarity-moderated scoring

Similarity-moderated regression

Nearest - Neighbor 方法注意點

可解釋性(Intelligibility)

可解釋性問題主要表現在兩個方面

  1. (依據模型做某項決策的理由)the justification of a specificdecision
    • 多數情況下是可以解釋清楚的,也能被大眾接受,例如Amazon和Netflix的推薦系統
    • 但是有的情況下卻無法讓使用者信服,例如抵押貸款申請被拒絕的理由是:我們發現您和王某某和李某某比較像,而且他們兩個都有違約情況(迴歸模型就不存在這樣的問題,例如模型中有一個引數是收入水平,那麼我們可以和客戶說,如果你的收入達到了XXX水平我們就不僅拒絕你的申請)
  2. (整體模型的解釋)the intelligibility of an entiremodel

綜上:如果模型的可解釋性和決策理由受到質疑時,我們應當儘量避免使用這個模型

維度問題和領域知識(Dimensionality and domain knowledge)

有太多的特徵,或者說有太多的與相似度計算無關的變數會成為一個問題,因為那些無關變數會對距離計算產生誤導,也即計算出來的距離不能很好地衡量我們希望看到的相似度。

在高緯度情況下這種現象可以稱為‘維度的詛咒’(curse of dimensionality

解決辦法:

  1. 變數選取(Feature selection)
    • 基於應用背景做出判斷(Background knowledge)
    • 自動選擇變數的一些方法(Automated feature selection method:例如主成分分析【PCA】)
  2. 依據領域知識給變數人工賦予不同權重

 Nearest-Neighbor Method 的效率

  • 該模型的優勢是訓練比較快,因為模型訓練部分只涉及到訓練樣本的儲存
  • 該模型的主要計算量集中在模型預測環節(這個環節Cost 較大)

(四)Hierarchical Clustering

  • This idea of finding natural groupings in the data may be called unsupervised segmentation, or more simplyclustering.
  • Supervised modeling involves discovering patterns to predict the value of a specified target variable, based on data where we know the values of the target variable. Unsupervised modeling does not focus on a target variable. Instead it looks for other sorts of regularities in a set of data.

Hierarchical cluster 的思想

 

  • Hierarchical cluster 的優勢是不用事先確定類別的數量,就可以觀察類別間的相似情況

(五)k-means method

對於聚類而言,最普遍的方法便是將每個類別用他們的聚類中心來表示

基於聚類中心的演算法中,我們最為熟悉的當屬 k-means cluster

  1. k-means  的第一步:

演算法開始的時候初始化k的聚類中心,一般是隨機生成,有時候會從資料點中選取出k個點,或者使用者自己認為給定k個點,或者利用一些預處理的方式選出k個點

  1. k-means 的第二步:

重新計算每個類別的聚類中心,替代掉原有的中心,一直迭代直到不再變動(或者到達迭代的閾值)

  • 由於k-means 方法 與初始點的選取有關(可能會得到區域性最優解),所以一般要多次執行k-means 演算法

  • 結果的判斷(使用偏離的距離平方和)

The results can be compared by examining the clusters(more on that in a minute), or by a numeric measure such as the clusters’ distortion,which is the sum of the squared differences between each data point and itscorresponding centroid. In the latter case, the clustering with the lowestdistortion value can be deemed the best clustering.

  • 就執行時間而言,k-means 要比 hierarchical cluster 效率更高
    • k-means 每次迭代只需要所有點與中心點的距離,而hierarchical cluster 則需要計算兩兩之間的所有距離
  • k-means 的關鍵在於如何確定k值
    • 嘗試不同的k值,檢視模型效果

Understand cluster

  • clustering often is used in exploratory analysis, so the whole point is to understand whether something was discovered, and if so, what?

Multiple cluster

We have k clusters so wecould set up a k-class task (one classper cluster). Alternatively, we could set up a kseparate learning tasks, each trying to differentiate one cluster from all theother (k–1) clusters.

Recall

  •  As we have emphasized, one of the fundamental concepts of data science is that one should work to define as precisely as possible the goal of any data mining.
  • we would like to explore our data, possibly with only a vague notion of the exact problem we are solving? The problems to which we apply clustering often fall into this category. We want to perform unsupervised segmentation: finding groups that “naturally” occur (subject, of course, to how we define our similarity measures).