The ML.RECOMMEND function

This document describes theML.RECOMMEND function, which lets you generatea predicted rating for every user-item rowcombination for amatrix factorization model.Because the input data for amatrix factorization model tends to be a sparse matrix with missing values,ML.RECOMMEND can return the predictions for those missing values withoutrequiring specification of each entry.

Note:ML.RECOMMEND can generate large outputs. Consider saving the output to atable for analysis.

Syntax

ML.RECOMMEND(  MODEL `PROJECT_ID.DATASET.MODEL_NAME`,  [, { TABLE `PROJECT_ID.DATASET.TABLE` | (QUERY_STATEMENT) }]  [, STRUCT(TRIAL_ID AS trial_id)])

Arguments

ML.RECOMMEND takes the following arguments:

Output

ML.RECOMMEND outputs at least 3 columns for all cases; theuser column, theitem column and a column for predicted recommendations.

The output ofML.RECOMMEND is computed as follows:

  • If both the user and item columns are in the input data,thenML.RECOMMEND returns a rating for each user-item pair.
  • If only the user or only the item is specified (for example, if the tableidentified by thetable argument onlycontains the user column), then all the item ratings for every user in thetable are outputted.
  • If either the user or item feature was not in the training dataset, the ratingthat is returned is the intercept of the feature that was provided, eitheritem or user, added to theglobal__intercept__ offset. For example,global__intercept__ + __intercept__['user_a'].
  • If input data is specified but does not provideeither the user or item column,ML.RECOMMEND returns an error.
  • If no input data is specified,ML.RECOMMENDoutputs the ratings for every user and item combination seen during training.

If the model was trained withfeedback_type=EXPLICIT, a user column calleduser, and an item column calleditem, thenML.RECOMMEND returns thefollowing columns:

  • user: aSTRING value containing the user data.
  • item: aSTRING value containing the item data
  • predicted_<rating_col_name>: aFLOAT64 value that contains the rating foreach user-item pair. Because the input ratings from training are assumed to beexplicit feedback, the predicted ratings are approximately in the range of theoriginal input, although ratings outside the range are also normal.

If the model was trained withfeedback_type=IMPLICIT, a user column calleduser, and an item column calleditem, thenML.RECOMMEND returns thefollowing columns:

  • user: aSTRING value containing the user data.
  • item: aSTRING value containing the item data
  • predicted_<rating_col_name>_confidence: aFLOAT64 value that contains therelative confidence for each user-item pair. The input ratings fromtraining are assumed to be a proxy for user confidence. Therefore, if the modelhas converged, the predicted confidences lie between approximately 0 and 1 (butcan lie just outside that range). If the model hasn't converged, the predictedconfidences can be any value. If your model isn't converging and your ratingsare very large, try decreasing theWALS_ALPHA value that's specified in theCREATE MODEL statement for the model. If your model isn't converging andyour ratings are very small, try increasing theWALS_ALPHA value.

Examples

The following examples show how to use theML.RECOMMEND function.

No input data

The following example generates predicted ratings for everyuser-item pair in the inputs ofmymodel because there is no input dataspecified.

SELECT*FROMML.RECOMMEND(MODEL`mydataset.mymodel`)

With input data

The following example generates predicted ratings for each user-item row inmydataset.mytable assuming thatmydataset.mymodel was trained using the usercolumnuser and item columnitem.

SELECT*FROMML.RECOMMEND(MODEL`mydataset.mymodel`,(SELECTuser,itemFROM`mydataset.mytable`))

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.