{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/46011586","id":46011586,"diff_hunk":"@@ -211,14 +213,16 @@ private object IDFModel {\n     val n = v.size\n     v match {\n       case SparseVector(size, indices, values) =>\n-        val nnz = indices.size\n-        val newValues = new Array[Double](nnz)\n+        val newElements = new ArrayBuffer[(Int, Double)]\n         var k = 0\n-        while (k < nnz) {\n-          newValues(k) = values(k) * idf(indices(k))\n+        while (k < indices.size) {\n+          val newValue = values(k) * idf(indices(k))","path":"mllib/src/main/scala/org/apache/spark/mllib/feature/IDF.scala","position":20,"original_position":20,"commit_id":"105153594eb3dc9a2f37a88eb14539d4cbdf14ee","original_commit_id":"105153594eb3dc9a2f37a88eb14539d4cbdf14ee","user":{"login":"dbtsai","id":1134574,"avatar_url":"https://avatars.githubusercontent.com/u/1134574?v=3","gravatar_id":"","url":"https://api.github.com/users/dbtsai","html_url":"https://github.com/dbtsai","followers_url":"https://api.github.com/users/dbtsai/followers","following_url":"https://api.github.com/users/dbtsai/following{/other_user}","gists_url":"https://api.github.com/users/dbtsai/gists{/gist_id}","starred_url":"https://api.github.com/users/dbtsai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dbtsai/subscriptions","organizations_url":"https://api.github.com/users/dbtsai/orgs","repos_url":"https://api.github.com/users/dbtsai/repos","events_url":"https://api.github.com/users/dbtsai/events{/privacy}","received_events_url":"https://api.github.com/users/dbtsai/received_events","type":"User","site_admin":false},"body":"Since `idf` can be sparse, lookup will not be constant time operation. You need to make it 4 cases. Also, `indices.size` is not good since in each tight iteration, the `size` method will be called. The following code should work, but I just wrote it here without testing so may have some bugs. You need to add the tests for four different scenarios. \r\n\r\n```scala\r\n    (idf, v) match {\r\n      case (didf: DenseVector, dv: DenseVector) =>\r\n        val didfValues = didf.values\r\n        val dvValues = dv.values\r\n\r\n        val newValues = new Array[Double](n)\r\n        var j = 0\r\n        while (j < n) {\r\n          newValues(j) = dvValues(j) * didfValues(j)\r\n          j += 1\r\n        }\r\n        Vectors.dense(newValues)\r\n\r\n      case (didf: DenseVector, sv: SparseVector) =>\r\n        val didfValues = didf.values\r\n        val svIndices = sv.indices\r\n        val svValues = sv.values\r\n        val svNnz = svIndices.length\r\n\r\n        val newValues = new Array[Double](svNnz)\r\n        var k = 0\r\n        while (k < svNnz) {\r\n          newValues(k) = svValues(k) * didfValues(svIndices(k))\r\n          k += 1\r\n        }\r\n        Vectors.sparse(n, svIndices, newValues)\r\n\r\n      case (sidf: SparseVector, dv: DenseVector) =>\r\n        val dvValues = dv.values\r\n        val sidfIndices = sidf.indices\r\n        val sidfValues = sidf.values\r\n        val sidfNnz = sidfIndices.length\r\n\r\n        val newValues = new Array[Double](sidfNnz)\r\n        var k = 0\r\n        while (k < sidfNnz) {\r\n          newValues(k) = sidfValues(k) * dvValues(sidfIndices(k))\r\n          k += 1\r\n        }\r\n        Vectors.sparse(n, sidfIndices, newValues)\r\n\r\n      case (sidf: SparseVector, sv: SparseVector) =>\r\n        val (largeIndices, largeValues, largeNnz, smallIndices, smallValues, smallNnz) = {\r\n          val sidfIndices = sidf.indices\r\n          val sidfValues = sidf.values\r\n          val sidfNnz = sidfIndices.length\r\n\r\n          val svIndices = sv.indices\r\n          val svValues = sv.values\r\n          val svNnz = svIndices.length\r\n          if (sidfNnz > svNnz) {\r\n            (sidfIndices, sidfValues, sidfNnz, svIndices, svValues, svNnz)\r\n          } else {\r\n            (svIndices, svValues, svNnz, sidfIndices, sidfValues, sidfNnz)\r\n          }\r\n        }\r\n\r\n        val newIndices = new ArrayBuffer[Int](smallNnz)\r\n        val newValues = new ArrayBuffer[Double](smallNnz)\r\n\r\n        var j = 0\r\n        var k = 0\r\n        while (j < smallNnz && k < largeNnz) {\r\n          val smallIndex = smallIndices(j)\r\n          val largeIndex = largeIndices(k)\r\n          \r\n          if (smallIndex == largeIndex) {\r\n            newIndices.append(smallIndex)\r\n            newValues.append(smallValues(j) * largeValues(k))\r\n            j += 1\r\n            k += 1\r\n          } else if (smallIndex > largeIndex) {\r\n            k += 1\r\n          } else {\r\n            j += 1\r\n          }\r\n        }\r\n        Vectors.sparse(n, newIndices.toArray, newValues.toArray)\r\n\r\n      case (idfOther, vOther) =>\r\n        throw new UnsupportedOperationException(\r\n          s\"Only sparse and dense vectors are supported but got ${idfOther.getClass} \" +\r\n          s\"for idf vector, and ${vOther.getClass} for term frequence vector.\")\r\n    }\r\n```","created_at":"2015-11-27T00:47:01Z","updated_at":"2015-11-27T00:47:01Z","html_url":"https://github.com/apache/spark/pull/9843#discussion_r46011586","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/9843","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/46011586"},"html":{"href":"https://github.com/apache/spark/pull/9843#discussion_r46011586"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/9843"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/9843","id":51278789,"html_url":"https://github.com/apache/spark/pull/9843","diff_url":"https://github.com/apache/spark/pull/9843.diff","patch_url":"https://github.com/apache/spark/pull/9843.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/9843","number":9843,"state":"open","locked":false,"title":"[SPARK-7857][MLLIB] Prevent IDFModel from returning zeros in SparseVe…","user":{"login":"karlhigley","id":885295,"avatar_url":"https://avatars.githubusercontent.com/u/885295?v=3","gravatar_id":"","url":"https://api.github.com/users/karlhigley","html_url":"https://github.com/karlhigley","followers_url":"https://api.github.com/users/karlhigley/followers","following_url":"https://api.github.com/users/karlhigley/following{/other_user}","gists_url":"https://api.github.com/users/karlhigley/gists{/gist_id}","starred_url":"https://api.github.com/users/karlhigley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/karlhigley/subscriptions","organizations_url":"https://api.github.com/users/karlhigley/orgs","repos_url":"https://api.github.com/users/karlhigley/repos","events_url":"https://api.github.com/users/karlhigley/events{/privacy}","received_events_url":"https://api.github.com/users/karlhigley/received_events","type":"User","site_admin":false},"body":"…ctors\r\n\r\nWhen the IDF model's `minDocFreq` parameter is set to a non-zero threshold, the\r\nIDF for any feature below that threshold is set to zero. When the model is used\r\nto transform a set of SparseVectors containing that feature, the resulting\r\nSparseVectors contain entries whose values are explicitly zero. The zero entries\r\nshould be omitted in order to simplify downstream processing.","created_at":"2015-11-19T20:50:47Z","updated_at":"2015-11-27T00:47:01Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d8cd31f76d66f57de5d6db9ed88f838cf6f0709a","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/9843/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/9843/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/9843/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/105153594eb3dc9a2f37a88eb14539d4cbdf14ee","head":{"label":"karlhigley:fix/idf-zeros","ref":"fix/idf-zeros","sha":"105153594eb3dc9a2f37a88eb14539d4cbdf14ee","user":{"login":"karlhigley","id":885295,"avatar_url":"https://avatars.githubusercontent.com/u/885295?v=3","gravatar_id":"","url":"https://api.github.com/users/karlhigley","html_url":"https://github.com/karlhigley","followers_url":"https://api.github.com/users/karlhigley/followers","following_url":"https://api.github.com/users/karlhigley/following{/other_user}","gists_url":"https://api.github.com/users/karlhigley/gists{/gist_id}","starred_url":"https://api.github.com/users/karlhigley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/karlhigley/subscriptions","organizations_url":"https://api.github.com/users/karlhigley/orgs","repos_url":"https://api.github.com/users/karlhigley/repos","events_url":"https://api.github.com/users/karlhigley/events{/privacy}","received_events_url":"https://api.github.com/users/karlhigley/received_events","type":"User","site_admin":false},"repo":{"id":34802753,"name":"spark","full_name":"karlhigley/spark","owner":{"login":"karlhigley","id":885295,"avatar_url":"https://avatars.githubusercontent.com/u/885295?v=3","gravatar_id":"","url":"https://api.github.com/users/karlhigley","html_url":"https://github.com/karlhigley","followers_url":"https://api.github.com/users/karlhigley/followers","following_url":"https://api.github.com/users/karlhigley/following{/other_user}","gists_url":"https://api.github.com/users/karlhigley/gists{/gist_id}","starred_url":"https://api.github.com/users/karlhigley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/karlhigley/subscriptions","organizations_url":"https://api.github.com/users/karlhigley/orgs","repos_url":"https://api.github.com/users/karlhigley/repos","events_url":"https://api.github.com/users/karlhigley/events{/privacy}","received_events_url":"https://api.github.com/users/karlhigley/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/karlhigley/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/karlhigley/spark","forks_url":"https://api.github.com/repos/karlhigley/spark/forks","keys_url":"https://api.github.com/repos/karlhigley/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/karlhigley/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/karlhigley/spark/teams","hooks_url":"https://api.github.com/repos/karlhigley/spark/hooks","issue_events_url":"https://api.github.com/repos/karlhigley/spark/issues/events{/number}","events_url":"https://api.github.com/repos/karlhigley/spark/events","assignees_url":"https://api.github.com/repos/karlhigley/spark/assignees{/user}","branches_url":"https://api.github.com/repos/karlhigley/spark/branches{/branch}","tags_url":"https://api.github.com/repos/karlhigley/spark/tags","blobs_url":"https://api.github.com/repos/karlhigley/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/karlhigley/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/karlhigley/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/karlhigley/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/karlhigley/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/karlhigley/spark/languages","stargazers_url":"https://api.github.com/repos/karlhigley/spark/stargazers","contributors_url":"https://api.github.com/repos/karlhigley/spark/contributors","subscribers_url":"https://api.github.com/repos/karlhigley/spark/subscribers","subscription_url":"https://api.github.com/repos/karlhigley/spark/subscription","commits_url":"https://api.github.com/repos/karlhigley/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/karlhigley/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/karlhigley/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/karlhigley/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/karlhigley/spark/contents/{+path}","compare_url":"https://api.github.com/repos/karlhigley/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/karlhigley/spark/merges","archive_url":"https://api.github.com/repos/karlhigley/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/karlhigley/spark/downloads","issues_url":"https://api.github.com/repos/karlhigley/spark/issues{/number}","pulls_url":"https://api.github.com/repos/karlhigley/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/karlhigley/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/karlhigley/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/karlhigley/spark/labels{/name}","releases_url":"https://api.github.com/repos/karlhigley/spark/releases{/id}","created_at":"2015-04-29T15:43:31Z","updated_at":"2015-04-29T15:43:51Z","pushed_at":"2015-11-26T18:28:49Z","git_url":"git://github.com/karlhigley/spark.git","ssh_url":"git@github.com:karlhigley/spark.git","clone_url":"https://github.com/karlhigley/spark.git","svn_url":"https://github.com/karlhigley/spark","homepage":null,"size":132869,"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":"7d4aba18722727c85893ad8d8f07d4494665dcfc","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-11-26T22:17:35Z","pushed_at":"2015-11-27T00:34:19Z","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":387226,"stargazers_count":6366,"watchers_count":6366,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":5854,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":382,"forks":5854,"open_issues":382,"watchers":6366,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/9843"},"html":{"href":"https://github.com/apache/spark/pull/9843"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/9843"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/9843/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/9843/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/9843/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/105153594eb3dc9a2f37a88eb14539d4cbdf14ee"}}}}