-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclusters.py
More file actions
158 lines (94 loc) · 3.7 KB
/
clusters.py
File metadata and controls
158 lines (94 loc) · 3.7 KB
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
from sklearn.manifold import TSNE
from sklearn.cluster import KMeans, DBSCAN
import numpy as np
import pandas as pd
from cvjson.cvj import CVJ
from experiment_methods import get_data
import os
import plotting
def kmeans_cluster_analysis(hdf5_path, cvj_obj, file_name=None):
"""
This method clusters the data found in the hdf5_path and then clusters them using Kmeans.
This method takes the clusters and the classes and figures out which cluster each class has
been most assigned to. This is done 10 times and then stored in a DataFrame object using
pandas.
The results of this method are only interesting if one looks at how frequently the classes
get assigned to the same cluster. This tells us that there is similarity between those classes
if they are consistently assigned to the same cluster.
Parameters
----------
hdf5_path: DataFrame object
This dataframe object comes from TwoD_data()
cvj_obj: CVJ object
This a CVJ object from the CVJSON library. This most likely should be filled with
the data from the training set. However, this cvj object is only used to convert
the ids to their respective names.
file_name: str
This the file name/file path where the file should be saved.
Returns
-------
df: DataFrame object
This dataframe holds all of the information regarding how the classes were being clustered.
"""
X, y, labels, counts = get_data(hdf5_path)
# Clustering and inspecting
pseudo_df = []
for i in range(10):
model = KMeans()
clusters = model.fit(X).labels_
values = np.unique(clusters)
df = pd.DataFrame(y, index=None)
df.columns = ["y_values"]
df["cluster"] = clusters
counts = df.groupby("y_values")["cluster"].value_counts()
viewed_indices = []
most_index_cluster = []
for k in counts.index.tolist():
if k[0] not in viewed_indices:
viewed_indices.append(k[0])
most_index_cluster.append(k[1])
pseudo_df.append(most_index_cluster)
df = pd.DataFrame(pseudo_df)
df = df.transpose()
df["class"] = [cvj_obj.get_class_id_2_name(i) for i in viewed_indices]
if file_name == None:
file_name = "kmeans_cluster_analysis"
df.to_csv(file_name + ".csv")
print(df)
return df
### Threshold is for the total amount of annotations a certain combination has.
# threshold = 50
# output_dir = "/home/ben/Desktop/mapping_labels/combination_csvs"
# values, value_counts = np.unique(y, return_counts=True)
# name_2_count = {}
# for index, i in enumerate(values):
# if value_counts[index] < threshold:
# continue
# name_2_count[train_cvj.get_class_id_2_name(i)] = [value_counts[index]]
# name_2_count = pd.DataFrame.from_dict(name_2_count)
# cats = list(name_2_count.keys())
# list_of_combinations = []
# for i in range(2, 6):
# combinations = list(set(itertools.combinations(cats, i)))
# for i in combinations:
# df = name_2_count[list(i)]
# sum_ = df.loc[0, :].sum()
# if sum_ < threshold:
# continue
# else:
# list_of_combinations.append(list(i))
# for i in list_of_combinations:
# df = name_2_count[i]
# df["sum"] = df.loc[0, :].sum()
# df.to_csv(os.path.join(output_dir, '_'.join(i) + ".csv"))
# model = TSNE(n_components=3)
# tsne_matrix = model.fit_transform(X)
# for i in list_of_combinations
# ids = list(train_cvj.get_class_id_2_name().keys())
# print(ids[:3])
# print(y.shape)
# print(np.unique(y))
# hmm = y[np.isin(y, ids[:3])]
# print(ids[:3])
# print(hmm.shape)
# print(np.unique(hmm))