Class KMeans

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

public class KMeans extends Estimator<KMeansModel> implements KMeansParams, DefaultParamsWritable
K-means clustering with support for k-means|| initialization proposed by Bahmani et al.

See Also:
  • Constructor Details

    • KMeans

      public KMeans(String uid)
    • KMeans

      public KMeans()
  • Method Details

    • load

      public static KMeans load(String path)
    • read

      public static MLReader<T> read()
    • 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)
    • copy

      public KMeans 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 Estimator<KMeansModel>
      Parameters:
      extra - (undocumented)
      Returns:
      (undocumented)
    • setFeaturesCol

      public KMeans setFeaturesCol(String value)
    • setPredictionCol

      public KMeans setPredictionCol(String value)
    • setK

      public KMeans setK(int value)
    • setInitMode

      public KMeans setInitMode(String value)
    • setDistanceMeasure

      public KMeans setDistanceMeasure(String value)
    • setInitSteps

      public KMeans setInitSteps(int value)
    • setMaxIter

      public KMeans setMaxIter(int value)
    • setTol

      public KMeans setTol(double value)
    • setSeed

      public KMeans setSeed(long value)
    • setWeightCol

      public KMeans setWeightCol(String value)
      Sets the value of param weightCol(). If this is not set or empty, we treat all instance weights as 1.0. Default is not set, so all instances have weight one.

      Parameters:
      value - (undocumented)
      Returns:
      (undocumented)
    • setSolver

      public KMeans setSolver(String value)
      Sets the value of param solver(). Default is "auto".

      Parameters:
      value - (undocumented)
      Returns:
      (undocumented)
    • setMaxBlockSizeInMB

      public KMeans setMaxBlockSizeInMB(double value)
      Sets the value of param maxBlockSizeInMB(). Default is 0.0, then 1.0 MB will be chosen.

      Parameters:
      value - (undocumented)
      Returns:
      (undocumented)
    • fit

      public KMeansModel fit(Dataset<?> dataset)
      Description copied from class: Estimator
      Fits a model to the input data.
      Specified by:
      fit in class Estimator<KMeansModel>
      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)