Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. mysvm<-function(X,y,cost){
  2. library(kernlab)
  3. #----------------------------------------------------#
  4. # Optimization uing ipop() function of kernlab package
  5. #----------------------------------------------------#
  6. n <- dim(X)[1]
  7. # build the system matrices
  8. Q <- sapply(1:n, function(i) y[i]*t(X)[,i])
  9. D <- t(Q)%*%Q
  10. d <- matrix(1, nrow=n)
  11.  
  12. uu <- cost
  13. eps <- 1e-2
  14. b <- 0
  15. r <- 0
  16. A2 <- t(y)
  17. l <- matrix(0, nrow=n, ncol=1)
  18. u <- matrix(uu, nrow=n, ncol=1)
  19.  
  20. capture.output(sol <- ipop(-d, t(Q)%*%Q+eps*diag(n), A2, b, l, u,
  21. r,verb=TRUE, sigf=5, margin=1e-8))
  22. ipopsol <- primal(sol)
  23. alpha<- matrix(ipopsol , nrow=n)
  24.  
  25. #--------------------------------------------------#
  26. # Calculation of the normal vector W and bias term b
  27. #--------------------------------------------------#
  28.  
  29. w=t(alpha*y)%*%(X) #W
  30. ff=matrix(rep(alpha*y,n),n,n)*X%*%t(X)
  31. fout=matrix(t(apply(ff,2,sum)))
  32. pos=which(alpha>1e-6)
  33. b = mean(y[pos]-fout[pos]) #b
  34. fx=t(w %*% t(as.matrix(X))) + b
  35.  
  36. #-----------------------------#
  37. # SVM line & support vectors
  38. #-----------------------------#
  39.  
  40. plot(X,pch=ifelse(y==1, 1, 3),col=ifelse(y==1, 1, 2))
  41. abline(a=-b/w[1,2], b=-w[1,1]/w[1,2], col="black", lty=1)
  42. abline(a=-(b+1)/w[1,2], b=-w[1,1]/w[1,2], col="orange", lty=3)
  43. abline(a=-(b-1)/w[1,2], b=-w[1,1]/w[1,2], col="orange", lty=3)
  44. points(X[pos,],col="blue",cex=2) # show the support vectors
  45.  
  46.  
  47. #------------------#
  48. # ACCURACY
  49. #------------------#
  50.  
  51. pred<-function(x,lable){
  52. C=NULL
  53. for (i in 1:length(x)){
  54. if(x[i]>0){C[i]=1}
  55. else {C[i]=-1}
  56. }
  57. accuracy=mean(C==lable)
  58. result=list(C,accuracy)
  59. return(accuracy)
  60. }
  61. pred(fx,y)
  62. }
  63. #------------------#
  64. # DATA
  65. #------------------#
  66.  
  67. library(MASS)
  68. set.seed(44)#44
  69.  
  70. groupN=mvrnorm(20,c(rep(-2,2)),diag(c(rep(1,2))))
  71. groupP=mvrnorm(20,c(rep(2,2)),diag(c(rep(1,2))))
  72. X=rbind(groupN,groupP)
  73. y<-rep(c(-1,1),each=20)
  74. a1=c(0,0);a2=c(0,-1);a3=c(-1,-1);a4=c(1,0)
  75. X=rbind(X,a1,a2,a3,a4)
  76. y=c(y,1,1,1,-1)
  77. mysvm(X,y,10)
  78.  
  79. #------------------#
  80. # FUZZY SVM
  81. #------------------#
  82.  
  83. Si<-function(x,y,delta){
  84. Pxbar<-apply(groupP,2,mean)
  85. Nxbar<-apply(groupN,2,mean)
  86. S=NULL
  87. for (i in 1:nrow(x)){
  88. for (j in 1:ncol(x)){
  89. if (y[i]==1)
  90. {S[i]=1-sqrt(sum((Pxbar[j]-x[i,j])^2))/(max(sqrt(sum((Pxbar[j]-x[i ,j])^2)
  91. ))+delta)}
  92. else
  93. {S[i]=1-sqrt(sum((Nxbar[j]-x[i,j])^2))/(max(sqrt(sum((Nxbar[j]
  94. - x[i,j])^2)))+delta)}
  95. }}
  96. return(S)
  97. }
  98. mysvm(X,y,10*Si(X,y,0.5))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement