机器学习中的聚类算法
1. 概述
根據所擁有的數據,可以使用三種不同的機器學習方法,包括監督學習、半監督學習和無監督學習。
在監督學習中,根據已標記數據,因此可以確定輸出是關于輸入的某種(隱函數)的正確值。通過半監督學習,用戶將擁有一個大型數據集,其中一些數據已標記,但大部分未標記。由于涵蓋了大量真實世界的數據,針對性標記每個數據點的成本可能很高,這就可以通過結合使用監督學習和非監督學習來解決這個問題。
無監督學習意味著我們擁有一個完全未標記的數據集,用戶不知道數據中是否隱藏了任何模式,所以需將其留給算法去尋找它能找到的任何東西。這正是聚類算法的用武之地,它針對的是無監督學習問題。
聚類是一項無監督的機器學習任務,有時亦將其稱為聚類分析。使用聚類算法意味著將為算法提供大量沒有標簽的輸入數據,并讓它在數據中找到它可以找到的任何分組。這些分組稱為簇,簇是一組數據點,這些數據點基于它們與周圍數據點的關系而彼此相似。聚類用于特征工程或模式發現之類的場景。
當我們從一無所知的數據入手時,聚類獲得分組可能是對數據獲得一些洞察或初探數據內部結構的好方法。
2.聚類方法
有不同類型的聚類算法可以處理具有各種特性的數據。通常可將聚類算法分為基于密度、基于分布、基于質心、基于層次等類別。
2.1基于密度(Density-based)
在基于密度的聚類中,數據按數據點密度低的區域包圍數據點密度高的區域進行分組。基本上,該算法會找到數據點密集的地方并形成我們常說的“簇”。
簇可以是任何形狀,不受預期條件的限制。這種類型的聚類算法不會嘗試將異常值分配給簇,因此聚類后它們會被作為噪聲點予以忽略或剔除。
2.2基于分布(Distribution-based)
使用基于分布的聚類方法,所有數據點都被視為基于它們屬于給定簇的概率分布的一部分。
它是這樣工作的:有一個中心點,隨著數據點與中心的距離增加,它成為該簇一部分的概率會降低。
如果我們不確定數據的分布情況,我們應該考慮使用不同類型的算法。
2.3基于質心(Centroid-based)
基于質心的聚類是我們可能聽說得最多的一種。它對我們給它的初始參數有點敏感,但它又快又高效。
這些類型的算法根據數據中的多個質心來分離數據點。每個數據點根據其與質心的平方距離分配給一個簇。這是最常用的聚類類型。
2.4基于層次(Hierarchical-based)
基于層次的聚類通常用于層次數據,就像我們從公司數據庫或分類法中獲得的那樣。它構建了一個簇樹,因此所有內容都是自上而下組織的。這比其他聚類類型更具限制性,但它非常適合特定類型的數據集。
3.聚類的應用場景
當我們有一組未標記的數據時,我們很可能會使用某種無監督學習算法。有許多不同的無監督學習技術,例如神經網絡、強化學習和聚類。我們要使用的特定算法類型將具體取決于我們的數據特征與結構。
當我們嘗試進行異常檢測以嘗試查找數據中的異常值時,我們可能希望使用聚類。它有助于找到那些簇組并顯示確定數據點是否為異常值的邊界。
當不確定使用何種機器學習模型時,聚類可以用來找出數據中突出內容的模式,從而更好理解數據。
聚類對于探索我們一無所知的數據特別有用。找出哪種類型的聚類算法效果最好可能需要一些時間,但當我們這樣做時,我們將獲得對數據的準確見解,從中甚至可以發現我們從未想到過的某種關聯。
聚類的一些實際應用包括保險中的欺詐檢測、圖書館中的圖書分類以及市場營銷中的客戶或群體細分。它還可以用于更大的問題,例如地震分析或城市規劃等。
4.常用的幾種聚類算法
現在我們已經了解聚類算法的工作原理和可用的不同類型的一些背景知識,我們可以討論在實踐中經常看到的各種算法。在 Python的sklearn 庫中的示例數據集上實施這些算法。具體來說,使用 sklearn 庫中的make_classification數據集來演示不同的聚類算法為何不適合所有聚類問題。
4.1 K-means
K-均值聚類是最常用的聚類算法。它是一種基于質心的算法,也是最簡單的無監督學習算法。
該算法試圖最小化簇內數據點的方差。這也是大多數人接觸無監督機器學習的方式。
K-means 最適用于較小的數據集,因為它會遍歷所有數據點。這意味著如果數據集中有大量數據點,則需要更多時間對數據點進行分類。
由于這是 k-means 聚類數據點的方式,因此它不能很好地擴展。具體實現見如下代碼。
from numpy import unique from numpy import where from sklearn.datasets import make_classification from sklearn.cluster import KMeans from matplotlib import pyplot# initialize the data set we'll work with training_data, _ = make_classification(n_samples=1000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=4 )# define the model kmeans_model = KMeans(n_clusters=2)kmeans_model.fit(training_data)kmeans_result = kmeans_model.predict(training_data)# assign each data point to a cluster # kmeans_result = kmeans_model.fit_predict(training_data)# get all of the unique clusters kmeans_clusters = unique(kmeans_result)# plot the KMeans clusters for kmeans_cluster in kmeans_clusters:# get data points that fall in this clusterindex = where(kmeans_result == kmeans_cluster)# make the plotpyplot.scatter(training_data[index, 0], training_data[index, 1])# show the KMeans plot pyplot.show()4.2 Mini Batch K-Means
Mini Batch K-Means是K-Means的一個修改版本,它使用小批量樣本而不是整個數據集來更新聚類質心,這可以使其在大型數據集上更快,并且可能對統計噪聲更為魯棒。該算法的缺點是速度提升會降低聚類簇的質量。
通過提出將小批量優化用于k均值聚類。與經典的批處理算法相比,這將計算成本降低了幾個數量級,同時產生了比在線隨機梯度下降更好的解決效果。具體實現見如下代碼。
from numpy import unique from numpy import where from sklearn.datasets import make_classification from sklearn.cluster import MiniBatchKMeans from matplotlib import pyplot # define dataset training_data, _ = make_classification(n_samples=1000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=4) # define the model model = MiniBatchKMeans(n_clusters=2) # fit the model model.fit(training_data) # assign a cluster to each example kmeans_result = model.predict(training_data) # retrieve unique clusters clusters = unique(kmeans_result) # create scatter plot for samples from each cluster for cluster in clusters:# get row indexes for samples with this clusterindex = where(kmeans_result == cluster)# create scatter of these samplespyplot.scatter(training_data[index, 0], training_data[index, 1]) # show the plot pyplot.show()4.3 DBSCAN
DBSCAN 代表具有噪聲的應用程序的基于密度的空間聚類。與 k-means 不同,它是一種基于密度的聚類算法。這是在數據集中查找離群點的好算法。它根據不同區域數據點的密度發現任意形狀的聚類。它按低密度區域分隔區域,以便它可以檢測高密度簇之間的異常值。在處理奇形怪狀的數據時,該算法優于 k-means。
DBSCAN 使用兩個參數來確定聚類的定義方式:minPts(一個被認為是高密度區域需要聚類在一起的最小數據點數)和eps(用于確定一個數據點與其它屬于同一個簇的數據點的距離閾值)。
選擇正確的初始參數對于該算法的工作至關重要。具體實現見如下代碼。
from numpy import unique from numpy import where from matplotlib import pyplot from sklearn.datasets import make_classification from sklearn.cluster import DBSCAN# initialize the data set we'll work with training_data, _ = make_classification(n_samples=1000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=4 )# define the model dbscan_model = DBSCAN(eps=0.25, min_samples=9)# train the model # dbscan_model.fit(training_data)# assign each data point to a cluster dbscan_result = dbscan_model.fit_predict(training_data)# get all of the unique clusters dbscan_clusters = unique(dbscan_result)# plot the DBSCAN clusters for dbscan_cluster in dbscan_clusters:# get data points that fall in this clusterindex = where(dbscan_result == dbscan_cluster)# make the plotpyplot.scatter(training_data[index, 0], training_data[index, 1])# show the DBSCAN plot pyplot.show()4.4 Gaussian-Mixture Model
k-means 的問題之一是數據需要遵循某種圓形區域的范式。k-means 計算數據點之間距離的方式與圓形路徑有關,因此非圓形數據無法正確聚類。這是高斯混合模型解決的問題。我們不需要圓形數據就可以正常工作。
高斯混合模型使用多個高斯分布來擬合任意形狀的數據。在這個混合模型中有幾個單一的高斯模型充當隱藏層。因此,該模型計算數據點屬于特定高斯分布的概率,這就是它將屬于的簇。具體實現如下。
from numpy import unique from numpy import where from matplotlib import pyplot from sklearn.datasets import make_classification from sklearn.mixture import GaussianMixture# initialize the data set we'll work with training_data, _ = make_classification(n_samples=1000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=4 )# define the model gaussian_model = GaussianMixture(n_components=2)# train the model gaussian_model.fit(training_data)# assign each data point to a cluster gaussian_result = gaussian_model.predict(training_data)# get all of the unique clusters gaussian_clusters = unique(gaussian_result)# plot Gaussian Mixture the clusters for gaussian_cluster in gaussian_clusters:# get data points that fall in this clusterindex = where(gaussian_result == gaussian_cluster)# make the plotpyplot.scatter(training_data[index, 0], training_data[index, 1])# show the Gaussian Mixture plot pyplot.show()4.5 BIRCH
Balance Iterative Reducing and Clustering using Hierarchies (BIRCH) 算法在大型數據集上比 k-means 算法效果更好。它將數據分解成summaries 后再聚類,而不是基于原始數據點直接聚類。summaries 包含盡可能多的關于數據點的分布信息。
該算法通常與其他聚類算法一起使用,因為其他聚類技術可用于 BIRCH 生成的summaries 。
BIRCH 算法的主要缺點是它僅適用于數字數據值。除非進行一些必要的數據轉換,否則不能將其用于分類值。具體實現見如下代碼。
from numpy import unique from numpy import where from matplotlib import pyplot from sklearn.datasets import make_classification from sklearn.cluster import Birch# initialize the data set we'll work with training_data, _ = make_classification(n_samples=1000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=4 )# define the model birch_model = Birch(threshold=0.03, n_clusters=2)# train the model birch_model.fit(training_data)# assign each data point to a cluster birch_result = birch_model.predict(training_data)# get all of the unique clusters birch_clusters = unique(birch_result)# plot the BIRCH clusters for birch_cluster in birch_clusters:# get data points that fall in this clusterindex = where(birch_result == birch_cluster)# make the plotpyplot.scatter(training_data[index, 0], training_data[index, 1])# show the BIRCH plot pyplot.show()4.6 Affinity Propagation
類同傳播聚類也稱之為親密度傳播聚類(Affinity Propagation),這種聚類算法在聚類數據的方式上與其他算法完全不同。每個數據點都與所有其他數據點進行信息傳遞,讓彼此知道它們有多相似,并開始揭示數據中的簇。我們不必在初始化參數中預先設定該算法期望有多少個簇。
當信息在數據點之間發送時,會找到稱為樣本的數據集,它們代表簇。
在數據點相互傳遞消息并就哪個數據點最能代表簇達成共識后,就會找到一個范例。
當我們不確定期望有多少簇時,例如在計算機視覺問題中,這是一個很好的算法。具體實現見如下代碼。
from numpy import unique from numpy import where from matplotlib import pyplot from sklearn.datasets import make_classification from sklearn.cluster import AffinityPropagation# initialize the data set we'll work with training_data, _ = make_classification(n_samples=1000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=4 )# define the model model = AffinityPropagation(damping=0.7)# train the model model.fit(training_data)# assign each data point to a cluster result = model.predict(training_data)# get all of the unique clusters clusters = unique(result)# plot the clusters for cluster in clusters:# get data points that fall in this clusterindex = where(result == cluster)# make the plotpyplot.scatter(training_data[index, 0], training_data[index, 1])# show the plot pyplot.show()4.7 Mean-Shift
這是另一種對處理圖像和計算機視覺處理特別有用的算法。
Mean-shift 類似于 BIRCH 算法,因為它也可以在沒有設置初始簇數的情況下找到簇。
這是一種層次聚類算法,但缺點是在處理大型數據集時無法很好地擴展。
它通過迭代所有數據點并將它們移向pattern來工作。本文中的pattern是一個區域中數據點的高密度區域。
這就是為什么我們可能會聽到將此算法稱為模式搜索算法的原因。它將對每個數據點進行此迭代過程,并將它們移動到更靠近其他數據點的位置,直到所有數據點都已分配給一個簇。具體實現見如下代碼。
from numpy import unique from numpy import where from matplotlib import pyplot from sklearn.datasets import make_classification from sklearn.cluster import MeanShift# initialize the data set we'll work with training_data, _ = make_classification(n_samples=1000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=4 )# define the model mean_model = MeanShift()# assign each data point to a cluster mean_result = mean_model.fit_predict(training_data)# get all of the unique clusters mean_clusters = unique(mean_result)# plot Mean-Shift the clusters for mean_cluster in mean_clusters:# get data points that fall in this clusterindex = where(mean_result == mean_cluster)# make the plotpyplot.scatter(training_data[index, 0], training_data[index, 1])# show the Mean-Shift plot pyplot.show()4.8 OPTICS
OPTICS 代表用于識別聚類結構的排序點。它是一種類似于 DBSCAN 的基于密度的算法,但它更好,因為它可以在密度不同的數據中找到有意義的聚類。它通過對數據點進行排序來實現這一點,以便最近的點是排序中的鄰居。
這使得檢測不同密度的簇變得更加容易。OPTICS 算法只對每個數據點處理一次,類似于 DBSCAN(盡管它運行速度比 DBSCAN 慢)。還為每個數據點存儲了一個特殊的距離,指示該點屬于特定的簇。
具體實現見如下代碼。
from numpy import unique from numpy import where from matplotlib import pyplot from sklearn.datasets import make_classification from sklearn.cluster import OPTICS# initialize the data set we'll work with training_data, _ = make_classification(n_samples=1000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=4 )# define the model optics_model = OPTICS(eps=0.75, min_samples=10)# assign each data point to a cluster optics_result = optics_model.fit_predict(training_data)# get all of the unique clusters optics_clusters = unique(optics_clusters)# plot OPTICS the clusters for optics_cluster in optics_clusters:# get data points that fall in this clusterindex = where(optics_result == optics_cluster)# make the plotpyplot.scatter(training_data[index, 0], training_data[index, 1])# show the OPTICS plot pyplot.show()4.9 Agglomerative clustering
這是最常見的層次聚類算法。它用于根據彼此之間的相似程度將對象分組到簇中。
這也是一種自下而上的聚類方式,其中每個數據點都分配給自己的簇。然后這些簇連接在一起。
在每次迭代中,合并相似的簇,直到所有數據點都屬于一個大根簇。
凝聚聚類最擅長尋找小簇。最終結果看起來像一個樹狀圖,以便我們可以在算法完成時輕松地可視化簇。
具體實現見如下代碼。
from numpy import unique from numpy import where from matplotlib import pyplot from sklearn.datasets import make_classification from sklearn.cluster import AgglomerativeClustering# initialize the data set we'll work with training_data, _ = make_classification(n_samples=1000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=4 )# define the model agglomerative_model = AgglomerativeClustering(n_clusters=2)# assign each data point to a cluster agglomerative_result = agglomerative_model.fit_predict(training_data)# get all of the unique clusters agglomerative_clusters = unique(agglomerative_result)# plot the clusters for agglomerative_cluster in agglomerative_clusters:# get data points that fall in this clusterindex = where(agglomerative_result == agglomerative_cluster)# make the plotpyplot.scatter(training_data[index, 0], training_data[index, 1])# show the Agglomerative Hierarchy plot pyplot.show()4.10 Spectral clustering
譜聚類演化于圖論,此后由于其表現出優秀的性能被廣泛應用于聚類分析中,對比其他無監督聚類(如kmeans),spectral clustering的優點主要有以下:
1.過程對數據結構并沒有太多的假設要求,如kmeans則要求數據為凸集。2.可以通過構造稀疏similarity graph,使得對于更大的數據集表現出明顯優于其他算法的計算速度。
3.由于spectral clustering是對圖切割處理,不會存在像kmesns聚類時將離散的小簇聚合在一起的情況。
4.無需像GMM一樣對數據的概率分布做假設。from numpy import unique from numpy import where from sklearn.datasets import make_classification from sklearn.cluster import SpectralClustering from matplotlib import pyplot # define dataset training_data, _ = make_classification(n_samples=1000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=4)# define the model model = SpectralClustering(n_clusters=2)# fit model and predict clusters spectral_result = model.fit_predict(training_data)# retrieve unique clusters clusters = unique(spectral_result)# create scatter plot for samples from each cluster for cluster in clusters:# get row indexes for samples with this clusterindex = where(spectral_result == cluster)# create scatter of these samplespyplot.scatter(training_data[index, 0], training_data[index, 1])# show the plot pyplot.show()
4.11 其他聚類算法
我們已經介紹了10種頂級聚類算法,但還有更多可用的算法。有一些經過專門調整的聚類算法可以快速準確地處理特定的數據。
還有另一種與Agglomerative方法相反的分層算法。它從自上而下的簇策略開始。所以它將從一個大的根簇開始,然后從那里分解出各個小簇。
這被稱為Divisive Hierarchical聚類算法。有研究表明,這會創建比凝聚聚類更準確的層次結構,但它要復雜得多。
5.其他想法
需要注意的是,聚類算法需要考慮尺度問題。我們的數據集可能有數百萬個數據點,并且由于聚類算法通過計算所有數據點對之間的相似性來工作,因此我們最終可能會得到一個無法很好擴展的算法,有必要對聚類的數據集進行尺度或規模上的處理。
6. 結論
聚類算法是從舊數據中學習新事物的好方法。有時使用者會對得到的結果簇感到驚訝,它可能會幫助使用者理解問題。
將聚類用于無監督學習的最酷的事情之一是可以在監督學習問題中使用聚類的結果作為標記值。
簇可能是我們在完全不同的數據集上使用的新功能!我們幾乎可以對任何無監督機器學習問題使用聚類,但前提是請確保我們知道如何分析結果的準確性。
總結
以上是生活随笔為你收集整理的机器学习中的聚类算法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 初中计算机flash考试题,【信息技术中
- 下一篇: React--简单的抽卡模拟器