-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanopy.py
More file actions
167 lines (111 loc) · 3.89 KB
/
Copy pathCanopy.py
File metadata and controls
167 lines (111 loc) · 3.89 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# -*- coding:utf-8 -*-
# author Jinglebo
# date 2018/11/23
import math
import numpy as np
import matplotlib.pyplot as plt
import sklearn.datasets as ds
import matplotlib.colors
from datetime import datetime
import sklearn.preprocessing
def draw_scatter(data):
new_data_0 = data[data[:,2] == 0]
new_data_1 = data[data[:,2] == 1]
new_data_2 = data[data[:,2] == 2]
new_data_3 = data[data[:,2] == 3]
plt.scatter(new_data_0[:,0], new_data_0[:,1], color = 'red')
plt.scatter(new_data_1[:,0], new_data_1[:,1], color = 'blue')
plt.scatter(new_data_2[:,0], new_data_2[:,1], color = 'green')
plt.scatter(new_data_3[:,0], new_data_3[:,1], color = 'yellow')
def calc_distance(vec1, vec2):
return math.sqrt(((vec1 - vec2)**2).sum())
def draw_canopy(data, cluster, center, far):
colors = [
'brown', 'green', 'blue', 'y', 'r', 'tan', 'dodgerblue', 'deeppink',
'orangered', 'peru', 'blue', 'y', 'r', 'gold', 'dimgray', 'darkorange',
'peru', 'blue', 'y', 'r', 'cyan', 'tan', 'orchid', 'peru', 'blue', 'y',
'r', 'sienna'
]
markers = [
'*', 'h', 'H', '+', 'o', '1', '2', '3', ',', 'v', 'H', '+', '1', '2',
'^', '<', '>', '.', '4', 'H', '+', '1', '2', 's', 'p', 'x', 'D', 'd',
'|', '_'
]
n = len(data)
#draw center
centers = len(center)
#for all sample
for idx in range(n):
center_num = len(cluster[idx])
values = []
cols = []
for center_idx in range(center_num):
values.append( 100 / center_num)
cols.append(colors[center.index(cluster[idx][center_idx])])
plt.pie(values,colors=cols, center= (data[idx, 0], data[idx, 1]), radius= far * len(center) * 1.5/ len(cluster))
plt.axis('equal')
def calc_candidate(cluster):
cnt = len(cluster)
for i in range(cnt):
if len(cluster[i]) > 0:
cnt = cnt - 1
return cnt
def get_new_canopy(cluster, ran_idx):
cnt = len(cluster)
for i in range(cnt):
if len(cluster[i]) == 0:
if ran_idx > 1:
ran_idx = ran_idx - 1
else:
return i
return -1
def generate_canopy(data, t_near, t_far):
cnt = len(data)
cluster = [[] for i in range(cnt)]
center = []
candidate_sample = cnt
while candidate_sample > 0:
ran_idx = np.random.randint(candidate_sample)
data_idx = get_new_canopy(cluster, ran_idx)
center.append(data_idx)
if data_idx == -1:
break
for j in range(cnt):
distance = calc_distance(data[data_idx], data[j])
if distance < t_near:
cluster[j] = [data_idx]
elif distance < t_far:
cluster[j].append(data_idx)
candidate_sample = calc_candidate(cluster)
return cluster, center
def main():
#generate the sample data
N = 100
centers = 4
data, y = ds.make_blobs(N, n_features=2, centers=centers, random_state=2)
new_data = (np.concatenate((data, np.reshape(y, (N,1))), axis = 1))
#draw the original
plt.subplot(121)
draw_scatter(new_data)
x_min = np.min(data[:,0])
x_max = np.max(data[:,0])
y_min = np.min(data[:,1])
y_max = np.max(data[:,1])
t_near = np.max([np.abs(x_max-x_min), np.abs(y_max - y_min)]) / (2 * centers)
t_far = np.max([np.abs(x_max-x_min), np.abs(y_max - y_min)]) / centers
cluster , center = generate_canopy(data, t_near, t_far)
plt.subplot(122)
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
my_x_ticks = np.arange(x_min, x_max, 0.5)
my_y_ticks = np.arange(y_min, y_max, 0.5)
plt.xticks(my_x_ticks)
plt.yticks(my_y_ticks)
draw_canopy(data, cluster, center, t_far)
plt.show()
if __name__ == '__main__':
t_start = datetime.now()
main()
t_end = datetime.now()
usedtime = t_start - t_end
print('[%s]' % usedtime)