SciPy - complete() 方法



SciPy complete() 方法對壓縮距離矩陣執行完全連鎖(最大點)的任務。

壓縮矩陣本身就是個陣列,它收集了用於處理陣列的各種方法。此外,它將兩個叢集之間的距離定義為任意兩點(第一個叢集中的單個數據點和第二個叢集中的任意單個數據點)之間的最遠距離。

語法

以下是 SciPy complete() 方法的語法 −

complete(y)

引數

此方法接受一個引數 −

  • y:此引數儲存陣列矩陣的距離。

返回值

此方法返回連鎖矩陣(結果)。

示例 1

以下 SciPy complete() 方法展示如何在給定的距離矩陣上執行完全連鎖聚類,並使用 dendrogram() 視覺化輸出。

import numpy as np
from scipy.cluster.hierarchy import complete, dendrogram
import matplotlib.pyplot as plt

# Distance matrix
y = np.array([0.5, 0.2, 0.3, 0.3, 0.8, 0.6])

# complete linkage clustering
result = complete(y)

# plot the dendrogram
plt.figure(figsize=(6, 4))
dendrogram(result)
plt.title('Dendrogram - Complete Linkage')
plt.xlabel('indexes')
plt.ylabel('Distance')
plt.show()

輸出

以上程式碼產生以下結果 −

scipy_complete_method_one

示例 2

以下示例給出一個隨機資料集,它允許使用距離矩陣並執行完全連鎖聚類。然後,它使用該方法 dendrogram () 視覺化層次聚類。

import numpy as np
from scipy.spatial.distance import pdist
from scipy.cluster.hierarchy import complete, dendrogram
import matplotlib.pyplot as plt

# random data
data = np.random.rand(6, 2)

# calculate the distance matrix
y = pdist(data, metric='euclidean')

# complete linkage clustering
result = complete(y)

# plot the dendrogram
plt.figure(figsize=(6, 4))
dendrogram(result)
plt.title('Dendrogram - Complete Linkage on Random Data')
plt.xlabel('indexes')
plt.ylabel('Distance')
plt.show()

輸出

以上程式碼產生以下結果 −

scipy_complete_method_two

示例 3

在示例中,我們將說明使用自定義距離度量進行完全連鎖聚類任務。

import numpy as np
from scipy.spatial.distance import pdist
from scipy.cluster.hierarchy import complete, dendrogram
import matplotlib.pyplot as plt

# Sample data
data = np.array([[1, 3], [2, 4], [8, 6], [7, 8]])

# calculate the distance matrix using a custom metric 
y = pdist(data, metric='cityblock')

# complete linkage clustering
result = complete(y)

# Plot the dendrogram
plt.figure(figsize=(6, 4))
dendrogram(result)
plt.title('Dendrogram - Complete Linkage with Cityblock Distance')
plt.xlabel('Sample index')
plt.ylabel('Distance')
plt.show()

輸出

以上程式碼產生以下結果 −

scipy_complete_method_three
scipy_reference.htm
廣告
© . All rights reserved.