本文比較了基于歐氏距離和DTW聚類對時(shí)間序列聚類和分類的可靠性。
從抽樣的樣本中發(fā)現(xiàn)了基于DTW聚類算法遠(yuǎn)遠(yuǎn)比基于歐氏距離的聚類算法好;從分類的角度中,基于DTW特征提取的分類準(zhǔn)確率為87%,而基于歐氏距離的分類準(zhǔn)確率為80%,顯而易見基于DTW的聚類算法遠(yuǎn)遠(yuǎn)的優(yōu)于基于歐氏距離的分類算法。
##數(shù)據(jù)下載鏈接
## ?http://kdd.ics.uci.edu/databases/synthetic_control/
sc <-read.table("synthetic_control.data.txt", header=F,sep="")
# show one sample from each class
##1-100為隨機(jī)波動(dòng)
##101-200 周期
##201-300 上升
##301-400 下降
##401-500 向上偏移
##501-600 向下偏移
idx <-c(1,101,201,301,401,501)
sample1 <-t(sc[idx,])
plot.ts(sample1,main="") ##可視化每類的一個(gè)圖像
#基于歐氏距離的層次聚類
set.seed(6218)
n <-10
s <-sample(1:100, n)
idx <-c(s,100+s,200+s,300+s,400+s,500+s)
sample2 <-sc[idx,]
observedLabels <-rep(1:6, each=n)
#層次聚類,基于歐氏距離
hc <-hclust(dist(sample2),method="average")
plot(hc,labels=observedLabels,main="")
# 分為6類
rect.hclust(hc,k=6)
memb <-cutree(hc,k=6)
table(observedLabels,memb)
## ? ? ? ? ? ? ? memb
## observedLabels ?1 2 ?3 ?4 5 ?6
## ? ? ? ? ? ? 1 10 ?0 ?0 0 ?0 ?0
## ? ? ? ? ? ? 2 ?1 ?6 2 ?1 ?0 ?0
## ? ? ? ? ? ? 3 ?0 ?0 0 ?0 10 ?0
## ? ? ? ? ? ? 4 ?0 ?0 0 ?0 ?0 10
## ? ? ? ? ? ? 5 ?0 ?0 0 ?0 10 ?0
## ? ? ? ? ? ? 6 ?0 ?0 0 ?0 ?0 10
###################################################
#基于DTW距離的層次聚類
library(dtw)
## Loading required package:proxy
##
## Attaching package: 'proxy'
## The following objects aremasked from 'package:stats':
##
## ? ?as.dist, dist
## The following object ismasked from 'package:base':
##
## ? ?as.matrix
## Loaded dtw v1.18-1. See?dtw for help, citation("dtw") for use in publication.
##基于dtw的聚類
distMatrix <-dist(sample2, method="DTW")
hc <-hclust(distMatrix,method="average")
plot(hc,labels=observedLabels,main="")
# 分為6類
rect.hclust(hc,k=6)
memb <-cutree(hc,k=6)
table(observedLabels,memb)
## ? ? ? ? ? ? ? memb
## observedLabels ?1 2 ?3 ?4 5 ?6
## ? ? ? ? ? ? 1 10 ?0 ?0 0 ?0 ?0
## ? ? ? ? ? ? 2 ?0 ?7 3 ?0 ?0 ?0
## ? ? ? ? ? ? 3 ?0 ?0 ?010 ?0 0
## ? ? ? ? ? ? 4 ?0 ?0 0 ?0 ?7 ?3
## ? ? ? ? ? ? 5 ?2 0 ?0 ?8 ?0 ?0
## ? ? ? ? ? ? 6 ?0 ?0 0 ?0 ?0 10
###################################################
##時(shí)間序列進(jìn)行分類
classId <-rep(as.character(1:6), each=100)
newSc <-data.frame(cbind(classId,sc))
library(party)
## Loading required package:grid
## Loading required package:mvtnorm
## Loading required package:modeltools
## Loading required package:stats4
## Loading required package:strucchange
## Loading required package:zoo
##
## Attaching package: 'zoo'
## The following objects aremasked from 'package:base':
##
## ? ?as.Date, as.Date.numeric
## Loading required package:sandwich
ct <-ctree(classId~., data=newSc,
? ? ? ? ? controls =ctree_control(minsplit=30, minbucket=10, maxdepth=5))
pClassId <-predict(ct)
table(classId,pClassId)
## ? ? ? ?pClassId
## classId ?1 ? 2 ? 3 ?4 ? 5 ? 6
## ? ? ?1 ?97 ? 0 ?0 ? 0 ? 0 ? 3
## ? ? ?2 ? 1 ?93 ?2 ? 0 ? 0 ? 4
## ? ? ?3 ? 0 ? 0 96 ? 0 ? 4 ? 0
## ? ? ?4 ? 0 ? 0 ? 0100 ? 0 ?0
## ? ? ?5 ? 4 ? 0 10 ? 0 ?86 ? 0
## ? ? ?6 ? 0 ? 0 ?0 ?87 ? 0 ?13
#計(jì)算準(zhǔn)確率
(sum(classId==pClassId)) /nrow(sc)
## [1] 0.8083333
plot(ct, ip_args=list(pval=FALSE), ep_args=list(digits=0))
###################################################
#基于dtw的分類
library(wavelets)
wtData <-NULL
for (i in 1:nrow(sc)) {
?a <-t(sc[i,])
?wt<-dwt(a,filter="haar", boundary="periodic")
?wtData<-rbind(wtData,unlist(c(wt@W,wt@V[[wt@level]])))
}
wtData <-as.data.frame(wtData)
wtSc <-data.frame(cbind(classId,wtData))
###################################################
# build a decision tree with DWT coefficients
ct <-ctree(classId~.,data=wtSc,
? ? ? ? ? controls =ctree_control(minsplit=30, minbucket=10, maxdepth=5))
pClassId <-predict(ct)
table(classId,pClassId)
## ? ? ? ?pClassId
## classId 1 ?2 ?3 4 ?5 ?6
## ? ? ?1 97 ?3 ?0 0 ?0 ?0
## ? ? ?2 ?1 99 ?0 0 ?0 ?0
## ? ? ?3 ?0 ?0 81 ?019 ?0
## ? ? ?4 ?0 ?0 ?063 ?0 37
## ? ? ?5 ?0 ?0 16 ?084 ?0
## ? ? ?6 ?0 ?0 0 ?1 ?0 99
(sum(classId==pClassId)) /nrow(wtSc)
## [1] 0.8716667
plot(ct, ip_args=list(pval=FALSE), ep_args=list(digits=0))
set.seed(10)
k <- 20
# create a new time series by adding noise to time series 501
newTS <- sc[501,] + runif(100)*15
distances <- dist(newTS, sc, method="DTW")
s <- sort(as.vector(distances),index.return=TRUE)
# class IDs of k nearest neighbors
table(classId[s$ix[1:k]])