Class KMeansModel

All Implemented Interfaces:
Serializable, org.apache.spark.internal.Logging, KMeansParams, Params, HasDistanceMeasure, HasFeaturesCol, HasMaxBlockSizeInMB, HasMaxIter, HasPredictionCol, HasSeed, HasSolver, HasTol, HasWeightCol, GeneralMLWritable, HasTrainingSummary<KMeansSummary>, Identifiable, MLWritable, scala.Serializable

public class KMeansModel extends Model<KMeansModel> implements KMeansParams, GeneralMLWritable, HasTrainingSummary<KMeansSummary>
Model fitted by KMeans.

param: parentModel a model trained by spark.mllib.clustering.KMeans.

See Also:
  • Method Details

    • read

      public static MLReader<KMeansModel> read()
    • load

      public static KMeansModel load(String path)
    • k

      public final IntParam k()
      Description copied from interface: KMeansParams
      The number of clusters to create (k). Must be &gt; 1. Note that it is possible for fewer than k clusters to be returned, for example, if there are fewer than k distinct points to cluster. Default: 2.
      Specified by:
      k in interface KMeansParams
      Returns:
      (undocumented)
    • initMode

      public final Param<String> initMode()
      Description copied from interface: KMeansParams
      Param for the initialization algorithm. This can be either "random" to choose random points as initial cluster centers, or "k-means||" to use a parallel variant of k-means++ (Bahmani et al., Scalable K-Means++, VLDB 2012). Default: k-means||.
      Specified by:
      initMode in interface KMeansParams
      Returns:
      (undocumented)
    • initSteps

      public final IntParam initSteps()
      Description copied from interface: KMeansParams
      Param for the number of steps for the k-means|| initialization mode. This is an advanced setting -- the default of 2 is almost always enough. Must be &gt; 0. Default: 2.
      Specified by:
      initSteps in interface KMeansParams
      Returns:
      (undocumented)
    • solver

      public final Param<String> solver()
      Description copied from interface: KMeansParams
      Param for the name of optimization method used in KMeans. Supported options: - "auto": Automatically select the solver based on the input schema and sparsity: If input instances are arrays or input vectors are dense, set to "block". Else, set to "row". - "row": input instances are processed row by row, and triangle-inequality is applied to accelerate the training. - "block": input instances are stacked to blocks, and GEMM is applied to compute the distances. Default is "auto".

      Specified by:
      solver in interface HasSolver
      Specified by:
      solver in interface KMeansParams
      Returns:
      (undocumented)
    • maxBlockSizeInMB

      public final DoubleParam maxBlockSizeInMB()
      Description copied from interface: HasMaxBlockSizeInMB
      Param for Maximum memory in MB for stacking input data into blocks. Data is stacked within partitions. If more than remaining data size in a partition then it is adjusted to the data size. Default 0.0 represents choosing optimal value, depends on specific algorithm. Must be &gt;= 0..
      Specified by:
      maxBlockSizeInMB in interface HasMaxBlockSizeInMB
      Returns:
      (undocumented)
    • weightCol

      public final Param<String> weightCol()
      Description copied from interface: HasWeightCol
      Param for weight column name. If this is not set or empty, we treat all instance weights as 1.0.
      Specified by:
      weightCol in interface HasWeightCol
      Returns:
      (undocumented)
    • distanceMeasure

      public final Param<String> distanceMeasure()
      Description copied from interface: HasDistanceMeasure
      Param for The distance measure. Supported options: 'euclidean' and 'cosine'.
      Specified by:
      distanceMeasure in interface HasDistanceMeasure
      Returns:
      (undocumented)
    • tol

      public final DoubleParam tol()
      Description copied from interface: HasTol
      Param for the convergence tolerance for iterative algorithms (&gt;= 0).
      Specified by:
      tol in interface HasTol
      Returns:
      (undocumented)
    • predictionCol

      public final Param<String> predictionCol()
      Description copied from interface: HasPredictionCol
      Param for prediction column name.
      Specified by:
      predictionCol in interface HasPredictionCol
      Returns:
      (undocumented)
    • seed

      public final LongParam seed()
      Description copied from interface: HasSeed
      Param for random seed.
      Specified by:
      seed in interface HasSeed
      Returns:
      (undocumented)
    • featuresCol

      public final Param<String> featuresCol()
      Description copied from interface: HasFeaturesCol
      Param for features column name.
      Specified by:
      featuresCol in interface HasFeaturesCol
      Returns:
      (undocumented)
    • maxIter

      public final IntParam maxIter()
      Description copied from interface: HasMaxIter
      Param for maximum number of iterations (&gt;= 0).
      Specified by:
      maxIter in interface HasMaxIter
      Returns:
      (undocumented)
    • uid

      public String uid()
      Description copied from interface: Identifiable
      An immutable unique ID for the object and its derivatives.
      Specified by:
      uid in interface Identifiable
      Returns:
      (undocumented)
    • numFeatures

      public int numFeatures()
    • copy

      public KMeansModel copy(ParamMap extra)
      Description copied from interface: Params
      Creates a copy of this instance with the same UID and some extra params. Subclasses should implement this method and set the return type properly. See defaultCopy().
      Specified by:
      copy in interface Params
      Specified by:
      copy in class Model<KMeansModel>
      Parameters:
      extra - (undocumented)
      Returns:
      (undocumented)
    • setFeaturesCol

      public KMeansModel setFeaturesCol(String value)
    • setPredictionCol

      public KMeansModel setPredictionCol(String value)
    • transform

      public Dataset<Row> transform(Dataset<?> dataset)
      Description copied from class: Transformer
      Transforms the input dataset.
      Specified by:
      transform in class Transformer
      Parameters:
      dataset - (undocumented)
      Returns:
      (undocumented)
    • transformSchema

      public StructType transformSchema(StructType schema)
      Description copied from class: PipelineStage
      Check transform validity and derive the output schema from the input schema.

      We check validity for interactions between parameters during transformSchema and raise an exception if any parameter value is invalid. Parameter value checks which do not depend on other parameters are handled by Param.validate().

      Typical implementation should first conduct verification on schema change and parameter validity, including complex parameter interaction checks.

      Specified by:
      transformSchema in class PipelineStage
      Parameters:
      schema - (undocumented)
      Returns:
      (undocumented)
    • predict

      public int predict(Vector features)
    • clusterCenters

      public Vector[] clusterCenters()
    • write

      public GeneralMLWriter write()
      Returns a GeneralMLWriter instance for this ML instance.

      For KMeansModel, this does NOT currently save the training summary(). An option to save summary() may be added in the future.

      Specified by:
      write in interface GeneralMLWritable
      Specified by:
      write in interface MLWritable
      Returns:
      (undocumented)
    • toString

      public String toString()
      Specified by:
      toString in interface Identifiable
      Overrides:
      toString in class Object
    • summary

      public KMeansSummary summary()
      Gets summary of model on training set. An exception is thrown if hasSummary is false.
      Specified by:
      summary in interface HasTrainingSummary<KMeansSummary>
      Returns:
      (undocumented)