How can I perform novelty detection with ksvm in R?

I am trying to implement a novelty detector using the kernlab library (ksvm function) in R. Here is a simple example of what I'm trying to do:

# Training data xxTrain <- matrix(rnorm(2000), nrow=1000, ncol=2, byrow=TRUE) y <- rep(1,1000) classifier <- ksvm(xxTrain, y, type="one-svc", kernel="rbfdot", kpar="automatic") # Test data x1 <- rnorm(1000) scale <- c(rep(1,500), rep(10,100), rep(1,400)) x2 <- rnorm(1000)*scale xxTest <- matrix(c(x1,x2), nrow=1000, ncol=2, byrow=TRUE) # Prediction p <- predict(classifier, xxTest, type="response") # Visualization plot(x2, type='l') lines(x1, col="red") points(5*as.integer(p), type='l', col="blue") 

enter image description here

The figure above is the result that I get. The blue trace is a prediction, and it clearly shows the period in which it is 0. But it does not coincide in time or width with the ejection of the black trace. There are 100 points (black line) that have a large amplitude, and the output that I get in blue does not match the black line.

What am I doing wrong?

+4
source share
1 answer

Here is what you are doing wrong:

 xxTest <- matrix(c(x1,x2), nrow=1000, ncol=2, byrow=TRUE) 

it should be

 xxTest <- matrix(c(x1,x2), nrow=1000, ncol=2, byrow=F ) 

or better

 xxTest <- cbind( x1, x2 ) 

or simply

 p <- predict( classifier, cbind( x1, x2 ), type= "response" ) 

Result (I used gray for x2):

enter image description here

Explanation: with specyfying byrow=T you first take the x1 elements to fill the first 500 rows (alternatively, columns 1 and 2) and then x2 to fill the remaining 500 rows with xxTest . Since the singularity was about 500-600 inches x2, it appeared in both xxTest columns xxTest about (500 + 500) / 2 - (500 + 600) / 2, which is 750-800, which is what you can see.

+1
source

Source: https://habr.com/ru/post/1440263/


All Articles