-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-15819][PYSPARK][ML] Add KMeanSummary in KMeans of PySpark #13557
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
9 commits
Select commit
Hold shift + click to select a range
59e67c8
[SPARK-15819][PYSPARK] Add KMeanSummary in KMeans of PySpark
zjffdu 112316d
fix unit test failure
zjffdu 11e5d8e
fix unit test failure
zjffdu 73b3763
fix code style
zjffdu 356885b
trigger build
zjffdu 2c78c69
fix test failure
zjffdu ef9436a
address comments
zjffdu b3a8068
extends ClusteringSummary
zjffdu 032bb9d
address the comments
zjffdu 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -296,6 +296,17 @@ def probability(self): | |
| return self._call_java("probability") | ||
|
|
||
|
|
||
| class KMeansSummary(ClusteringSummary): | ||
| """ | ||
| .. note:: Experimental | ||
|
|
||
| Summary of KMeans. | ||
|
|
||
| .. versionadded:: 2.1.0 | ||
| """ | ||
| pass | ||
|
|
||
|
|
||
| class KMeansModel(JavaModel, JavaMLWritable, JavaMLReadable): | ||
| """ | ||
| Model fitted by KMeans. | ||
|
|
@@ -316,6 +327,27 @@ def computeCost(self, dataset): | |
| """ | ||
| return self._call_java("computeCost", dataset) | ||
|
|
||
| @property | ||
| @since("2.1.0") | ||
| def hasSummary(self): | ||
| """ | ||
| Indicates whether a training summary exists for this model instance. | ||
| """ | ||
| return self._call_java("hasSummary") | ||
|
|
||
| @property | ||
| @since("2.1.0") | ||
| def summary(self): | ||
| """ | ||
| Gets summary (e.g. cluster assignments, cluster sizes) of the model trained on the | ||
| training set. An exception is thrown if no summary exists. | ||
| """ | ||
| if self.hasSummary: | ||
| return KMeansSummary(self._call_java("summary")) | ||
| else: | ||
| raise RuntimeError("No training summary available for this %s" % | ||
| self.__class__.__name__) | ||
|
|
||
|
|
||
| @inherit_doc | ||
| class KMeans(JavaEstimator, HasFeaturesCol, HasPredictionCol, HasMaxIter, HasTol, HasSeed, | ||
|
|
@@ -341,6 +373,13 @@ class KMeans(JavaEstimator, HasFeaturesCol, HasPredictionCol, HasMaxIter, HasTol | |
| True | ||
| >>> rows[2].prediction == rows[3].prediction | ||
| True | ||
| >>> model.hasSummary | ||
| True | ||
| >>> summary = model.summary | ||
| >>> summary.k | ||
| 2 | ||
| >>> summary.clusterSizes | ||
| [2, 2] | ||
| >>> kmeans_path = temp_path + "/kmeans" | ||
| >>> kmeans.save(kmeans_path) | ||
| >>> kmeans2 = KMeans.load(kmeans_path) | ||
|
|
@@ -349,6 +388,8 @@ class KMeans(JavaEstimator, HasFeaturesCol, HasPredictionCol, HasMaxIter, HasTol | |
| >>> model_path = temp_path + "/kmeans_model" | ||
|
||
| >>> model.save(model_path) | ||
| >>> model2 = KMeansModel.load(model_path) | ||
| >>> model2.hasSummary | ||
| False | ||
| >>> model.clusterCenters()[0] == model2.clusterCenters()[0] | ||
| array([ True, True], dtype=bool) | ||
| >>> model.clusterCenters()[1] == model2.clusterCenters()[1] | ||
|
|
||
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
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.
Can you match the other implementations here as well?