{"action":"created","issue":{"url":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/4879","labels_url":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/4879/labels{/name}","comments_url":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/4879/comments","events_url":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/4879/events","html_url":"https://github.com/scikit-learn/scikit-learn/issues/4879","id":89825596,"number":4879,"title":"RandomForestClassifier changes label values","user":{"login":"mbatchkarov","id":937114,"avatar_url":"https://avatars.githubusercontent.com/u/937114?v=3","gravatar_id":"","url":"https://api.github.com/users/mbatchkarov","html_url":"https://github.com/mbatchkarov","followers_url":"https://api.github.com/users/mbatchkarov/followers","following_url":"https://api.github.com/users/mbatchkarov/following{/other_user}","gists_url":"https://api.github.com/users/mbatchkarov/gists{/gist_id}","starred_url":"https://api.github.com/users/mbatchkarov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mbatchkarov/subscriptions","organizations_url":"https://api.github.com/users/mbatchkarov/orgs","repos_url":"https://api.github.com/users/mbatchkarov/repos","events_url":"https://api.github.com/users/mbatchkarov/events{/privacy}","received_events_url":"https://api.github.com/users/mbatchkarov/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-06-20T22:16:55Z","updated_at":"2015-06-21T07:44:52Z","closed_at":null,"body":"Consider the following example:\r\n```python\r\nimport numpy as np\r\nfrom sklearn.ensemble import RandomForestClassifier\r\n\r\nCLASSES = 15\r\nX = np.arange(CLASSES).reshape(-1, 1)\r\ny = [ch for ch in 'ABCDEFGHIJKLMNOPQRSTU'[:CLASSES]]\r\n\r\nprint(RandomForestClassifier().fit(X, y).predict(X))\r\n```\r\n\r\nThe output is\r\n\r\n`['A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'B' 'B' 'B' 'B' 'B']`\r\n\r\nwhereas I expect it to be\r\n\r\n`['A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O']`\r\n\r\nI traced the cause of the error to [here](https://github.com/scikit-learn/scikit-learn/blob/733629d256abaf9a31d6e6305f859768432907ed/sklearn/ensemble/forest.py#L418). I'm happy to put in a pull request, but can others please verify they get the same unexpected behaviour first?\r\n\r\nSystem information:\r\n``\r\nCPython 3.3.5\r\nIPython 3.1.0\r\n\r\nnumpy 1.9.2\r\nscipy 0.14.0\r\nscikit-learn 0.16.1(conda)\r\n\r\ncompiler   : GCC 4.2.1 (Apple Inc. build 5577)\r\nsystem     : Darwin\r\nrelease    : 13.4.0\r\nmachine    : x86_64\r\nprocessor  : i386\r\nCPU cores  : 4\r\ninterpreter: 64bit\r\n```"},"comment":{"url":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/comments/113871328","html_url":"https://github.com/scikit-learn/scikit-learn/issues/4879#issuecomment-113871328","issue_url":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/4879","id":113871328,"user":{"login":"mbatchkarov","id":937114,"avatar_url":"https://avatars.githubusercontent.com/u/937114?v=3","gravatar_id":"","url":"https://api.github.com/users/mbatchkarov","html_url":"https://github.com/mbatchkarov","followers_url":"https://api.github.com/users/mbatchkarov/followers","following_url":"https://api.github.com/users/mbatchkarov/following{/other_user}","gists_url":"https://api.github.com/users/mbatchkarov/gists{/gist_id}","starred_url":"https://api.github.com/users/mbatchkarov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mbatchkarov/subscriptions","organizations_url":"https://api.github.com/users/mbatchkarov/orgs","repos_url":"https://api.github.com/users/mbatchkarov/repos","events_url":"https://api.github.com/users/mbatchkarov/events{/privacy}","received_events_url":"https://api.github.com/users/mbatchkarov/received_events","type":"User","site_admin":false},"created_at":"2015-06-21T07:44:52Z","updated_at":"2015-06-21T07:44:52Z","body":"I'll have a closer look later. Currently I've got the following evidence:\r\n\r\n1) `y` changes at [this line](https://github.com/scikit-learn/scikit-learn/blob/733629d256abaf9a31d6e6305f859768432907ed/sklearn/ensemble/forest.py#L222). What goes into the `_validate_y_class_weight` is \r\n```\r\n>>> y.T\r\nOut[1]: \r\narray([['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O']], \r\n      dtype='<U1'\r\n``` \r\nand what comes out is\r\n```\r\n>>> y.T\r\nOut[3]: \r\narray([['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '1', '1', '1', '1', '1']], \r\n      dtype='<U1') \r\n```\r\n\r\nI always get suspicious when I see digits encoded as str, so I tried this instead\r\n2)\r\n\r\n```\r\n>>> RandomForestClassifier().fit(X, LabelEncoder().fit_transform(y)).predict(X)\r\nOut[2]: array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])\r\n```\r\nI agree this could be the effect of bootstrapping.\r\n\r\n3) I first encountered this issue when training on a larger dataset, with 15 classes and 100-200 instances per class. In each case the first 10 classes were predicted correctly, and all other classes were predicted as `B`. 10 seems like a magic number that appears because of labels being encoded as digits. \r\n\r\nWhen I eliminate one of the labels, e.g. `A`:\r\n\r\n```\r\ny = [ch for ch in 'BBCDEFGHIJKLMNOPQRSTU'[:CLASSES]]\r\nRandomForestClassifier().fit(X, y).predict(X)\r\nOut[1]: \r\narray(['B', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'C', 'C', 'C', 'C'], \r\n      dtype='<U1')\r\n```\r\n\r\nit is again the first 10 labels that are predicted correctly, and all others are predicted as the second label. With `y = [ch for ch in 'BBBDEFGHIJKLMNOPQRSTU'[:CLASSES]]`, I get a bunch of `D`s at the end of the array."}}