{"action":"created","comment":{"url":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/comments/44588054","id":44588054,"diff_hunk":"@@ -0,0 +1,173 @@\n+from __future__ import division\n+\n+import numpy as np\n+from numpy.testing import run_module_suite\n+from scipy.sparse import csr_matrix\n+\n+from sklearn.utils.testing import (\n+    assert_array_equal, assert_almost_equal, assert_true)\n+from sklearn.feature_selection import MinRedundancyMaxRelevance\n+from sklearn.feature_selection.mutual_info import _compute_mi\n+\n+\n+class TestMIComputation(object):\n+    def test_dd(self):\n+        # In discrete case computations are straightforward and can be done\n+        # by hand on given vectors.\n+        x = np.array([0, 1, 1, 0, 0])\n+        y = np.array([1, 0, 0, 0, 1])\n+\n+        H_x = H_y = -(3/5) * np.log(3/5) - (2/5) * np.log(2/5)\n+        H_xy = -1/5 * np.log(1/5) - 2/5 * np.log(2/5) - 2/5 * np.log(2/5)\n+        I_xy = H_x + H_y - H_xy\n+\n+        assert_almost_equal(_compute_mi(x, y, True, True), I_xy)\n+\n+    def test_cc(self):\n+        # For two continuous variables a good approach is to test on bivariate\n+        # normal distribution, where mutual information is known.\n+\n+        # Mean of the distribution, irrelevant for mutual information.\n+        mean = np.zeros(2)\n+\n+        # Setup covariance matrix with correlation coeff. equal 0.5.\n+        sigma_1 = 1\n+        sigma_2 = 10\n+        corr = 0.5\n+        cov = np.array([\n+            [sigma_1**2, corr * sigma_1 * sigma_2],\n+            [corr * sigma_1 * sigma_2, sigma_2**2]\n+        ])\n+\n+        # True theoretical mutual information.\n+        I_theory = (np.log(sigma_1) + np.log(sigma_2) -\n+                    0.5 * np.log(np.linalg.det(cov)))\n+\n+        np.random.seed(0)\n+        Z = np.random.multivariate_normal(mean, cov, size=1000)\n+\n+        x, y = Z[:, 0], Z[:, 1]\n+        I_computed = _compute_mi(x, y, False, False)\n+\n+        # Theory and computed values won't be very close, assert that relative\n+        # error is less than 10%.\n+        assert_true(np.abs(I_computed - I_theory) < 0.1 * I_theory)\n+\n+    def test_cd(self):\n+        # To test define a joint distribution as follows:\n+        # p(x, y) = p(x) p(y | x)\n+        # X ~ Bernoulli(0.5)\n+        # (Y | x = 0) ~ Uniform(-1, 1)\n+        # (Y | x = 1) ~ Uniform(0, 2)\n+\n+        # Use the following formula for mutual information:\n+        # I(X; Y) = H(Y) - H(Y | X)\n+        # Two entropies can be computed by hand:\n+        # H(Y) = 3/2 * ln(2)\n+        # H(Y | X) = ln(2)\n+        # So finally we have:\n+        # I(X; Y) = 1/2 * ln(2)\n+\n+        # Now we need to implement sampling from out distribution, which is\n+        # done easily using conditional distribution logic.\n+\n+        n_samples = 1000\n+        np.random.seed(0)\n+        Z = np.empty((n_samples, 2))\n+        Z[:, 0] = np.random.uniform(size=n_samples) > 0.5\n+        mask = Z[:, 0] == 0\n+        Z[mask, 1] = np.random.uniform(-1, 1, size=np.sum(mask))\n+        mask = ~mask\n+        Z[mask, 1] = np.random.uniform(0, 2, size=np.sum(mask))\n+\n+        x = Z[:, 0].astype(int)\n+        y = Z[:, 1]\n+\n+        I_theory = 0.5 * np.log(2)\n+        I_computed = _compute_mi(x, y, True, False)\n+\n+        # Again assert that relative error is less than 10%.\n+        assert_true(np.abs(I_computed - I_theory) < 0.1 * I_theory)\n+\n+\n+class TestMinRedundancyMaxRelevance(object):\n+    # We'll have to rely on \"common sense\" to test our procedure. That is\n+    # test on data where we can judge which features are \"most relevant and\n+    # least redundant\".\n+    def test_categorical(self):\n+        X = np.array([\n+            [0, 0, 0, 0],\n+            [1, 1, 0, 1],\n+            [2, 2, 1, 0],\n+            [2, 2, 1, 0],\n+            [2, 2, 1, 0]\n+        ], dtype=float)\n+        y = np.array([0, 1, 2, 2, 1])\n+\n+        # Here x[0] is the most informative feature, x[1] is its duplicate","path":"sklearn/feature_selection/tests/test_mutual_info.py","position":107,"original_position":107,"commit_id":"1a72bbf1c5db2134435faf8fab190fda38eb23d3","original_commit_id":"1a72bbf1c5db2134435faf8fab190fda38eb23d3","user":{"login":"MechCoder","id":1867024,"avatar_url":"https://avatars.githubusercontent.com/u/1867024?v=3","gravatar_id":"","url":"https://api.github.com/users/MechCoder","html_url":"https://github.com/MechCoder","followers_url":"https://api.github.com/users/MechCoder/followers","following_url":"https://api.github.com/users/MechCoder/following{/other_user}","gists_url":"https://api.github.com/users/MechCoder/gists{/gist_id}","starred_url":"https://api.github.com/users/MechCoder/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MechCoder/subscriptions","organizations_url":"https://api.github.com/users/MechCoder/orgs","repos_url":"https://api.github.com/users/MechCoder/repos","events_url":"https://api.github.com/users/MechCoder/events{/privacy}","received_events_url":"https://api.github.com/users/MechCoder/received_events","type":"User","site_admin":false},"body":"technically x[:, 0]","created_at":"2015-11-11T21:22:35Z","updated_at":"2015-11-11T21:22:35Z","html_url":"https://github.com/scikit-learn/scikit-learn/pull/5372#discussion_r44588054","pull_request_url":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/5372","_links":{"self":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/comments/44588054"},"html":{"href":"https://github.com/scikit-learn/scikit-learn/pull/5372#discussion_r44588054"},"pull_request":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/5372"}}},"pull_request":{"url":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/5372","id":47215063,"html_url":"https://github.com/scikit-learn/scikit-learn/pull/5372","diff_url":"https://github.com/scikit-learn/scikit-learn/pull/5372.diff","patch_url":"https://github.com/scikit-learn/scikit-learn/pull/5372.patch","issue_url":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/5372","number":5372,"state":"open","locked":false,"title":"[MRG] ENH: Feature selection based on mutual information","user":{"login":"nmayorov","id":7074023,"avatar_url":"https://avatars.githubusercontent.com/u/7074023?v=3","gravatar_id":"","url":"https://api.github.com/users/nmayorov","html_url":"https://github.com/nmayorov","followers_url":"https://api.github.com/users/nmayorov/followers","following_url":"https://api.github.com/users/nmayorov/following{/other_user}","gists_url":"https://api.github.com/users/nmayorov/gists{/gist_id}","starred_url":"https://api.github.com/users/nmayorov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nmayorov/subscriptions","organizations_url":"https://api.github.com/users/nmayorov/orgs","repos_url":"https://api.github.com/users/nmayorov/repos","events_url":"https://api.github.com/users/nmayorov/events{/privacy}","received_events_url":"https://api.github.com/users/nmayorov/received_events","type":"User","site_admin":false},"body":"Hi! This is my attempt to finish/rework #2547\r\n\r\nI tried to address code style issues and also added algorithms estimating mutual information with continuous variable involved.\r\n\r\nThere are places for trivial optimization, but for now I tried to keep the code as transparent as possible. \r\n\r\nIt would be great if some of the core developers can start seriously reviewing with PR.","created_at":"2015-10-08T22:32:49Z","updated_at":"2015-11-11T21:22:35Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4bd9d3bae86a1a25c8b4b55e0ba239370fa7756d","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/5372/commits","review_comments_url":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/5372/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/5372/comments","statuses_url":"https://api.github.com/repos/scikit-learn/scikit-learn/statuses/1a72bbf1c5db2134435faf8fab190fda38eb23d3","head":{"label":"nmayorov:mrmr","ref":"mrmr","sha":"1a72bbf1c5db2134435faf8fab190fda38eb23d3","user":{"login":"nmayorov","id":7074023,"avatar_url":"https://avatars.githubusercontent.com/u/7074023?v=3","gravatar_id":"","url":"https://api.github.com/users/nmayorov","html_url":"https://github.com/nmayorov","followers_url":"https://api.github.com/users/nmayorov/followers","following_url":"https://api.github.com/users/nmayorov/following{/other_user}","gists_url":"https://api.github.com/users/nmayorov/gists{/gist_id}","starred_url":"https://api.github.com/users/nmayorov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nmayorov/subscriptions","organizations_url":"https://api.github.com/users/nmayorov/orgs","repos_url":"https://api.github.com/users/nmayorov/repos","events_url":"https://api.github.com/users/nmayorov/events{/privacy}","received_events_url":"https://api.github.com/users/nmayorov/received_events","type":"User","site_admin":false},"repo":{"id":18151685,"name":"scikit-learn","full_name":"nmayorov/scikit-learn","owner":{"login":"nmayorov","id":7074023,"avatar_url":"https://avatars.githubusercontent.com/u/7074023?v=3","gravatar_id":"","url":"https://api.github.com/users/nmayorov","html_url":"https://github.com/nmayorov","followers_url":"https://api.github.com/users/nmayorov/followers","following_url":"https://api.github.com/users/nmayorov/following{/other_user}","gists_url":"https://api.github.com/users/nmayorov/gists{/gist_id}","starred_url":"https://api.github.com/users/nmayorov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nmayorov/subscriptions","organizations_url":"https://api.github.com/users/nmayorov/orgs","repos_url":"https://api.github.com/users/nmayorov/repos","events_url":"https://api.github.com/users/nmayorov/events{/privacy}","received_events_url":"https://api.github.com/users/nmayorov/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/nmayorov/scikit-learn","description":"scikit-learn: machine learning in Python","fork":true,"url":"https://api.github.com/repos/nmayorov/scikit-learn","forks_url":"https://api.github.com/repos/nmayorov/scikit-learn/forks","keys_url":"https://api.github.com/repos/nmayorov/scikit-learn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nmayorov/scikit-learn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nmayorov/scikit-learn/teams","hooks_url":"https://api.github.com/repos/nmayorov/scikit-learn/hooks","issue_events_url":"https://api.github.com/repos/nmayorov/scikit-learn/issues/events{/number}","events_url":"https://api.github.com/repos/nmayorov/scikit-learn/events","assignees_url":"https://api.github.com/repos/nmayorov/scikit-learn/assignees{/user}","branches_url":"https://api.github.com/repos/nmayorov/scikit-learn/branches{/branch}","tags_url":"https://api.github.com/repos/nmayorov/scikit-learn/tags","blobs_url":"https://api.github.com/repos/nmayorov/scikit-learn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nmayorov/scikit-learn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nmayorov/scikit-learn/git/refs{/sha}","trees_url":"https://api.github.com/repos/nmayorov/scikit-learn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nmayorov/scikit-learn/statuses/{sha}","languages_url":"https://api.github.com/repos/nmayorov/scikit-learn/languages","stargazers_url":"https://api.github.com/repos/nmayorov/scikit-learn/stargazers","contributors_url":"https://api.github.com/repos/nmayorov/scikit-learn/contributors","subscribers_url":"https://api.github.com/repos/nmayorov/scikit-learn/subscribers","subscription_url":"https://api.github.com/repos/nmayorov/scikit-learn/subscription","commits_url":"https://api.github.com/repos/nmayorov/scikit-learn/commits{/sha}","git_commits_url":"https://api.github.com/repos/nmayorov/scikit-learn/git/commits{/sha}","comments_url":"https://api.github.com/repos/nmayorov/scikit-learn/comments{/number}","issue_comment_url":"https://api.github.com/repos/nmayorov/scikit-learn/issues/comments{/number}","contents_url":"https://api.github.com/repos/nmayorov/scikit-learn/contents/{+path}","compare_url":"https://api.github.com/repos/nmayorov/scikit-learn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nmayorov/scikit-learn/merges","archive_url":"https://api.github.com/repos/nmayorov/scikit-learn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nmayorov/scikit-learn/downloads","issues_url":"https://api.github.com/repos/nmayorov/scikit-learn/issues{/number}","pulls_url":"https://api.github.com/repos/nmayorov/scikit-learn/pulls{/number}","milestones_url":"https://api.github.com/repos/nmayorov/scikit-learn/milestones{/number}","notifications_url":"https://api.github.com/repos/nmayorov/scikit-learn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nmayorov/scikit-learn/labels{/name}","releases_url":"https://api.github.com/repos/nmayorov/scikit-learn/releases{/id}","created_at":"2014-03-26T19:52:15Z","updated_at":"2015-09-01T04:46:47Z","pushed_at":"2015-11-09T21:19:22Z","git_url":"git://github.com/nmayorov/scikit-learn.git","ssh_url":"git@github.com:nmayorov/scikit-learn.git","clone_url":"https://github.com/nmayorov/scikit-learn.git","svn_url":"https://github.com/nmayorov/scikit-learn","homepage":"http://scikit-learn.org","size":67820,"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":"e5f71e7f5e0ff789fed7bdea475223b8ff6a5576","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-11-11T18:55:39Z","pushed_at":"2015-11-11T19:28:03Z","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":141799,"stargazers_count":8223,"watchers_count":8223,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4886,"mirror_url":null,"open_issues_count":865,"forks":4886,"open_issues":865,"watchers":8223,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/5372"},"html":{"href":"https://github.com/scikit-learn/scikit-learn/pull/5372"},"issue":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/5372"},"comments":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/5372/comments"},"review_comments":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/5372/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/5372/commits"},"statuses":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/statuses/1a72bbf1c5db2134435faf8fab190fda38eb23d3"}}}}