
使用這些基本 REST API 最佳實踐構建出色的 API
import sklearn
print(sklearn.__version__)
運行該示例時,您應該看到以下版本號或更高版本。
0.22.1
我們將使用 make _ classification ()函數(shù)創(chuàng)建一個測試二分類數(shù)據(jù)集。數(shù)據(jù)集將有1000個示例,每個類有兩個輸入要素和一個群集。這些群集在兩個維度上是可見的,因此我們可以用散點圖繪制數(shù)據(jù),并通過指定的群集對圖中的點進行顏色繪制。
這將有助于了解,至少在測試問題上,群集的識別能力如何。該測試問題中的群集基于多變量高斯,并非所有聚類算法都能有效地識別這些類型的群集。因此,本教程中的結果不應用作比較一般方法的基礎。下面列出了創(chuàng)建和匯總合成聚類數(shù)據(jù)集的示例。
# 綜合分類數(shù)據(jù)集
from numpy import where
from sklearn.datasets import make_classification
from matplotlib import pyplot
%matplotlib inline
# 定義數(shù)據(jù)集
X, y = make_classification(n_samples=1000,
n_features=2,
n_informative=2,
n_redundant=0,
n_clusters_per_class=1,
random_state=4)
# 為每個類的樣本創(chuàng)建散點圖
for class_value in range(2):
# 獲取此類的示例的行索引
row_ix = where(y == class_value)
# 創(chuàng)建這些樣本的散布
pyplot.scatter(X[row_ix, 0], X[row_ix, 1])
# 繪制散點圖
pyplot.show()
運行該示例將創(chuàng)建合成的聚類數(shù)據(jù)集,然后創(chuàng)建輸入數(shù)據(jù)的散點圖,其中點由類標簽(理想化的群集)著色。我們可以清楚地看到兩個不同的數(shù)據(jù)組在兩個維度,并希望一個自動的聚類算法可以檢測這些分組。
圖:已知聚類著色點的合成聚類數(shù)據(jù)集的散點圖
接下來,我們可以開始查看應用于此數(shù)據(jù)集的聚類算法的示例。我已經做了一些最小的嘗試來調整每個方法到數(shù)據(jù)集。
親和力傳播包括找到一組最能概括數(shù)據(jù)的范例。
我們設計了一種名為“親和傳播”的方法,它作為兩對數(shù)據(jù)點之間相似度的輸入度量。在數(shù)據(jù)點之間交換實值消息,直到一組高質量的范例和相應的群集逐漸出現(xiàn)
—源自:《通過在數(shù)據(jù)點之間傳遞消息》2007。
它是通過 AffinityPropagation 類實現(xiàn)的,要調整的主要配置是將“ 阻尼 ”設置為0.5到1,甚至可能是“首選項”。
下面列出了完整的示例。
# 親和力傳播聚類
from numpy import unique
from numpy import where
from sklearn.datasets import make_classification
from sklearn.cluster import AffinityPropagation
from matplotlib import pyplot
# 定義數(shù)據(jù)集
X, _ = make_classification(n_samples=1000,
n_features=2,
n_informative=2,
n_redundant=0,
n_clusters_per_class=1,
random_state=4)
# 定義模型
model = AffinityPropagation(damping=0.9)
# 匹配模型
model.fit(X)
# 為每個示例分配一個集群
yhat = model.predict(X)
# 檢索唯一群集
clusters = unique(yhat)
# 為每個群集的樣本創(chuàng)建散點圖
for cluster in clusters:
# 獲取此群集的示例的行索引
row_ix = where(yhat == cluster)
# 創(chuàng)建這些樣本的散布
pyplot.scatter(X[row_ix, 0], X[row_ix, 1])
# 繪制散點圖
pyplot.show()
運行該示例符合訓練數(shù)據(jù)集上的模型,并預測數(shù)據(jù)集中每個示例的群集。然后創(chuàng)建一個散點圖,并由其指定的群集著色。在這種情況下,我無法取得良好的結果。
圖:數(shù)據(jù)集的散點圖,具有使用親和力傳播識別的聚類
聚合聚類涉及合并示例,直到達到所需的群集數(shù)量為止。它是層次聚類方法的更廣泛類的一部分,通過 AgglomerationClustering 類實現(xiàn)的,主要配置是“ n _ clusters ”集,這是對數(shù)據(jù)中的群集數(shù)量的估計,例如2。下面列出了完整的示例。
# 聚合聚類
from numpy import unique
from numpy import where
from sklearn.datasets import make_classification
from sklearn.cluster import AgglomerativeClustering
from matplotlib import pyplot
# 定義數(shù)據(jù)集
X, _ = make_classification(n_samples=1000,
n_features=2,
n_informative=2,
n_redundant=0,
n_clusters_per_class=1,
random_state=4)
# 定義模型
model = AgglomerativeClustering(n_clusters=2)
# 模型擬合與聚類預測
yhat = model.fit_predict(X)
# 檢索唯一群集
clusters = unique(yhat)
# 為每個群集的樣本創(chuàng)建散點圖
for cluster in clusters:
# 獲取此群集的示例的行索引
row_ix = where(yhat == cluster)
# 創(chuàng)建這些樣本的散布
pyplot.scatter(X[row_ix, 0], X[row_ix, 1])
# 繪制散點圖
pyplot.show()
運行該示例符合訓練數(shù)據(jù)集上的模型,并預測數(shù)據(jù)集中每個示例的群集。然后創(chuàng)建一個散點圖,并由其指定的群集著色。在這種情況下,可以找到一個合理的分組。
圖:使用聚集聚類識別出具有聚類的數(shù)據(jù)集的散點圖
BIRCH 聚類( BIRCH 是平衡迭代減少的縮寫,聚類使用層次結構)包括構造一個樹狀結構,從中提取聚類質心。
BIRCH 遞增地和動態(tài)地群集傳入的多維度量數(shù)據(jù)點,以嘗試利用可用資源(即可用內存和時間約束)產生最佳質量的聚類。
—源自:《 BIRCH :1996年大型數(shù)據(jù)庫的高效數(shù)據(jù)聚類方法》
它是通過 Birch 類實現(xiàn)的,主要配置是“ threshold ”和“ n _ clusters ”超參數(shù),后者提供了群集數(shù)量的估計。下面列出了完整的示例。
# birch聚類
from numpy import unique
from numpy import where
from sklearn.datasets import make_classification
from sklearn.cluster import Birch
from matplotlib import pyplot
# 定義數(shù)據(jù)集
X, _ = make_classification(n_samples=1000,
n_features=2,
n_informative=2,
n_redundant=0,
n_clusters_per_class=1,
random_state=4)
# 定義模型
model = Birch(threshold=0.01, n_clusters=2)
# 適配模型
model.fit(X)
# 為每個示例分配一個集群
yhat = model.predict(X)
# 檢索唯一群集
clusters = unique(yhat)
# 為每個群集的樣本創(chuàng)建散點圖
for cluster in clusters:
# 獲取此群集的示例的行索引
row_ix = where(yhat == cluster)
# 創(chuàng)建這些樣本的散布
pyplot.scatter(X[row_ix, 0], X[row_ix, 1])
# 繪制散點圖
pyplot.show()
運行該示例符合訓練數(shù)據(jù)集上的模型,并預測數(shù)據(jù)集中每個示例的群集。然后創(chuàng)建一個散點圖,并由其指定的群集著色。在這種情況下,可以找到一個很好的分組。
圖:使用BIRCH聚類確定具有聚類的數(shù)據(jù)集的散點圖
DBSCAN 聚類(其中 DBSCAN 是基于密度的空間聚類的噪聲應用程序)涉及在域中尋找高密度區(qū)域,并將其周圍的特征空間區(qū)域擴展為群集。
…我們提出了新的聚類算法 DBSCAN 依賴于基于密度的概念的集群設計,以發(fā)現(xiàn)任意形狀的集群。DBSCAN 只需要一個輸入參數(shù),并支持用戶為其確定適當?shù)闹?/p>
-源自:《基于密度的噪聲大空間數(shù)據(jù)庫聚類發(fā)現(xiàn)算法》,1996
它是通過 DBSCAN 類實現(xiàn)的,主要配置是“ eps ”和“ min _ samples ”超參數(shù)。
下面列出了完整的示例。
# dbscan 聚類
from numpy import unique
from numpy import where
from sklearn.datasets import make_classification
from sklearn.cluster import DBSCAN
from matplotlib import pyplot
# 定義數(shù)據(jù)集
X, _ = make_classification(n_samples=1000,
n_features=2,
n_informative=2,
n_redundant=0,
n_clusters_per_class=1,
random_state=4)
# 定義模型
model = DBSCAN(eps=0.30, min_samples=9)
# 模型擬合與聚類預測
yhat = model.fit_predict(X)
# 檢索唯一群集
clusters = unique(yhat)
# 為每個群集的樣本創(chuàng)建散點圖
for cluster in clusters:
# 獲取此群集的示例的行索引
row_ix = where(yhat == cluster)
# 創(chuàng)建這些樣本的散布
pyplot.scatter(X[row_ix, 0], X[row_ix, 1])
# 繪制散點圖
pyplot.show()
運行該示例符合訓練數(shù)據(jù)集上的模型,并預測數(shù)據(jù)集中每個示例的群集。然后創(chuàng)建一個散點圖,并由其指定的群集著色。在這種情況下,盡管需要更多的調整,但是找到了合理的分組。
圖:使用DBSCAN集群識別出具有集群的數(shù)據(jù)集的散點圖
K-均值聚類可以是最常見的聚類算法,并涉及向群集分配示例,以盡量減少每個群集內的方差。
本文的主要目的是描述一種基于樣本將 N 維種群劃分為 k 個集合的過程。這個叫做“ K-均值”的過程似乎給出了在類內方差意義上相當有效的分區(qū)。
-源自:《關于多元觀測的分類和分析的一些方法》1967年
它是通過 K-均值類實現(xiàn)的,要優(yōu)化的主要配置是“ n _ clusters ”超參數(shù)設置為數(shù)據(jù)中估計的群集數(shù)量。下面列出了完整的示例。
# 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
# 定義數(shù)據(jù)集
X, _ = make_classification(n_samples=1000,
n_features=2,
n_informative=2,
n_redundant=0,
n_clusters_per_class=1,
random_state=4)
# 定義模型
model = KMeans(n_clusters=2)
# 模型擬合
model.fit(X)
# 為每個示例分配一個集群
yhat = model.predict(X)
# 檢索唯一群集
clusters = unique(yhat)
# 為每個群集的樣本創(chuàng)建散點圖
for cluster in clusters:
# 獲取此群集的示例的行索引
row_ix = where(yhat == cluster)
# 創(chuàng)建這些樣本的散布
pyplot.scatter(X[row_ix, 0], X[row_ix, 1])
# 繪制散點圖
pyplot.show()
運行該示例符合訓練數(shù)據(jù)集上的模型,并預測數(shù)據(jù)集中每個示例的群集。然后創(chuàng)建一個散點圖,并由其指定的群集著色。在這種情況下,可以找到一個合理的分組,盡管每個維度中的不等等方差使得該方法不太適合該數(shù)據(jù)集。
圖:使用K均值聚類識別出具有聚類的數(shù)據(jù)集的散點圖
Mini-Batch K-均值是 K-均值的修改版本,它使用小批量的樣本而不是整個數(shù)據(jù)集對群集質心進行更新,這可以使大數(shù)據(jù)集的更新速度更快,并且可能對統(tǒng)計噪聲更健壯。
…我們建議使用 k-均值聚類的迷你批量優(yōu)化。與經典批處理算法相比,這降低了計算成本的數(shù)量級,同時提供了比在線隨機梯度下降更好的解決方案。
—源自:《Web-Scale K-均值聚類》2010
它是通過 MiniBatchKMeans 類實現(xiàn)的,要優(yōu)化的主配置是“ n _ clusters ”超參數(shù),設置為數(shù)據(jù)中估計的群集數(shù)量。下面列出了完整的示例。
# mini-batch k均值聚類
from numpy import unique
from numpy import where
from sklearn.datasets import make_classification
from sklearn.cluster import MiniBatchKMeans
from matplotlib import pyplot
# 定義數(shù)據(jù)集
X, _ = make_classification(n_samples=1000,
n_features=2,
n_informative=2,
n_redundant=0,
n_clusters_per_class=1,
random_state=4)
# 定義模型
model = MiniBatchKMeans(n_clusters=2)
# 模型擬合
model.fit(X)
# 為每個示例分配一個集群
yhat = model.predict(X)
# 檢索唯一群集
clusters = unique(yhat)
# 為每個群集的樣本創(chuàng)建散點圖
for cluster in clusters:
# 獲取此群集的示例的行索引
row_ix = where(yhat == cluster)
# 創(chuàng)建這些樣本的散布
pyplot.scatter(X[row_ix, 0], X[row_ix, 1])
# 繪制散點圖
pyplot.show()
運行該示例符合訓練數(shù)據(jù)集上的模型,并預測數(shù)據(jù)集中每個示例的群集。然后創(chuàng)建一個散點圖,并由其指定的群集著色。在這種情況下,會找到與標準 K-均值算法相當?shù)慕Y果。
圖:帶有最小批次K均值聚類的聚類數(shù)據(jù)集的散點圖
均值漂移聚類涉及到根據(jù)特征空間中的實例密度來尋找和調整質心。
對離散數(shù)據(jù)證明了遞推平均移位程序收斂到最接近駐點的基礎密度函數(shù),從而證明了它在檢測密度模式中的應用。
—源自:《Mean Shift :面向特征空間分析的穩(wěn)健方法》,2002
它是通過 MeanShift 類實現(xiàn)的,主要配置是“帶寬”超參數(shù)。下面列出了完整的示例。
# 均值漂移聚類
from numpy import unique
from numpy import where
from sklearn.datasets import make_classification
from sklearn.cluster import MeanShift
from matplotlib import pyplot
# 定義數(shù)據(jù)集
X, _ = make_classification(n_samples=1000,
n_features=2,
n_informative=2,
n_redundant=0,
n_clusters_per_class=1,
random_state=4)
# 定義模型
model = MeanShift()
# 模型擬合與聚類預測
yhat = model.fit_predict(X)
# 檢索唯一群集
clusters = unique(yhat)
# 為每個群集的樣本創(chuàng)建散點圖
for cluster in clusters:
# 獲取此群集的示例的行索引
row_ix = where(yhat == cluster)
# 創(chuàng)建這些樣本的散布
pyplot.scatter(X[row_ix, 0], X[row_ix, 1])
# 繪制散點圖
pyplot.show()
運行該示例符合訓練數(shù)據(jù)集上的模型,并預測數(shù)據(jù)集中每個示例的群集。然后創(chuàng)建一個散點圖,并由其指定的群集著色。在這種情況下,可以在數(shù)據(jù)中找到一組合理的群集。
圖:具有均值漂移聚類的聚類數(shù)據(jù)集散點圖
OPTICS 聚類( OPTICS 短于訂購點數(shù)以標識聚類結構)是上述 DBSCAN 的修改版本。
我們?yōu)榫垲惙治鲆肓艘环N新的算法,它不會顯式地生成一個數(shù)據(jù)集的聚類;而是創(chuàng)建表示其基于密度的聚類結構的數(shù)據(jù)庫的增強排序。此群集排序包含相當于密度聚類的信息,該信息對應于范圍廣泛的參數(shù)設置。
—源自:《OPTICS :排序點以標識聚類結構》,1999
它是通過 OPTICS 類實現(xiàn)的,主要配置是“ eps ”和“ min _ samples ”超參數(shù)。下面列出了完整的示例。
# optics聚類
from numpy import unique
from numpy import where
from sklearn.datasets import make_classification
from sklearn.cluster import OPTICS
from matplotlib import pyplot
# 定義數(shù)據(jù)集
X, _ = make_classification(n_samples=1000,
n_features=2,
n_informative=2,
n_redundant=0,
n_clusters_per_class=1,
random_state=4)
# 定義模型
model = OPTICS(eps=0.8, min_samples=10)
# 模型擬合與聚類預測
yhat = model.fit_predict(X)
# 檢索唯一群集
clusters = unique(yhat)
# 為每個群集的樣本創(chuàng)建散點圖
for cluster in clusters:
# 獲取此群集的示例的行索引
row_ix = where(yhat == cluster)
# 創(chuàng)建這些樣本的散布
pyplot.scatter(X[row_ix, 0], X[row_ix, 1])
# 繪制散點圖
pyplot.show()
運行該示例符合訓練數(shù)據(jù)集上的模型,并預測數(shù)據(jù)集中每個示例的群集。然后創(chuàng)建一個散點圖,并由其指定的群集著色。在這種情況下,我無法在此數(shù)據(jù)集上獲得合理的結果。
圖:使用OPTICS聚類確定具有聚類的數(shù)據(jù)集的散點圖
光譜聚類是一類通用的聚類方法,取自線性線性代數(shù)。
最近在許多領域出現(xiàn)的一個有希望的替代方案是使用聚類的光譜方法。這里,使用從點之間的距離導出的矩陣的頂部特征向量。
—源自:《關于光譜聚類:分析和算法》,2002年
它是通過 Spectral 聚類類實現(xiàn)的,而主要的 Spectral 聚類是一個由聚類方法組成的通用類,取自線性線性代數(shù)。要優(yōu)化的是“ n _ clusters ”超參數(shù),用于指定數(shù)據(jù)中的估計群集數(shù)量。下面列出了完整的示例。
# spectral clustering
from numpy import unique
from numpy import where
from sklearn.datasets import make_classification
from sklearn.cluster import SpectralClustering
from matplotlib import pyplot
# 定義數(shù)據(jù)集
X, _ = make_classification(n_samples=1000,
n_features=2,
n_informative=2,
n_redundant=0,
n_clusters_per_class=1,
random_state=4)
# 定義模型
model = SpectralClustering(n_clusters=2)
# 模型擬合與聚類預測
yhat = model.fit_predict(X)
# 檢索唯一群集
clusters = unique(yhat)
# 為每個群集的樣本創(chuàng)建散點圖
for cluster in clusters:
# 獲取此群集的示例的行索引
row_ix = where(yhat == cluster)
# 創(chuàng)建這些樣本的散布
pyplot.scatter(X[row_ix, 0], X[row_ix, 1])
# 繪制散點圖
pyplot.show()
運行該示例符合訓練數(shù)據(jù)集上的模型,并預測數(shù)據(jù)集中每個示例的群集。然后創(chuàng)建一個散點圖,并由其指定的群集著色。
在這種情況下,找到了合理的集群。
圖:使用光譜聚類聚類識別出具有聚類的數(shù)據(jù)集的散點圖
高斯混合模型總結了一個多變量概率密度函數(shù),顧名思義就是混合了高斯概率分布。它是通過 Gaussian Mixture 類實現(xiàn)的,要優(yōu)化的主要配置是“ n _ clusters ”超參數(shù),用于指定數(shù)據(jù)中估計的群集數(shù)量。下面列出了完整的示例。
# 高斯混合模型
from numpy import unique
from numpy import where
from sklearn.datasets import make_classification
from sklearn.mixture import GaussianMixture
from matplotlib import pyplot
# 定義數(shù)據(jù)集
X, _ = make_classification(n_samples=1000,
n_features=2,
n_informative=2,
n_redundant=0,
n_clusters_per_class=1,
random_state=4)
# 定義模型
model = GaussianMixture(n_components=2)
# 模型擬合
model.fit(X)
# 為每個示例分配一個集群
yhat = model.predict(X)
# 檢索唯一群集
clusters = unique(yhat)
# 為每個群集的樣本創(chuàng)建散點圖
for cluster in clusters:
# 獲取此群集的示例的行索引
row_ix = where(yhat == cluster)
# 創(chuàng)建這些樣本的散布
pyplot.scatter(X[row_ix, 0], X[row_ix, 1])
# 繪制散點圖
pyplot.show()
運行該示例符合訓練數(shù)據(jù)集上的模型,并預測數(shù)據(jù)集中每個示例的群集。然后創(chuàng)建一個散點圖,并由其指定的群集著色。在這種情況下,我們可以看到群集被完美地識別。這并不奇怪,因為數(shù)據(jù)集是作為 Gaussian 的混合生成的。
圖:使用高斯混合聚類識別出具有聚類的數(shù)據(jù)集的散點圖
在本教程中,您發(fā)現(xiàn)了如何在 Python 中安裝和使用頂級聚類算法。具體來說,你學到了:
文章轉自微信公眾號@算法進階