-
Notifications
You must be signed in to change notification settings - Fork 29.1k
[SPARK-6612] [MLLib] [PySpark] Python KMeans parity #5647
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
5 commits
Select commit
Hold shift + click to select a range
21eb84c
Python Kmeans - setEpsilon, setInitializationSteps, k and computeCost…
FlytxtRnD 4d4e695
added arguments in python tests
FlytxtRnD 20b3c68
python 3 fixes
FlytxtRnD 5fd3ced
doc test corrections
FlytxtRnD b9e451b
set seed to fixed value in doc test
FlytxtRnD 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 |
|---|---|---|
|
|
@@ -40,19 +40,25 @@ class KMeansModel(Saveable, Loader): | |
|
|
||
| >>> data = array([0.0,0.0, 1.0,1.0, 9.0,8.0, 8.0,9.0]).reshape(4, 2) | ||
| >>> model = KMeans.train( | ||
| ... sc.parallelize(data), 2, maxIterations=10, runs=30, initializationMode="random") | ||
| ... sc.parallelize(data), 2, maxIterations=10, runs=30, initializationMode="random", | ||
| ... seed=50, initializationSteps=5, epsilon=1e-4) | ||
| >>> model.predict(array([0.0, 0.0])) == model.predict(array([1.0, 1.0])) | ||
| True | ||
| >>> model.predict(array([8.0, 9.0])) == model.predict(array([9.0, 8.0])) | ||
| True | ||
| >>> model.k | ||
| 2 | ||
| >>> model.computeCost(sc.parallelize(data)) | ||
| 2.0000000000000004 | ||
| >>> model = KMeans.train(sc.parallelize(data), 2) | ||
| >>> sparse_data = [ | ||
| ... SparseVector(3, {1: 1.0}), | ||
| ... SparseVector(3, {1: 1.1}), | ||
| ... SparseVector(3, {2: 1.0}), | ||
| ... SparseVector(3, {2: 1.1}) | ||
| ... ] | ||
| >>> model = KMeans.train(sc.parallelize(sparse_data), 2, initializationMode="k-means||") | ||
| >>> model = KMeans.train(sc.parallelize(sparse_data), 2, initializationMode="k-means||", | ||
|
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. here too |
||
| ... seed=50, initializationSteps=5, epsilon=1e-4) | ||
| >>> model.predict(array([0., 1., 0.])) == model.predict(array([0, 1.1, 0.])) | ||
| True | ||
| >>> model.predict(array([0., 0., 1.])) == model.predict(array([0, 0, 1.1])) | ||
|
|
@@ -83,6 +89,11 @@ def clusterCenters(self): | |
| """Get the cluster centers, represented as a list of NumPy arrays.""" | ||
| return self.centers | ||
|
|
||
| @property | ||
| def k(self): | ||
| """Total number of clusters.""" | ||
| return len(self.centers) | ||
|
|
||
| def predict(self, x): | ||
| """Find the cluster to which x belongs in this model.""" | ||
| best = 0 | ||
|
|
@@ -95,6 +106,15 @@ def predict(self, x): | |
| best_distance = distance | ||
| return best | ||
|
|
||
| def computeCost(self, rdd): | ||
| """ | ||
| Return the K-means cost (sum of squared distances of points to | ||
| their nearest center) for this model on the given data. | ||
| """ | ||
| cost = callMLlibFunc("computeCostKmeansModel", rdd.map(_convert_to_vector), | ||
| [_convert_to_vector(c) for c in self.centers]) | ||
| return cost | ||
|
|
||
| def save(self, sc, path): | ||
| java_centers = _py2java(sc, [_convert_to_vector(c) for c in self.centers]) | ||
| java_model = sc._jvm.org.apache.spark.mllib.clustering.KMeansModel(java_centers) | ||
|
|
@@ -109,10 +129,11 @@ def load(cls, sc, path): | |
| class KMeans(object): | ||
|
|
||
| @classmethod | ||
| def train(cls, rdd, k, maxIterations=100, runs=1, initializationMode="k-means||", seed=None): | ||
| def train(cls, rdd, k, maxIterations=100, runs=1, initializationMode="k-means||", | ||
| seed=None, initializationSteps=5, epsilon=1e-4): | ||
| """Train a k-means clustering model.""" | ||
| model = callMLlibFunc("trainKMeansModel", rdd.map(_convert_to_vector), k, maxIterations, | ||
| runs, initializationMode, seed) | ||
| runs, initializationMode, seed, initializationSteps, epsilon) | ||
| centers = callJavaFunc(rdd.context, model.clusterCenters) | ||
| return KMeansModel([c.toArray() for c in centers]) | ||
|
|
||
|
|
||
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.
@jkbradley , It seems we are not using this model anywhere. Did you mean to add the seed here too?
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.
If it's not used anywhere, then you can leave it as is. Thanks!
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.
@jkbradley , Shall we remove that line?
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.
No, let's keep it