Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Matrix Factorization Recommendation

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Matrix Factorization Recommendation. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Amazon Personalize needs a lightweight collaborative-filtering baseline for recommending items from Amazon Shopping interaction data. Implement matrix factorization using stochastic gradient descent, then return the highest-scoring unseen items for every user.

The input ratings is a rectangular list of lists. A positive value represents an observed rating, while -1 represents a missing rating. Initialize user and item factors deterministically using the formulas 0.1 * (1 + ((user_index + factor_index) % 3)) and 0.1 * (1 + ((item_index + 2 * factor_index) % 3)). For each observed rating r, update the factors using the prediction error r - dot(user_vector, item_vector), learning rate, and L2 regularization. Repeat for epochs passes in row-major order.

Return one list of item indices per user. Each list must contain at most k unseen items, sorted by decreasing predicted score. Break equal-score ties by smaller item index.

Formal Specification

Input: ratings, a nonempty m x n matrix of floats; k, num_factors, epochs, learning_rate, and regularization, all numeric parameters. Output: a list of m lists containing item indices.

Constraints

  • 1 <= len(ratings), len(ratings[0]) <= 500
  • All rows have the same length
  • ratings[u][i] is -1 or a value in [0, 5]
  • 1 <= num_factors <= 20
  • 0 <= epochs <= 100
  • 0 <= k <= len(ratings[0])
  • learning_rate >= 0 and regularization >= 0

Function Signature

def recommend_items(ratings, k, num_factors, epochs, learning_rate, regularization):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output