# Using SparseM/Matrix Sparse Matrix in training SVM from e1071 returning different results from same data in standard matrix

Using sparse matrix objects in svm training in e1071 returns different results than running on the same data represented as standard matrix:

library(e1071)
library(Matrix)
library(SparseM)
m=10
n=800
#means <- colMeans(myData)
#stdDevs <- apply(myData, 2, sd)
means <- c(0.042154664, 0.010474473, 0.106408354, 0.002237226, 0.089791084,
0.072792224, 0.001884146, 0.002925725, 0.010788693, 0.151466160)
stdDevs <- c(0.017026132, 0.012720986, 0.026721416, 0.004810966, 0.026454962,
0.025349870, 0.004165095, 0.005573776, 0.009063219, 0.036368062)
clss = rep(0,n)
clss[sample(1:n,n/2)] <- 1
clss <- factor(clss)
ftrs <- matrix(nrow=n,ncol=m)
for (i in seq(m)){ftrs[,i] <- rnorm(n,means[i],stdDevs[i])}
ftrs[ftrs<0] <- 0
for (i in seq(m)){ftrs[sample(1:n,(n/10)),i]<-0}
ftrs.csr <- as.matrix.csr(ftrs)
ftrs.Mtrx <- Matrix(ftrs, sparse=TRUE)
mod1 <- svm(ftrs,clss, kernel='linear')
mod2 <- svm(ftrs.csr,clss, kernel='linear')
mod3 <- svm(ftrs.Mtrx,clss, kernel='linear')
prf(data.frame(fitted(mod1),clss))
prf(data.frame(fitted(mod2),clss))
prf(data.frame(fitted(mod3),clss))


The above code simulates a subset of one of my feature sets pretty closely, and if you run it repeatedly, the prf results will sometimes be very close and sometimes be very different, i.e. but they are never matching:

> prf(data.frame(fitted(mod1),clss))
Acc   P_0       R_0       F_0    P_1       R_1       F_1
[1,] 0.52125 0.675 0.5162524 0.5850488 0.3675 0.5306859 0.4342688
> prf(data.frame(fitted(mod2),clss))
Acc   P_0       R_0       F_0  P_1       R_1       F_1
[1,] 0.5025 0.775 0.5016181 0.6090373 0.23 0.5054945 0.3161512
> prf(data.frame(fitted(mod3),clss))
Acc   P_0       R_0       F_0  P_1       R_1       F_1
[1,] 0.5025 0.775 0.5016181 0.6090373 0.23 0.5054945 0.3161512

> prf(data.frame(fitted(mod1),clss))
Acc  P_0      R_0     F_0  P_1       R_1      F_1
[1,] 0.545 0.61 0.539823 0.57277 0.48 0.5517241 0.513369
> prf(data.frame(fitted(mod2),clss))
Acc    P_0       R_0       F_0   P_1    R_1       F_1
[1,] 0.50375 0.2225 0.5085714 0.3095652 0.785 0.5024 0.6126829
> prf(data.frame(fitted(mod3),clss))
Acc    P_0       R_0       F_0   P_1    R_1       F_1
[1,] 0.50375 0.2225 0.5085714 0.3095652 0.785 0.5024 0.6126829


Is this something I'm doing incorrectly? Or is training on sparse matrix representations just more inexact?

Thanks, Rob

prf Function:

prf <- function(predAct){
## predAct is two col dataframe of pred,act
preds = predAct[,1]
trues = predAct[,2]
xTab <- table(preds, trues)
clss <- as.character(sort(unique(preds)))
r <- matrix(NA, ncol = 7, nrow = 1,
dimnames = list(c(),c('Acc',
paste("P",clss[1],sep='_'),
paste("R",clss[1],sep='_'),
paste("F",clss[1],sep='_'),
paste("P",clss[2],sep='_'),
paste("R",clss[2],sep='_'),
paste("F",clss[2],sep='_'))))
r[1,1] <- sum(xTab[1,1],xTab[2,2])/sum(xTab) # Accuracy
r[1,2] <- xTab[1,1]/sum(xTab[,1]) # Miss Precision
r[1,3] <- xTab[1,1]/sum(xTab[1,]) # Miss Recall
r[1,4] <- (2*r[1,2]*r[1,3])/sum(r[1,2],r[1,3]) # Miss F
r[1,5] <- xTab[2,2]/sum(xTab[,2]) # Hit Precision
r[1,6] <- xTab[2,2]/sum(xTab[2,]) # Hit Recall
r[1,7] <- (2*r[1,5]*r[1,6])/sum(r[1,5],r[1,6]) # Hit F
r}

-

The scale parameter of svm defaults to true in general, but false in the case of sparse input. If you manually set scale=F for the dense matrix approach, you will get the same results as in the sparse case. Unfortunately there's no way to use scaling with sparse input data, as in general the scaled input matrix is no longer sparse.

-
Thanks, that makes sense. –  Robert E Mealey Mar 23 '12 at 21:23