{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/29304924","id":29304924,"diff_hunk":"@@ -0,0 +1,109 @@\n+/*\n+ * Licensed to the Apache Software Foundation (ASF) under one or more\n+ * contributor license agreements.  See the NOTICE file distributed with\n+ * this work for additional information regarding copyright ownership.\n+ * The ASF licenses this file to You under the Apache License, Version 2.0\n+ * (the \"License\"); you may not use this file except in compliance with\n+ * the License.  You may obtain a copy of the License at\n+ *\n+ *    http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+package org.apache.spark.mllib.feature\n+\n+import org.apache.spark.api.java.JavaRDD\n+import org.apache.spark.mllib.linalg._\n+import org.apache.spark.mllib.linalg.distributed.RowMatrix\n+import org.apache.spark.rdd.RDD\n+\n+/**\n+ * A feature transformer that projects vectors to a low-dimensional space using PCA.\n+ *\n+ * @param k num of principal components\n+ */\n+\n+class PCA(val k: Int) {\n+  require(k >= 1)\n+\n+  /**\n+   * Computes a [[PCAModel]] that contains the principal components of the input vectors.\n+   *\n+   * @param sources source vectors\n+   */\n+  def fit(sources: RDD[Vector]): PCAModel = {\n+    require(k <= sources.first().size)\n+\n+    val mat = new RowMatrix(sources)\n+    val pc = mat.computePrincipalComponents(k) match {\n+      case dm: DenseMatrix =>\n+        dm\n+      /*\n+       * Convert a sparse matrix to dense.\n+       *\n+       * RowMatrix.computePrincipalComponents always returns a dense matrix.\n+       * The following code is a safeguard.\n+       */\n+      case sm: SparseMatrix =>\n+        sm.toDense\n+      case _ =>\n+        throw new IllegalArgumentException(\"Unsupported matrix format. Expected \" +\n+          s\"SparseMatrix or DenseMatrix. Instead got: ${mat.getClass}\")\n+\n+    }\n+    new PCAModel(k, pc)\n+  }\n+\n+  /**\n+   * Computes a [[PCAModel]] that contains the principal components of the input vectors.\n+   *\n+   * @param sources source vectors\n+   */\n+  def fit(k: Int, sources: JavaRDD[Vector]): PCAModel =\n+    fit(k, sources.rdd)\n+}\n+\n+/**\n+ * Model fitted by [[PCA]] that can project vectors to a low-dimensional space space using PCA.\n+ *\n+ * @param k num of principal components.\n+ * @param pc a principal components Matrix\n+ */\n+class PCAModel private[mllib](val k: Int, val pc: DenseMatrix) extends VectorTransformer {\n+  /**\n+   * Transform a vector by computed Principal Components.\n+   *\n+   * @param vector vector to be transformed.","path":"mllib/src/main/scala/org/apache/spark/mllib/feature/PCA.scala","position":81,"original_position":81,"commit_id":"d7f6725f22943130d01a9dcb8b7f5688ff708834","original_commit_id":"d7f6725f22943130d01a9dcb8b7f5688ff708834","user":{"login":"jkbradley","id":5084283,"avatar_url":"https://avatars.githubusercontent.com/u/5084283?v=3","gravatar_id":"","url":"https://api.github.com/users/jkbradley","html_url":"https://github.com/jkbradley","followers_url":"https://api.github.com/users/jkbradley/followers","following_url":"https://api.github.com/users/jkbradley/following{/other_user}","gists_url":"https://api.github.com/users/jkbradley/gists{/gist_id}","starred_url":"https://api.github.com/users/jkbradley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jkbradley/subscriptions","organizations_url":"https://api.github.com/users/jkbradley/orgs","repos_url":"https://api.github.com/users/jkbradley/repos","events_url":"https://api.github.com/users/jkbradley/events{/privacy}","received_events_url":"https://api.github.com/users/jkbradley/received_events","type":"User","site_admin":false},"body":"Augment doc: \"Vector must be the same length as the source vectors given to PCA.fit()\"","created_at":"2015-04-29T01:32:27Z","updated_at":"2015-04-29T01:32:27Z","html_url":"https://github.com/apache/spark/pull/4304#discussion_r29304924","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/4304","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/29304924"},"html":{"href":"https://github.com/apache/spark/pull/4304#discussion_r29304924"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/4304"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/4304","id":28444703,"html_url":"https://github.com/apache/spark/pull/4304","diff_url":"https://github.com/apache/spark/pull/4304.diff","patch_url":"https://github.com/apache/spark/pull/4304.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/4304","number":4304,"state":"open","locked":false,"title":"[SPARK-5521] PCA wrapper for easy transform vectors","user":{"login":"catap","id":37775,"avatar_url":"https://avatars.githubusercontent.com/u/37775?v=3","gravatar_id":"","url":"https://api.github.com/users/catap","html_url":"https://github.com/catap","followers_url":"https://api.github.com/users/catap/followers","following_url":"https://api.github.com/users/catap/following{/other_user}","gists_url":"https://api.github.com/users/catap/gists{/gist_id}","starred_url":"https://api.github.com/users/catap/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/catap/subscriptions","organizations_url":"https://api.github.com/users/catap/orgs","repos_url":"https://api.github.com/users/catap/repos","events_url":"https://api.github.com/users/catap/events{/privacy}","received_events_url":"https://api.github.com/users/catap/received_events","type":"User","site_admin":false},"body":"I implement a simple PCA wrapper for easy transform of vectors by PCA for example LabeledPoint or another complicated structure.\r\n\r\nExample of usage:\r\n```\r\n  import org.apache.spark.mllib.regression.LinearRegressionWithSGD\r\n  import org.apache.spark.mllib.regression.LabeledPoint\r\n  import org.apache.spark.mllib.linalg.Vectors\r\n  import org.apache.spark.mllib.feature.PCA\r\n\r\n  val data = sc.textFile(\"data/mllib/ridge-data/lpsa.data\").map { line =>\r\n    val parts = line.split(',')\r\n    LabeledPoint(parts(0).toDouble, Vectors.dense(parts(1).split(' ').map(_.toDouble)))\r\n  }.cache()\r\n\r\n  val splits = data.randomSplit(Array(0.6, 0.4), seed = 11L)\r\n  val training = splits(0).cache()\r\n  val test = splits(1)\r\n\r\n  val pca = PCA.create(training.first().features.size/2, data.map(_.features))\r\n  val training_pca = training.map(p => p.copy(features = pca.transform(p.features)))\r\n  val test_pca = test.map(p => p.copy(features = pca.transform(p.features)))\r\n\r\n  val numIterations = 100\r\n  val model = LinearRegressionWithSGD.train(training, numIterations)\r\n  val model_pca = LinearRegressionWithSGD.train(training_pca, numIterations)\r\n\r\n  val valuesAndPreds = test.map { point =>\r\n    val score = model.predict(point.features)\r\n    (score, point.label)\r\n  }\r\n\r\n  val valuesAndPreds_pca = test_pca.map { point =>\r\n    val score = model_pca.predict(point.features)\r\n    (score, point.label)\r\n  }\r\n\r\n  val MSE = valuesAndPreds.map{case(v, p) => math.pow((v - p), 2)}.mean()\r\n  val MSE_pca = valuesAndPreds_pca.map{case(v, p) => math.pow((v - p), 2)}.mean()\r\n\r\n  println(\"Mean Squared Error = \" + MSE)\r\n  println(\"PCA Mean Squared Error = \" + MSE_pca)\r\n```","created_at":"2015-02-02T04:26:05Z","updated_at":"2015-04-29T01:32:27Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f81114e6f6de66996839f52900384d9d871b8123","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/4304/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/4304/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/4304/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/d7f6725f22943130d01a9dcb8b7f5688ff708834","head":{"label":"catap:pca","ref":"pca","sha":"d7f6725f22943130d01a9dcb8b7f5688ff708834","user":{"login":"catap","id":37775,"avatar_url":"https://avatars.githubusercontent.com/u/37775?v=3","gravatar_id":"","url":"https://api.github.com/users/catap","html_url":"https://github.com/catap","followers_url":"https://api.github.com/users/catap/followers","following_url":"https://api.github.com/users/catap/following{/other_user}","gists_url":"https://api.github.com/users/catap/gists{/gist_id}","starred_url":"https://api.github.com/users/catap/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/catap/subscriptions","organizations_url":"https://api.github.com/users/catap/orgs","repos_url":"https://api.github.com/users/catap/repos","events_url":"https://api.github.com/users/catap/events{/privacy}","received_events_url":"https://api.github.com/users/catap/received_events","type":"User","site_admin":false},"repo":{"id":29677797,"name":"spark","full_name":"catap/spark","owner":{"login":"catap","id":37775,"avatar_url":"https://avatars.githubusercontent.com/u/37775?v=3","gravatar_id":"","url":"https://api.github.com/users/catap","html_url":"https://github.com/catap","followers_url":"https://api.github.com/users/catap/followers","following_url":"https://api.github.com/users/catap/following{/other_user}","gists_url":"https://api.github.com/users/catap/gists{/gist_id}","starred_url":"https://api.github.com/users/catap/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/catap/subscriptions","organizations_url":"https://api.github.com/users/catap/orgs","repos_url":"https://api.github.com/users/catap/repos","events_url":"https://api.github.com/users/catap/events{/privacy}","received_events_url":"https://api.github.com/users/catap/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/catap/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/catap/spark","forks_url":"https://api.github.com/repos/catap/spark/forks","keys_url":"https://api.github.com/repos/catap/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/catap/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/catap/spark/teams","hooks_url":"https://api.github.com/repos/catap/spark/hooks","issue_events_url":"https://api.github.com/repos/catap/spark/issues/events{/number}","events_url":"https://api.github.com/repos/catap/spark/events","assignees_url":"https://api.github.com/repos/catap/spark/assignees{/user}","branches_url":"https://api.github.com/repos/catap/spark/branches{/branch}","tags_url":"https://api.github.com/repos/catap/spark/tags","blobs_url":"https://api.github.com/repos/catap/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/catap/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/catap/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/catap/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/catap/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/catap/spark/languages","stargazers_url":"https://api.github.com/repos/catap/spark/stargazers","contributors_url":"https://api.github.com/repos/catap/spark/contributors","subscribers_url":"https://api.github.com/repos/catap/spark/subscribers","subscription_url":"https://api.github.com/repos/catap/spark/subscription","commits_url":"https://api.github.com/repos/catap/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/catap/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/catap/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/catap/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/catap/spark/contents/{+path}","compare_url":"https://api.github.com/repos/catap/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/catap/spark/merges","archive_url":"https://api.github.com/repos/catap/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/catap/spark/downloads","issues_url":"https://api.github.com/repos/catap/spark/issues{/number}","pulls_url":"https://api.github.com/repos/catap/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/catap/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/catap/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/catap/spark/labels{/name}","releases_url":"https://api.github.com/repos/catap/spark/releases{/id}","created_at":"2015-01-22T12:29:55Z","updated_at":"2015-02-13T09:38:08Z","pushed_at":"2015-03-11T04:07:45Z","git_url":"git://github.com/catap/spark.git","ssh_url":"git@github.com:catap/spark.git","clone_url":"https://github.com/catap/spark.git","svn_url":"https://github.com/catap/spark","homepage":null,"size":91749,"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":"1c8633f3fe9d814c83384e339b958740c250c00c","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-04-29T00:41:23Z","pushed_at":"2015-04-29T01:02:48Z","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":1576726,"stargazers_count":3786,"watchers_count":3786,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":3290,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":308,"forks":3290,"open_issues":308,"watchers":3786,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/4304"},"html":{"href":"https://github.com/apache/spark/pull/4304"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/4304"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/4304/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/4304/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/4304/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/d7f6725f22943130d01a9dcb8b7f5688ff708834"}}}}