Table Of Contents

Previous topic

mrec.examples Package

This Page

mrec.item_similarity Package

slim Module

Train a Sparse Linear Methods (SLIM) item similarity model using various methods for sparse regression.

See:

Efficient Top-N Recommendation by Linear Regression, M. Levy and K. Jack, LSRS workshop at RecSys 2013.

SLIM: Sparse linear methods for top-n recommender systems, X. Ning and G. Karypis, ICDM 2011. http://glaros.dtc.umn.edu/gkhome/fetch/papers/SLIM2011icdm.pdf

class mrec.item_similarity.slim.NNFeatureSelectingSGDRegressor(model, k)

Bases: object

Wraps nearest-neighbour feature selection and regression in a single model.

Methods

fit(A, a)
fit(A, a)
class mrec.item_similarity.slim.SLIM(l1_reg=0.001, l2_reg=0.0001, fit_intercept=False, ignore_negative_weights=False, num_selected_features=200, model='sgd')

Bases: mrec.item_similarity.recommender.ItemSimilarityRecommender

Parameters :

l1_reg : float

L1 regularisation constant.

l2_reg : float

L2 regularisation constant.

fit_intercept : bool

Whether to fit a constant term.

ignore_negative_weights : bool

If true discard any computed negative similarity weights.

num_selected_features : int

The number of “features” (i.e. most similar items) to retain when using feature selection.

model : string

The underlying model to use: sgd, elasticnet, fs_sgd. :sgd: SGDRegressor with elasticnet penalty :elasticnet: ElasticNet :fs_sgd: NNFeatureSelectingSGDRegressor

Methods

batch_recommend_items(dataset[, max_items, ...]) Recommend new items for all users in the training dataset.
compute_similarities(dataset, j) Compute item similarity weights for item j.
compute_similarities_from_vec(dataset, a) Compute item similarity weights for out-of-dataset item vector.
fit(dataset[, item_features]) Learn the complete similarity matrix from a user-item matrix.
get_similar_items(j[, max_similar_items, ...]) Get the most similar items to a supplied item.
load(filepath) Load a recommender model from file after it has been serialized with save().
load_similarity_matrix(filepath, num_items) Load a precomputed similarity matrix from tsv.
range_recommend_items(dataset, user_start, ...) Recommend new items for a range of users in the training dataset.
read_recommender_description(filepath) Read a recommender model description from file after it has been saved by save(), without loading any additional associated data into memory.
recommend_items(dataset, u[, max_items, ...]) Recommend new items for a user.
save(filepath) Serialize model to file.
compute_similarities(dataset, j)

Compute item similarity weights for item j.

compute_similarities_from_vec(dataset, a)

Compute item similarity weights for out-of-dataset item vector.

mrec.item_similarity.slim.parse_version(version_string)

knn Module

Brute-force k-nearest neighbour recommenders intended to provide evaluation baselines.

class mrec.item_similarity.knn.CosineKNNRecommender(k)

Bases: mrec.item_similarity.knn.KNNRecommender

Similarity between two items is their cosine distance.

Methods

batch_recommend_items(dataset[, max_items, ...]) Recommend new items for all users in the training dataset.
compute_all_similarities(A, a)
compute_similarities(dataset, j)
fit(dataset[, item_features]) Learn the complete similarity matrix from a user-item matrix.
get_similar_items(j[, max_similar_items, ...]) Get the most similar items to a supplied item.
load(filepath) Load a recommender model from file after it has been serialized with save().
load_similarity_matrix(filepath, num_items) Load a precomputed similarity matrix from tsv.
range_recommend_items(dataset, user_start, ...) Recommend new items for a range of users in the training dataset.
read_recommender_description(filepath) Read a recommender model description from file after it has been saved by save(), without loading any additional associated data into memory.
recommend_items(dataset, u[, max_items, ...]) Recommend new items for a user.
save(filepath) Serialize model to file.
compute_all_similarities(A, a)
class mrec.item_similarity.knn.DotProductKNNRecommender(k)

Bases: mrec.item_similarity.knn.KNNRecommender

Similarity between two items is their dot product (i.e. cooccurrence count if input data is binary).

Methods

