'Using sparse matrix in random forest

For efficient memory utilization, I have used “matrix" package in R to create sparse matrix using code:

library(randomForest)
library(Matrix)
documentTermMatrixFrame <- Matrix(documentTermMatrixFrame, sparse = TRUE)
trainSet <- documentTermMatrixFrame[1:750,]
testSet <- documentTermMatrixFrame[751:999,]
fit <- randomForest(trainingColumnNames, data=trainSet)

executing random forest got error as below

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

<html>
  
  <title> sample of dataset</title>
  <h1>This is a sample of dataset which i am using</h1>
    <table style="width:100%">
  <tr>
    <th>nitemid</th>
    <th>sUnSpsc</th> 
    <th>productDescription</th>
  </tr>
      <tr>
    <td>7460893</td>
    <td>26121609 </td> 
    <td>Category 6A, Advanced MaTriX, 4-pair, 23 AWG, U/UTP copper cable, Plenum (CMP) Rated, White, 1000ft/305m ""</td>
  </tr>
       <tr>
    <td>7460456</td>
    <td>26121709 </td> 
    <td>Shielded marine MUD-resistant armored copper cable, category 7 S/FTP, low smoke zero halogen (LSZH), 4-pair, conductors are 22 AWG construction with foamed PE insulation, twisted in pairs</td>
  </tr>
       <tr>
    <td>7460856</td>
    <td>26121890 </td> 
    <td>1 PC. = 100 M 2 X 1.5 QMM, 100M SPECIAL DESIGN TO UL CLASS 2 YELLOW TPE OIL-RESISTANT AS-INTERFACE SHAPED CABLE</td>
  </tr>
</html>

Now I need to know how to use that sparse matrix as input to randomforest() function in randomForest package.. Could anyone please help me out??

Thanks in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source