Structure

MLRecommender

A model you train to make recommendations based on item similarity, grouping, and, optionally, item ratings.

Declaration

struct MLRecommender

Overview

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

You create a recommender model by training it with tabular data that includes columns for the recommendation items and the groups the items belong to. You also have the option to include an item rating column, which gives higher-rated items more weight than those with lesser or negative ratings. The recommender uses the training information to find similarity patterns by looking at items that occur in groups or have similar ratings within groups.

After you train a recommender, you save it as a Core ML model file with the .mlmodel extension. Import this model file into your Xcode project by dragging it into the Project navigator. At runtime, use the recommender to make item suggestions to the user based on the patterns in training data and the user’s item history. For example, a hiking app can recommend trails based on the trails a user has previously hiked and their ratings of those trails.

Topics

Creating and Training a Recommender

init(trainingData: MLDataTable, userColumn: String, itemColumn: String, ratingColumn: String?, parameters: MLRecommender.ModelParameters)

Creates a recommender from a data table containing columns for recommendation items, item groupings, and, optionally, item ratings.

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.

var userIdentifierColumn: String

The name of the column you selected at initialization to define the user identifiers.

var itemIdentifierColumn: String

The name of the column you selected at initialization to define the item identifiers.

var ratingColumn: String?

The name of the column you selected at initialization to define the ratings.

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 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.