batch_recommend_items(dataset[, max_items, ...]) Recommend new items for all users in the training dataset.
compute_all_similarities(A, a)
compute_similarities(dataset, j)
fit(dataset[, item_features]) Learn the complete similarity matrix from a user-item matrix.
get_similar_items(j[, max_similar_items, ...]) Get the most similar items to a supplied item.
load(filepath) Load a recommender model from file after it has been serialized with save().
load_similarity_matrix(filepath, num_items) Load a precomputed similarity matrix from tsv.
range_recommend_items(dataset, user_start, ...) Recommend new items for a range of users in the training dataset.
read_recommender_description(filepath) Read a recommender model description from file after it has been saved by save(), without loading any additional associated data into memory.
recommend_items(dataset, u[, max_items, ...]) Recommend new items for a user.
save(filepath) Serialize model to file.
compute_all_similarities(A, a)
class mrec.item_similarity.knn.KNNRecommender(k)

Bases: mrec.item_similarity.recommender.ItemSimilarityRecommender

Abstract base class for k-nn recommenders. You must supply an implementation of the compute_all_similarities() method.

Parameters :

k : int

The number of nearest neighbouring items to retain

Methods

batch_recommend_items(dataset[, max_items, ...]) Recommend new items for all users in the training dataset.
compute_all_similarities(A, a) Compute similarity scores between item vector a
compute_similarities(dataset, j)
fit(dataset[, item_features]) Learn the complete similarity matrix from a user-item matrix.
get_similar_items(j[, max_similar_items, ...]) Get the most similar items to a supplied item.
load(filepath) Load a recommender model from file after it has been serialized with save().
load_similarity_matrix(filepath, num_items) Load a precomputed similarity matrix from tsv.
range_recommend_items(dataset, user_start, ...) Recommend new items for a range of users in the training dataset.
read_recommender_description(filepath) Read a recommender model description from file after it has been saved by save(), without loading any additional associated data into memory.
recommend_items(dataset, u[, max_items, ...]) Recommend new items for a user.
save(filepath) Serialize model to file.
compute_all_similarities(A, a)

Compute similarity scores between item vector a and all the rows of A.

Parameters :

A : scipy.sparse.csr_matrix

Matrix of item vectors.

a : array_like

The item vector to be compared to each row of A.

Returns :

similarities : numpy.ndarray

Vector of similarity scores.

compute_similarities(dataset, j)

precomputed Module

Make recommendations from a precomputed item similarity matrix.

class mrec.item_similarity.precomputed.PrecomputedItemSimilarityRecommender(description, similarity_matrix)

Bases: mrec.item_similarity.recommender.ItemSimilarityRecommender

Wrapper class to make recommendations using a precomputed item similarity matrix.

Parameters :

description : str

Printable name for this recommender.

similarity_matrix : array_like(num_items,num_items)

The precomputed item similarity matrix.

Methods

batch_recommend_items(dataset[, max_items, ...]) Recommend new items for all users in the training dataset.
compute_similarities(j)
fit(dataset[, item_features])
get_similar_items(j[, max_similar_items, ...]) Get the most similar items to a supplied item.
load(filepath) Load a recommender model from file after it has been serialized with save().
load_similarity_matrix(filepath, num_items) Load a precomputed similarity matrix from tsv.
range_recommend_items(dataset, user_start, ...) Recommend new items for a range of users in the training dataset.
read_recommender_description(filepath) Read a recommender model description from file after it has been saved by save(), without loading any additional associated data into memory.
recommend_items(dataset, u[, max_items, ...]) Recommend new items for a user.
save(filepath) Serialize model to file.
set_similarity_matrix(similarity_matrix)
compute_similarities(j)
fit(dataset, item_features=None)
set_similarity_matrix(similarity_matrix)

recommender Module

Base class for item similarity recommenders.

class mrec.item_similarity.recommender.ItemSimilarityRecommender

Bases: mrec.base_recommender.BaseRecommender

Abstract base class for recommenders that generate recommendations from an item similarity matrix. To implement a recommender you just need to supply the compute_similarities() method.

Methods

