/
main.py
234 lines (189 loc) · 7.52 KB
/
main.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from dataManager import getData, getSBS
from algorithms.IG import IG
from algorithms.SPG import SPG
from algorithms.Apriori import Apriori
from algorithms.UBP import UBP
from clustering import KMeans, AgglomerativeHierarchical, AffinityPropagation, MeanShift
from sampling import sampling
from dimension_reduction import SOM
import numpy as np
import time
import csv
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"--do_reduce",
action="store_true",
help="Reduce dimensions using Self Organisig Maps before clustering.",
)
parser.add_argument(
"--read_data",
action="store_true",
default=True,
help="Read Data",
)
parser.add_argument(
"--create_data",
action="store_true",
help="Create Data",
)
parser.add_argument(
"--read_sbs",
action="store_true",
default=True,
help="Read SBS Table",
)
parser.add_argument(
"--create_sbs",
action="store_true",
help="Create New SBS table",
)
args = parser.parse_args()
# ====================================FILE HANDLING======================================
# Change the fileLocation to your car.data folder
dataLocation = "./data_process/car.csv"
sbsLocation = "./data_process/car_SBS.csv"
datasetLocation = "./dataset/car/car.data"
# get data
if args.create_data:
data = getData(_createData=True, fileLocation=datasetLocation)
elif args.read_data:
data = getData(fileLocation=dataLocation)
# get SBS
if args.create_sbs:
SBS = getSBS(_createSBS=True, fileLocation=datasetLocation, data=data)
elif args.read_sbs:
SBS = getSBS(fileLocation=sbsLocation)
# Reducing Dimensionality
if args.do_reduce:
print('------Reducing Data------')
data = SOM.SelfOrganisedMap(data)
data = np.array(data)
# =======================================GLOBALS=========================================
rows=len(SBS)
cols=len(SBS[0])
# Creating IDs of Customers, Candidate Products and Existing Products.
C = np.arange(0, cols)
EP = np.arange(0, (rows*30)//100)
CP = np.arange((rows*30)//100, rows)
# Number of products to select
k = 5
# =================================BEFORE CLUSTERING=====================================
try:
print('BEFORE CLUSTERING:')
# IG
timeTaken = int(round(time.time() * 1000))
selectedProdsIG, productScore = IG(k, C, SBS, EP, CP)
timeTaken = int(round(time.time() * 1000)) - timeTaken
print("Incremental Based Greedy Algorithm : \n", selectedProdsIG)
print("Time taken in millis:", timeTaken)
# print("Product score:", productScore)
# SPGA
timeTaken = int(round(time.time() * 1000))
selectedProdsSPG, productScore = SPG(k, C, SBS, EP, CP)
timeTaken = int(round(time.time() * 1000)) - timeTaken
print("Single Product Based Greedy Algorithm : \n", selectedProdsSPG)
print("Time taken in millis:", timeTaken)
# print("Product score:", productScore)
except:
print("Error before clustering")
# =====================================SAMPLING==========================================
sampledEP, sampledCP = sampling(EP, CP)
bestSampledProds, productScore = SPG(k*2, C, SBS, sampledEP, sampledCP)
# print("Best sampled products:", bestSampledProds)
# =================================AFFINITY PROPAGATION==================================
try:
timeTaken = int(round(time.time() * 1000))
EP_New, CP_New, n_clusters = AffinityPropagation.Affinity_Propagation(data, SBS, C, EP, CP, bestSampledProds)
timeTaken = int(round(time.time() * 1000)) - timeTaken
print('Affinity Propagation time:', timeTaken)
print('AFTER AFFINITY PROPAGATION:')
# IG
selectedProdsIG, productScore = IG(k, C, SBS, EP_New, CP_New)
timeTaken = int(round(time.time() * 1000)) - timeTaken
print("Incremental Based Greedy Algorithm : \n", selectedProdsIG)
print("Time taken in millis:", timeTaken)
# print("Product score:", productScore)
# SPG
timeTaken = int(round(time.time() * 1000))
selectedProdsSPG, productScore = SPG(k, C, SBS, EP_New, CP_New)
timeTaken = int(round(time.time() * 1000)) - timeTaken
print("Single Product Based Greedy Algorithm : \n", selectedProdsSPG)
print("Time taken in millis:", timeTaken)
# print("Product score:", productScore)
except:
print("Error in Affinity Propagation")
# =================================MEAN SHIFT CLUSTERING=================================
try:
timeTaken = int(round(time.time() * 1000))
EP_New, CP_New = MeanShift.Mean_Shift(data, SBS, C, EP, CP, bestSampledProds)
timeTaken = int(round(time.time() * 1000)) - timeTaken
print('Mean Shift time:', timeTaken)
print('AFTER MEAN SHIFT CLUSTERING:')
# IG
timeTaken = int(round(time.time() * 1000))
selectedProdsIG, productScore = IG(k, C, SBS, EP_New, CP_New)
timeTaken = int(round(time.time() * 1000)) - timeTaken
print("Incremental Based Greedy Algorithm : \n", selectedProdsIG)
print("Time taken in millis:", timeTaken)
# print("Product score:", productScore)
# SPG
timeTaken = int(round(time.time() * 1000))
selectedProdsSPG, productScore = SPG(k, C, SBS, EP_New, CP_New)
timeTaken = int(round(time.time() * 1000)) - timeTaken
print("Single Product Based Greedy Algorithm : \n", selectedProdsSPG)
print("Time taken in millis:", timeTaken)
# print("Product score:", productScore)
except:
print("Error in Mean Shift")
# =================================KMEANS CLUSTERING=====================================
try:
timeTaken = int(round(time.time() * 1000))
EP_New, CP_New = KMeans.K_Means(data, SBS, C, EP, CP, bestSampledProds, 10)
timeTaken = int(round(time.time() * 1000)) - timeTaken
print('Kmeans time:', timeTaken)
print('AFTER KMEANS CLUSTERING:')
# IG
timeTaken = int(round(time.time() * 1000))
selectedProdsIG, productScore = IG(k, C, SBS, EP_New, CP_New)
timeTaken = int(round(time.time() * 1000)) - timeTaken
print("Incremental Based Greedy Algorithm : \n", selectedProdsIG)
print("Time taken in millis:", timeTaken)
# print("Product score:", productScore)
# SPG
timeTaken = int(round(time.time() * 1000))
selectedProdsSPG, productScore = SPG(k, C, SBS, EP_New, CP_New)
timeTaken = int(round(time.time() * 1000)) - timeTaken
print("Single Product Based Greedy Algorithm : \n", selectedProdsSPG)
print("Time taken in millis:", timeTaken)
# print("Product score:", productScore)
except:
print("Error in K-Means")
# ================================AGGLOMERATIVE CLUSTERING===============================
try:
timeTaken = int(round(time.time() * 1000))
EP_New, CP_New = AgglomerativeHierarchical.Agglomerative_Clustering(data, SBS, C, EP, CP, bestSampledProds, 10)
timeTaken = int(round(time.time() * 1000)) - timeTaken
print('Agglomerative time:', timeTaken)
print('AFTER AGGLOMERATIVE CLUSTERING:')
# IG
timeTaken = int(round(time.time() * 1000))
selectedProdsIG, productScore = IG(k, C, SBS, EP_New, CP_New)
timeTaken = int(round(time.time() * 1000)) - timeTaken
print("Incremental Based Greedy Algorithm : \n", selectedProdsIG)
print("Time taken in millis:", timeTaken)
# print("Product score:", productScore)
# SPG
timeTaken = int(round(time.time() * 1000))
selectedProdsSPG, productScore = SPG(k, C, SBS, EP_New, CP_New)
timeTaken = int(round(time.time() * 1000)) - timeTaken
print("Single Product Based Greedy Algorithm : \n", selectedProdsSPG)
print("Time taken in millis:", timeTaken)
# print("Product score:", productScore)
except:
print("Error in Agglomerative clustering")
# ===================================PRINT OUTPUT TO FILE================================
with open('BestCandidates.csv', 'w') as csvfile:
writer = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
for prod in selectedProdsSPG:
writer.writerow([prod])