http://biones.dip.jp/space/space.py (鯖が止まってると動かない可能性あり)
的なモノを昔作って何か分析しようとおもいつつ放置していたのに再着手。 青色の点が目的地。
歩行軌跡間の「近さ」はデータの長さが違っても類似度が計算できるDynamic Time Warping距離
ダイナミックタイムワーピング距離に基づくストリーム処理 http://www.ieice.org/iss/de/DEWS/DEWS2007/pdf/l6-5.pdf
で測って、距離行列を作成、k-medroid(pamパッケージ)でクラスタリングして、何処に向かっているか判定します。
生成の方のコードは長くかつ汚いので省略(このRコードもきたな・・・)。
m=read.csv("~/Desktop/traject.csv") m=m[,-1] library(dtw) hns=sort(m$human_no) hns=unique(hns) library(ggplot2) gdf=data.frame(matrix(ncol = 4)) #ggplot用 colnames(gdf)=c("hn","x","y","cl") #gdf=gdf[-1,] #プロット用 plt=function(gdf){ gdf$cl=factor(gdf$cl) for(hn in hns){ x=subset(m,human_no==hn)$x y=subset(m,human_no==hn)$y hoge=pm$clustering cl=hoge[names(hoge)==as.integer(hn)] if(length(cl)<=0){ next } tdf=cbind(hn,x,y,cl) gdf=rbind(gdf,tdf) } gp=ggplot(data=gdf,aes(x=x,y=y,color=cl))+geom_point() print(gp) } #距離行列の計算 hoge=combn(hns,2) dmat=data.frame(NULL) cc=combn(hns,2) for(j in 1:dim(hoge)[2]){ c=cc[,j] x1=subset(m,human_no==c[1])$x y1=subset(m,human_no==c[1])$y x2=subset(m,human_no==c[2])$x y2=subset(m,human_no==c[2])$y dist=dtw(x=cbind(x1,y1),y=cbind(x2,y2)) #DTW距離 dmat[paste(c[1]),paste(c[2])]<-dist$distance if(j%%1000==0){ print(j) } } for(i in 1:dim(dmat)[1]){ for(j in 1:dim(dmat)[2]){ if(is.na(dmat[i,j])){ dmat[i,j]<-0 } } } #クラスタリング pm=pam(dmat,k=5)#クラスタ数は既知 pred=pm$clustering mokutekiti=floor(as.numeric(names(pred))/1000)+1 #human_noの4桁目で目的地を分けているので res=table(mokutekiti,pred) print(res) plt(gdf)
目的地の生成部分
f.generate("S1",10,10) f.generate("S2",70,60) f.generate("S3",10,40) f.generate("S4",10,20) f.generate("S5",80,70)
結果。
pred mokutekiti 1 2 3 4 5 1 100 0 0 0 0 2 0 100 0 0 0 3 0 3 94 3 0 4 0 0 17 83 0 5 0 0 0 4 95
ggplot頑張ったけど見づらいですorz
距離行列の計算が重くて500人程度でも時間がかかるので、隠れマルコフモデルで再チャレンジしてみようかと・・・。