batch_recommend_items(dataset[, max_items, ...]) Recommend new items for all users in the training dataset.
compute_similarities(dataset, j) Compute pairwise similarity scores between the j-th item and every item in the dataset.
fit(dataset[, item_features]) Learn the complete similarity matrix from a user-item matrix.
get_similar_items(j[, max_similar_items, ...]) Get the most similar items to a supplied item.
load(filepath) Load a recommender model from file after it has been serialized with save().
load_similarity_matrix(filepath, num_items) Load a precomputed similarity matrix from tsv.
range_recommend_items(dataset, user_start, ...) Recommend new items for a range of users in the training dataset.
read_recommender_description(filepath) Read a recommender model description from file after it has been saved by save(), without loading any additional associated data into memory.
recommend_items(dataset, u[, max_items, ...]) Recommend new items for a user.
save(filepath) Serialize model to file.
batch_recommend_items(dataset, max_items=10, return_scores=True, show_progress=False, item_features=None)

Recommend new items for all users in the training dataset. Assumes you’ve already called fit() to learn the similarity matrix.

Parameters :

dataset : scipy.sparse.csr_matrix

User-item matrix containing known items.

max_items : int

Maximum number of recommended items to return.

return_scores : bool

If true return a score along with each recommended item.

show_progress: bool :

If true print something to stdout to show progress.

item_features : array_like, shape = [num_items, num_features]

Features for items in training set, ignored here.

Returns :

recs : list of lists

Each entry is a list of (idx,score) pairs if return_scores is True, else just a list of idxs.

compute_similarities(dataset, j)

Compute pairwise similarity scores between the j-th item and every item in the dataset.

Parameters :

j : int

Index of item for which to compute similarity scores.

dataset : mrec.sparse.fast_sparse_matrix

The user-item matrix.

Returns :

similarities : numpy.ndarray

Vector of similarity scores.

fit(dataset, item_features=None)

Learn the complete similarity matrix from a user-item matrix.

Parameters :

dataset : scipy sparse matrix or mrec.sparse.fast_sparse_matrix, shape = [num_users, num_items]

The matrix of user-item counts, row i holds the counts for the i-th user.

item_features : array_like, shape = [num_items, num_features]

Features for items in training set, ignored here.

get_similar_items(j, max_similar_items=30, dataset=None)

Get the most similar items to a supplied item.

Parameters :

j : int

Index of item for which to get similar items.

max_similar_items : int

Maximum number of similar items to return.

dataset : mrec.sparse.fast_sparse_matrix

The user-item matrix. Not required if you’ve already called fit() to learn the similarity matrix.

Returns :

sims : list

Sorted list of similar items, best first. Each entry is a tuple of the form (i,score).

load_similarity_matrix(filepath, num_items, offset=1)

Load a precomputed similarity matrix from tsv.

Parameters :

filepath : str

Filepath to tsv file holding externally computed similarity matrix.

num_items : int

Total number of items (might exceed highest ID in a sparse similarity matrix).

offset : int

Item index offset i.e. 1 if indices in file are 1-indexed.

range_recommend_items(dataset, user_start, user_end, max_items=10, return_scores=True, item_features=None)

Recommend new items for a range of users in the training dataset. Assumes you’ve already called fit() to learn the similarity matrix.

Parameters :

dataset : scipy.sparse.csr_matrix

User-item matrix containing known items.

user_start : int

Index of first user in the range to recommend.

user_end : int

Index one beyond last user in the range to recommend.

max_items : int

Maximum number of recommended items to return.

return_scores : bool

If true return a score along with each recommended item.

item_features : array_like, shape = [num_items, num_features]

Features for items in training set, ignored here.

Returns :

recs : list of lists

Each entry is a list of (idx,score) pairs if return_scores is True, else just a list of idxs.

recommend_items(dataset, u, max_items=10, return_scores=True, item_features=None)

Recommend new items for a user. Assumes you’ve already called fit() to learn the similarity matrix.

Parameters :

dataset : scipy.sparse.csr_matrix

User-item matrix containing known items.

u : int

Index of user for which to make recommendations.

max_items : int

Maximum number of recommended items to return.

return_scores : bool

If true return a score along with each recommended item.

item_features : array_like, shape = [num_items, num_features]

Features for items in training set, ignored here.

Returns :

recs : list

List of (idx,score) pairs if return_scores is True, else just a list of idxs.