-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bin_deleni.py
86 lines (78 loc) · 2.88 KB
/
Bin_deleni.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
from Kmeans import K_means
from Agl_m_v01 import nactiDataDoPole, vykresliDataPodleLabelu
import matplotlib.pyplot as plt
import numpy as np
def rovnomerneBinDeleni(data, pocetTrid):
'''
metoda pro rovnomerne binarni deleni
:param data: vstupni data pro deleni
:param pocetTrid: pocet trid, do kterych se maji data rozdelit
:return: labely odpovidajici vstupnim datum, stredni hodnoty shluku, ceny jednotlivych trid a celkovou cenu shlukovani
'''
tridy = [] #pole pro zaznam jednotlivych trid
tempdata = np.array(data) #docasna data, se kterymi se pracuje a ktera se dale rozdeluji na shluky
prevTi = np.array(range(len(data))) #pamet pro zaznamenani posleniho Ti
while True:
Ti, J, u, cenaTrid = K_means(tempdata, 2)
tempdata1 = tempdata[Ti[0]]
tridy.append((tempdata1,u[0],cenaTrid[0],prevTi[Ti[0]]))
tempdata2 = tempdata[Ti[1]]
tridy.append((tempdata2, u[1], cenaTrid[1],prevTi[Ti[1]]))
if len(tridy) == pocetTrid:
break
tempdata = tridy.pop(0)
prevTi = tempdata[3]
tempdata = tempdata[0]
labels = np.zeros(len(data), dtype=int)
stredy = []
for i in range(pocetTrid):
labels[tridy[i][3]] = i
stredy.append(tridy[i][1])
J = sum(cenaTrid)
return labels, stredy, cenaTrid, J
def nerovnomerneBinDeleni(data, pocetTrid):
'''
metoda pro nerovnomerne binarni deleni
:param data: vstupni data pro deleni
:param pocetTrid: pocet trid, do kterych se maji data rozdelit
:return: labely odpovidajici vstupnim datum, stredni hodnoty shluku, ceny jednotlivych trid a celkovou cenu shlukovani
'''
tridy = []
tempdata = np.array(data)
prevTi = np.array(range(len(data)))
while True:
Ti, J, u, cenaTrid = K_means(tempdata, 2)
tempdata1 = tempdata[Ti[0]]
tridy.append((tempdata1, u[0], cenaTrid[0], prevTi[Ti[0]]))
tempdata2 = tempdata[Ti[1]]
tridy.append((tempdata2, u[1], cenaTrid[1], prevTi[Ti[1]]))
if len(tridy) == pocetTrid:
break
ceny = np.zeros(len(tridy), dtype=int)
for j in range(len(tridy)):
ceny[j] = tridy[j][2]
tempdata = tridy.pop(np.argmax(ceny))
prevTi = tempdata[3]
tempdata = tempdata[0]
labels = np.zeros(len(data), dtype=int)
stredy = []
for i in range(pocetTrid):
labels[tridy[i][3]] = i
stredy.append(tridy[i][1])
J = sum(cenaTrid)
return labels, stredy, cenaTrid, J
if __name__ =="__main__":
nazev = 'data' # "dataTest2"
X, Y = nactiDataDoPole(nazev)
# X, Y = shuffleAndPickData(X,Y,60)
plt.figure()
plt.scatter(X, Y)
plt.show()
data = np.stack((X, Y), axis=-1)
# vykresliBody(X, Y)
labels, stredy, cenaTrid, J = nerovnomerneBinDeleni(data, 4)
plt.figure()
vykresliDataPodleLabelu(X,Y,labels)
plt.show()
# print(Ti)
#print(J)