{
  "id": 5692,
  "title": "Really Fast PYPY code to load contest data",
  "url": "/competitions/accelerometer-biometric-competition/discussion/5692",
  "author_name": "",
  "post_date": "2013-09-07T19:18:47.157Z",
  "votes": null,
  "comment_count": 4,
  "views": 1799,
  "content": "<p>After waiting countless hours trying to re-load and re-process the train and test files for this contest, I investigated faster versions of Python and discovered PYPY. &nbsp;</p>\n<p>PYPY has a JIT compiler for raw python code (not all libraries are supported).&nbsp; But it runs REALLY fast (on par with straight c code)</p>\n<p>For anyone still struggling with this&nbsp; Here is an excerpt from my generate features code.&nbsp; I just cut and pasted some of the key lines, so you will have to tailor it for your needs...</p>\n<p>What you need to run this:</p>\n<p>1.Install PYPY</p>\n<p>2.Change permissions on the file to allow execute</p>\n<p>3.Run it from the command line:&nbsp; ./gen_features.py</p>\n<p>&nbsp;</p>\n<p>&nbsp;</p>\n<p>#!/usr/bin/pypy<br><br>import os<br>import csv<br><br>parseStr = lambda x: float(x) if '.' in x else int(x)<br><br>allData = []<br>os.nice(9)</p>\n<p>f=open('train.csv')<br>reader = csv.reader(f)<br>for row in reader:<br>&nbsp;&nbsp;&nbsp; if row[0]== 'T':&nbsp; # skip first row.&nbsp; TODO: move this before the loop <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; continue<br>&nbsp;&nbsp;&nbsp; else:&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; convertedRow = []<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for token in row:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newTok = parseStr(token)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except (ValueError):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print token<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise ValueError<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; convertedRow.append(newTok)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; allData.append(convertedRow)<br>f.close()&nbsp;&nbsp; &nbsp;<br><br><br></p>",
  "messages": [
    {
      "id": "30445",
      "postDate": "09/07/2013 19:18:47",
      "content": "<p>After waiting countless hours trying to re-load and re-process the train and test files for this contest, I investigated faster versions of Python and discovered PYPY. &nbsp;</p>\n<p>PYPY has a JIT compiler for raw python code (not all libraries are supported).&nbsp; But it runs REALLY fast (on par with straight c code)</p>\n<p>For anyone still struggling with this&nbsp; Here is an excerpt from my generate features code.&nbsp; I just cut and pasted some of the key lines, so you will have to tailor it for your needs...</p>\n<p>What you need to run this:</p>\n<p>1.Install PYPY</p>\n<p>2.Change permissions on the file to allow execute</p>\n<p>3.Run it from the command line:&nbsp; ./gen_features.py</p>\n<p>&nbsp;</p>\n<p>&nbsp;</p>\n<p>#!/usr/bin/pypy<br><br>import os<br>import csv<br><br>parseStr = lambda x: float(x) if '.' in x else int(x)<br><br>allData = []<br>os.nice(9)</p>\n<p>f=open('train.csv')<br>reader = csv.reader(f)<br>for row in reader:<br>&nbsp;&nbsp;&nbsp; if row[0]== 'T':&nbsp; # skip first row.&nbsp; TODO: move this before the loop <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; continue<br>&nbsp;&nbsp;&nbsp; else:&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; convertedRow = []<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for token in row:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newTok = parseStr(token)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except (ValueError):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print token<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise ValueError<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; convertedRow.append(newTok)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; allData.append(convertedRow)<br>f.close()&nbsp;&nbsp; &nbsp;<br><br><br></p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "30463",
      "postDate": "09/08/2013 02:07:11",
      "content": "<p>I suppose you meant:</p>\n<p>...</p>\n<p>os.nice(9)</p>\n<p>f = open('train.csv')</p>\n<p>...</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "30467",
      "postDate": "09/08/2013 03:28:53",
      "content": "<p>Yes...good catch.&nbsp; (I had copy and paste issue.)</p>\n<p>&nbsp;fixed above</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "30515",
      "postDate": "09/09/2013 02:38:05",
      "content": "<p>Another way of having very fast IO is to use the HDFS pandas support, eg:</p>\n<p>&nbsp;</p>\n<p><code>import pandas as pd<br>store = pd.HDFStore('data_store.h5')<br>train = pd.read_csv('train.csv')<br>train.sort(['Device', 'T'], ascending = [True, True], inplace = True)<br>store['train'] = train<br>store.close()</code></p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "30529",
      "postDate": "09/09/2013 13:32:44",
      "content": "<p>&nbsp;</p>\n<p>&nbsp;</p>\n<p>Thanks, I looked into HDF a bit but have not used it.&nbsp; Seems like it saves a lot of time parsing and data conversion each time you want to load.</p>\n<p>Of course, the real benefit of&nbsp; PYPY is the high speed number crunching AFTER your data is loaded.&nbsp; The code I posted is actually slower in loading than pandas.&nbsp;</p>\n<p>However, once loaded, PYPY can do high speed processing of the data.&nbsp; I was able to reduce my feature generation from 4 hours down to 4 minutes (on test.csv)!&nbsp;&nbsp;</p>\n<p>&nbsp;</p>\n<p>&nbsp;</p>\n<p>&nbsp;</p>",
      "rawMarkdown": "",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 30463,
      "author_name": "ghegoo",
      "author_url": "",
      "post_date": "09/08/2013 02:07:11",
      "content": "<p>I suppose you meant:</p>\n<p>...</p>\n<p>os.nice(9)</p>\n<p>f = open('train.csv')</p>\n<p>...</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 30467,
      "author_name": "geringer",
      "author_url": "",
      "post_date": "09/08/2013 03:28:53",
      "content": "<p>Yes...good catch.&nbsp; (I had copy and paste issue.)</p>\n<p>&nbsp;fixed above</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 30515,
      "author_name": "ghegoo",
      "author_url": "",
      "post_date": "09/09/2013 02:38:05",
      "content": "<p>Another way of having very fast IO is to use the HDFS pandas support, eg:</p>\n<p>&nbsp;</p>\n<p><code>import pandas as pd<br>store = pd.HDFStore('data_store.h5')<br>train = pd.read_csv('train.csv')<br>train.sort(['Device', 'T'], ascending = [True, True], inplace = True)<br>store['train'] = train<br>store.close()</code></p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 30529,
      "author_name": "geringer",
      "author_url": "",
      "post_date": "09/09/2013 13:32:44",
      "content": "<p>&nbsp;</p>\n<p>&nbsp;</p>\n<p>Thanks, I looked into HDF a bit but have not used it.&nbsp; Seems like it saves a lot of time parsing and data conversion each time you want to load.</p>\n<p>Of course, the real benefit of&nbsp; PYPY is the high speed number crunching AFTER your data is loaded.&nbsp; The code I posted is actually slower in loading than pandas.&nbsp;</p>\n<p>However, once loaded, PYPY can do high speed processing of the data.&nbsp; I was able to reduce my feature generation from 4 hours down to 4 minutes (on test.csv)!&nbsp;&nbsp;</p>\n<p>&nbsp;</p>\n<p>&nbsp;</p>\n<p>&nbsp;</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "30445": "",
    "30463": "",
    "30467": "",
    "30515": "",
    "30529": ""
  },
  "source": "meta"
}