Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Recommenders team Recommenders NCF Predict

From Leeroopedia


Knowledge Sources
Domains Recommender Systems, Deep Learning, Model Inference
Last Updated 2026-02-10 00:00 GMT

Overview

Concrete tool for generating predictions from a trained Neural Collaborative Filtering model provided by the recommenders library.

Description

NCF.predict takes user and item identifiers (either single values or lists) and returns the model's predicted interaction scores. The method supports two modes: element-wise (single user-item pair, returns a float) and list-wise (arrays of users and items, returns a list of floats). Internally, the method converts original user/item IDs to internal indices via user2id and item2id, then runs the TensorFlow session's forward pass through the trained computation graph. The list-wise mode (is_list=True) is significantly more efficient because it batches all pairs into a single TensorFlow session call.

Usage

Call predict after training the NCF model with fit(). For evaluation, pass arrays of user and item IDs from the test set with is_list=True to score all test pairs efficiently. For single-pair queries (e.g., real-time serving), pass individual user and item IDs with is_list=False. The returned scores are in the range (0, 1) and can be used for ranking.

Code Reference

Source Location

  • Repository: recommenders
  • File: recommenders/models/ncf/ncf_singlenode.py
  • Lines: 416-435

Signature

def predict(self, user_input, item_input, is_list=False):

Import

from recommenders.models.ncf.ncf_singlenode import NCF

I/O Contract

Inputs

Name Type Required Description
user_input single value or list Yes A single user ID or a list/array of user IDs. Must be original IDs present in the training data's user2id mapping
item_input single value or list Yes A single item ID or a list/array of item IDs. Must be original IDs present in the training data's item2id mapping. When is_list=True, must be the same length as user_input
is_list bool No If True, treat inputs as parallel lists and perform batch prediction. If False, treat inputs as a single user-item pair. Defaults to False

Outputs

Name Type Description
predictions list or float When is_list=True: a list of floats, one predicted score per input pair, each in range (0, 1). When is_list=False: a single float representing the predicted score for the given user-item pair

Usage Examples

Basic Usage

from recommenders.models.ncf.ncf_singlenode import NCF
from recommenders.models.ncf.dataset import Dataset

# Assume model is already trained (see NCF_Init_And_Fit)
# model = NCF(...)
# model.fit(data)

# Single-pair prediction
score = model.predict(user_input=1, item_input=42, is_list=False)
print(f"Predicted score for user 1, item 42: {score:.4f}")
# Output: Predicted score for user 1, item 42: 0.8732

# Batch prediction (much faster for multiple pairs)
users = [1, 1, 2, 2, 3]
items = [10, 20, 10, 30, 50]
scores = model.predict(user_input=users, item_input=items, is_list=True)
print(scores)
# Output: [0.8732, 0.2145, 0.6543, 0.1234, 0.9012]

Evaluation Workflow

import numpy as np

# Score all test pairs for ranking evaluation
test_users = test_df["userID"].values
test_items = test_df["itemID"].values

predictions = model.predict(
    user_input=test_users,
    item_input=test_items,
    is_list=True,
)

test_df["prediction"] = predictions

# Use predictions for ranking metrics (Hit Rate, NDCG)
# See: Ranking Evaluation principles

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment