forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-6227][MLLIB][PYSPARK] Implement PySpark wrappers for SVD and P…
…CA (v2) Add PCA and SVD to PySpark's wrappers for `RowMatrix` and `IndexedRowMatrix` (SVD only). Based on apache#7963, updated. ## How was this patch tested? New doc tests and unit tests. Ran all examples locally. Author: MechCoder <manojkumarsivaraj334@gmail.com> Author: Nick Pentreath <nickp@za.ibm.com> Closes apache#17621 from MLnick/SPARK-6227-pyspark-svd-pca.
- Loading branch information
Showing
9 changed files
with
408 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
from pyspark import SparkContext | ||
# $example on$ | ||
from pyspark.mllib.linalg import Vectors | ||
from pyspark.mllib.linalg.distributed import RowMatrix | ||
# $example off$ | ||
|
||
if __name__ == "__main__": | ||
sc = SparkContext(appName="PythonPCAOnRowMatrixExample") | ||
|
||
# $example on$ | ||
rows = sc.parallelize([ | ||
Vectors.sparse(5, {1: 1.0, 3: 7.0}), | ||
Vectors.dense(2.0, 0.0, 3.0, 4.0, 5.0), | ||
Vectors.dense(4.0, 0.0, 0.0, 6.0, 7.0) | ||
]) | ||
|
||
mat = RowMatrix(rows) | ||
# Compute the top 4 principal components. | ||
# Principal components are stored in a local dense matrix. | ||
pc = mat.computePrincipalComponents(4) | ||
|
||
# Project the rows to the linear space spanned by the top 4 principal components. | ||
projected = mat.multiply(pc) | ||
# $example off$ | ||
collected = projected.rows.collect() | ||
print("Projected Row Matrix of principal component:") | ||
for vector in collected: | ||
print(vector) | ||
sc.stop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
from pyspark import SparkContext | ||
# $example on$ | ||
from pyspark.mllib.linalg import Vectors | ||
from pyspark.mllib.linalg.distributed import RowMatrix | ||
# $example off$ | ||
|
||
if __name__ == "__main__": | ||
sc = SparkContext(appName="PythonSVDExample") | ||
|
||
# $example on$ | ||
rows = sc.parallelize([ | ||
Vectors.sparse(5, {1: 1.0, 3: 7.0}), | ||
Vectors.dense(2.0, 0.0, 3.0, 4.0, 5.0), | ||
Vectors.dense(4.0, 0.0, 0.0, 6.0, 7.0) | ||
]) | ||
|
||
mat = RowMatrix(rows) | ||
|
||
# Compute the top 5 singular values and corresponding singular vectors. | ||
svd = mat.computeSVD(5, computeU=True) | ||
U = svd.U # The U factor is a RowMatrix. | ||
s = svd.s # The singular values are stored in a local dense vector. | ||
V = svd.V # The V factor is a local dense matrix. | ||
# $example off$ | ||
collected = U.rows.collect() | ||
print("U factor is:") | ||
for vector in collected: | ||
print(vector) | ||
print("Singular values are: %s" % s) | ||
print("V factor is:\n%s" % V) | ||
sc.stop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.