如何得到dtwclust的结果
How to get the result of dtwclust
现在我正在使用 dtwclust
包(感谢作者 Alexis Sarda-Espinosa 和 Alexis Sarda~)
我遇到了一个简单的问题。这是我的代码。
sc <- read.table("D:/handling data/confirm.csv", header=T, sep="," )
rownames(sc) <- sc$STDR_YM_CD
sc$STDR_YM_CD <- NULL
sc <- t(sc)
hc_sbd <- dtwclust(sc, type = 'h', k=3L, method = 'average', preproc = zscore,
distance = 'dtw', control = list(trace=TRUE) )
plot(hc_sbd@cluster)
plot(hc_sbd, type='sc')
plot(hc_sbd, type='series', clus=2)
plot(hc_sbd, type='centroids', clus=2)
head(hc_sbd)
write.xlsx(hc_sbd, "D:/handling data/tab1clustn.xlsx")
我得到了这张照片。
我想用集群标签导出我的数据。喜欢第二张图
我假设 STDR_YM_CD 是您希望与 DTW 聚类的唯一标识符。
sc <- read.table("D:/handling data/confirm.csv", header=T, sep="," )
df.labels <- sc$STDR_YM_CD #rownames(sc) <- sc$STDR_YM_CD
sc$STDR_YM_CD <- NULL
sc <- t(sc)
hc_sbd <- dtwclust(sc, type = 'h', k=3L, method = 'average', preproc = zscore,
distance = 'dtw', control = list(trace=TRUE) )
hc.clust <- data.frame(STDR_YM_CD = df.labels, dtwclust = hc_sbd@cluster)
sc <- merge(sc,hc.clust, by.x = "STDR_YM_CD", by.y = "STDR_YM_CD")
我只是提取标签,即您要聚类的变量,然后我使用列名称 dtwclust 从 dtwclust 结果创建一个新数据框。我认为根据我们独特的标签将它们合并回去。还有其他方法可以做到这一点,但这是一种选择。希望对您有所帮助!
@Wayne Lee 的回答结束了。无需声明 data.frame
,我们也不需要 merge
数据。
我知道的所有聚类算法,return一个聚类分配向量cluster
,其长度与df
具有相同的行。因此只是 cbind
cluster
数据向量 df
:
add_cluster_to_csv<-cbind(df,cluster=hc_sbd@cluster)
这也应该减少计算时间,因为我们不使用 merge
并且 cbind
比 data.frame
快得多。
附录:
整个代码如下所示:
### Pass the data into a dataframe:
df <- read.csv('D:/handling data/confirm.csv',header=TRUE,sep=',')
### Run dtwclust:
hc_sbd <- dtwclust(sc, type = 'h', k=3L, method = 'average', preproc = zscore,
distance = 'dtw', control = list(trace=TRUE)
cluster <- hc_sbd@cluster ### Extract the cluster
add_cluster_to_csv<-cbind(df,cluster) ### Combine the original dataframe with the vector
### Write to new csv:
write.csv(add_cluster_to_csv,'Csv_with_cluster.csv')
现在我正在使用 dtwclust
包(感谢作者 Alexis Sarda-Espinosa 和 Alexis Sarda~)
我遇到了一个简单的问题。这是我的代码。
sc <- read.table("D:/handling data/confirm.csv", header=T, sep="," )
rownames(sc) <- sc$STDR_YM_CD
sc$STDR_YM_CD <- NULL
sc <- t(sc)
hc_sbd <- dtwclust(sc, type = 'h', k=3L, method = 'average', preproc = zscore,
distance = 'dtw', control = list(trace=TRUE) )
plot(hc_sbd@cluster)
plot(hc_sbd, type='sc')
plot(hc_sbd, type='series', clus=2)
plot(hc_sbd, type='centroids', clus=2)
head(hc_sbd)
write.xlsx(hc_sbd, "D:/handling data/tab1clustn.xlsx")
我得到了这张照片。 我想用集群标签导出我的数据。喜欢第二张图
我假设 STDR_YM_CD 是您希望与 DTW 聚类的唯一标识符。
sc <- read.table("D:/handling data/confirm.csv", header=T, sep="," )
df.labels <- sc$STDR_YM_CD #rownames(sc) <- sc$STDR_YM_CD
sc$STDR_YM_CD <- NULL
sc <- t(sc)
hc_sbd <- dtwclust(sc, type = 'h', k=3L, method = 'average', preproc = zscore,
distance = 'dtw', control = list(trace=TRUE) )
hc.clust <- data.frame(STDR_YM_CD = df.labels, dtwclust = hc_sbd@cluster)
sc <- merge(sc,hc.clust, by.x = "STDR_YM_CD", by.y = "STDR_YM_CD")
我只是提取标签,即您要聚类的变量,然后我使用列名称 dtwclust 从 dtwclust 结果创建一个新数据框。我认为根据我们独特的标签将它们合并回去。还有其他方法可以做到这一点,但这是一种选择。希望对您有所帮助!
@Wayne Lee 的回答结束了。无需声明 data.frame
,我们也不需要 merge
数据。
我知道的所有聚类算法,return一个聚类分配向量cluster
,其长度与df
具有相同的行。因此只是 cbind
cluster
数据向量 df
:
add_cluster_to_csv<-cbind(df,cluster=hc_sbd@cluster)
这也应该减少计算时间,因为我们不使用 merge
并且 cbind
比 data.frame
快得多。
附录:
整个代码如下所示:
### Pass the data into a dataframe:
df <- read.csv('D:/handling data/confirm.csv',header=TRUE,sep=',')
### Run dtwclust:
hc_sbd <- dtwclust(sc, type = 'h', k=3L, method = 'average', preproc = zscore,
distance = 'dtw', control = list(trace=TRUE)
cluster <- hc_sbd@cluster ### Extract the cluster
add_cluster_to_csv<-cbind(df,cluster) ### Combine the original dataframe with the vector
### Write to new csv:
write.csv(add_cluster_to_csv,'Csv_with_cluster.csv')