-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-3961] [MLlib] [PySpark] Python API for mllib.feature #2819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8a50584
Python API for mllib.feature
davies 486795f
update programming guide, HashTF -> HashingTF
davies 7a1891a
fix tests
davies a405ae7
fix tests
davies 59781b9
Merge branch 'master' of github.com:apache/spark into feature
davies 3abb8c2
address comments
806c7c2
address comments
efb4f4f
Merge branch 'master' into feature
b628693
rollback changes in Word2Vec
67f6d21
address comments
4f48f48
add a note for HashingTF
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Python API for mllib.feature
- Loading branch information
commit 8a50584ed6ea38b5fccc64e6da3fc18d4513c9c5
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -29,8 +29,7 @@ import org.apache.spark.annotation.DeveloperApi | |
| import org.apache.spark.api.java.{JavaRDD, JavaSparkContext} | ||
| import org.apache.spark.mllib.classification._ | ||
| import org.apache.spark.mllib.clustering._ | ||
| import org.apache.spark.mllib.feature.Word2Vec | ||
| import org.apache.spark.mllib.feature.Word2VecModel | ||
| import org.apache.spark.mllib.feature._ | ||
| import org.apache.spark.mllib.optimization._ | ||
| import org.apache.spark.mllib.linalg._ | ||
| import org.apache.spark.mllib.random.{RandomRDDs => RG} | ||
|
|
@@ -289,6 +288,43 @@ class PythonMLLibAPI extends Serializable { | |
| ALS.trainImplicit(ratingsJRDD.rdd, rank, iterations, lambda, blocks, alpha) | ||
| } | ||
|
|
||
| /** | ||
| * Java stub for Normalizer.transform() | ||
| */ | ||
| def normalizeVector(p: Double, vector: Vector): Vector = { | ||
| new Normalizer(p).transform(vector) | ||
| } | ||
|
|
||
| /** | ||
| * Java stub for Normalizer.transform() | ||
| */ | ||
| def normalizeVector(p: Double, rdd: JavaRDD[Vector]): JavaRDD[Vector] = { | ||
| new Normalizer(p).transform(rdd) | ||
| } | ||
|
|
||
| /** | ||
| * Java stub for IDF.fit(). This stub returns a | ||
| * handle to the Java object instead of the content of the Java object. | ||
| * Extra care needs to be taken in the Python code to ensure it gets freed on | ||
| * exit; see the Py4J documentation. | ||
| */ | ||
| def fitStandardScaler( | ||
| withMean: Boolean, | ||
| withStd: Boolean, | ||
| data: JavaRDD[Vector]): StandardScalerModel = { | ||
| new StandardScaler(withMean, withStd).fit(data.rdd) | ||
| } | ||
|
|
||
| /** | ||
| * Java stub for IDF.fit(). This stub returns a | ||
| * handle to the Java object instead of the content of the Java object. | ||
| * Extra care needs to be taken in the Python code to ensure it gets freed on | ||
| * exit; see the Py4J documentation. | ||
| */ | ||
| def fitIDF(minDocFreq: Int, dataset: JavaRDD[Vector]): IDFModel = { | ||
| new IDF(minDocFreq).fit(dataset) | ||
| } | ||
|
|
||
| /** | ||
| * Java stub for Python mllib Word2Vec fit(). This stub returns a | ||
| * handle to the Java object instead of the content of the Java object. | ||
|
|
@@ -326,6 +362,16 @@ class PythonMLLibAPI extends Serializable { | |
| model.transform(word) | ||
| } | ||
|
|
||
| /** | ||
| * TODO: model is not serializable | ||
| * Transforms an RDD of words to its vector representation | ||
| * @param rdd an RDD of words | ||
| * @return an RDD of vector representations of words | ||
| */ | ||
| def transform(rdd: JavaRDD[String]): JavaRDD[Vector] = { | ||
| rdd.rdd.map(model.transform(_)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could remove "(_)" |
||
| } | ||
|
|
||
| def findSynonyms(word: String, num: Int): java.util.List[java.lang.Object] = { | ||
| val vec = transform(word) | ||
| findSynonyms(vec, num) | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ package org.apache.spark.mllib.feature | |
| import org.apache.spark.annotation.DeveloperApi | ||
| import org.apache.spark.mllib.linalg.Vector | ||
| import org.apache.spark.rdd.RDD | ||
| import org.apache.spark.api.java.JavaRDD | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. organize imports in alphabetical order |
||
|
|
||
| /** | ||
| * :: DeveloperApi :: | ||
|
|
@@ -48,4 +49,14 @@ trait VectorTransformer extends Serializable { | |
| data.map(x => this.transform(x)) | ||
| } | ||
|
|
||
| /** | ||
| * Applies transformation on an JavaRDD[Vector]. | ||
| * | ||
| * @param data JavaRDD[Vector] to be transformed. | ||
| * @return transformed JavaRDD[Vector]. | ||
| */ | ||
| def transform(data: JavaRDD[Vector]): JavaRDD[Vector] = { | ||
| transform(data.rdd) | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this an outdated comment?