This topic describes how to generate recommended items for users.
For known users and known items, the output includes a list of items that the user will most likely give a high rating and the predicted rating or ranking.
For a new user, and an explicit feedback model, the prediction is the global top K items that received the average highest ratings.
For a new user, and an implicit feedback model, the prediction is the global top K items with the highest number of interactions.
For a user who has tried all known items, the prediction is an empty list because it is not possible to recommend any other items. Set
remove_seen
tofalse
to repeat existing interactions from the training table.
Review and complete the following tasks:
When you run
ML_PREDICT_TABLE
or
ML_PREDICT_ROW
to generate item recommendations, a default value of three
items are recommended. To change this value, set the
topk
parameter.
-
If not already done, load the model. You can use the session variable for the model that is valid for the duration of the connection. Alternatively, you can use the model handle previously set. For the option to set the user name, you can set it to
NULL
.The following example uses the session variable.
mysql> CALL sys.ML_MODEL_LOAD(@model, NULL);
The following example uses the model handle.
mysql> CALL sys.ML_MODEL_LOAD('recommendation_use_case', NULL);
-
Make predictions for the test dataset by using the
ML_PREDICT_TABLE
routine.mysql> CALL sys.ML_PREDICT_TABLE(table_name, model_handle, output_table_name), [options]);
Replace
table_name
,model_handle
, andoutput_table_name
with your own values. Addoptions
as needed.The following example runs
ML_PREDICT_TABLE
on the testing dataset previously created and sets thetopk
parameter to 2, so only two items are recommended.mysql> CALL sys.ML_PREDICT_TABLE('recommendation_data.testing_dataset', @model, 'recommendation_data.item_recommendations', JSON_OBJECT('recommend', 'items', 'topk', 2));
Where:
recommendation_data.testing_dataset
is the fully qualified name of the input table that contains the data to generate predictions for (database_name.table_name
).@model
is the session variable for the model handle.recommendation_data.item_recommendations
is the fully qualified name of the output table with recommendations (database_name.table_name
).JSON_OBJECT('recommend', 'items', 'topk', 2)
sets the recommendation task to recommend items to users. A maximum of two items to recommend is set.
-
Query the output table to review the recommended top two items for each user in the output table.
mysql> SELECT * from item_recommendations; +---------+---------+--------+--------------------------------------------------------------------+ | user_id | item_id | rating | ml_results | +---------+---------+--------+--------------------------------------------------------------------+ | 1 | 2 | 4.0 | {"predictions": {"item_id": ["20", "18"], "rating": [4.7, 3.48]}} | | 1 | 4 | 7.0 | {"predictions": {"item_id": ["20", "18"], "rating": [4.7, 3.48]}} | | 1 | 6 | 1.5 | {"predictions": {"item_id": ["20", "18"], "rating": [4.7, 3.48]}} | | 1 | 8 | 3.5 | {"predictions": {"item_id": ["20", "18"], "rating": [4.7, 3.48]}} | | 10 | 18 | 1.5 | {"predictions": {"item_id": ["20", "3"], "rating": [4.9, 4.65]}} | | 10 | 2 | 6.5 | {"predictions": {"item_id": ["20", "3"], "rating": [4.9, 4.65]}} | | 10 | 5 | 3.0 | {"predictions": {"item_id": ["20", "3"], "rating": [4.9, 4.65]}} | | 10 | 6 | 5.5 | {"predictions": {"item_id": ["20", "3"], "rating": [4.9, 4.65]}} | | 2 | 1 | 5.0 | {"predictions": {"item_id": ["3", "17"], "rating": [4.65, 3.38]}} | | 2 | 3 | 8.0 | {"predictions": {"item_id": ["3", "17"], "rating": [4.65, 3.38]}} | | 2 | 5 | 2.5 | {"predictions": {"item_id": ["3", "17"], "rating": [4.65, 3.38]}} | | 2 | 7 | 6.5 | {"predictions": {"item_id": ["3", "17"], "rating": [4.65, 3.38]}} | | 3 | 18 | 7.0 | {"predictions": {"item_id": ["20", "3"], "rating": [4.39, 4.17]}} | | 3 | 2 | 3.5 | {"predictions": {"item_id": ["20", "3"], "rating": [4.39, 4.17]}} | | 3 | 5 | 6.5 | {"predictions": {"item_id": ["20", "3"], "rating": [4.39, 4.17]}} | | 3 | 8 | 2.5 | {"predictions": {"item_id": ["20", "3"], "rating": [4.39, 4.17]}} | | 4 | 1 | 5.5 | {"predictions": {"item_id": ["20", "3"], "rating": [5.71, 5.42]}} | | 4 | 3 | 8.5 | {"predictions": {"item_id": ["20", "3"], "rating": [5.71, 5.42]}} | | 4 | 6 | 2.0 | {"predictions": {"item_id": ["20", "3"], "rating": [5.71, 5.42]}} | | 4 | 7 | 5.5 | {"predictions": {"item_id": ["20", "3"], "rating": [5.71, 5.42]}} | | 5 | 12 | 5.0 | {"predictions": {"item_id": ["20", "18"], "rating": [5.05, 3.74]}} | | 5 | 2 | 7.0 | {"predictions": {"item_id": ["20", "18"], "rating": [5.05, 3.74]}} | | 5 | 4 | 1.5 | {"predictions": {"item_id": ["20", "18"], "rating": [5.05, 3.74]}} | | 5 | 6 | 4.0 | {"predictions": {"item_id": ["20", "18"], "rating": [5.05, 3.74]}} | | 6 | 3 | 6.0 | {"predictions": {"item_id": ["20", "3"], "rating": [5.25, 4.98]}} | | 6 | 5 | 1.5 | {"predictions": {"item_id": ["20", "3"], "rating": [5.25, 4.98]}} | | 6 | 7 | 4.5 | {"predictions": {"item_id": ["20", "3"], "rating": [5.25, 4.98]}} | | 6 | 8 | 7.0 | {"predictions": {"item_id": ["20", "3"], "rating": [5.25, 4.98]}} | | 7 | 1 | 6.5 | {"predictions": {"item_id": ["20", "3"], "rating": [5.41, 5.13]}} | | 7 | 4 | 3.0 | {"predictions": {"item_id": ["20", "3"], "rating": [5.41, 5.13]}} | | 7 | 5 | 5.5 | {"predictions": {"item_id": ["20", "3"], "rating": [5.41, 5.13]}} | | 7 | 9 | 8.0 | {"predictions": {"item_id": ["20", "3"], "rating": [5.41, 5.13]}} | | 8 | 2 | 8.5 | {"predictions": {"item_id": ["20", "18"], "rating": [4.53, 3.35]}} | | 8 | 4 | 2.5 | {"predictions": {"item_id": ["20", "18"], "rating": [4.53, 3.35]}} | | 8 | 6 | 5.0 | {"predictions": {"item_id": ["20", "18"], "rating": [4.53, 3.35]}} | | 8 | 9 | 3.5 | {"predictions": {"item_id": ["20", "18"], "rating": [4.53, 3.35]}} | | 9 | 1 | 5.0 | {"predictions": {"item_id": ["20", "3"], "rating": [5.09, 4.83]}} | | 9 | 3 | 8.0 | {"predictions": {"item_id": ["20", "3"], "rating": [5.09, 4.83]}} | | 9 | 7 | 2.5 | {"predictions": {"item_id": ["20", "3"], "rating": [5.09, 4.83]}} | | 9 | 8 | 5.5 | {"predictions": {"item_id": ["20", "3"], "rating": [5.09, 4.83]}} | +---------+---------+--------+--------------------------------------------------------------------+ 40 rows in set (0.0387 sec)
Review the recommended items in the
ml_results
column next toitem_id
. For example, user 1 is predicted to like items 20 and 18. Review the ratings in theml_results
column to review the expected ratings for each recommended item. For example, user 1 is expected to rate item 20 with a value of 4.7, and item 18 with a value of 3.48. -
Alternatively, if you do not want to generate an entire table of recommended items, you can run
ML_PREDICT_ROW
to specify a user to recommend items for.mysql> SELECT sys.ML_PREDICT_ROW(input_data, model_handle), [options]);
Replace
input_data
andmodel_handle
with your own values. Addoptions
as needed.The following example runs
ML_PREDICT_ROW
and specifies user 1 with a limit of two recommended items.mysql> SELECT sys.ML_PREDICT_ROW('{"user_id": "1"}', @model, JSON_OBJECT('recommend', 'users_to_items', 'topk', 2)); +--------------------------------------------------------------------------------------------------------+ | sys.ML_PREDICT_ROW('{"user_id": "1"}', @model, JSON_OBJECT('recommend', 'users_to_items', 'topk', 2)) | +--------------------------------------------------------------------------------------------------------+ | {"user_id": "1", "ml_results": {"predictions": {"rating": [4.7, 3.48], "item_id": ["20", "18"]}}} | +--------------------------------------------------------------------------------------------------------+ 1 row in set (0.7899 sec)
The predicted items of 20 and 18 and predicted ratings are the same as the one in the output table previously created.
-
Learn how to generate different types of recommendations:
Learn how to Score a Recommendation Model.