-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
297 lines (235 loc) · 10.7 KB
/
app.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import streamlit as st
import numpy as np
import pandas as pd
import scipy
import matplotlib.pyplot as plt
from settings import DATASET_DIR as dataset
from settings import IMAGE_DIR
from scipy import ndimage
from scipy.cluster import hierarchy
from scipy.spatial import distance_matrix
from sklearn.cluster import AgglomerativeClustering
from sklearn.datasets import make_blobs
def plotFinalCluster(colors, cluster_labels, agg_cars):
fig2 = plt.figure(figsize=(16,10))
for color, label in zip(colors, cluster_labels):
subset = agg_cars.loc[(label,),]
for i in subset.index:
plt.text(subset.loc[i][0]+5, subset.loc[i][2], 'type='+str(int(i)) + ', price='+str(int(subset.loc[i][3]))+'k')
plt.scatter(subset.horsepow, subset.mpg, s=subset.price*20, c=color, label='cluster'+str(label))
plt.legend()
plt.title('Clusters')
plt.xlabel('horsepow')
plt.ylabel('mpg')
plt.savefig(IMAGE_DIR+'/'+'plotFinalCluster.jpg')
st.pyplot(fig2)
def plotCluster(pdf, agglom, agg_cars):
import matplotlib.cm as cm
n_clusters = max(agglom.labels_)+1
colors = cm.rainbow(np.linspace(0, 1, n_clusters))
cluster_labels = list(range(0, n_clusters))
# Create a figure of size 6 inches by 4 inches.
fig1 = plt.figure(figsize=(16,14))
for color, label in zip(colors, cluster_labels):
subset = pdf[pdf.cluster_ == label]
for i in subset.index:
plt.text(subset.horsepow[i], subset.mpg[i],str(subset['model'][i]), rotation=25)
plt.scatter(subset.horsepow, subset.mpg, s= subset.price*10, c=color, label='cluster'+str(label),alpha=0.5)
# plt.scatter(subset.horsepow, subset.mpg)
plt.legend()
plt.title('Clusters')
plt.xlabel('horsepow')
plt.ylabel('mpg')
plt.savefig(IMAGE_DIR+'/'+'plotCluster.jpg')
st.pyplot(fig1)
plotFinalCluster(colors, cluster_labels, agg_cars)
def main():
cell_df = pd.read_csv('dataset/cars_clus.csv')
pdf = pd.read_csv('dataset/cars_clus.csv')
num = 6
st.title("Agglomerative clustering Implementation")
st.write("")
# st.image('img/svm1.jpg')
st.sidebar.title("Evaluating different parameters")
st.sidebar.subheader("View dataset")
num = st.sidebar.number_input("Choose number of data to view", 5, 30)
pdf[[ 'sales', 'resale', 'type', 'price', 'engine_s',
'horsepow', 'wheelbas', 'width', 'length', 'curb_wgt', 'fuel_cap',
'mpg', 'lnsales']] = cell_df[['sales', 'resale', 'type', 'price', 'engine_s',
'horsepow', 'wheelbas', 'width', 'length', 'curb_wgt', 'fuel_cap',
'mpg', 'lnsales']].apply(pd.to_numeric, errors='coerce')
pdf = pdf.dropna()
pdf = pdf.reset_index(drop=True)
if st.sidebar.checkbox('Show data'):
st.write(pdf.head(num))
st.write("Shape of data(before cleaning): ", cell_df.shape)
#cleaning dataset by dropping 'null' value
st.write("Shape of data(after cleaning): ", pdf.shape)
#selecting featureset
featureset = pdf[['engine_s', 'horsepow', 'wheelbas', 'width', 'length', 'curb_wgt', 'fuel_cap', 'mpg']]
#Normalization (translates each feature individually such that it is between zero and one)
from sklearn.preprocessing import MinMaxScaler
x = featureset.values #returns a numpy array
min_max_scaler = MinMaxScaler()
feature_mtx = min_max_scaler.fit_transform(x)
#Choosing a library
lib = st.sidebar.selectbox('Library selection',('Choose a library ', 'SciKit Learn', 'SciPy'))
if lib == 'SciPy':
#importing necessary libraries
import pylab
import scipy.cluster.hierarchy
from scipy.cluster.hierarchy import fcluster
# def llf(id):
# return '[%s %s %s]' % (pdf['manufact'][id], pdf['model'][id], int(float(pdf['type'][id])) )
leng = feature_mtx.shape[0]
D = np.zeros([leng,leng])
for i in range(leng):
for j in range(leng):
D[i,j] = scipy.spatial.distance.euclidean(feature_mtx[i], feature_mtx[j])
method = st.sidebar.selectbox('Methods', ('Select a method', 'Single', 'Complete', 'Weighted', 'Average', 'Centroid'))
if method == 'Average':
Z = hierarchy.linkage(D, 'average')
if st.sidebar.button("View Dendrogram"):
fig = pylab.figure(figsize=(10,20))
def llf(id):
return '[%s %s %s]' % (pdf['manufact'][id], pdf['model'][id], int(float(pdf['type'][id])) )
dendro = hierarchy.dendrogram(Z, leaf_label_func=llf, leaf_rotation=0, leaf_font_size =9, orientation = 'right')
plt.savefig(IMAGE_DIR+'/'+'dendrogram.jpg')
st.pyplot(fig)
#selecting criterion
crt = st.sidebar.selectbox('Select criterion', ('Choose a criterion', 'Distance', 'Maxclust'))
#selecting number of clusters
num = st.sidebar.number_input('Choose number of clusters', 5, 10)
if crt == 'Distance':
clusters = fcluster(Z, num, criterion='distance')
elif crt == 'Maxclust':
clusters = fcluster(Z, num, criterion='maxclust')
else:
st.write("Please choose a criterion")
elif method == 'Single':
Z = hierarchy.linkage(D, 'single')
if st.sidebar.button("View Dendrogram"):
fig = pylab.figure(figsize=(10,20))
def llf(id):
return '[%s %s %s]' % (pdf['manufact'][id], pdf['model'][id], int(float(pdf['type'][id])) )
dendro = hierarchy.dendrogram(Z, leaf_label_func=llf, leaf_rotation=0, leaf_font_size =9, orientation = 'right')
plt.savefig(IMAGE_DIR+'/'+'dendrogram.jpg')
st.pyplot(fig)
#selecting criterion
crt = st.sidebar.selectbox('Select criterion', ('Choose a criterion', 'Distance', 'Maxclust'))
#selecting number of clusters
num = st.sidebar.number_input('Choose number of clusters', 5, 10)
if crt == 'Distance':
clusters = fcluster(Z, num, criterion='distance')
elif crt == 'Maxclust':
clusters = fcluster(Z, num, criterion='maxclust')
else:
st.write("Please choose a criterion")
elif method == 'Complete':
Z = hierarchy.linkage(D, 'complete')
if st.sidebar.button("View Dendrogram"):
fig = pylab.figure(figsize=(10,20))
def llf(id):
return '[%s %s %s]' % (pdf['manufact'][id], pdf['model'][id], int(float(pdf['type'][id])) )
dendro = hierarchy.dendrogram(Z, leaf_label_func=llf, leaf_rotation=0, leaf_font_size =9, orientation = 'right')
plt.savefig(IMAGE_DIR+'/'+'dendrogram.jpg')
st.pyplot(fig)
#selecting criterion
crt = st.sidebar.selectbox('Select criterion', ('Choose a criterion', 'Distance', 'Maxclust'))
#selecting number of clusters
num = st.sidebar.number_input('Choose number of clusters', 5, 10)
if crt == 'Distance':
clusters = fcluster(Z, num, criterion='distance')
elif crt == 'Maxclust':
clusters = fcluster(Z, num, criterion='maxclust')
else:
st.write("Please choose a criterion")
elif method == 'Weighted':
Z = hierarchy.linkage(D, 'weighted')
if st.sidebar.button("View Dendrogram"):
fig = pylab.figure(figsize=(10,20))
def llf(id):
return '[%s %s %s]' % (pdf['manufact'][id], pdf['model'][id], int(float(pdf['type'][id])) )
dendro = hierarchy.dendrogram(Z, leaf_label_func=llf, leaf_rotation=0, leaf_font_size =9, orientation = 'right')
plt.savefig(IMAGE_DIR+'/'+'dendrogram.jpg')
st.pyplot(fig)
#selecting criterion
crt = st.sidebar.selectbox('Select criterion', ('Choose a criterion', 'Distance', 'Maxclust'))
#selecting number of clusters
num = st.sidebar.number_input('Choose number of clusters', 5, 10)
if crt == 'Distance':
clusters = fcluster(Z, num, criterion='distance')
elif crt == 'Maxclust':
clusters = fcluster(Z, num, criterion='maxclust')
else:
st.write("Please choose a criterion")
elif method == 'Centroid':
Z = hierarchy.linkage(D, 'centroid')
if st.sidebar.button("View Dendrogram"):
fig = pylab.figure(figsize=(10,20))
def llf(id):
return '[%s %s %s]' % (pdf['manufact'][id], pdf['model'][id], int(float(pdf['type'][id])) )
dendro = hierarchy.dendrogram(Z, leaf_label_func=llf, leaf_rotation=0, leaf_font_size =9, orientation = 'right')
plt.savefig(IMAGE_DIR+'/'+'dendrogram.jpg')
st.pyplot(fig)
#selecting criterion
crt = st.sidebar.selectbox('Select criterion', ('Choose a criterion', 'Distance', 'Maxclust'))
#selecting number of clusters
num = st.sidebar.number_input('Choose number of clusters', 5, 10)
if crt == 'Distance':
clusters = fcluster(Z, num, criterion='distance')
elif crt == 'Maxclust':
clusters = fcluster(Z, num, criterion='maxclust')
else:
st.write("Please choose a criterion")
elif lib == 'SciKit Learn':
dist_matrix = distance_matrix(feature_mtx,feature_mtx)
lnk = st.sidebar.selectbox('Select linkage', ('Choose linkage', 'Ward', 'Complete', 'Average', 'Single'))
if lnk == 'Ward':
k = st.sidebar.number_input("Choose number of clusters", 5, 10)
agglom = AgglomerativeClustering(n_clusters = k, linkage = 'ward')
agglom.fit(feature_mtx)
pdf['cluster_'] = agglom.labels_
st.write("Number of cases in each group")
st.write(pdf.groupby(['cluster_','type'])['cluster_'].count())
st.write("Characterstics of each cluster")
agg_cars = pdf.groupby(['cluster_','type'])['horsepow','engine_s','mpg','price'].mean()
st.table(agg_cars)
plotCluster(pdf, agglom, agg_cars) #for plotting
elif lnk == 'Complete':
k = st.sidebar.number_input("Choose number of clusters", 5, 10)
agglom = AgglomerativeClustering(n_clusters = k, linkage = 'complete')
agglom.fit(feature_mtx)
pdf['cluster_'] = agglom.labels_
st.write("Number of cases in each group")
st.write(pdf.groupby(['cluster_','type'])['cluster_'].count())
st.write("Characterstics of each cluster")
agg_cars = pdf.groupby(['cluster_','type'])['horsepow','engine_s','mpg','price'].mean()
st.table(agg_cars)
plotCluster(pdf, agglom, agg_cars) #for plotting
elif lnk == 'Average':
k = st.sidebar.number_input("Choose number of clusters", 5, 10)
agglom = AgglomerativeClustering(n_clusters = k, linkage = 'average')
agglom.fit(feature_mtx)
pdf['cluster_'] = agglom.labels_
st.write("Number of cases in each group")
st.write(pdf.groupby(['cluster_','type'])['cluster_'].count())
st.write("Characterstics of each cluster")
agg_cars = pdf.groupby(['cluster_','type'])['horsepow','engine_s','mpg','price'].mean()
st.table(agg_cars)
plotCluster(pdf, agglom, agg_cars) #for plotting
elif lnk == 'Single':
k = st.sidebar.number_input("Choose number of clusters", 5, 10)
agglom = AgglomerativeClustering(n_clusters = k, linkage = 'single')
agglom.fit(feature_mtx)
pdf['cluster_'] = agglom.labels_
st.write("Number of cases in each group")
st.write(pdf.groupby(['cluster_','type'])['cluster_'].count())
st.write("Characterstics of each cluster")
agg_cars = pdf.groupby(['cluster_','type'])['horsepow','engine_s','mpg','price'].mean()
st.table(agg_cars)
plotCluster(pdf, agglom, agg_cars) #for plotting
else:
st.write("Please select one of the libraries")
if __name__=='__main__':
main()