-
Notifications
You must be signed in to change notification settings - Fork 28.1k
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
[SPARK-5012][MLLib][PySpark]Python API for Gaussian Mixture Model #4059
Closed
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
fda60f3
Python API for Gaussian Mixture Model
FlytxtRnD 5c83825
Split input file with space delimiter
FlytxtRnD f82750b
Fixed style issues
FlytxtRnD 332bad1
Merge branch 'PythonGmmWrapper', remote-tracking branch 'upstream/mas…
FlytxtRnD 426d130
Added random seed parameter
FlytxtRnD c1d4c71
Merge branch 'PythonGmmWrapper', remote-tracking branch 'origin/Pytho…
FlytxtRnD 3464d19
Merge branch 'PythonGmmWrapper', remote-tracking branch 'upstream/mas…
FlytxtRnD 05767c7
Merge branch 'PythonGmmWrapper', remote-tracking branch 'upstream/mas…
FlytxtRnD 2e14d82
Incorporate MultivariateGaussian instances in GaussianMixtureModel
FlytxtRnD b22532c
Merge branch 'PythonGmmWrapper', remote-tracking branch 'upstream/mas…
FlytxtRnD 2e9f12a
Added mllib/stat/distribution.py and fixed style issues
FlytxtRnD 3aee84b
Fixed style issues
FlytxtRnD 6671ea1
Added mllib/stat/distribution.py
FlytxtRnD ac134f1
Merge branch 'PythonGmmWrapper' of https://github.com/FlytxtRnD/spark…
FlytxtRnD d5b36ab
Changed argument names to lowercase
FlytxtRnD fa0a142
New line added
FlytxtRnD 339b09c
Added MultivariateGaussian namedtuple and Arraybuffer in trainGaussi…
FlytxtRnD c973ab3
Merge branch 'PythonGmmWrapper', remote-tracking branch 'upstream/mas…
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 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,65 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
""" | ||
A Gaussian Mixture Model clustering program using MLlib. | ||
""" | ||
import sys | ||
import random | ||
import argparse | ||
import numpy as np | ||
|
||
from pyspark import SparkConf, SparkContext | ||
from pyspark.mllib.clustering import GaussianMixture | ||
|
||
|
||
def parseVector(line): | ||
return np.array([float(x) for x in line.split(' ')]) | ||
|
||
|
||
if __name__ == "__main__": | ||
""" | ||
Parameters | ||
---------- | ||
:param inputFile: Input file path which contains data points | ||
:param k: Number of mixture components | ||
:param convergenceTol: Convergence threshold. Default to 1e-3 | ||
:param maxIterations: Number of EM iterations to perform. Default to 100 | ||
:param seed: Random seed | ||
""" | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument('inputFile', help='Input File') | ||
parser.add_argument('k', type=int, help='Number of clusters') | ||
parser.add_argument('--convergenceTol', default=1e-3, type=float, help='convergence threshold') | ||
parser.add_argument('--maxIterations', default=100, type=int, help='Number of iterations') | ||
parser.add_argument('--seed', default=random.getrandbits(19), | ||
type=long, help='Random seed') | ||
args = parser.parse_args() | ||
|
||
conf = SparkConf().setAppName("GMM") | ||
sc = SparkContext(conf=conf) | ||
|
||
lines = sc.textFile(args.inputFile) | ||
data = lines.map(parseVector) | ||
model = GaussianMixture.train(data, args.k, args.convergenceTol, | ||
args.maxIterations, args.seed) | ||
for i in range(args.k): | ||
print ("weight = ", model.weights[i], "mu = ", model.gaussians[i].mu, | ||
"sigma = ", model.gaussians[i].sigma.toArray()) | ||
print ("Cluster labels (first 100): ", model.predict(data).take(100)) | ||
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
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,31 @@ | ||
# | ||
# 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 collections import namedtuple | ||
|
||
__all__ = ['MultivariateGaussian'] | ||
|
||
|
||
class MultivariateGaussian(namedtuple('MultivariateGaussian', ['mu', 'sigma'])): | ||
|
||
""" Represents a (mu, sigma) tuple | ||
>>> m = MultivariateGaussian(Vectors.dense([11,12]),DenseMatrix(2, 2, (1.0, 3.0, 5.0, 2.0))) | ||
>>> (m.mu, m.sigma.toArray()) | ||
(DenseVector([11.0, 12.0]), array([[ 1., 5.],[ 3., 2.]])) | ||
>>> (m[0], m[1]) | ||
(DenseVector([11.0, 12.0]), array([[ 1., 5.],[ 3., 2.]])) | ||
""" |
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
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.
separate spark imports from python imports