0
votes

I have converted a dataframe (10430 X 1415) into a Sparse Matrix using Matrix package. I sampled the data into Training data and Testing data. I want to build an SVM model ('e1071') using the Sparse Matrix. Could any one please help me in getting the result. Below is the code that I am trying.

library('e1071')

svm.model<-svm(trainData[,"Target"] ~.,data= trainData,kernel='linear', scale=FALSE)

Error: "cannot coerce class "structure("dgCMatrix", package = "Matrix")" to a data.frame"

Also Please suggest how to use predict function on the testData.

1

1 Answers

-1
votes

To create a sparse matrix you can do this:

library(Matrix)
library(SpareM)
sMatrix <- Matrix(data=as.matrix(trainData), sparse=TRUE)

Then you can use that on your svm model

svm.model <- svm(trainData[,"Target"] ~., data=sMatrix, kernel="linear")

To predict you should make sure your train data has the correct format and then you can use:

predicted <- predict(svm.model, testData)