-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkMeans.py
162 lines (133 loc) · 5.49 KB
/
kMeans.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
# %%
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA
from sklearn.metrics import calinski_harabasz_score
# %%
# Generates the Clusters
def generate_clusters(data, means):
clusters = [[] for _ in range(means.shape[1])] #generating list of clusters
for i in range(data.shape[0]):
distances = np.linalg.norm(data[i] - means.T, axis=1)
cluster_index = np.argmin(distances) #cluster_index is the minimum distance cluster, assigned to the ith gene
clusters[cluster_index].append(i) #ith gene added to the cluster with index cluster_index
return clusters
# Updates the clusters and determines if convergence has or has not reached convergence
def update_clusters(data, clusters, means):
converged = True #setting a converge variable to check whether the means change after updating
for i in range(len(clusters)):
cluster = clusters[i]
if len(cluster) == 0: #checking if the cluster is empty
continue
new_mean = np.mean(data[cluster], axis=0) #new centroid is the means of data points of old cluster
if not np.array_equal(new_mean, means[:, i]): #if means change, the algorithm has not converged
converged = False
means[:, i] = new_mean
return converged, means
def k_means(data,means, k, max_iterations):
iterations = 0
obj_values = []
while iterations < max_iterations: #continue clustering until max iterations reached
clusters = generate_clusters(data, means)
obj_value = 0
for i in range(len(clusters)): #ranging over the clusters
cluster = clusters[i]
for j in range(len(cluster)): #ranging over the number of elements in ith cluster
data_point = data[cluster[j]] #retrieving the (cluster[j])th element in data
distance = np.square(euclidean_distance(data_point, means[:, i])) #calculating distance of gene from centroid
obj_value += distance
obj_values.append(obj_value)
converged, means = update_clusters(data, clusters, means)
if len(clusters[0]) == data.shape[0] or converged: #if algorithm has converged or all elements in one cluster
break
iterations += 1
return clusters, means, obj_values
def euclidean_distance(x, y):
return np.linalg.norm(x - y)
def random_clusters(data,k):
means_random = np.random.randint(1,data.shape[0]+1,size=k)
means_array=np.zeros((data.shape[1],k))
for i in range(len(means_random)):
means_array[:,i]=data[means_random[i],:]
clusters, means, obj_values = k_means(data, means_array, k, 50)
return clusters, means, obj_values
# %%
#load and process data here
data_pd = pd.read_csv("filtered_data_BAL.csv").iloc[:,1:]
data = data_pd.to_numpy()
data.shape
# %%
clusters,means,obj_values=random_clusters(data.T,3)
min_obj=obj_values[-1]
plt.plot(range(1, len(obj_values)+1), obj_values)
plt.title('K-means Objective over Iterations Randomized - K=3')
plt.xlabel('Iterations - Obj Value: {x}'.format(x=min_obj))
plt.ylabel('Objective')
plt.savefig("k-means3.png")
plt.show()
# %%
labels = np.concatenate([np.full(len(sublist), i, dtype=int) for i, sublist in enumerate(clusters)])
ch_score = calinski_harabasz_score(data.T, labels)
print(ch_score)
# %%
print(len(clusters[0]))
# %%
# raw = np.corrcoef(data.T, rowvar=False)
# plt.imshow(raw)
# plt.colorbar()
# plt.title('Correlation Coefficient Matrix of Raw Data')
# plt.savefig("CCMatrix.png")
# plt.show()
# %%
def visualize_clusters_pca(data, clusters):
pca = PCA(n_components=2)
pca_data = pca.fit_transform(data.T)
# Plot each cluster with a different color
colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
for i, cluster in enumerate(clusters):
cluster_data = pca_data[cluster]
plt.scatter(cluster_data[:,0], cluster_data[:,1], c=colors[i % len(colors)], label='Cluster {}'.format(i+1))
plt.legend()
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.title('PCA Visualization of Clusters')
plt.show()
visualize_clusters_pca(data, clusters)
# %%
# def heatmap(clusters,data,minimum):
# grouped_data = []
# for i in range(len(clusters)):
# cluster_data = np.zeros((data.shape[1], len(clusters[i])))
# for j in range(len(clusters[i])):
# cluster_data[:, j] = data.T[:, clusters[i][j]]
# grouped_data.append(cluster_data)
# grouped_data = np.concatenate(grouped_data, axis=1)
# grouped = np.corrcoef(grouped_data)
# title="CC Matrix of Grouped, Objective Function:"+str(minimum)
# plt.imshow(grouped)
# plt.colorbar()
# plt.title(title, fontsize=5.9)
# plt.show
# heatmap(clusters, data, min(obj_values))
# %%
#BEC Clustering
data_bec_pd = pd.read_csv("filtered_bec.csv").iloc[:,1:-1]
data_bec = data_bec_pd.to_numpy()
data_bec.shape
# %%
clusters_bec,means_bec,obj_values_bec=random_clusters(data_bec.T,2)
min_obj=obj_values_bec[-1]
plt.plot(range(1, len(obj_values_bec)+1), obj_values_bec)
plt.title('K-means Objective over Iterations Randomized - K=2')
plt.xlabel('Iterations - Obj Value: {x}'.format(x=min_obj))
plt.ylabel('Objective')
plt.savefig("k-means3.png")
plt.show()
# %%
labels_bec = np.concatenate([np.full(len(sublist), i, dtype=int) for i, sublist in enumerate(clusters_bec)])
ch_score_bec = calinski_harabasz_score(data_bec.T, labels_bec)
print(ch_score_bec)
visualize_clusters_pca(data_bec, clusters_bec)