-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestCLOPE.py
151 lines (129 loc) · 5.09 KB
/
testCLOPE.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
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
# -*- coding: utf-8 -*-
import CLOPE
import getBins
import Utils
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pprint import pprint
def get_count_clusters(data, clope):
'''
Вывод распределения по оценочному признаку
Input parametres:
data -- исходный список транзакций
clope -- объект CLOPE (рез-тат кластеризации)
'''
answ = []
# clope.transaction -- cловарь <№транзакции/№кластера>
for item in range(0, clope.max_cluster_number):
# генератор образа распределения
answ.append({'aa': 0,
'ba': 0,
'ca': 0,
'da': 0,
'ea': 0,
'fa': 0,
'ga': 0,
'ha': 0})
# print('cluster appended')
# print('max_cluster_number:', clope.max_cluster_number)
print('Final clusters:')
pprint(clope.clusters)
# itemTransact = № транзакции
for itemTransact in clope.transaction:
# = № кластера
cluster = clope.transaction[itemTransact]
# data[itemTransact] - исходная транзакция
# data[itemTransact][-1] = значение ценового признака
answ[cluster][data[itemTransact][-1]] += 1
return pd.DataFrame(answ)
def printCLOPER(transacts, seed, noiseLimit):
'''
Отрисовка вариационных кривых для
различных параметров отталкивания
Input parametres:
transacts -- подготовленный набор транзакций
'''
# noiseLimit = 0
# seed = 0
plt.figure(figsize=(20, 10))
plt.subplot()
# назначение функции np.hstack?
# linspace = np.hstack((np.arange(1.2, 2.6, 0.2),
# np.arange(2.6, 3.5, 0.1),
# np.arange(3.5, 5, 0.5)))
linspace = np.arange(1.6, 3.5, 0.1)
linspace = [round(i, 3) for i in linspace]
for r in linspace:
clope = CLOPE.CLOPE(print_step=0,
is_save_history=True,
random_seed=seed)
clope.init_clusters(transacts, r, noiseLimit)
df = get_count_clusters(transacts, clope)
df['sum'] = df['aa7'] + df['ba7'] + df['ca7'] + df['da7'] + df['ea7'] + df['fa7'] + df['ga7'] + df['ha7']
df = df.sort_values(by='sum')
plt.plot(list(df['sum']))
# plt.title('Вариационные ряды для размера кластера при различных r')
plt.xlabel('Порядковый номер отсортированных по размеру кластеров')
plt.ylabel('Размер кластера')
plt.legend(linspace)
plt.show()
if __name__ == '__main__':
preparedData = getBins.prepareData(
Utils.readExcelData(
'data/CarData1Lab.xlsx'
)
)
# print('Here is prepared data:')
# pprint(preparedData)
# Перемешивание данных
seed = 0
# seed = 40
np.random.seed(seed)
np.random.shuffle(preparedData)
transacts = {}
for i in range(0, len(preparedData)):
for j in range(0, len(preparedData[i])):
# для создания списка признаков в else
if j != 0:
# j=8 - 9-й эл-т, price, пропускаем
if j != 8:
if preparedData[i][j] != '?':
transacts[i][j] = preparedData[i][j] + str(j)
else:
print('miss object')
else:
pass
else:
transacts[i] = [''] * (len(preparedData[i]) - 1)
transacts[i][j] = preparedData[i][j] + str(j)
# print('Transacts:')
# pprint(transacts)
clope = CLOPE.CLOPE(print_step=1000,
is_save_history=True,
random_seed=seed)
# Начальные данные
repulsion = 1.5
noiseLimit = 3
# Инициализируем алгоритм
clope.init_clusters(transacts, repulsion, noiseLimit)
clope.print_history_count(repulsion, seed)
# Тест:
# print('max_cluster_number', clope.max_cluster_number, '(инициализация)')
# print('Clusters dict:')
# pprint(clope.clusters)
# print('Noise clusters dict:')
# pprint(clope.noise_clusters)
# Итерируемся
while clope.next_step(transacts, repulsion, noiseLimit) > 0:
clope.print_history_count(repulsion, seed)
# Тест:
# print('max_cluster_number', clope.max_cluster_number, '(итерация)')
# print('Clusters dict:')
# pprint(clope.clusters)
# print('Noise clusters dict:')
# pprint(clope.noise_clusters)
# pass
clope.print_history_count(repulsion, seed)
# printCLOPER(transacts, seed, noiseLimit)
print(get_count_clusters(preparedData, clope))