{
  "id": 2299,
  "title": "Getting Started - Python Sample Code (Random Forest)",
  "url": "/competitions/digit-recognizer/discussion/2299",
  "author_name": "cclark",
  "post_date": "2012-08-02T18:12:38.687000",
  "votes": 28,
  "comment_count": 10,
  "views": null,
  "content": "<p>Here's some quick Python code to generate the Random Forest benchmark. For help setting up your Python environment and other tips, see&nbsp;<a href=\"https://www.kaggle.com/wiki/GettingStartedWithPythonForDataScience\">https://www.kaggle.com/wiki/GettingStartedWithPythonForDataScience</a></p>\r\n<p>Here's the code - have fun!</p>\r\n<blockquote>\r\n<pre>from sklearn.ensemble import RandomForestClassifier<br>from numpy import genfromtxt, savetxt<br><br>def main():<br>    #create the training &amp; test sets, skipping the header row with [1:]<br>    dataset = genfromtxt(open('Data/train.csv','r'), delimiter=',', dtype='f8')[1:]    <br>    target = [x[0] for x in dataset]<br>    train = [x[1:] for x in dataset]<br>    test = genfromtxt(open('Data/test.csv','r'), delimiter=',', dtype='f8')[1:]<br>    <br>    #create and train the random forest<br>    #multi-core CPUs can use: rf = RandomForestClassifier(n_estimators=100, n_jobs=2)<br>    rf = RandomForestClassifier(n_estimators=100)<br>    rf.fit(train, target)<br><br>    savetxt('Data/submission2.csv', rf.predict(test), delimiter=',', fmt='%f')<br><br>if __name__==&quot;__main__&quot;:<br>    main()</pre>\r\n</blockquote>",
  "messages": [
    {
      "id": 12884,
      "postDate": "2012-08-02T18:12:38.687Z",
      "content": "<p>Here's some quick Python code to generate the Random Forest benchmark. For help setting up your Python environment and other tips, see&nbsp;<a href=\"https://www.kaggle.com/wiki/GettingStartedWithPythonForDataScience\">https://www.kaggle.com/wiki/GettingStartedWithPythonForDataScience</a></p>\r\n<p>Here's the code - have fun!</p>\r\n<blockquote>\r\n<pre>from sklearn.ensemble import RandomForestClassifier<br>from numpy import genfromtxt, savetxt<br><br>def main():<br>    #create the training &amp; test sets, skipping the header row with [1:]<br>    dataset = genfromtxt(open('Data/train.csv','r'), delimiter=',', dtype='f8')[1:]    <br>    target = [x[0] for x in dataset]<br>    train = [x[1:] for x in dataset]<br>    test = genfromtxt(open('Data/test.csv','r'), delimiter=',', dtype='f8')[1:]<br>    <br>    #create and train the random forest<br>    #multi-core CPUs can use: rf = RandomForestClassifier(n_estimators=100, n_jobs=2)<br>    rf = RandomForestClassifier(n_estimators=100)<br>    rf.fit(train, target)<br><br>    savetxt('Data/submission2.csv', rf.predict(test), delimiter=',', fmt='%f')<br><br>if __name__==&quot;__main__&quot;:<br>    main()</pre>\r\n</blockquote>",
      "votes": 28
    },
    {
      "id": 135103,
      "postDate": "2016-09-11T05:23:09.187Z",
      "content": "<p>Thanks for the code, but the submission format is a little different from the  sample submission file.</p>\n\n<p>SO I slightly modify the code for saving csv file as follows:</p>\n\n<pre><code>predicted_digits = rf.predict(test)\n\nwith open(&quot;rf_benchmark.csv&quot;, &quot;wb&quot;) as f:\n    writer = csv.writer(f)\n    writer.writerow([&quot;ImageId&quot;,&quot;Label&quot;])\n    for i, predicted_digit in enumerate(predicted_digits):\n        writer.writerow((i+1, int(predicted_digit)))\n</code></pre>",
      "rawMarkdown": "Thanks for the code, but the submission format is a little different from the  sample submission file.\r\n\r\nSO I slightly modify the code for saving csv file as follows:\r\n\r\n    predicted_digits = rf.predict(test)\r\n\r\n    with open(\"rf_benchmark.csv\", \"wb\") as f:\r\n        writer = csv.writer(f)\r\n        writer.writerow([\"ImageId\",\"Label\"])\r\n        for i, predicted_digit in enumerate(predicted_digits):\r\n            writer.writerow((i+1, int(predicted_digit)))\r\n",
      "votes": 2
    },
    {
      "id": 140705,
      "postDate": "2016-10-22T12:06:51.730Z",
      "content": "<p>Hi,</p>\n\n<p>I couldn't find anything at  <a href=\"https://www.kaggle.com/wiki/GettingStartedWithPythonForDataScience\">https://www.kaggle.com/wiki/GettingStartedWithPythonForDataScience</a></p>\n\n<p>can anyone help, please?</p>",
      "rawMarkdown": "Hi,\r\n\r\nI couldn't find anything at  https://www.kaggle.com/wiki/GettingStartedWithPythonForDataScience\r\n\r\ncan anyone help, please?",
      "votes": 1
    },
    {
      "id": 13725,
      "postDate": "2012-08-31T23:07:44.907Z",
      "content": "<p>To save people just getting starting from having to search for package names:</p>\r\n<p>pip install scipy numpy scikit-learn</p>",
      "votes": 3
    },
    {
      "id": 1534699,
      "postDate": "2021-10-05T07:00:05.917Z",
      "content": "<p>Thank you so much for this short tutorial, this has made the entire process of working with the random forest regressor model very easy. <a href=\"https://www.kaggle.com/cclark\" target=\"_blank\">@cclark</a> </p>",
      "rawMarkdown": "Thank you so much for this short tutorial, this has made the entire process of working with the random forest regressor model very easy. @cclark "
    },
    {
      "id": 156940,
      "postDate": "2017-01-18T17:35:23.053Z",
      "content": "<p>Did you try the decode command?\nEg: text.decode(encoding)</p>",
      "rawMarkdown": "Did you try the decode command?\r\nEg: text.decode(encoding)"
    },
    {
      "id": 156937,
      "postDate": "2017-01-18T17:11:21.890Z",
      "content": "<p>hi, \nI'm getting error :\nCan't Convert 'bytes' object to str implicitly\n             what should I do ???</p>",
      "rawMarkdown": "hi, \r\nI'm getting error :\r\nCan't Convert 'bytes' object to str implicitly\r\n             what should I do ???"
    },
    {
      "id": 72381,
      "postDate": "2015-04-19T02:37:56.273Z",
      "content": "<p>This was super helpful! &nbsp;Thank you so much!</p>"
    },
    {
      "id": 25804,
      "postDate": "2013-06-17T12:54:13.817Z",
      "content": "<p>Hello All.</p>\r\n<p>I really appreciate the tutorial in the wiki titled &quot;<span>Getting Started With Python For Data Science&quot;. It was signed by Chris Clark and last updated by &nbsp;<span>Kirstin Rhys. I had no idea where to start on Kaggle and this gave me a starting point.</span></span></p>\r\n<p>After trying several Python and numerical module installs (I don't get the 2.X versus 3.X series of Python), I finally got around the memory errors and found a combo that would run the Random Forest example (Python 2.7 64-bit windows, with 64-bit windows\r\n versions of NumPy, SciPy, and SciKit-Learn; although I never got setuptools, iPython, or PyDev to work); as long as I didn't try to use more than 1 core.</p>\r\n<p>Once I finally got the program to run I got this error: &quot;ValueError: Number of features of the model must &nbsp;match the input. Model n_features is 784 and &nbsp;input n_features is 1776&quot;.</p>\r\n<p>I'll list the whole traceback at the bottom. Any help to get this moving again would be greatly appreciated. I'm stuck and since I was blindly following an example to get a feel for how Kaggle works, I don't know where to begin debugging. The Value Error\r\n seems deep in one of the numerical packages.</p>\r\n<p></p>\r\n<p>Thanks,</p>\r\n<p>--&gt;Rob</p>\r\n<p></p>\r\n<p><strong><span style=\"text-decoration:underline\">COMPLETE ERROR MESSAGE:</span></strong></p>\r\n<p>Traceback (most recent call last):<br>\r\nFile &quot;C:\\Users\\Robert\\Documents\\Data Science Class\\week5-Machine Learning\\Kaggle Assignment\\bioresp.py&quot;, line 20, in &lt;module&gt;<br>\r\nmain()<br>\r\nFile &quot;C:\\Users\\Robert\\Documents\\Data Science Class\\week5-Machine Learning\\Kaggle Assignment\\bioresp.py&quot;, line 15, in main<br>\r\npredicted_probs = [x[1] for x in rf.predict_proba(test)]<br>\r\nFile &quot;C:\\Python27\\lib\\site-packages\\sklearn\\ensemble\\forest.py&quot;, line 560, in predict_proba<br>\r\nfor i in xrange(n_jobs))<br>\r\nFile &quot;C:\\Python27\\lib\\site-packages\\sklearn\\externals\\joblib\\parallel.py&quot;, line 514, in __call__<br>\r\nself.dispatch(function, args, kwargs)<br>\r\nFile &quot;C:\\Python27\\lib\\site-packages\\sklearn\\externals\\joblib\\parallel.py&quot;, line 311, in dispatch<br>\r\njob = ImmediateApply(func, args, kwargs)<br>\r\nFile &quot;C:\\Python27\\lib\\site-packages\\sklearn\\externals\\joblib\\parallel.py&quot;, line 135, in __init__<br>\r\nself.results = func(*args, **kwargs)<br>\r\nFile &quot;C:\\Python27\\lib\\site-packages\\sklearn\\ensemble\\forest.py&quot;, line 123, in _parallel_predict_proba<br>\r\nproba_tree = tree.predict_proba(X)<br>\r\nFile &quot;C:\\Python27\\lib\\site-packages\\sklearn\\tree\\tree.py&quot;, line 572, in predict_proba<br>\r\n% (self.n_features_, n_features))<br>\r\nValueError: Number of features of the model must match the input. Model n_features is 784 and input n_features is 1776</p>\r\n<p></p>\r\n<p></p>\r\n<p></p>\r\n<p></p>\r\n<p></p>\r\n<p></p>\r\n<p></p>\r\n<p></p>\r\n<p></p>"
    },
    {
      "id": 14034,
      "postDate": "2012-09-07T00:43:18.503Z",
      "content": "<p>I seem to be running into MemoryError<br>\r\nSystem specs:<br>\r\n4GB RAM<br>\r\nWindows 7 64 bit<br>\r\nCore i7 @ 2.00 GHz</p>"
    },
    {
      "id": 1619540,
      "postDate": "2021-12-16T03:18:07.123Z",
      "content": "<p>hi. this was super helpful. thank you!</p>",
      "rawMarkdown": "hi. this was super helpful. thank you!"
    }
  ],
  "comments": [
    {
      "id": 135103,
      "author_name": "hysic",
      "author_url": "",
      "post_date": "2016-09-11T05:23:09.187000",
      "content": "<p>Thanks for the code, but the submission format is a little different from the  sample submission file.</p>\n\n<p>SO I slightly modify the code for saving csv file as follows:</p>\n\n<pre><code>predicted_digits = rf.predict(test)\n\nwith open(&quot;rf_benchmark.csv&quot;, &quot;wb&quot;) as f:\n    writer = csv.writer(f)\n    writer.writerow([&quot;ImageId&quot;,&quot;Label&quot;])\n    for i, predicted_digit in enumerate(predicted_digits):\n        writer.writerow((i+1, int(predicted_digit)))\n</code></pre>",
      "votes": 2,
      "replies": []
    },
    {
      "id": 140705,
      "author_name": "PRB",
      "author_url": "",
      "post_date": "2016-10-22T12:06:51.730000",
      "content": "<p>Hi,</p>\n\n<p>I couldn't find anything at  <a href=\"https://www.kaggle.com/wiki/GettingStartedWithPythonForDataScience\">https://www.kaggle.com/wiki/GettingStartedWithPythonForDataScience</a></p>\n\n<p>can anyone help, please?</p>",
      "votes": 1,
      "replies": []
    },
    {
      "id": 13725,
      "author_name": "ferrouswheel",
      "author_url": "",
      "post_date": "2012-08-31T23:07:44.907000",
      "content": "<p>To save people just getting starting from having to search for package names:</p>\r\n<p>pip install scipy numpy scikit-learn</p>",
      "votes": 3,
      "replies": []
    },
    {
      "id": 1534699,
      "author_name": "HeisenbergOP",
      "author_url": "",
      "post_date": "2021-10-05T07:00:05.917000",
      "content": "<p>Thank you so much for this short tutorial, this has made the entire process of working with the random forest regressor model very easy. <a href=\"https://www.kaggle.com/cclark\" target=\"_blank\">@cclark</a> </p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 156940,
      "author_name": "Jitesh Krishna",
      "author_url": "",
      "post_date": "2017-01-18T17:35:23.053000",
      "content": "<p>Did you try the decode command?\nEg: text.decode(encoding)</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 156937,
      "author_name": "DaminiKataria",
      "author_url": "",
      "post_date": "2017-01-18T17:11:21.890000",
      "content": "<p>hi, \nI'm getting error :\nCan't Convert 'bytes' object to str implicitly\n             what should I do ???</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 72381,
      "author_name": "Kevin Zhao",
      "author_url": "",
      "post_date": "2015-04-19T02:37:56.273000",
      "content": "<p>This was super helpful! &nbsp;Thank you so much!</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 25804,
      "author_name": "Rob Godbey",
      "author_url": "",
      "post_date": "2013-06-17T12:54:13.817000",
      "content": "<p>Hello All.</p>\r\n<p>I really appreciate the tutorial in the wiki titled &quot;<span>Getting Started With Python For Data Science&quot;. It was signed by Chris Clark and last updated by &nbsp;<span>Kirstin Rhys. I had no idea where to start on Kaggle and this gave me a starting point.</span></span></p>\r\n<p>After trying several Python and numerical module installs (I don't get the 2.X versus 3.X series of Python), I finally got around the memory errors and found a combo that would run the Random Forest example (Python 2.7 64-bit windows, with 64-bit windows\r\n versions of NumPy, SciPy, and SciKit-Learn; although I never got setuptools, iPython, or PyDev to work); as long as I didn't try to use more than 1 core.</p>\r\n<p>Once I finally got the program to run I got this error: &quot;ValueError: Number of features of the model must &nbsp;match the input. Model n_features is 784 and &nbsp;input n_features is 1776&quot;.</p>\r\n<p>I'll list the whole traceback at the bottom. Any help to get this moving again would be greatly appreciated. I'm stuck and since I was blindly following an example to get a feel for how Kaggle works, I don't know where to begin debugging. The Value Error\r\n seems deep in one of the numerical packages.</p>\r\n<p></p>\r\n<p>Thanks,</p>\r\n<p>--&gt;Rob</p>\r\n<p></p>\r\n<p><strong><span style=\"text-decoration:underline\">COMPLETE ERROR MESSAGE:</span></strong></p>\r\n<p>Traceback (most recent call last):<br>\r\nFile &quot;C:\\Users\\Robert\\Documents\\Data Science Class\\week5-Machine Learning\\Kaggle Assignment\\bioresp.py&quot;, line 20, in &lt;module&gt;<br>\r\nmain()<br>\r\nFile &quot;C:\\Users\\Robert\\Documents\\Data Science Class\\week5-Machine Learning\\Kaggle Assignment\\bioresp.py&quot;, line 15, in main<br>\r\npredicted_probs = [x[1] for x in rf.predict_proba(test)]<br>\r\nFile &quot;C:\\Python27\\lib\\site-packages\\sklearn\\ensemble\\forest.py&quot;, line 560, in predict_proba<br>\r\nfor i in xrange(n_jobs))<br>\r\nFile &quot;C:\\Python27\\lib\\site-packages\\sklearn\\externals\\joblib\\parallel.py&quot;, line 514, in __call__<br>\r\nself.dispatch(function, args, kwargs)<br>\r\nFile &quot;C:\\Python27\\lib\\site-packages\\sklearn\\externals\\joblib\\parallel.py&quot;, line 311, in dispatch<br>\r\njob = ImmediateApply(func, args, kwargs)<br>\r\nFile &quot;C:\\Python27\\lib\\site-packages\\sklearn\\externals\\joblib\\parallel.py&quot;, line 135, in __init__<br>\r\nself.results = func(*args, **kwargs)<br>\r\nFile &quot;C:\\Python27\\lib\\site-packages\\sklearn\\ensemble\\forest.py&quot;, line 123, in _parallel_predict_proba<br>\r\nproba_tree = tree.predict_proba(X)<br>\r\nFile &quot;C:\\Python27\\lib\\site-packages\\sklearn\\tree\\tree.py&quot;, line 572, in predict_proba<br>\r\n% (self.n_features_, n_features))<br>\r\nValueError: Number of features of the model must match the input. Model n_features is 784 and input n_features is 1776</p>\r\n<p></p>\r\n<p></p>\r\n<p></p>\r\n<p></p>\r\n<p></p>\r\n<p></p>\r\n<p></p>\r\n<p></p>\r\n<p></p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 14034,
      "author_name": "mayank",
      "author_url": "",
      "post_date": "2012-09-07T00:43:18.503000",
      "content": "<p>I seem to be running into MemoryError<br>\r\nSystem specs:<br>\r\n4GB RAM<br>\r\nWindows 7 64 bit<br>\r\nCore i7 @ 2.00 GHz</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 1619540,
      "author_name": "suptudy",
      "author_url": "",
      "post_date": "2021-12-16T03:18:07.123000",
      "content": "<p>hi. this was super helpful. thank you!</p>",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "12884": "",
    "135103": "Thanks for the code, but the submission format is a little different from the  sample submission file.\r\n\r\nSO I slightly modify the code for saving csv file as follows:\r\n\r\n    predicted_digits = rf.predict(test)\r\n\r\n    with open(\"rf_benchmark.csv\", \"wb\") as f:\r\n        writer = csv.writer(f)\r\n        writer.writerow([\"ImageId\",\"Label\"])\r\n        for i, predicted_digit in enumerate(predicted_digits):\r\n            writer.writerow((i+1, int(predicted_digit)))\r\n",
    "140705": "Hi,\r\n\r\nI couldn't find anything at  https://www.kaggle.com/wiki/GettingStartedWithPythonForDataScience\r\n\r\ncan anyone help, please?",
    "13725": "",
    "1534699": "Thank you so much for this short tutorial, this has made the entire process of working with the random forest regressor model very easy. @cclark ",
    "156940": "Did you try the decode command?\r\nEg: text.decode(encoding)",
    "156937": "hi, \r\nI'm getting error :\r\nCan't Convert 'bytes' object to str implicitly\r\n             what should I do ???",
    "72381": "",
    "25804": "",
    "14034": "",
    "1619540": "hi. this was super helpful. thank you!"
  }
}