Description

Make predictions based on model trained from AlsTrainBatchOp.

There are two types of predictions: 1) rating prediction: given user and item, predict the rating. 2) recommend prediction: given a list of users, recommend k items for each users.

Parameters

Name Description Type Required? Default Value
topK Number of items recommended Integer 100
userCol User column name String
predictionCol Column name of prediction. String

Script Example

Code

  1. data = np.array([
  2. [1, 1, 0.6],
  3. [2, 2, 0.8],
  4. [2, 3, 0.6],
  5. [4, 1, 0.6],
  6. [4, 2, 0.3],
  7. [4, 3, 0.4],
  8. ])
  9. df_data = pd.DataFrame({
  10. "user": data[:, 0],
  11. "item": data[:, 1],
  12. "rating": data[:, 2],
  13. })
  14. df_data["user"] = df_data["user"].astype('int')
  15. df_data["item"] = df_data["item"].astype('int')
  16. data = dataframeToOperator(df_data, schemaStr='user bigint, item bigint, rating double', op_type='batch')
  17. als = AlsTrainBatchOp().setUserCol("user").setItemCol("item").setRateCol("rating") \
  18. .setNumIter(10).setRank(10).setLambda(0.01)
  19. predictor = AlsTopKPredictBatchOp()\
  20. .setUserCol("user").setPredictionCol("recommend")
  21. model = als.linkFrom(data)
  22. predictor.linkFrom(model, data.select("user").distinct()).print()