This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Session8.py
130 lines (94 loc) · 3.35 KB
/
Session8.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
from time import time
import numpy as np
from matplotlib import pyplot as plt
import pandas as pd
from sklearn import manifold, datasets
from sklearn.cluster import AgglomerativeClustering
missing_value=['?']
data= pd.read_csv("wiki4HE.csv", na_values=missing_value)
print(data.isnull().sum())
data.dropna(inplace=True)
print(data.shape)
X=data.values
y=X[:,2]
X_edit=np.delete(X,[2],1)
# from scipy.cluster.hierarchy import dendrogram, linkage
# from matplotlib import pyplot as plt
#
# linked = linkage(X_edit, 'ward')
#
# # labelList = range(1, 11)
#
# plt.figure(figsize=(10, 7))
# plt.title('Hierarchical Clustering Dendrogram (truncated)')
# dendrogram(linked,
# orientation='top',
# labels=y,
# distance_sort='descending',
# show_leaf_counts=True)
# plt.show()
########Silhouette Coefficient
from sklearn import metrics
from sklearn.cluster import KMeans
kmeans_model = KMeans(n_clusters=3, random_state=1).fit(X)
labels = kmeans_model.labels_
KMeans_Sil = metrics.silhouette_score(X, labels, metric='euclidean')
print(KMeans_Sil)
linkage = "single"
n_clusters = 4
model = AgglomerativeClustering(linkage=linkage, n_clusters=n_clusters).fit(X)
labels = model.labels_
Agglo_Sil = metrics.silhouette_score(X, labels, metric='euclidean')
print(Agglo_Sil)
################Visualisation for Agglomerative Clustering
n_samples, n_features = X_edit.shape
digits = datasets.load_digits(n_class=10)
X = digits.data
y = digits.target
# np.random.seed(0)
# def nudge_images(X, y):
# # Having a larger dataset shows more clearly the behavior of the
# # methods, but we multiply the size of the dataset only by 2, as the
# # cost of the hierarchical clustering methods are strongly
# # super-linear in n_samples
# shift = lambda x: ndimage.shift(x.reshape((8, 8)),
# .3 * np.random.normal(size=2),
# mode='constant',
# ).ravel()
# X = np.concatenate([X, np.apply_along_axis(shift, 1, X)])
# Y = np.concatenate([y, y], axis=0)
# return X, Y
#
#
# X_edit, y = nudge_images(X_edit, y)
#----------------------------------------------------------------------
###### Visualize the clustering
def plot_clustering(X_red, labels, title=None):
x_min, x_max = np.min(X_red, axis=0), np.max(X_red, axis=0)
X_red = (X_red - x_min) / (x_max - x_min)
plt.figure(figsize=(6, 4))
for i in range(X_red.shape[0]):
plt.text(X_red[i, 0], X_red[i, 1], str(y[i]),
color=plt.cm.nipy_spectral(labels[i] / 10.),
fontdict={'weight': 'bold', 'size': 9})
plt.xticks([])
plt.yticks([])
if title is not None:
plt.title(title, size=17)
plt.axis('off')
plt.tight_layout(rect=[0, 0.03, 1, 0.95])
#----------------------------------------------------------------------
# 2D embedding of dataset
print("Computing embedding")
X_red = manifold.SpectralEmbedding(n_components=2).fit_transform(X_edit)
print("Done.")
for linkage in ('ward', 'average', 'complete', 'single'):
clustering = AgglomerativeClustering(linkage=linkage, n_clusters=6)
t0 = time()
clustering.fit(X)
print("%s :\t%.2fs" % (linkage, time() - t0))
labels = clustering.labels_
Agglo_Sil = metrics.silhouette_score(X, labels, metric='euclidean')
print(Agglo_Sil)
plt.show()
1+1