-
Notifications
You must be signed in to change notification settings - Fork 0
/
Max-Min.py
92 lines (80 loc) · 3.04 KB
/
Max-Min.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*-coding: utf-8 -*-
"""
@Project: 聚类算法:最大值-最小值距离
@Author : Alex
@Date : 2019-11-20 11:24:30
"""
import math
import numpy as np
def calcuDistance(data1, data2):
'''
计算两个模式样本之间的欧式距离
:param data1:
:param data2:
:return:
'''
distance = 0
for i in range(len(data1)):
distance = pow((data1[i] - data2[i]), 2)
return math.sqrt(distance)
# 计算绝对值距离
def calcuDistance_abs(data1,data2):
dist_abs = 0
for i in range(len(data1)):
dist_abs = abs(data1[i] - data2[i])
return dist_abs # 计算绝对值距离
def maxmin_distance_cluster(data, Theta):
'''
:param data: 输入样本数据,每行一个特征
:param Theta:阈值,一般设置为0.5,阈值越小聚类中心越多
:return:样本分类,聚类中心
'''
maxDistance = 0
start = 0 # 初始选一个中心点
index = start # 相当于指针指示新中心点的位置
k = 0 # 中心点计数,也即是类别
dataNum = len(data) # 样本数
distance = np.zeros((dataNum,))
minDistance = np.zeros((dataNum,))
classes = np.zeros((dataNum,))
centerIndex = [index]
# 初始选择第一个为聚类中心点
ptrCen = data[0]
# 寻找第二个聚类中心,即与第一个聚类中心最大距离的样本点
for i in range(dataNum):
ptr1 = data[i]
# d = calcuDistance(ptr1, ptrCen) # 欧式距离
d = calcuDistance_abs(ptr1,ptrCen) # 绝对值距离
distance[i] = d
classes[i] = k 1
if (maxDistance < d):
maxDistance = d
index = i # 与第一个聚类中心距离最大的样本
print("与第一个聚类中心的距离", distance[i]) # 打印欧式距离
minDistance = distance.copy()
maxVal = maxDistance
# print("maxDistance:" maxDistance)
while maxVal > (maxDistance * Theta):
k = k 1
centerIndex = [index] # 新的聚类中心
for i in range(dataNum):
ptr1 = data[i]
ptrCen = data[centerIndex[k]]
d = calcuDistance(ptr1, ptrCen)
distance[i] = d
# 按照当前最近临方式分类,哪个近就分哪个类别
if minDistance[i] > distance[i]:
minDistance[i] = distance[i]
classes[i] = k 1
# print(minDistance)
# 寻找minDistance中的最大距离,若maxVal > (maxDistance * Theta),则说明存在下一个聚类中心
index = np.argmax(minDistance)
print("最小里的最大值:,索引为:", minDistance[i], index)
maxVal = minDistance[index]
return classes, centerIndex
if __name__ == '__main__':
data = [[0, 0, 0, 0], [30, 8, 15, 20], [92, 92, 94, 93], [91, 91, 87, 88], [95, 93, 89, 98], [64, 68, 60, 70], [46, 33, 37, 78], [95, 93, 89, 98], [94, 88, 96, 88], [66, 63, 64, 78]]
Theta = 0.5
classes, centerIndex = maxmin_distance_cluster(data, Theta)
print(classes)
print(centerIndex)