Github user jkbradley commented on a diff in the pull request:

    https://github.com/apache/spark/pull/12402#discussion_r60271552
  
    --- Diff: python/pyspark/ml/clustering.py ---
    @@ -20,9 +20,150 @@
     from pyspark.ml.wrapper import JavaEstimator, JavaModel
     from pyspark.ml.param.shared import *
     from pyspark.mllib.common import inherit_doc
    +from pyspark.mllib.stat.distribution import MultivariateGaussian
     
     __all__ = ['BisectingKMeans', 'BisectingKMeansModel',
    -           'KMeans', 'KMeansModel']
    +           'KMeans', 'KMeansModel',
    +           'GaussianMixture', 'GaussianMixtureModel']
    +
    +
    +class GaussianMixtureModel(JavaModel, JavaMLWritable, JavaMLReadable):
    +    """
    +    .. note:: Experimental
    +
    +    Model fitted by GaussianMixture.
    +
    +    .. versionadded:: 2.0.0
    +    """
    +
    +    @property
    +    @since("2.0.0")
    +    def weights(self):
    +        """
    +        Weights for each Gaussian distribution in the mixture, where 
weights[i] is
    +        the weight for Gaussian i, and weights.sum == 1.
    +        """
    +        return self._call_java("weights")
    +
    +    @property
    +    @since("2.0.0")
    +    def gaussians(self):
    +        """
    +        Array of MultivariateGaussian where gaussians[i] represents
    +        the Multivariate Gaussian (Normal) Distribution for Gaussian i.
    +        """
    +        return [
    +            MultivariateGaussian(gaussian[0], gaussian[1])
    +            for gaussian in self._call_java("gaussiansPyDump")]
    +
    +
    +@inherit_doc
    +class GaussianMixture(JavaEstimator, HasFeaturesCol, HasPredictionCol, 
HasMaxIter, HasTol, HasSeed,
    +                      HasProbabilityCol, JavaMLWritable, JavaMLReadable):
    +    """
    +    .. note:: Experimental
    +
    +    GaussianMixture clustering.
    +
    +    >>> from pyspark.mllib.linalg import Vectors
    +
    +    >>> data1 = [(Vectors.dense([-0.1, -0.05 ]),),
    +    ...          (Vectors.dense([-0.01, -0.1]),),
    +    ...          (Vectors.dense([0.9, 0.8]),),
    +    ...          (Vectors.dense([0.75, 0.935]),),
    +    ...          (Vectors.dense([-0.83, -0.68]),),
    +    ...          (Vectors.dense([-0.91, -0.76]),)]
    +    >>> df1 = sqlContext.createDataFrame(data1, ["features"])
    +    >>> gaussianmixture = GaussianMixture(k=3, tol=0.0001,
    +    ...                                    maxIter=50, seed=10)
    +    >>> model = gaussianmixture.fit(df1)
    +    >>> weights = model.weights
    +    >>> len(weights)
    +    3
    +    >>> gaussians = model.gaussians
    +    >>> len(gaussians)
    +    3
    +    >>> transformed = model.transform(df1).select("features", "prediction")
    +    >>> rows = transformed.collect()
    +    >>> rows[0].prediction == rows[2].prediction
    +    False
    +    >>> rows[4].prediction == rows[5].prediction
    +    True
    +    >>> rows[1].prediction == rows[5].prediction
    +    False
    +    >>> rows[2].prediction == rows[3].prediction
    +    True
    +    >>> gmm_path = temp_path + "/gmm"
    +    >>> gaussianmixture.save(gmm_path)
    +    >>> gaussianmixture2 = GaussianMixture.load(gmm_path)
    +    >>> gaussianmixture2.getK()
    +    3
    +    >>> model_path = temp_path + "/gmm_model"
    +    >>> model.save(model_path)
    +    >>> model2 = GaussianMixtureModel.load(model_path)
    +    >>> model2.weights == model.weights
    +    True
    +    >>> model2.gaussians == model.gaussians
    +    True
    +    >>> mus, sigmas = list(
    +    ...     zip(*[(g.mu, g.sigma) for g in model.gaussians]))
    +    >>> sameMus, sameSigmas = list(
    +    ...     zip(*[(g.mu, g.sigma) for g in model2.gaussians]))
    +    >>> mus == sameMus
    +    True
    +    >>> sigmas == sameSigmas
    +    True
    +
    +    .. versionadded:: 2.0.0
    +    """
    +
    +    k = Param(Params._dummy(), "k", "number of clusters to create",
    +              typeConverter=TypeConverters.toInt)
    +
    +    @keyword_only
    +    def __init__(self, featuresCol="features", predictionCol="prediction", 
k=2,
    +                 probabilityCol="probability", tol=0.01, maxIter=100, 
seed=None):
    +        """
    +        __init__(self, featuresCol="features", predictionCol="prediction", 
k=2, \
    +                 probabilityCol="probability", tol=0.01, maxIter=100, 
seed=None)
    +        """
    +        super(GaussianMixture, self).__init__()
    +        self._java_obj = 
self._new_java_obj("org.apache.spark.ml.clustering.GaussianMixture",
    +                                            self.uid)
    +        self._setDefault(k=2, tol=0.01, maxIter=100)
    +        kwargs = self.__init__._input_kwargs
    +        self.setParams(**kwargs)
    +
    +    def _create_model(self, java_model):
    +        return GaussianMixtureModel(java_model)
    +
    +    @keyword_only
    +    @since("2.0.0")
    +    def setParams(self, featuresCol="features", 
predictionCol="prediction", k=2,
    +                  probabilityCol="probability", tol=0.01, maxIter=100, 
seed=None):
    +        """
    +        setParams(self, featuresCol="features", 
predictionCol="prediction", k=2, \
    +                  probabilityCol="probability", tol=0.01, maxIter=100, 
seed=None)
    +
    +        Sets params for GaussianMixture.
    +        """
    +        kwargs = self.setParams._input_kwargs
    +        return self._set(**kwargs)
    +
    +    @since("2.0.0")
    +    def setK(self, value):
    +        """
    +        Sets the value of :py:attr:`k`.
    +        """
    +        self._paramMap[self.k] = value
    --- End diff --
    
    Use ```self._set``` instead of accessing paramMap directly.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to