Class ALSModel

All Implemented Interfaces:
Serializable, org.apache.spark.internal.Logging, Params, HasBlockSize, HasPredictionCol, ALSModelParams, Identifiable, MLWritable

public class ALSModel extends Model<ALSModel> implements ALSModelParams, MLWritable
Model fitted by ALS.

param: rank rank of the matrix factorization model param: userFactors a DataFrame that stores user factors in two columns: id and features param: itemFactors a DataFrame that stores item factors in two columns: id and features

See Also:
  • Method Details

    • read

      public static MLReader<ALSModel> read()
    • load

      public static ALSModel load(String path)
    • userCol

      public Param<String> userCol()
      Description copied from interface: ALSModelParams
      Param for the column name for user ids. Ids must be integers. Other numeric types are supported for this column, but will be cast to integers as long as they fall within the integer value range. Default: "user"
      Specified by:
      userCol in interface ALSModelParams
      Returns:
      (undocumented)
    • itemCol

      public Param<String> itemCol()
      Description copied from interface: ALSModelParams
      Param for the column name for item ids. Ids must be integers. Other numeric types are supported for this column, but will be cast to integers as long as they fall within the integer value range. Default: "item"
      Specified by:
      itemCol in interface ALSModelParams
      Returns:
      (undocumented)
    • coldStartStrategy

      public Param<String> coldStartStrategy()
      Description copied from interface: ALSModelParams
      Param for strategy for dealing with unknown or new users/items at prediction time. This may be useful in cross-validation or production scenarios, for handling user/item ids the model has not seen in the training data. Supported values: - "nan": predicted value for unknown ids will be NaN. - "drop": rows in the input DataFrame containing unknown ids will be dropped from the output DataFrame containing predictions. Default: "nan".
      Specified by:
      coldStartStrategy in interface ALSModelParams
      Returns:
      (undocumented)
    • blockSize

      public final IntParam blockSize()
      Description copied from interface: HasBlockSize
      Param for block size for stacking input data in matrices. Data is stacked within partitions. If block size is more than remaining data in a partition then it is adjusted to the size of this data..
      Specified by:
      blockSize in interface HasBlockSize
      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)
    • 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)
    • rank

      public int rank()
    • userFactors

      public Dataset<Row> userFactors()
    • itemFactors

      public Dataset<Row> itemFactors()
    • setUserCol

      public ALSModel setUserCol(String value)
    • setItemCol

      public ALSModel setItemCol(String value)
    • setPredictionCol

      public ALSModel setPredictionCol(String value)
    • setColdStartStrategy

      public ALSModel setColdStartStrategy(String value)
    • setBlockSize

      public ALSModel setBlockSize(int value)
      Set block size for stacking input data in matrices. Default is 4096.

      Parameters:
      value - (undocumented)
      Returns:
      (undocumented)
    • 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)
    • copy

      public ALSModel 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<ALSModel>
      Parameters:
      extra - (undocumented)
      Returns:
      (undocumented)
    • write

      public MLWriter write()
      Description copied from interface: MLWritable
      Returns an MLWriter instance for this ML instance.
      Specified by:
      write in interface MLWritable
      Returns:
      (undocumented)
    • toString

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

      public Dataset<Row> recommendForAllUsers(int numItems)
      Returns top numItems items recommended for each user, for all users.
      Parameters:
      numItems - max number of recommendations for each user
      Returns:
      a DataFrame of (userCol: Int, recommendations), where recommendations are stored as an array of (itemCol: Int, rating: Float) Rows.
    • recommendForUserSubset

      public Dataset<Row> recommendForUserSubset(Dataset<?> dataset, int numItems)
      Returns top numItems items recommended for each user id in the input data set. Note that if there are duplicate ids in the input dataset, only one set of recommendations per unique id will be returned.
      Parameters:
      dataset - a Dataset containing a column of user ids. The column name must match userCol.
      numItems - max number of recommendations for each user.
      Returns:
      a DataFrame of (userCol: Int, recommendations), where recommendations are stored as an array of (itemCol: Int, rating: Float) Rows.
    • recommendForAllItems

      public Dataset<Row> recommendForAllItems(int numUsers)
      Returns top numUsers users recommended for each item, for all items.
      Parameters:
      numUsers - max number of recommendations for each item
      Returns:
      a DataFrame of (itemCol: Int, recommendations), where recommendations are stored as an array of (userCol: Int, rating: Float) Rows.
    • recommendForItemSubset

      public Dataset<Row> recommendForItemSubset(Dataset<?> dataset, int numUsers)
      Returns top numUsers users recommended for each item id in the input data set. Note that if there are duplicate ids in the input dataset, only one set of recommendations per unique id will be returned.
      Parameters:
      dataset - a Dataset containing a column of item ids. The column name must match itemCol.
      numUsers - max number of recommendations for each item.
      Returns:
      a DataFrame of (itemCol: Int, recommendations), where recommendations are stored as an array of (userCol: Int, rating: Float) Rows.