Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library('quadprog')
- # load the dataset and shuffles it
- datafilename <- "data/iris.data"
- dataset <- read.table(datafilename,header=FALSE,sep=",")
- dataset <- data.frame(dataset[,1:(length(dataset)-1)],unclass(dataset[,length(dataset)]))
- #convert to two class classification problem
- #keep the selected class and merge all the others to a single one
- selectedClass <- 1
- dataset[dataset[,length(dataset)]==selectedClass,][length(dataset)] <- -1
- dataset[dataset[,length(dataset)]!=-1,][length(dataset)] <- 1
- #attributes to use for training/testing and plotting
- xIndex <- 3
- yIndex <- 4
- #select only two attributes to use for learning,
- #so that the results can be easily visualized
- train.data <- data.frame(dataset[,xIndex],dataset[,yIndex],dataset[,dim(dataset)[2]])
- # Z is a diagonal matrix created from the class column vector z
- Z <- as.matrix(diag(dataset[,dim(dataset)[2]]))
- z <- diag(Z)
- # matrix Y = (1, Y_x)
- ones <- rep(1,dim(dataset)[1])
- Y_x <- as.matrix(train.data[,-dim(train.data)[2]])
- Y <- cbind(ones,Y_x)
- # preparation of the variables for quadprog library
- D <- Z %*% Y_x %*% t(Y_x) %*% Z
- d <- ones
- I <- diag(ones) # Identity matrix
- At <- rbind(I,t(z))
- A <- t(At)
- # solve the optimization problem and get the alpha values
- alpha <- solve.QP(D,d,A, factorized=TRUE)$solution
- print(alpha)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement