Bird
Raised Fist0
ML Pythonml~20 mins

Matrix factorization basics in ML Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Matrix Factorization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the main goal of matrix factorization in machine learning?

Matrix factorization is often used in recommendation systems and dimensionality reduction. What is the primary goal when applying matrix factorization?

ATo randomly shuffle the elements of the matrix to improve randomness
BTo decompose a matrix into product of two or more matrices to capture latent features
CTo convert a matrix into a single vector for easier processing
DTo increase the size of the original matrix by adding more rows and columns
Attempts:
2 left
💡 Hint

Think about how matrix factorization helps reveal hidden patterns or features.

❓ Predict Output
intermediate
2:00remaining
Output of matrix multiplication after factorization

Given matrices U and V below, what is the product U x V?

ML Python
import numpy as np
U = np.array([[1, 2], [3, 4]])
V = np.array([[2, 0], [1, 3]])
result = U @ V
print(result)
A
[[2 3]
 [6 7]]
B
[[4 6]
 [8 12]]
C
[[2 6]
 [3 12]]
D
[[4 6]
 [10 12]]
Attempts:
2 left
💡 Hint

Recall how matrix multiplication works: multiply rows of U by columns of V and sum.

❓ Hyperparameter
advanced
2:00remaining
Choosing the rank (k) in matrix factorization

In matrix factorization, the rank k controls the number of latent features. What is a common effect of choosing a very large k?

AThe model will underfit and fail to capture important patterns
BThe model will always perform better with no downsides
CThe model may overfit the training data and lose generalization
DThe model will ignore the latent features and use raw data only
Attempts:
2 left
💡 Hint

Think about what happens when a model is too complex for the data.

❓ Metrics
advanced
2:00remaining
Evaluating matrix factorization with RMSE

After training a matrix factorization model, you calculate the Root Mean Squared Error (RMSE) on test data. What does a lower RMSE indicate?

AThe predicted values are closer to the actual values, indicating better performance
BThe model predictions are more random and less reliable
CThe model is underfitting and missing important patterns
DThe model has a higher bias and lower variance
Attempts:
2 left
💡 Hint

RMSE measures the average difference between predicted and actual values.

🔧 Debug
expert
2:00remaining
Identifying error in matrix factorization code snippet

What error will this Python code raise when performing matrix factorization using numpy?

ML Python
import numpy as np
R = np.array([[5, 3], [4, 0], [1, 1]])
U = np.random.rand(3, 2)
V = np.random.rand(2, 3)
P = U @ V
print(P)
ANo error, prints a 3x3 matrix
BTypeError: unsupported operand type(s) for @: 'list' and 'list'
CValueError: shapes (3,2) and (3,2) not aligned: 2 (dim 1) != 3 (dim 0)
DIndexError: index out of bounds
Attempts:
2 left
💡 Hint

Check the shapes of matrices U and V before multiplication.

Practice

(1/5)
1. What is the main goal of matrix factorization in machine learning?
easy
A. To break a large matrix into smaller matrices to find hidden patterns
B. To increase the size of a matrix for better accuracy
C. To convert a matrix into a single number
D. To sort the matrix rows in ascending order

Solution

  1. Step 1: Understand matrix factorization purpose

    Matrix factorization breaks a big matrix into smaller ones to reveal hidden structures or patterns.
  2. Step 2: Compare options with this purpose

    Only To break a large matrix into smaller matrices to find hidden patterns correctly describes this goal; others describe unrelated tasks.
  3. Final Answer:

    To break a large matrix into smaller matrices to find hidden patterns -> Option A
  4. Quick Check:

    Matrix factorization = breaking matrix into smaller ones [OK]
Hint: Matrix factorization means splitting matrix to find patterns [OK]
Common Mistakes:
  • Thinking matrix factorization increases matrix size
  • Confusing factorization with sorting or summarizing
  • Assuming it converts matrix to a single value
2. Which of the following is the correct way to represent matrix factorization of a matrix R into two matrices P and Q?
easy
A. R = P + Q
B. R = P * Q (matrix multiplication)
C. R = P - Q
D. R = P / Q

