-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIRIS_data.py
283 lines (211 loc) · 7.9 KB
/
IRIS_data.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
# coding: utf-8
# Ali Nehrani
import numpy as np
import pandas as pd
from sklearn import (datasets, metrics, cluster, feature_selection, manifold,
decomposition, preprocessing, mixture)
from matplotlib import pyplot as plt
from IPython.core.debugger import Tracer
from sklearn.metrics import normalized_mutual_info_score
import pdb
# Load the irsi dataset to variable
iris=datasets.load_iris()
resultDataFrame = pd.DataFrame(columns=['Cluster', 'Metrics Accuracy', 'NMIS Accuracy'])
df = pd.DataFrame(iris.data)
df.columns=iris.feature_names
df['target']=iris.target
# iris data
X=iris.data
print('iris data:', X)
# iris features
print('iris features:', iris.feature_names)
# iris target
Y=iris.target
print('iris target:', Y)
# printing target names
print('iris features:', iris.target_names)
# printing the shapes (data and target)
print('iris data shape', iris.data.shape)
print('iris target shape', iris.target.shape)
# --------------------------------Feautre Selection----------------------------
# --------------- t-SNE to feature selection
tsne = manifold.TSNE(n_components=2, init='pca')
X_tsn = tsne.fit_transform(X)
print('tsne shape of the data', X_tsn.shape)
# --------------------------Clustering Methods --------------------------------
# 1- Kmeans Clustering --------------------------------------------------------
KMeans_x = cluster.KMeans(n_clusters=len(iris.target_names))
# fitting
KMeans_x.fit(X_tsn,Y)
#prediction
Y_pred = KMeans_x.predict(X_tsn)
# data frame
df = pd.DataFrame(X_tsn)
df['target'] = Y
df['Y_pred'] = Y_pred
# Visualize Clustered data VS Feature selected data from raw
plt.figure(figsize=(9, 8))
plt.subplot(1, 2, 1)
plt.title("Feature Selection of raw data")
ind = Y==0
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='red',marker='*')
ind = Y==1
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='cyan',marker='*')
ind = Y==2
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='black',marker='*')
plt.subplot(1, 2, 2)
plt.title("Kmeans Clustered Data")
ind = KMeans_x.labels_==0
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='red',marker='o')
ind = KMeans_x.labels_==1
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='blue',marker='o')
ind = KMeans_x.labels_==2
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='yellow',marker='o')
plt.show()
# printing the erorrs
matrix_ac_score = metrics.accuracy_score(Y, Y_pred)
NMIS_km = normalized_mutual_info_score(Y, Y_pred)
#print('Matrix accuracy score for Kmeans Clustering is: {0:.2f}'.format(matrix_ac_score))
print('NMI score for Kmeans Clustering is: {0:.2f}'.format(NMIS_km))
#pdb.set_trace()
#
resultDataFrame.loc[0]=['Kmeans clustering', NMIS_km, matrix_ac_score]
print('Data frame Result for Kmeans clustering', resultDataFrame.loc[0])
#
# 2- Gaussian Mixture clustering ----------------------------------------------
#
gmmcl = mixture.GMM(n_components=3, covariance_type='full')
gmmcl.fit(X_tsn,Y)
Y_pred = gmmcl.predict(X_tsn)
# Visualize Clustered data VS Feature selected data from raw
plt.figure(figsize=(9, 8))
plt.subplot(1, 2, 1)
plt.title("Feature Selection of raw data")
ind = Y==0
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='red',marker='*')
ind = Y==1
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='green',marker='*')
ind = Y==2
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='cyan',marker='*')
plt.subplot(1, 2, 2)
plt.title("Gaussian Mixture Clustered Data")
ind = Y_pred==0
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='red',marker='<')
ind = Y_pred==1
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='green',marker='<')
ind = Y_pred==2
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='cyan',marker='<')
plt.show()
# printing the erorrs
gmmcl_mat_acc_acr = metrics.accuracy_score(Y, Y_pred)
NMIS_gm = normalized_mutual_info_score(Y, Y_pred)
#print('Matrix accuracy score for Gaussian Mixture Clustering is: {0:.2f}'.format(matrix_ac_score))
print('NMI score for Gaussian Mixture Clustering is: {0:.2f}'.format(NMIS_gm))
#
resultDataFrame.loc[1]=['Gaussian Mixture clustering', NMIS_gm, gmmcl_mat_acc_acr]
print('Data frame Result for Gaussian Mixture clustering', resultDataFrame.loc[1])
#
# 3- AffinityPropagation Clustering---------------------------------------------
#
affpr = cluster.AffinityPropagation()
affpr.fit(X_tsn,Y)
# prediction based on features
Y_pred = affpr.predict(X_tsn)
# Visualize Clustered data VS Feature selected data from raw
plt.figure(figsize=(9, 8))
plt.subplot(1, 2, 1)
plt.title("Feature Selection of raw data")
ind = Y==0
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='red',marker='*')
ind = Y==1
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='green',marker='*')
ind = Y==2
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='cyan',marker='*')
plt.subplot(1, 2, 2)
plt.title("Affinity Propagation Clustered Data")
ind = Y_pred==0
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='red',marker='+')
ind = Y_pred==1
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='green',marker='+')
ind = Y_pred==2
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='cyan',marker='+')
plt.show()
#
# printing the erorrs
affpr_mat_acc_scr = metrics.accuracy_score(Y, Y_pred)
NMIS_ap = normalized_mutual_info_score(Y, Y_pred)
#print('Matrix accuracy score for Affinity Propagation Clustering is: {0:.2f}'.format(matrix_ac_score))
print('NMI score for Affinity Propagation Clustering is: {0:.2f}'.format(NMIS_ap))
#
resultDataFrame.loc[2]=['Affinity Propagation clustering', NMIS_ap, affpr_mat_acc_scr]
print('Data frame Result for Affinity Propagation clustering', resultDataFrame.loc[2])
#
# 4- Birch clustering -------------------------------------------------------
#
birch = cluster.Birch()
birch.fit(X_tsn, Y)
Y_pred = birch.predict(X_tsn)
# Visualize Clustered data VS original Feature selected data
plt.figure(figsize=(9, 8))
plt.subplot(1, 2, 1)
plt.title("Feature Selection of raw data")
ind = Y==0
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='red',marker='*')
ind = Y==1
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='green',marker='*')
ind = Y==2
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='cyan',marker='*')
plt.subplot(1, 2, 2)
plt.title("Birch Clustered Data")
ind = Y_pred==0
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='red',marker='^')
ind = Y_pred==1
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='green',marker='^')
ind = Y_pred==2
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='cyan',marker='^')
plt.show()
# printing the erorrs
brch_mat_acc_acr = metrics.accuracy_score(Y, Y_pred)
NMIS_br = normalized_mutual_info_score(Y, Y_pred)
#print('Matrix accuracy score for Birch Clustering is: {0:.2f}'.format(matrix_ac_score))
print('NMI score for Birch Clustering is: {0:.2f}'.format(NMIS_br))
#
resultDataFrame.loc[3]=['Birch clustering', NMIS_br, brch_mat_acc_acr]
print('Data frame Result for Birch clustering', resultDataFrame.loc[3])
#
# 5- Agglomerative Clustering clustering ---------------------------------------
#
aggcl = cluster.AgglomerativeClustering()
aggcl.fit(X_tsn,Y)
#
Y_pred = aggcl.labels_
#
# Visualize Clustered data VS Feature selected data from raw
plt.figure(figsize=(9, 8))
plt.subplot(1, 2, 1)
plt.title("Feature Selection of raw data")
ind = Y==0
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='red',marker='*')
ind = Y==1
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='green',marker='*')
ind = Y==2
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='cyan',marker='*')
plt.subplot(1, 2, 2)
plt.title("Agglomorative Clustered Data")
ind = Y_pred==0
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='red',marker='>')
ind = Y_pred==1
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='green',marker='>')
ind = Y_pred==2
plt.scatter(X_tsn[ind,0],X_tsn[ind,1],c='cyan',marker='>')
plt.show()
# printing the erorrs
aggcl_mat_acc_acr = metrics.accuracy_score(Y, Y_pred)
NMIS_ag = normalized_mutual_info_score(Y, Y_pred)
#print('Matrix accuracy score for Agglomerative Clustering is: {0:.2f}'.format(matrix_ac_score))
print('NMI score for Agglomerative Clustering is: {0:.2f}'.format(NMIS_ag))
#
resultDataFrame.loc[4]=['Agglomerative clustering', NMIS_ag, aggcl_mat_acc_acr]
print('Data frame Result for Agglomerative clustering', resultDataFrame.loc[4])
# resulted data frame for all clusterings
print('Data frame Result for 5 clustering', resultDataFrame)