Structure

MLRecommender

A model you train to make recommendations based on item similarities, and optionally, user ratings.

Declaration

struct MLRecommender

Overview

Use an MLRecommender to train a machine learning model you include in your app to make recommendations for the user, while keeping their data on-device.

When you create a recommender model, you train it with tabular data that includes columns for item and user identifiers. Optionally, you can also include a rating column which is a score of that activity by the user listed in that row. The recommender uses all other columns to look for similarities between items. For example, you can provide the recommender model with a data table of Yosemite hiking trail names (items), hiker ID numbers (user identifiers), and each hiker’s ratings of the trails. The remaining columns, which the recommender uses to determine item similarity, might contain trail attributes, such as hiking distance, average elevation, elevation gain, and difficulty.

After you train a recommender, you save it as a Core ML model file with the .mlmodel extension. You can then import this model file into an app that uses a recommender model to make on-device recommendations, given the user’s history of items and their ratings. For example, a hiking app can recommend hiking trails based on the trails the user has already hiked and their ratings of those trails.

Topics

Creating and Training a Recommender

struct MLRecommender.ModelParameters

Parameters that affect the process of training a recommender model.

let modelParameters: MLRecommender.ModelParameters

The configuration parameters that the recommender used for training during initialization.

Evaluating a Recommender

protocol MLIdentifier

A set of properties that defines the interface for user and item identifiers used in recommender data.

struct MLRecommenderMetrics

Metrics used to evaluate a recommender’s performance.

Testing a Recommender

func recommendations(fromUsers: [MLIdentifier], maxCount: Int, restrictingToItems: [MLIdentifier]?, excluding: MLDataTable?, excludingObserved: Bool) -> MLDataTable

Retrieves the highest scored item for the given array of users, based on item similarity and the rating column.

func recommendations<T>(fromUsers: MLDataColumn<T>, maxCount: Int, restrictingToItems: MLDataColumn<T>?, excluding: MLDataTable?, excludingObserved: Bool) -> MLDataTable

Retrieves the highest scored items for the given column of users, based on item similarity and the rating column.

func getSimilarItems(fromItems: [MLIdentifier], maxCount: Int) -> MLDataTable

Returns the top ranked similar items based on the model’s similarity type.

func getSimilarItems<T>(fromItems: MLDataColumn<T>, maxCount: Int) -> MLDataTable

Returns the top ranked similar items based on the model’s similarity type.

Saving a Recommender

func write(to: URL, metadata: MLModelMetadata?)

Exports the recommender as a Core ML model file at the given URL.

func write(toFile: String, metadata: MLModelMetadata?)

Exports the recommender as a Core ML model file at the given file path.

Describing a Recommender

var itemIdentifierColumn: String

The name of the column containing the item identifiers.

var userIdentifierColumn: String

The name of the column containing the user identifiers.

var ratingColumn: String?

The name of the column containing the ratings.

var model: MLModel

The underlying Core ML model of the recommender stored in memory.

See Also

Tabular Data

Creating a Model from Tabular Data

Train a machine learning model by using Core ML to import and manage tabular data.

enum MLClassifier

A model you train to classify data into discrete categories.

enum MLRegressor

A model you train to estimate continuous values.

struct MLDataTable

A table of data for training or evaluating a machine learning model.

enum MLDataValue

The value of a cell in a data table.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software