第1关:距离的计算
import numpy as np
def calc_min_dist(cluster1, cluster2):
'''
计算簇间最小距离
:param cluster1:簇1中的样本数据,类型为ndarray
:param cluster2:簇2中的样本数据,类型为ndarray
:return:簇1与簇2之间的最小距离
'''
#********* Begin *********#
min_dist = np.inf
for i in range(len(cluster1)):
for j in range(len(cluster2)):
dist = np.sqrt(np.sum(np.square(cluster1[i] - cluster2[j])))
if dist < min_dist:
min_dist = dist
return min_dist
#********* End *********#
def calc_max_dist(cluster1, cluster2):
'''
计算簇间最大距离
:param cluster1:簇1中的样本数据,类型为ndarray
:param cluster2:簇2中的样本数据,类型为ndarray
:return:簇1与簇2之间的最大距离
'''
#********* Begin *********#
max_dist = 0
for i in range(len(cluster1)):
for j in range(len(cluster2)):
dist = np.sqrt(np.sum(np.square(cluster1[i] - cluster2[j])))
if dist > max_dist:
max_dist = dist
return max_dist
def calc_avg_dist(cluster1, cluster2):
'''
计算簇间平均距离
:param cluster1:簇1中的样本数据,类型为ndarray
:param cluster2:簇2中的样本数据,类型为ndarray
:return:簇1与簇2之间的平均距离
'''
#********* Begin *********#
num = len(cluster1) * len(cluster2)
avg_dist = 0
for i in range(len(cluster1)):
for j in range(len(cluster2)):
dist = np.sqrt(np.sum(np.square(cluster1[i] - cluster2[j])))
avg_dist += dist
avg_dist = avg_dist / num
return avg_dist
#********* End *********#
第2关:AGNES算法流程
import numpy as np
def AGNES(feature, k):
'''
AGNES聚类并返回聚类结果
假设数据集为`[1, 2], [10, 11], [1, 3]],那么聚类结果可能为`[[1, 2], [1, 3]], [[10, 11]]]
:param feature:训练数据集所有特征组成的ndarray
:param k:表示想要将数据聚成`k`类,类型为`int`
:return:聚类结果
'''
#********* Begin *********#
# 找到距离最小的下标
def find_Min(M):
min = np.inf
x = 0;
y = 0
for i in range(len(M)):
for j in range(len(M[i])):
if i != j and M[i][j] < min:
min = M[i][j];
x = i;
y = j
return (x, y, min)
#计算簇间最大距离
def calc_max_dist(cluster1, cluster2):
max_dist = 0
for i in range(len(cluster1)):
for j in range(len(cluster2)):
dist = np.sqrt(np.sum(np.square(cluster1[i] - cluster2[j])))
if dist > max_dist:
max_dist = dist
return max_dist
#初始化C和M
C = []
M = []
for i in feature:
Ci = []
Ci.append(i)
C.append(Ci)
for i in C:
Mi = []
for j in C:
Mi.append(calc_max_dist(i, j))
M.append(Mi)
q = len(feature)
#合并更新
while q > k:
x, y, min = find_Min(M)
C[x].extend(C[y])
C.pop(y)
M = []
for i in C:
Mi = []
for j in C:
Mi.append(calc_max_dist(i, j))
M.append(Mi)
q -= 1
return C
#********* End *********#
第3关:红酒聚类
from sklearn.cluster import AgglomerativeClustering
from sklearn.preprocessing import StandardScaler
def Agglomerative_cluster(data):
'''
对红酒数据进行聚类
:param data: 数据集,类型为ndarray
:return: 聚类结果,类型为ndarray
'''
# 数据预处理:标准化处理,使得每个特征的均值为0,标准差为1
scaler = StandardScaler()
data_scaled = scaler.fit_transform(data)
# 创建AgglomerativeClustering实例,设置聚类数为3,使用ward方法来计算簇间距离
agnes = AgglomerativeClustering(n_clusters=3, linkage='ward')
# 训练模型并获取聚类结果
result = agnes.fit_predict(data_scaled)
return result
# 示例调用
# 假设有一个名为wine_data的ndarray,包含了红酒数据集
# result = Agglomerative_cluster(wine_data)
# print(result)
from sklearn.cluster import AgglomerativeClustering
from sklearn.preprocessing import StandardScaler
def Agglomerative_cluster(data):
'''
对红酒数据进行聚类
:param data: 数据集,类型为ndarray
:return: 聚类结果,类型为ndarray
'''
# 数据预处理:标准化处理,使得每个特征的均值为0,标准差为1
scaler = StandardScaler()
data_scaled = scaler.fit_transform(data)
# 创建AgglomerativeClustering实例,设置聚类数为3,使用ward方法来计算簇间距离
agnes = AgglomerativeClustering(n_clusters=3, linkage='ward')
# 训练模型并获取聚类结果
result = agnes.fit_predict(data_scaled)
return result
# 示例调用
# 假设有一个名为wine_data的ndarray,包含了红酒数据集
# result = Agglomerative_cluster(wine_data)
# print(result)
标签:dist,return,np,头歌,cluster2,机器,cluster1,data,AGNES
From: https://blog.csdn.net/maodao666/article/details/143453695