Solution

  1. Step 1: Recall matrix factorization formula

    Matrix factorization expresses R as the product of two smaller matrices P and Q.
  2. Step 2: Identify correct mathematical operation

    Matrix multiplication (*) is used, not addition, subtraction, or division.
  3. Final Answer:

    R = P * Q (matrix multiplication) -> Option B
  4. Quick Check:

    Matrix factorization = matrix multiplication [OK]
Hint: Matrix factorization uses multiplication, not addition [OK]
Common Mistakes:
  • Using addition or subtraction instead of multiplication
  • Confusing element-wise operations with matrix multiplication
  • Thinking division applies to matrices here
3. Given the following Python code using NumPy for matrix factorization approximation:
import numpy as np
R = np.array([[5, 3], [4, 0]])
P = np.array([[1, 0], [0, 1]])
Q = np.array([[5, 3], [4, 2]])
approx = np.dot(P, Q)
print(approx)

What is the output printed?
medium
A. [[5 3] [4 2]]
B. [[5 3] [0 0]]
C. [[1 0] [0 1]]
D. [[9 3] [4 2]]

Solution

  1. Step 1: Calculate matrix multiplication np.dot(P, Q)

    Multiplying P=[[1,0],[0,1]] by Q=[[5,3],[4,2]] results in Q itself because P is identity-like.
  2. Step 2: Confirm output matches Q

    The result is [[5,3],[4,2]], matching [[5 3] [4 2]] exactly.
  3. Final Answer:

    [[5 3] [4 2]] -> Option A
  4. Quick Check:

    Identity matrix times Q = Q [OK]
Hint: Identity matrix times any matrix equals that matrix [OK]
Common Mistakes:
  • Confusing element-wise multiplication with dot product
  • Misreading P as zero matrix
  • Expecting zeros in output due to zero in R
4. Consider this Python snippet for matrix factorization update step:
import numpy as np
R = np.array([[5, 3], [4, 0]])
P = np.array([[1, 0], [0, 1]])
Q = np.array([[5, 3], [4, 2]])
for i in range(2):
    for j in range(2):
        if R[i][j] != 0:
            error = R[i][j] - np.dot(P[i, :], Q[:, j])
            P[i, :] += 0.01 * error * Q[:, j]
print(P)

What is the main issue with this code?
medium
A. The update step incorrectly updates P using Q's columns instead of rows
B. The error calculation uses wrong indices for R
C. The learning rate 0.01 is too large causing divergence
D. The code misses updating Q matrix

Solution

  1. Step 1: Analyze update step for matrix factorization

    Matrix factorization updates both P and Q to reduce error, but code updates only P.
  2. Step 2: Identify missing update for Q

    Q is never updated, so factorization won't improve fully.
  3. Final Answer:

    The code misses updating Q matrix -> Option D
  4. Quick Check:

    Both P and Q must be updated [OK]
Hint: Update both P and Q matrices in factorization [OK]
Common Mistakes:
  • Thinking only P needs updating
  • Confusing error calculation indices
  • Assuming learning rate causes error here
5. You have a user-item rating matrix with many missing values. Which approach best uses matrix factorization to predict missing ratings?
hard
A. Fill missing values with zeros and factorize the full matrix directly
B. Ignore missing values and factorize only the submatrix of known ratings
C. Factorize the matrix into two smaller matrices and use gradient descent to minimize error only on known ratings
D. Use matrix addition to combine user and item matrices

Solution

  1. Step 1: Understand handling missing values in matrix factorization

    Missing values should not be treated as zeros; only known ratings guide factorization.
  2. Step 2: Use gradient descent minimizing error on known ratings

    This approach updates factor matrices to predict missing values accurately.
  3. Final Answer:

    Factorize the matrix into two smaller matrices and use gradient descent to minimize error only on known ratings -> Option C
  4. Quick Check:

    Minimize error on known ratings for prediction [OK]
Hint: Train only on known ratings, predict missing [OK]
Common Mistakes:
  • Filling missing values with zeros distorts learning
  • Ignoring missing values loses data context
  • Using addition instead of multiplication for factorization
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard