{
  "id": 10783,
  "title": "Reading Training data in R",
  "url": "/competitions/avazu-ctr-prediction/discussion/10783",
  "author_name": "",
  "post_date": "2014-10-29T09:43:23.517Z",
  "votes": 6,
  "comment_count": 5,
  "views": 4460,
  "content": "<p>I found this post on stackoverflow useful to read training data in R. The function read.csv was constantly running out of memory while reading the big training data set.</p>\n<p>library(RSQLite)<br># Create/Connect to a database<br>con &lt;- dbConnect(RSQLite::SQLite(), dbname = &quot;sample_db.sqlite&quot;)</p>\n<p># read csv file into sql database<br># Warning: this is going to take some time and disk space, <br># as your complete CSV file is transferred into an SQLite database.<br>dbWriteTable(con, name=&quot;sample_table&quot;, value=&quot;Your_Big_CSV_File.csv&quot;, <br> row.names=FALSE, header=TRUE, sep = &quot;,&quot;)</p>\n<p># Query your data as you like<br>yourData &lt;- dbGetQuery(con, &quot;SELECT * FROM sample_table LIMIT 10&quot;)</p>\n<p>dbDisconnect(con)</p>",
  "messages": [
    {
      "id": "56969",
      "postDate": "10/29/2014 09:43:23",
      "content": "<p>I found this post on stackoverflow useful to read training data in R. The function read.csv was constantly running out of memory while reading the big training data set.</p>\n<p>library(RSQLite)<br># Create/Connect to a database<br>con &lt;- dbConnect(RSQLite::SQLite(), dbname = &quot;sample_db.sqlite&quot;)</p>\n<p># read csv file into sql database<br># Warning: this is going to take some time and disk space, <br># as your complete CSV file is transferred into an SQLite database.<br>dbWriteTable(con, name=&quot;sample_table&quot;, value=&quot;Your_Big_CSV_File.csv&quot;, <br> row.names=FALSE, header=TRUE, sep = &quot;,&quot;)</p>\n<p># Query your data as you like<br>yourData &lt;- dbGetQuery(con, &quot;SELECT * FROM sample_table LIMIT 10&quot;)</p>\n<p>dbDisconnect(con)</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "56972",
      "postDate": "10/29/2014 10:08:42",
      "content": "<p>Thanks. Might give that a try. So far, I've been relying on a perl one liner - also via stackoverflow - to create a sample of the training set. To create a 1% sample from the terminal you just type:</p>\n<p><code>perl -ne 'print if (rand() &lt; 0.01)'&nbsp;train.csv &gt; trainSample.csv</code></p>\n<p>It only takes a few seconds to run through the entire training set, even on my basic laptop.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "57004",
      "postDate": "10/29/2014 17:54:50",
      "content": "<p>The ff and ffbase packages are great for reading in huge files and storing them on disk. They are transparently accessed using standard data.frame notation:</p>\n<p><code>library(ff)</code></p>\n<p><code>library(ffbase)</code></p>\n<p><code>df &lt;- read.csv.ffdf(file=gzfile('./train_rev2.gz'), VERBOSE=TRUE)<br></code></p>\n<p>You can then sample very easily like so:</p>\n<p><code>s &lt;- sample(nrow(df), nrow(df)*0.01)</code></p>\n<p><code></code><code>df.in.mem &lt;- df[s, ]</code></p>\n<p>I find this workflow to be great for having very quick access to the data I need using the same semantics I use for every day EDA.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "57026",
      "postDate": "10/29/2014 22:36:27",
      "content": "<p>My strategy is to build out an online version of gradient descent, and reading in the files via stdin. I'm using hashing to simulate how R handles factor variables.&nbsp;</p>\n\n<p>Obviously feel free to tell me if I'm going off the deep end. My biggest concern is that the growth of the object will result in long training times (I saw 1.3 seconds for the first 1,000 lines of data, 16 seconds for the first 10K, so these results are definitely less than encouraging to handle 40M.&nbsp;</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "63174",
      "postDate": "01/26/2015 04:43:51",
      "content": "<p>I am using a same method. it seems just reading the file takes a while. My machine has been running for several minutes now and still reading it.</p>\n<p>how long did it take R to read all the data, Zelazny7?</p>\n<p>Thanks</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "63175",
      "postDate": "01/26/2015 04:46:13",
      "content": "<p>It just finished reading. here is total time:</p>\n<p>csv-read=1091.917sec ffdf-write=947.458sec</p>\n<p>TOTAL=2039.375sec</p>",
      "rawMarkdown": "",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 56972,
      "author_name": "cartographic",
      "author_url": "",
      "post_date": "10/29/2014 10:08:42",
      "content": "<p>Thanks. Might give that a try. So far, I've been relying on a perl one liner - also via stackoverflow - to create a sample of the training set. To create a 1% sample from the terminal you just type:</p>\n<p><code>perl -ne 'print if (rand() &lt; 0.01)'&nbsp;train.csv &gt; trainSample.csv</code></p>\n<p>It only takes a few seconds to run through the entire training set, even on my basic laptop.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 57004,
      "author_name": "zelazny7",
      "author_url": "",
      "post_date": "10/29/2014 17:54:50",
      "content": "<p>The ff and ffbase packages are great for reading in huge files and storing them on disk. They are transparently accessed using standard data.frame notation:</p>\n<p><code>library(ff)</code></p>\n<p><code>library(ffbase)</code></p>\n<p><code>df &lt;- read.csv.ffdf(file=gzfile('./train_rev2.gz'), VERBOSE=TRUE)<br></code></p>\n<p>You can then sample very easily like so:</p>\n<p><code>s &lt;- sample(nrow(df), nrow(df)*0.01)</code></p>\n<p><code></code><code>df.in.mem &lt;- df[s, ]</code></p>\n<p>I find this workflow to be great for having very quick access to the data I need using the same semantics I use for every day EDA.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 57026,
      "author_name": "jdr0317",
      "author_url": "",
      "post_date": "10/29/2014 22:36:27",
      "content": "<p>My strategy is to build out an online version of gradient descent, and reading in the files via stdin. I'm using hashing to simulate how R handles factor variables.&nbsp;</p>\n\n<p>Obviously feel free to tell me if I'm going off the deep end. My biggest concern is that the growth of the object will result in long training times (I saw 1.3 seconds for the first 1,000 lines of data, 16 seconds for the first 10K, so these results are definitely less than encouraging to handle 40M.&nbsp;</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 63174,
      "author_name": "nubeee",
      "author_url": "",
      "post_date": "01/26/2015 04:43:51",
      "content": "<p>I am using a same method. it seems just reading the file takes a while. My machine has been running for several minutes now and still reading it.</p>\n<p>how long did it take R to read all the data, Zelazny7?</p>\n<p>Thanks</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 63175,
      "author_name": "nubeee",
      "author_url": "",
      "post_date": "01/26/2015 04:46:13",
      "content": "<p>It just finished reading. here is total time:</p>\n<p>csv-read=1091.917sec ffdf-write=947.458sec</p>\n<p>TOTAL=2039.375sec</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "56969": "",
    "56972": "",
    "57004": "",
    "57026": "",
    "63174": "",
    "63175": ""
  },
  "source": "meta"
}