-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclust_forc.py
185 lines (145 loc) · 7.01 KB
/
clust_forc.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
__author__ = 'Elnaz Azmi'
__email__ = '[email protected]'
__status__ = 'Development'
import os
import math
import numpy as np
import pandas as pd
import numpy.matlib
import preprocessing.dim_reduction as predim
import clustering.clustering as clusclus
import clustering.matcluster as clusmat
from sklearn import metrics
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from scipy.interpolate import interp1d
from scipy.spatial.distance import cdist
from scipy.io import loadmat
INPUTFILE = os.path.join(os.path.dirname(__file__), 'data/total_storage_166.csv')
INPUTCOMPTIME = os.path.join(os.path.dirname(__file__), 'data/hillslopes_computation_time_166.csv')
def clust_forc(matfile, k, ccenter, t):
mat = loadmat(matfile)
colname = np.array([])
for i in range(1, len(mat['clust_data'][0])):
colname = np.append(colname, mat['clust_data'][0][i][0])
indx = np.array([])
for i in range(1, len(mat['clust_data'])):
indx = np.append(indx, mat['clust_data'][i][0][0])
cols = np.zeros(shape=(len(mat['clust_data'])-1,len(mat['clust_data'][0])-1))
for i in range(1, len(mat['clust_data'])):
for j in range(1, len(mat['clust_data'][i])):
cols[i-1, j-1] = mat['clust_data'][i][j][0][0]
df = pd.DataFrame(data=cols, index=indx, columns=colname)
if ccenter.shape[1] > df.shape[1]:
pca = PCA(n_components=df.shape[1])
ccenter = pca.fit_transform(ccenter)
df_norm = predim.norm_features(df)
df_norm = df_norm.fillna(0.00)
x, y, z, ccenter = clusmat.mat_kmeans(df_norm, k, 'mat_kmeans_t'+str(t), ccenter)
clusters = pd.DataFrame(z).combine_first(df_norm)
return x, y, z, ccenter, clusters
def struct_features():
data = predim.read_data(INPUTFILE)
df_AS, AS, TtE = predim.calc_active_storage(data)
df_Feature = predim.extr_features(df_AS, AS, TtE)
df_Feat_Norm = predim.norm_features(df_Feature)
df_Corr_pearson = predim.calc_correlation(df_Feat_Norm)
s_feat = predim.filt_features(df_Feat_Norm, df_Corr_pearson)
return s_feat
def all_features(matfile, s_feat):
mat = loadmat(matfile)
colname = np.array([])
for i in range(1, len(mat['clust_data'][0])):
colname = np.append(colname, mat['clust_data'][0][i][0])
indx = np.array([])
for i in range(1, len(mat['clust_data'])):
indx = np.append(indx, mat['clust_data'][i][0][0])
cols = np.zeros(shape=(len(mat['clust_data']) - 1, len(mat['clust_data'][0]) - 1))
for i in range(1, len(mat['clust_data'])):
for j in range(1, len(mat['clust_data'][i])):
cols[i - 1, j - 1] = mat['clust_data'][i][j][0][0]
df = pd.DataFrame(data=cols, index=indx, columns=colname)
df_norm = predim.norm_features(df)
df_norm = df_norm.fillna(0.00)
a_feat = pd.concat([s_feat, df_norm], axis=1, sort=False)
return a_feat
def clust_forc_upd(a_feat, k, ccenter, t):
x, y, z, ccenter = clusmat.mat_kmeans(a_feat, k, 'mat_kmeans_t' + str(t), ccenter)
clusters = pd.DataFrame(z).combine_first(a_feat)
return x, y, z, ccenter, clusters
def apply_kmeans(df_Feat_Norm, df_HS_runtime):
df_rmse = pd.DataFrame()
arr_ctime = np.empty([0])
nr_itter = int((df_Feat_Norm.shape[0] + 1) / 3)
for i in range(1, nr_itter, 4):
mydata = df_Feat_Norm.copy()
n_clusters = i
kmeans = KMeans(init='k-means++', n_clusters=n_clusters, n_init=10, max_iter=300, random_state=0).fit(mydata)
pred = kmeans.labels_
ctime = 0.0
# representitive hillslope
rmse_ = pd.DataFrame()
represent = pd.DataFrame()
for j in np.unique(pred):
df_dist = pd.DataFrame(metrics.pairwise.euclidean_distances(mydata[pred == j], mydata[pred == j]),
index=mydata[pred == j].index.values, columns=mydata[pred == j].index.values)
reptiv = df_dist.sum().idxmin()
ctime = ctime + float(df_HS_runtime['C_Time'][df_HS_runtime.index.values == reptiv])
represent = represent.append({'representitive': reptiv, 'label': j}, ignore_index=True)
# Root Mean Square Error
for k in range(mydata[pred == j].shape[0]):
rmse = math.sqrt(
metrics.mean_squared_error(mydata.loc[mydata[pred == j].iloc[k].name], mydata.loc[reptiv]))
rmse_ = rmse_.append({'hname': mydata[pred == j].iloc[k].name, 'rmse': rmse}, ignore_index=True)
rmse_ = rmse_.set_index(['hname'])
rmse_ = rmse_.sort_index()
df_rmse[str(i)] = rmse_['rmse']
arr_ctime = np.append(arr_ctime, ctime)
df_RmseSum_Kmeans = pd.DataFrame({'rmse_sum': np.sqrt(np.square(df_rmse).sum()), 'ctime': arr_ctime})
return df_RmseSum_Kmeans
def k_determiner(features):
df_HS_runtime = clusclus.read_data(INPUTCOMPTIME)
df_RmseSum_Kmeans = apply_kmeans(features, df_HS_runtime)
df_Norm_Clust = clusclus.norm_rmse_ctime(df_RmseSum_Kmeans, df_HS_runtime)
# interpolate data points
f1 = interp1d(df_Norm_Clust['nr_cls'], df_Norm_Clust['ctime'], kind='cubic')
f2 = interp1d(df_Norm_Clust['nr_cls'], df_Norm_Clust['rmse_sum'], kind='cubic')
xnew = list(range(1, int(df_Norm_Clust.iloc[-1,2])))
# detect intersection by change in sign of difference
intersect = np.array([], dtype=np.int16)
diff = f2(xnew) - f1(xnew)
for i in range(len(diff) - 1):
if diff[i] == 0. or diff[i] * diff[i + 1] < 0.:
intersect = np.append(intersect, np.int16(i))
k = min(intersect)
return k
def apply_elbow(features):
md = pd.DataFrame(columns=['nr_cls', 'dist'])
nr_itter = int((features.shape[0] + 1))
for i in [1, 5, 10, 20, 30, 40, 50, 20479]: # range(1, nr_itter, 5):
mydata = pd.DataFrame()
mydata = features.copy()
if i != 20479:
kmeans = KMeans(init='k-means++', n_clusters=i, n_init=10, max_iter=300, random_state=0).fit(mydata)
md = md.append({'nr_cls':i, 'dist':sum(np.min(cdist(mydata, kmeans.cluster_centers_,
'euclidean'), axis=1))/mydata.shape[0]},
ignore_index=True, sort=False)
else:
md = md.append({'nr_cls':i, 'dist':0.0000}, ignore_index=True, sort=False)
f1 = interp1d(md['nr_cls'], md['dist'], kind='cubic')
xnew = list(range(1, int(md.iloc[-1, 0] + 1)))
ynew = f1(xnew)
# find elbow point
values=list(ynew)
nPoints = len(values)
allCoord = np.vstack((range(1, nPoints+1), values)).T
firstPoint = allCoord[0]
lineVec = allCoord[-1] - allCoord[0]
lineVecNorm = lineVec / np.sqrt(np.sum(lineVec**2))
vecFromFirst = allCoord - firstPoint
scalarProduct = np.sum(vecFromFirst * np.matlib.repmat(lineVecNorm, nPoints, 1), axis=1)
vecFromFirstParallel = np.outer(scalarProduct, lineVecNorm)
vecToLine = vecFromFirst - vecFromFirstParallel
distToLine = np.sqrt(np.sum(vecToLine ** 2, axis=1))
k = np.argmax(distToLine)
return np.int16(k)