-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-5987] [MLlib] Save/load for GaussianMixtureModels #4986
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cb77095
[SPARK-5987] Save/load for GaussianMixtureModels
MechCoder b9794e4
Minor
MechCoder 7422bb4
Store sigmas as Array[Double] instead of Array[Array[Double]]
MechCoder 505bd57
Rebased over master and used MatrixUDT
MechCoder 33c84f9
Store as Array[Data] instead of Data[Array]
MechCoder e7a14cb
Minor
MechCoder 7d2cd56
Iterate over dataframe in a better way
MechCoder 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
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
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
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 |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ import org.apache.spark.mllib.linalg.{Vectors, Matrices} | |
| import org.apache.spark.mllib.stat.distribution.MultivariateGaussian | ||
| import org.apache.spark.mllib.util.MLlibTestSparkContext | ||
| import org.apache.spark.mllib.util.TestingUtils._ | ||
| import org.apache.spark.util.Utils | ||
|
|
||
| class GaussianMixtureSuite extends FunSuite with MLlibTestSparkContext { | ||
| test("single cluster") { | ||
|
|
@@ -48,13 +49,7 @@ class GaussianMixtureSuite extends FunSuite with MLlibTestSparkContext { | |
| } | ||
|
|
||
| test("two clusters") { | ||
| val data = sc.parallelize(Array( | ||
| Vectors.dense(-5.1971), Vectors.dense(-2.5359), Vectors.dense(-3.8220), | ||
| Vectors.dense(-5.2211), Vectors.dense(-5.0602), Vectors.dense( 4.7118), | ||
| Vectors.dense( 6.8989), Vectors.dense( 3.4592), Vectors.dense( 4.6322), | ||
| Vectors.dense( 5.7048), Vectors.dense( 4.6567), Vectors.dense( 5.5026), | ||
| Vectors.dense( 4.5605), Vectors.dense( 5.2043), Vectors.dense( 6.2734) | ||
| )) | ||
| val data = sc.parallelize(GaussianTestData.data) | ||
|
|
||
| // we set an initial gaussian to induce expected results | ||
| val initialGmm = new GaussianMixtureModel( | ||
|
|
@@ -105,14 +100,7 @@ class GaussianMixtureSuite extends FunSuite with MLlibTestSparkContext { | |
| } | ||
|
|
||
| test("two clusters with sparse data") { | ||
| val data = sc.parallelize(Array( | ||
| Vectors.dense(-5.1971), Vectors.dense(-2.5359), Vectors.dense(-3.8220), | ||
| Vectors.dense(-5.2211), Vectors.dense(-5.0602), Vectors.dense( 4.7118), | ||
| Vectors.dense( 6.8989), Vectors.dense( 3.4592), Vectors.dense( 4.6322), | ||
| Vectors.dense( 5.7048), Vectors.dense( 4.6567), Vectors.dense( 5.5026), | ||
| Vectors.dense( 4.5605), Vectors.dense( 5.2043), Vectors.dense( 6.2734) | ||
| )) | ||
|
|
||
| val data = sc.parallelize(GaussianTestData.data) | ||
| val sparseData = data.map(point => Vectors.sparse(1, Array(0), point.toArray)) | ||
| // we set an initial gaussian to induce expected results | ||
| val initialGmm = new GaussianMixtureModel( | ||
|
|
@@ -138,4 +126,38 @@ class GaussianMixtureSuite extends FunSuite with MLlibTestSparkContext { | |
| assert(sparseGMM.gaussians(0).sigma ~== Esigma(0) absTol 1E-3) | ||
| assert(sparseGMM.gaussians(1).sigma ~== Esigma(1) absTol 1E-3) | ||
| } | ||
|
|
||
| test("model save / load") { | ||
| val data = sc.parallelize(GaussianTestData.data) | ||
|
|
||
| val gmm = new GaussianMixture().setK(2).setSeed(0).run(data) | ||
| val tempDir = Utils.createTempDir() | ||
| val path = tempDir.toURI.toString | ||
|
|
||
| try { | ||
| gmm.save(sc, path) | ||
|
|
||
| // TODO: GaussianMixtureModel should implement equals/hashcode directly. | ||
| val sameModel = GaussianMixtureModel.load(sc, path) | ||
| assert(sameModel.k === gmm.k) | ||
|
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. Please leave a TODO here for GMM's |
||
| (0 until sameModel.k).foreach { i => | ||
| assert(sameModel.gaussians(i).mu === gmm.gaussians(i).mu) | ||
| assert(sameModel.gaussians(i).sigma === gmm.gaussians(i).sigma) | ||
| } | ||
| } finally { | ||
| Utils.deleteRecursively(tempDir) | ||
| } | ||
| } | ||
|
|
||
| object GaussianTestData { | ||
|
|
||
| val data = Array( | ||
| Vectors.dense(-5.1971), Vectors.dense(-2.5359), Vectors.dense(-3.8220), | ||
| Vectors.dense(-5.2211), Vectors.dense(-5.0602), Vectors.dense( 4.7118), | ||
| Vectors.dense( 6.8989), Vectors.dense( 3.4592), Vectors.dense( 4.6322), | ||
| Vectors.dense( 5.7048), Vectors.dense( 4.6567), Vectors.dense( 5.5026), | ||
| Vectors.dense( 4.5605), Vectors.dense( 5.2043), Vectors.dense( 6.2734) | ||
| ) | ||
|
|
||
| } | ||
| } | ||
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.
Please also update the Java example.