Recommender Systems

Collaborative Filtering

touvlo.rec_sys.cf.cost_function(X, Y, R, theta, _lambda)[source]

Computes the cost function J for Collaborative Filtering.

Parameters:
  • X (numpy.array) – Matrix of product features.
  • Y (numpy.array) – Scores’ matrix.
  • R (numpy.array) – Matrix of 0s and 1s (whether there’s a rating).
  • theta (numpy.array) – Matrix of user features.
  • _lambda (float) – The regularization hyperparameter.
Returns:

Computed cost.

Return type:

float

touvlo.rec_sys.cf.grad(params, Y, R, num_users, num_products, num_features, _lambda)[source]

Calculates gradient of Collaborative Filtering’s parameters

Parameters:
  • params (numpy.array) – flattened product and user features..
  • Y (numpy.array) – Scores’ matrix.
  • R (numpy.array) – Matrix of 0s and 1s (whether there’s a rating).
  • num_users (int) – Number of users in this instance.
  • num_products (int) – Number of products in this instance.
  • num_features (int) – Number of features in this instance.
  • _lambda (float) – The regularization hyperparameter.
Returns:

Flattened gradient of product and user parameters.

Return type:

numpy.array

touvlo.rec_sys.cf.unravel_params(params, num_users, num_products, num_features)[source]

Unravels flattened array into features’ matrices

Parameters:
  • params (numpy.array) – Row vector of coefficients.
  • num_users (int) – Number of users in this instance.
  • num_products (int) – Number of products in this instance.
  • num_features (int) – Number of features in this instance.
Returns:

A 2-tuple consisting of a matrix of product features and a matrix of user features.

Return type:

(numpy.array, numpy.array)