{"action":"created","comment":{"url":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/comments/33699396","id":33699396,"diff_hunk":"@@ -332,6 +332,126 @@ def __len__(self):\n         return self.n_folds\n \n \n+def disjoint_label_folds(labels, n_folds=3):\n+    \"\"\"Creates folds where a same label is not in two different folds.\n+    \n+    Parameters\n+    ----------\n+    labels: numpy array, shape (n_samples,)\n+        Contains an id for each sample.\n+        The folds are built so that the same id doesn't appear in two different folds.\n+    \n+    n_folds: int, default=3\n+        Number of folds to split the data into.\n+        \n+    Returns\n+    -------\n+    folds: numpy array of shape (n_samples, )\n+        Array of integers between 0 and (n_folds - 1).\n+        Folds[i] contains the folds to which sample i is assigned.\n+        \n+    Notes\n+    -----\n+    The folds are built by distributing the labels by frequency of appearance.\n+    The number of labels has to be at least equal to the number of folds.\n+    \"\"\"\n+    labels = np.array(labels)\n+    unique_labels, labels = np.unique(labels, return_inverse=True)\n+    n_labels = len(unique_labels)\n+    if n_folds > n_labels:\n+        raise ValueError(\n+                (\"Cannot have number of folds n_folds={0} greater\"\n+                 \" than the number of labels: {1}.\").format(n_folds, n_labels))\n+    \n+    # number of occurrence of each label (its \"weight\")\n+    samples_per_label = np.bincount(labels)\n+    # We want to distribute the most frequent labels first\n+    ind = np.argsort(samples_per_label, kind='mergesort')[::-1]\n+    samples_per_label = samples_per_label[ind]\n+\n+    # Total weight of each fold\n+    samples_per_fold = np.zeros(n_folds)","path":"sklearn/cross_validation.py","position":42,"original_position":42,"commit_id":"90275bee75aa2e2fe5347ae805864d4abd96b58f","original_commit_id":"90275bee75aa2e2fe5347ae805864d4abd96b58f","user":{"login":"ogrisel","id":89061,"avatar_url":"https://avatars.githubusercontent.com/u/89061?v=3","gravatar_id":"","url":"https://api.github.com/users/ogrisel","html_url":"https://github.com/ogrisel","followers_url":"https://api.github.com/users/ogrisel/followers","following_url":"https://api.github.com/users/ogrisel/following{/other_user}","gists_url":"https://api.github.com/users/ogrisel/gists{/gist_id}","starred_url":"https://api.github.com/users/ogrisel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ogrisel/subscriptions","organizations_url":"https://api.github.com/users/ogrisel/orgs","repos_url":"https://api.github.com/users/ogrisel/repos","events_url":"https://api.github.com/users/ogrisel/events{/privacy}","received_events_url":"https://api.github.com/users/ogrisel/received_events","type":"User","site_admin":false},"body":"I think this should be `np.zeros(len(unique_labels), dtype=np.uint64)` instead.","created_at":"2015-07-01T16:57:52Z","updated_at":"2015-07-01T16:57:52Z","html_url":"https://github.com/scikit-learn/scikit-learn/pull/4444#discussion_r33699396","pull_request_url":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/4444","_links":{"self":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/comments/33699396"},"html":{"href":"https://github.com/scikit-learn/scikit-learn/pull/4444#discussion_r33699396"},"pull_request":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/4444"}}},"pull_request":{"url":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/4444","id":31839052,"html_url":"https://github.com/scikit-learn/scikit-learn/pull/4444","diff_url":"https://github.com/scikit-learn/scikit-learn/pull/4444.diff","patch_url":"https://github.com/scikit-learn/scikit-learn/pull/4444.patch","issue_url":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/4444","number":4444,"state":"open","locked":false,"title":"[MRG + 1] Added DisjointLabelKFold to perform K-Fold cv on sets with disjoint labels. ","user":{"login":"JeanKossaifi","id":832583,"avatar_url":"https://avatars.githubusercontent.com/u/832583?v=3","gravatar_id":"","url":"https://api.github.com/users/JeanKossaifi","html_url":"https://github.com/JeanKossaifi","followers_url":"https://api.github.com/users/JeanKossaifi/followers","following_url":"https://api.github.com/users/JeanKossaifi/following{/other_user}","gists_url":"https://api.github.com/users/JeanKossaifi/gists{/gist_id}","starred_url":"https://api.github.com/users/JeanKossaifi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JeanKossaifi/subscriptions","organizations_url":"https://api.github.com/users/JeanKossaifi/orgs","repos_url":"https://api.github.com/users/JeanKossaifi/repos","events_url":"https://api.github.com/users/JeanKossaifi/events{/privacy}","received_events_url":"https://api.github.com/users/JeanKossaifi/received_events","type":"User","site_admin":false},"body":"Added a SubjectIndependentKFold class to create subject independent folds.","created_at":"2015-03-24T15:43:46Z","updated_at":"2015-07-01T16:57:52Z","closed_at":null,"merged_at":null,"merge_commit_sha":"eb2328f427dff0e0621bddd282ed52cf8cce6371","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/4444/commits","review_comments_url":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/4444/comments","review_comment_url":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/comments{/number}","comments_url":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/4444/comments","statuses_url":"https://api.github.com/repos/scikit-learn/scikit-learn/statuses/90275bee75aa2e2fe5347ae805864d4abd96b58f","head":{"label":"JeanKossaifi:sid_kfold","ref":"sid_kfold","sha":"90275bee75aa2e2fe5347ae805864d4abd96b58f","user":{"login":"JeanKossaifi","id":832583,"avatar_url":"https://avatars.githubusercontent.com/u/832583?v=3","gravatar_id":"","url":"https://api.github.com/users/JeanKossaifi","html_url":"https://github.com/JeanKossaifi","followers_url":"https://api.github.com/users/JeanKossaifi/followers","following_url":"https://api.github.com/users/JeanKossaifi/following{/other_user}","gists_url":"https://api.github.com/users/JeanKossaifi/gists{/gist_id}","starred_url":"https://api.github.com/users/JeanKossaifi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JeanKossaifi/subscriptions","organizations_url":"https://api.github.com/users/JeanKossaifi/orgs","repos_url":"https://api.github.com/users/JeanKossaifi/repos","events_url":"https://api.github.com/users/JeanKossaifi/events{/privacy}","received_events_url":"https://api.github.com/users/JeanKossaifi/received_events","type":"User","site_admin":false},"repo":{"id":27300266,"name":"scikit-learn","full_name":"JeanKossaifi/scikit-learn","owner":{"login":"JeanKossaifi","id":832583,"avatar_url":"https://avatars.githubusercontent.com/u/832583?v=3","gravatar_id":"","url":"https://api.github.com/users/JeanKossaifi","html_url":"https://github.com/JeanKossaifi","followers_url":"https://api.github.com/users/JeanKossaifi/followers","following_url":"https://api.github.com/users/JeanKossaifi/following{/other_user}","gists_url":"https://api.github.com/users/JeanKossaifi/gists{/gist_id}","starred_url":"https://api.github.com/users/JeanKossaifi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JeanKossaifi/subscriptions","organizations_url":"https://api.github.com/users/JeanKossaifi/orgs","repos_url":"https://api.github.com/users/JeanKossaifi/repos","events_url":"https://api.github.com/users/JeanKossaifi/events{/privacy}","received_events_url":"https://api.github.com/users/JeanKossaifi/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/JeanKossaifi/scikit-learn","description":"scikit-learn: machine learning in Python","fork":true,"url":"https://api.github.com/repos/JeanKossaifi/scikit-learn","forks_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/forks","keys_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/teams","hooks_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/hooks","issue_events_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/issues/events{/number}","events_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/events","assignees_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/assignees{/user}","branches_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/branches{/branch}","tags_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/tags","blobs_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/git/refs{/sha}","trees_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/statuses/{sha}","languages_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/languages","stargazers_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/stargazers","contributors_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/contributors","subscribers_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/subscribers","subscription_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/subscription","commits_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/commits{/sha}","git_commits_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/git/commits{/sha}","comments_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/comments{/number}","issue_comment_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/issues/comments{/number}","contents_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/contents/{+path}","compare_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/merges","archive_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/downloads","issues_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/issues{/number}","pulls_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/pulls{/number}","milestones_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/milestones{/number}","notifications_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/labels{/name}","releases_url":"https://api.github.com/repos/JeanKossaifi/scikit-learn/releases{/id}","created_at":"2014-11-29T11:35:57Z","updated_at":"2015-03-24T15:32:49Z","pushed_at":"2015-06-29T14:38:14Z","git_url":"git://github.com/JeanKossaifi/scikit-learn.git","ssh_url":"git@github.com:JeanKossaifi/scikit-learn.git","clone_url":"https://github.com/JeanKossaifi/scikit-learn.git","svn_url":"https://github.com/JeanKossaifi/scikit-learn","homepage":"http://scikit-learn.org","size":61221,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"scikit-learn:master","ref":"master","sha":"c2471412c100f39eefd67e13d18f78ca3ff5b083","user":{"login":"scikit-learn","id":365630,"avatar_url":"https://avatars.githubusercontent.com/u/365630?v=3","gravatar_id":"","url":"https://api.github.com/users/scikit-learn","html_url":"https://github.com/scikit-learn","followers_url":"https://api.github.com/users/scikit-learn/followers","following_url":"https://api.github.com/users/scikit-learn/following{/other_user}","gists_url":"https://api.github.com/users/scikit-learn/gists{/gist_id}","starred_url":"https://api.github.com/users/scikit-learn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scikit-learn/subscriptions","organizations_url":"https://api.github.com/users/scikit-learn/orgs","repos_url":"https://api.github.com/users/scikit-learn/repos","events_url":"https://api.github.com/users/scikit-learn/events{/privacy}","received_events_url":"https://api.github.com/users/scikit-learn/received_events","type":"Organization","site_admin":false},"repo":{"id":843222,"name":"scikit-learn","full_name":"scikit-learn/scikit-learn","owner":{"login":"scikit-learn","id":365630,"avatar_url":"https://avatars.githubusercontent.com/u/365630?v=3","gravatar_id":"","url":"https://api.github.com/users/scikit-learn","html_url":"https://github.com/scikit-learn","followers_url":"https://api.github.com/users/scikit-learn/followers","following_url":"https://api.github.com/users/scikit-learn/following{/other_user}","gists_url":"https://api.github.com/users/scikit-learn/gists{/gist_id}","starred_url":"https://api.github.com/users/scikit-learn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scikit-learn/subscriptions","organizations_url":"https://api.github.com/users/scikit-learn/orgs","repos_url":"https://api.github.com/users/scikit-learn/repos","events_url":"https://api.github.com/users/scikit-learn/events{/privacy}","received_events_url":"https://api.github.com/users/scikit-learn/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/scikit-learn/scikit-learn","description":"scikit-learn: machine learning in Python","fork":false,"url":"https://api.github.com/repos/scikit-learn/scikit-learn","forks_url":"https://api.github.com/repos/scikit-learn/scikit-learn/forks","keys_url":"https://api.github.com/repos/scikit-learn/scikit-learn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/scikit-learn/scikit-learn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/scikit-learn/scikit-learn/teams","hooks_url":"https://api.github.com/repos/scikit-learn/scikit-learn/hooks","issue_events_url":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/events{/number}","events_url":"https://api.github.com/repos/scikit-learn/scikit-learn/events","assignees_url":"https://api.github.com/repos/scikit-learn/scikit-learn/assignees{/user}","branches_url":"https://api.github.com/repos/scikit-learn/scikit-learn/branches{/branch}","tags_url":"https://api.github.com/repos/scikit-learn/scikit-learn/tags","blobs_url":"https://api.github.com/repos/scikit-learn/scikit-learn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/scikit-learn/scikit-learn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/scikit-learn/scikit-learn/git/refs{/sha}","trees_url":"https://api.github.com/repos/scikit-learn/scikit-learn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/scikit-learn/scikit-learn/statuses/{sha}","languages_url":"https://api.github.com/repos/scikit-learn/scikit-learn/languages","stargazers_url":"https://api.github.com/repos/scikit-learn/scikit-learn/stargazers","contributors_url":"https://api.github.com/repos/scikit-learn/scikit-learn/contributors","subscribers_url":"https://api.github.com/repos/scikit-learn/scikit-learn/subscribers","subscription_url":"https://api.github.com/repos/scikit-learn/scikit-learn/subscription","commits_url":"https://api.github.com/repos/scikit-learn/scikit-learn/commits{/sha}","git_commits_url":"https://api.github.com/repos/scikit-learn/scikit-learn/git/commits{/sha}","comments_url":"https://api.github.com/repos/scikit-learn/scikit-learn/comments{/number}","issue_comment_url":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/comments{/number}","contents_url":"https://api.github.com/repos/scikit-learn/scikit-learn/contents/{+path}","compare_url":"https://api.github.com/repos/scikit-learn/scikit-learn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/scikit-learn/scikit-learn/merges","archive_url":"https://api.github.com/repos/scikit-learn/scikit-learn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/scikit-learn/scikit-learn/downloads","issues_url":"https://api.github.com/repos/scikit-learn/scikit-learn/issues{/number}","pulls_url":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls{/number}","milestones_url":"https://api.github.com/repos/scikit-learn/scikit-learn/milestones{/number}","notifications_url":"https://api.github.com/repos/scikit-learn/scikit-learn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/scikit-learn/scikit-learn/labels{/name}","releases_url":"https://api.github.com/repos/scikit-learn/scikit-learn/releases{/id}","created_at":"2010-08-17T09:43:38Z","updated_at":"2015-07-01T14:50:47Z","pushed_at":"2015-07-01T16:04:40Z","git_url":"git://github.com/scikit-learn/scikit-learn.git","ssh_url":"git@github.com:scikit-learn/scikit-learn.git","clone_url":"https://github.com/scikit-learn/scikit-learn.git","svn_url":"https://github.com/scikit-learn/scikit-learn","homepage":"http://scikit-learn.org","size":125816,"stargazers_count":6568,"watchers_count":6568,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3933,"mirror_url":null,"open_issues_count":694,"forks":3933,"open_issues":694,"watchers":6568,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/4444"},"html":{"href":"https://github.com/scikit-learn/scikit-learn/pull/4444"},"issue":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/4444"},"comments":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/4444/comments"},"review_comments":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/4444/comments"},"review_comment":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/4444/commits"},"statuses":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/statuses/90275bee75aa2e2fe5347ae805864d4abd96b58f"}}}}