{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/32915405","id":32915405,"diff_hunk":"@@ -116,6 +116,92 @@ class NaiveBayesSuite extends SparkFunSuite with MLlibTestSparkContext {\n     }\n   }\n \n+  def validatePredictionsProbabilities(predictionsProbabilities: Seq[Array[Double]],\n+                                       input: Seq[LabeledPoint],\n+                                       modelType: String = Multinomial) = {\n+    predictionsProbabilities.foreach { probabilities =>\n+      val sum = probabilities.sum\n+      // Check that prediction probabilities sum up to one\n+      // with an epsilon of 10^-2\n+      assert(sum > 0.99 && sum < 1.01)\n+    }\n+\n+    val wrongPredictions = predictionsProbabilities.zip(input).count {\n+      case (prediction, expected) =>\n+        prediction.indexOf(prediction.max).toDouble != expected.label\n+    }\n+    // At least 80% of the predictions should be on.\n+    assert(wrongPredictions < input.length / 5)\n+\n+    comparePosteriorsWithR(predictionsProbabilities.take(10), modelType)\n+  }\n+\n+  /**\n+   * The following is the instruction to reproduce the model using R's e1071 package.\n+   *\n+   * First of all, using the following scala code to save the data into `path`.\n+   *\n+   *    testRDD.map { x =>\n+   *      s\"${x.label}, ${x.features.toArray.mkString(\", \")}\"\n+   *    }.saveAsTextFile(\"path\")\n+   *\n+   * Using the following R code to load the data and train the model using e1071 package.\n+   *\n+   *    library(e1071)\n+   *    data <- read.csv(\"path\", header = FALSE)\n+   *    labels <- factor(data$V1)\n+   *    features <- data.frame(data$V2, data$V3, data$V4, data$V5)\n+   *    model <- naiveBayes(features, labels)\n+   *    predictions <- predict(model, features[1:10, -1], type = \"raw\")\n+   *\n+   */\n+  def comparePosteriorsWithR(predictionsProbabilities: Seq[Array[Double]],\n+                             modelType: String = Multinomial,\n+                             epsilon: Double = 0.1) = {\n+    require(predictionsProbabilities.length == 10)\n+\n+    val posteriorsFromR = modelType match {\n+      case Multinomial =>\n+        Array(\n+          Array(2.942994e-07, 5.467545e-11, 9.999997e-01),\n+          Array(2.931850e-07, 4.922381e-12, 9.999997e-01),\n+          Array(9.997708e-01, 3.424392e-06, 2.257879e-04),\n+          Array(9.991757e-01, 6.132008e-04, 2.110877e-04),\n+          Array(9.281650e-14, 8.199463e-17, 1.000000e+00),\n+          Array(8.099445e-01, 3.821142e-05, 1.900173e-01),\n+          Array(2.667884e-01, 7.331288e-01, 8.276015e-05),\n+          Array(9.999776e-01, 2.163690e-06, 2.023486e-05),\n+          Array(9.997814e-01, 2.441990e-06, 2.161960e-04),\n+          Array(8.850206e-14, 6.692205e-18, 1.000000e+00)\n+        )\n+      case Bernoulli =>\n+        Array(\n+          Array(1.048099e-09, 1.000000e+00, 1.578642e-09),\n+          Array(1.831993e-19, 9.999999e-01, 1.190036e-07),\n+          Array(4.664977e-12, 1.000000e+00, 1.291666e-12),\n+          Array(3.224249e-11, 2.433594e-02, 9.756641e-01),\n+          Array(9.610916e-01, 1.256859e-13, 3.890841e-02),\n+          Array(8.318820e-01, 1.097496e-01, 5.836849e-02),\n+          Array(8.318820e-01, 1.097496e-01, 5.836849e-02),\n+          Array(9.610916e-01, 1.256859e-13, 3.890841e-02),\n+          Array(8.318820e-01, 1.097496e-01, 5.836849e-02),\n+          Array(8.318820e-01, 1.097496e-01, 5.836849e-02)\n+        )\n+    }\n+\n+    predictionsProbabilities.zip(posteriorsFromR).foreach {\n+      case (probs, fromR) =>\n+        val p = probs.indexOf(probs.max)\n+        val r = fromR.indexOf(fromR.max)\n+        // Checking that the prediction is the same\n+        if (p == r) {","path":"mllib/src/test/scala/org/apache/spark/mllib/classification/NaiveBayesSuite.scala","position":82,"original_position":82,"commit_id":"49f0d5944fc0030dd445f6a8eb55975bc3c1b9a2","original_commit_id":"49f0d5944fc0030dd445f6a8eb55975bc3c1b9a2","user":{"login":"srowen","id":822522,"avatar_url":"https://avatars.githubusercontent.com/u/822522?v=3","gravatar_id":"","url":"https://api.github.com/users/srowen","html_url":"https://github.com/srowen","followers_url":"https://api.github.com/users/srowen/followers","following_url":"https://api.github.com/users/srowen/following{/other_user}","gists_url":"https://api.github.com/users/srowen/gists{/gist_id}","starred_url":"https://api.github.com/users/srowen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/srowen/subscriptions","organizations_url":"https://api.github.com/users/srowen/orgs","repos_url":"https://api.github.com/users/srowen/repos","events_url":"https://api.github.com/users/srowen/events{/privacy}","received_events_url":"https://api.github.com/users/srowen/received_events","type":"User","site_admin":false},"body":"You probably want to assert this?","created_at":"2015-06-22T08:55:04Z","updated_at":"2015-06-22T08:55:04Z","html_url":"https://github.com/apache/spark/pull/6761#discussion_r32915405","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/6761","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/32915405"},"html":{"href":"https://github.com/apache/spark/pull/6761#discussion_r32915405"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/6761"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/6761","id":37471672,"html_url":"https://github.com/apache/spark/pull/6761","diff_url":"https://github.com/apache/spark/pull/6761.diff","patch_url":"https://github.com/apache/spark/pull/6761.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/6761","number":6761,"state":"open","locked":false,"title":"[SPARK-4362] [MLLIB] Make prediction probability available in NaiveBayesModel","user":{"login":"acidghost","id":1787979,"avatar_url":"https://avatars.githubusercontent.com/u/1787979?v=3","gravatar_id":"","url":"https://api.github.com/users/acidghost","html_url":"https://github.com/acidghost","followers_url":"https://api.github.com/users/acidghost/followers","following_url":"https://api.github.com/users/acidghost/following{/other_user}","gists_url":"https://api.github.com/users/acidghost/gists{/gist_id}","starred_url":"https://api.github.com/users/acidghost/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/acidghost/subscriptions","organizations_url":"https://api.github.com/users/acidghost/orgs","repos_url":"https://api.github.com/users/acidghost/repos","events_url":"https://api.github.com/users/acidghost/events{/privacy}","received_events_url":"https://api.github.com/users/acidghost/received_events","type":"User","site_admin":false},"body":"There is currently no way to get the posterior probability of a prediction with Naive Baye's model during prediction. This is made available along with the label.\r\n\r\n@jkbradley @srowen ","created_at":"2015-06-11T14:50:49Z","updated_at":"2015-06-22T08:55:04Z","closed_at":null,"merged_at":null,"merge_commit_sha":"2ef9e3bb2ee59261baaf684836c88111adb4977b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/6761/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/6761/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/6761/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/49f0d5944fc0030dd445f6a8eb55975bc3c1b9a2","head":{"label":"storecove:nb-posterior","ref":"nb-posterior","sha":"49f0d5944fc0030dd445f6a8eb55975bc3c1b9a2","user":{"login":"storecove","id":9430917,"avatar_url":"https://avatars.githubusercontent.com/u/9430917?v=3","gravatar_id":"","url":"https://api.github.com/users/storecove","html_url":"https://github.com/storecove","followers_url":"https://api.github.com/users/storecove/followers","following_url":"https://api.github.com/users/storecove/following{/other_user}","gists_url":"https://api.github.com/users/storecove/gists{/gist_id}","starred_url":"https://api.github.com/users/storecove/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/storecove/subscriptions","organizations_url":"https://api.github.com/users/storecove/orgs","repos_url":"https://api.github.com/users/storecove/repos","events_url":"https://api.github.com/users/storecove/events{/privacy}","received_events_url":"https://api.github.com/users/storecove/received_events","type":"Organization","site_admin":false},"repo":{"id":37192985,"name":"spark","full_name":"storecove/spark","owner":{"login":"storecove","id":9430917,"avatar_url":"https://avatars.githubusercontent.com/u/9430917?v=3","gravatar_id":"","url":"https://api.github.com/users/storecove","html_url":"https://github.com/storecove","followers_url":"https://api.github.com/users/storecove/followers","following_url":"https://api.github.com/users/storecove/following{/other_user}","gists_url":"https://api.github.com/users/storecove/gists{/gist_id}","starred_url":"https://api.github.com/users/storecove/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/storecove/subscriptions","organizations_url":"https://api.github.com/users/storecove/orgs","repos_url":"https://api.github.com/users/storecove/repos","events_url":"https://api.github.com/users/storecove/events{/privacy}","received_events_url":"https://api.github.com/users/storecove/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/storecove/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/storecove/spark","forks_url":"https://api.github.com/repos/storecove/spark/forks","keys_url":"https://api.github.com/repos/storecove/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/storecove/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/storecove/spark/teams","hooks_url":"https://api.github.com/repos/storecove/spark/hooks","issue_events_url":"https://api.github.com/repos/storecove/spark/issues/events{/number}","events_url":"https://api.github.com/repos/storecove/spark/events","assignees_url":"https://api.github.com/repos/storecove/spark/assignees{/user}","branches_url":"https://api.github.com/repos/storecove/spark/branches{/branch}","tags_url":"https://api.github.com/repos/storecove/spark/tags","blobs_url":"https://api.github.com/repos/storecove/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/storecove/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/storecove/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/storecove/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/storecove/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/storecove/spark/languages","stargazers_url":"https://api.github.com/repos/storecove/spark/stargazers","contributors_url":"https://api.github.com/repos/storecove/spark/contributors","subscribers_url":"https://api.github.com/repos/storecove/spark/subscribers","subscription_url":"https://api.github.com/repos/storecove/spark/subscription","commits_url":"https://api.github.com/repos/storecove/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/storecove/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/storecove/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/storecove/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/storecove/spark/contents/{+path}","compare_url":"https://api.github.com/repos/storecove/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/storecove/spark/merges","archive_url":"https://api.github.com/repos/storecove/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/storecove/spark/downloads","issues_url":"https://api.github.com/repos/storecove/spark/issues{/number}","pulls_url":"https://api.github.com/repos/storecove/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/storecove/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/storecove/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/storecove/spark/labels{/name}","releases_url":"https://api.github.com/repos/storecove/spark/releases{/id}","created_at":"2015-06-10T11:24:55Z","updated_at":"2015-06-10T11:25:16Z","pushed_at":"2015-06-21T12:06:03Z","git_url":"git://github.com/storecove/spark.git","ssh_url":"git@github.com:storecove/spark.git","clone_url":"https://github.com/storecove/spark.git","svn_url":"https://github.com/storecove/spark","homepage":null,"size":110063,"stargazers_count":0,"watchers_count":0,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"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":"apache:master","ref":"master","sha":"424b0075a1a31c251451c6a75c6ba8e81c39453d","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":17165658,"name":"spark","full_name":"apache/spark","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/spark","description":"Mirror of Apache Spark","fork":false,"url":"https://api.github.com/repos/apache/spark","forks_url":"https://api.github.com/repos/apache/spark/forks","keys_url":"https://api.github.com/repos/apache/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/spark/teams","hooks_url":"https://api.github.com/repos/apache/spark/hooks","issue_events_url":"https://api.github.com/repos/apache/spark/issues/events{/number}","events_url":"https://api.github.com/repos/apache/spark/events","assignees_url":"https://api.github.com/repos/apache/spark/assignees{/user}","branches_url":"https://api.github.com/repos/apache/spark/branches{/branch}","tags_url":"https://api.github.com/repos/apache/spark/tags","blobs_url":"https://api.github.com/repos/apache/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/spark/languages","stargazers_url":"https://api.github.com/repos/apache/spark/stargazers","contributors_url":"https://api.github.com/repos/apache/spark/contributors","subscribers_url":"https://api.github.com/repos/apache/spark/subscribers","subscription_url":"https://api.github.com/repos/apache/spark/subscription","commits_url":"https://api.github.com/repos/apache/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/spark/contents/{+path}","compare_url":"https://api.github.com/repos/apache/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/spark/merges","archive_url":"https://api.github.com/repos/apache/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/spark/downloads","issues_url":"https://api.github.com/repos/apache/spark/issues{/number}","pulls_url":"https://api.github.com/repos/apache/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/spark/labels{/name}","releases_url":"https://api.github.com/repos/apache/spark/releases{/id}","created_at":"2014-02-25T08:00:08Z","updated_at":"2015-06-22T06:14:55Z","pushed_at":"2015-06-22T08:34:01Z","git_url":"git://github.com/apache/spark.git","ssh_url":"git@github.com:apache/spark.git","clone_url":"https://github.com/apache/spark.git","svn_url":"https://github.com/apache/spark","homepage":null,"size":1496005,"stargazers_count":4474,"watchers_count":4474,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":3936,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":352,"forks":3936,"open_issues":352,"watchers":4474,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/6761"},"html":{"href":"https://github.com/apache/spark/pull/6761"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/6761"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/6761/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/6761/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/6761/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/49f0d5944fc0030dd445f6a8eb55975bc3c1b9a2"}}}}