時(shí)間序列聚類和分析

本文比較了基于歐氏距離和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]])


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,578評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,094評論 25 709
  • 煙雨朦朦,有點(diǎn)冷,還是多穿點(diǎn)。 找不到單車就坐公交,其實(shí)雨天就不想騎車。 書還要嗎?再過幾天我就不在廣州了,不來拿...
    流浪癡人閱讀 350評論 0 0
  • 下面是稚雋教育小編為大家整理的一篇關(guān)于UKCAT考試能申請的英國醫(yī)學(xué)院的文章,供大家參考,下面是詳細(xì)內(nèi)容。 英國醫(yī)...
    peizhenjy閱讀 1,048評論 0 0
  • SqlSession 使用范圍 SqlSessionFactoryBuilder 通過SqlSessionFact...
    暗物質(zhì)閱讀 546評論 0 0

友情鏈接更多精彩內(nèi)容