def linkage(df, n_groups):
# create the distance matrix based on the forbenius norm: |A-B|_F where A is
# a 24 x N matrix with N the number of timeseries inside the dataframe df
# TODO: We can save have time as we only need the upper triangle once as the
# distance matrix is symmetric
if True:
Y = np.empty((n_groups, n_groups,))
Y[:] = np.NAN
for i in range(len(Y)):
for j in range(len(Y[i,:])):
A = df.loc[i+1].values
B = df.loc[j+1].values
#print('Computing distance of:{},{}'.format(i,j))
Y[i,j] = norm(A-B, ord='fro')
# condensed distance matrix as vector for linkage (upper triangle as a vector)
y = Y[np.triu_indices(n_groups, 1)]
# create linkage matrix with wards algorithm an euclidean norm
Z = hac.linkage(y, method='ward', metric='euclidean')
# R = hac.inconsistent(Z, d=10)
return Z
评论列表
文章目录