Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a matrix factorization model using numpy.
ML Python
import numpy as np # Initialize matrices R = np.array([[5, 3, 0], [4, 0, 0], [1, 1, 0]]) num_users, num_items = R.shape K = 2 P = np.random.rand(num_users, [1]) Q = np.random.rand(num_items, K)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using num_users or num_items as the second dimension of P instead of K.
Setting the dimension to 1 which is incorrect for latent features.
✗ Incorrect
The matrix P should have dimensions (num_users x K), where K is the number of latent features.
2fill in blank
mediumComplete the code to compute the predicted rating matrix by multiplying P and Q transpose.
ML Python
predicted_ratings = np.dot(P, [1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Multiplying P by Q without transpose causing dimension mismatch.
Using P.T or R which are incorrect for prediction.
✗ Incorrect
To get the predicted ratings matrix, multiply P (users x features) by Q transpose (features x items).
3fill in blank
hardFix the error in the gradient descent update step for matrix factorization.
ML Python
for i in range(num_users): for j in range(num_items): if R[i][j] > 0: eij = R[i][j] - np.dot(P[i, :], Q[j, :].T) for k in range(K): P[i][k] += [1] * (2 * eij * Q[j][k] - 0.01 * P[i][k])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a negative learning rate causing the model to diverge.
Using too large a learning rate causing unstable updates.
✗ Incorrect
The learning rate should be positive to move in the direction that reduces error.
4fill in blank
hardFill both blanks to complete the error calculation and regularization term in matrix factorization.
ML Python
error = 0 for i in range(num_users): for j in range(num_items): if R[i][j] > 0: error += (R[i][j] - np.dot(P[i, :], Q[j, :].T))[1] 2 for k in range(K): error += 0.01 / 2 * (P[i][k][2] 2 + Q[j][k][2] 2)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' instead of '**' which multiplies but does not square.
Using '+' or '-' which changes the meaning of the formula.
✗ Incorrect
The squared error uses the power operator '**2' to square the difference and the regularization terms.
5fill in blank
hardFill all three blanks to create a dictionary of predicted ratings for items with predicted rating above 3.
ML Python
predicted_dict = {item: predicted_ratings[user][[1]] for user in range(num_users) for item in range(num_items) if predicted_ratings[user][item] [2] 3 and predicted_ratings[user][item] [3] 5} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'user' instead of 'item' as dictionary keys.
Using wrong comparison operators causing incorrect filtering.
✗ Incorrect
The dictionary keys are item indices, and the condition filters ratings greater than 3 and less than 5.