聚类分析----最大最小距离法练习

数据集:

0 0
0 1
4 4
4 5
5 4
5 5
1 0

 

# 用最大最小距离法进行聚类分析

from fileUtil import FileUtil
from mathUtil import MathUtil
import math

X = FileUtil.openFile('2-1.txt')  # 数据
Z = []  # 聚类中心
theta = 0.7

# 1.选个中心
Z.append(X[0])
X.remove(X[0])

# 2.选个离第一个中心最远的点做第二个聚类中心
index = 1
p = 0
for i in range(X.__len__()):
    a = MathUtil.ED(Z[0], X[i])
    if p < a:
        p = a
        index = i
Z.append(X[index])
X.remove(X[index])

# 3.计算剩余各特征矢量到z_1,z_2的距离并求出最小值
d = []
for i in range(X.__len__()):
    a = []
    for j in range(Z.__len__()):
        a.append(MathUtil.ED(Z[j], X[i]))
    d.append(min(a))

# 4.看是否有其余中心点
l = MathUtil.ED(Z[0], Z[1])
f = 1
while f:
    d = []
    for i in range(X.__len__()):
        a = []
        for j in range(Z.__len__()):
            a.append(MathUtil.ED(Z[j], X[i]))
        d.append(min(a))

    a = max(d)
    if a > theta * l:
        Z.append(X[i])
        X.remove(X[i])
    else:f = 0

#5.将剩余点按最小距离原则分配
ans = [] #结果集
for i in range(Z.__len__()):
    ans.append([])
    ans[i].append(Z[i])

for i in range(X.__len__()):
    l = float('inf')
    idx = 0
    for j in range(Z.__len__()):
        b = MathUtil.ED(Z[j],X[i])
        if b

 

import math

class FileUtil:
    @staticmethod
    def openFile(fileName): #按行读取坐标信息
        f = open(fileName)
        X = []
        for line in f:
            data = line.split()
            X.append([int(data[0]), int(data[1])])
        return X


import math

class MathUtil:
    @staticmethod
    def ED(x,y): #计算欧式距离
        sum = 0
        for i in range(x.__len__()):
            sum += (x[i]-y[i])**2
        return math.sqrt(sum)

结果

第1类:
[[0, 0], [0, 1], [1, 0]]
第2类:
[[5, 5], [4, 4], [4, 5], [5, 4]]

你可能感兴趣的:(模式识别练习)