-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkshape.R
More file actions
63 lines (48 loc) · 1.96 KB
/
Copy pathkshape.R
File metadata and controls
63 lines (48 loc) · 1.96 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
# Davies-Bouldin index for k-Shape
DB_kshape <- function(clustering) {
n <- max(clustering@cluster)
R <- as.data.frame(matrix(NA,n,n))
M <- as.data.frame(matrix(NA,n,n))
D <- as.data.frame(matrix(NA,n,1))
for(i in 1:n) {
for(j in 1:n) {
M[i,j] <- SBD(clustering@centroids[[i]], clustering@centroids[[j]], znorm = FALSE, error.check = TRUE)$dist
if (i != j) {
R[i,j] <- sum(clustering@clusinfo[i,2],clustering@clusinfo[j,2])/M[i,j]
}
}
D[i,1] <- max(R[i,], na.rm = TRUE)
}
DB <- sum(D)/n
return(DB)
}
# k-Shape clustering
clusterKShape <- function(matrixOOM, k_low, k_high, ncores){
# PAA representation computing, z-score normalisation of original long time series windows
mat <- repr_matrix(matrixOOM, func = repr_paa,
args = list(q = 6, func = mean), normalise = TRUE,
func_norm = norm_z)
mat_sd <- apply(mat, 1, sd)
for(i in which(mat_sd == 0)) {
mat[i,] <- rnorm(ncol(mat), 0, 0.01)
}
cl <- makeCluster(ncores)
clusterExport(cl, varlist = c("mat", "k_low", "k_high"), envir = environment())
clusterCall(cl, function() library(dtwclust))
clusterCall(cl, function() library(proxy))
clusterings <- parLapply(cl, c(k_low:k_high), function(x) tsclust(mat, k = x,
distance = "sbd", centroid = "shape", type = "partitional", preproc = zscore,
control = partitional_control(pam.precompute = FALSE), trace = FALSE))
if(!is.null(cl)) {
parallel::stopCluster(cl)
cl <- c()
}
cl <- makeCluster(ncores)
clusterExport(cl, varlist = c("clusterings", "DB_kshape", "SBD"), envir = environment())
DB_values <- parSapply(cl, seq_along(clusterings), function(x) DB_kshape(clusterings[[x]]))
if(!is.null(cl)){
parallel::stopCluster(cl)
cl <- c()
}
return(clusterings[[which.min(DB_values)]]@cluster)
}