Non-negative matrix factorization
It's possible for one matrix to have two equally close factorizations where one has all positive values and the other has some negative values.
The matrix M
has been factored twice using two different factorizations. Take a look at each pair of factor matrices L
and U
, and W
and H
to see the differences. Then use their products to see that they produce essentially the same product.
This exercise is part of the course
Building Recommendation Engines with PySpark
Exercise instructions
- Use
print()
to view theL
andU
matrices. Notice that some values in matricesL
andU
are negative. - Use
print()
to view theW
andH
matrices. Notice that all values in these two matrices are positive. - The
L
andU
matrices andW
andH
matrices have been multiplied together to produce theLU
andWH
matrices respectively. UsegetRMSE(product_matrix, original_matrix)
to see how closeLU
is toM
compared to how closeWH
is toM
. Are they similar?
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# View the L, U, W, and H matrices.
print("Matrices L and U:")
print(____)
print(____)
print("Matrices W and H:")
print(____)
print(____)
# Calculate RMSE between LU and M
print("RMSE of LU: ", getRMSE(____, ____))
# Calculate RMSE between WH and M
print("RMSE of WH: ", getRMSE(____, ____))