Skip to content

Instantly share code, notes, and snippets.

@ac00std ac00std/Outliers2
Created Jul 9, 2018

Embed
What would you like to do?
Outliers2
set.seed(3383)
mu1=c(100, 100)
sigma1=matrix(c(10, 5, 5, 10), 2, 2)
data1=mvrnorm(1000, mu1, sigma1)
plot(data1)
mu2=c(150, 150)
sigma2=matrix(c(5, 0, 0, 5), 2, 2)
data2=mvrnorm(200, mu2, sigma2)
plot(data2)
x=runif(100,90,160)
y=runif(100,90,160)
data4=cbind(x,y)
data=rbind(data1,data2,data4)
plot(data)
#MT法
n=nrow(data) # 単位空間のサンプル数を計算
Ave= colMeans(data) # 単位空間の各変数の平均値を計算
Var=var(data)*(n-1)/n # 単位空間の共分散行列を計算
k=ncol(data) # 変数の数を計算
MD=mahalanobis(data, Ave, Var)/k # 単位空間のMDの2乗を計算
plot(MD)
plot(data, pch=21, bg=c("red","blue")[(MD>2)+1])
# 決定境界プロット用のメッシュ作成
px <- seq(80, 180, 1)
py <- seq(80, 180, 1)
pgrid <- expand.grid(px, py)
names(pgrid)<-c("x","y")
contour(px,py,array(mahalanobis(pgrid, Ave, Var)/k,dim=c(length(px), length(py))),
xlim=c(80, 180), ylim=c(80, 180),
col="orange", lwd=2, drawlabels=F,add=T,levels=2)
#one Class SVM
data.t=data.frame(type=1, data)
data.ksvm= ksvm(type~.,data=data.t,type="one-svc",kernel="rbfdot",
kpar=list(sigma=5),nu=0.1)
data.ksvm.predict=predict(data.ksvm)
plot(data, pch=21, bg=c("red","green")[2-(data.ksvm.predict)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.