// Demonstrates the iterative approach. import edu.gwu.lintool.*; import java.util.*; public class MovieRatingsIterativeSVD { public static void main (String[] argv) { // Rows are users, columns are movies. // Missing ratings are marked by 0 double[][] X = { // Movies: 1 2 3 4 5 6 1+2=action, 3+4=drama, 5+6=comedy {5, 4, 1, 1, 4, 0}, // User 1, users 1 & 2 are similar {4, 5, 1, 2, 0, 5}, // User 2 {0, 0, 4, 0, 5, 4}, // User 3 users 3 and 4 are similar {4, 0, 4, 0, 5, 0}, // User 4 {4, 4, 1, 0, 1, 1}, // User 5 5 & 6 are similar {5, 5, 0, 1, 2, 1}, // User 6 {3, 3, 3, 3, 3, 3}, // User 7 7 & 8 are similar {2, 3, 2, 3, 2, 3}, // User 8 {5, 0, 0, 0, 0, 0} // User 9 9 is very dissimilar from all }; iterativeSVD (X, 1000); } static void iterativeSVD (double[][] X, int numIterations) { // Initial matrices U (m x m) and (m x n) int m = X.length, n = X[0].length; // The U matrix that'll store the r_i vectors: one r_i in each row. double[][] U = new double[m][m]; // The W matrix that'll store each w_j matrix, one w_j per column. double[][] W = new double[m][n]; // Initialize both sets of vectors to 1 for (int i=0; i 0.01) { // Non-zero // Let r_i = row i of U, w_j = col j of W // First, the dot product double rDotw = 0; for (int k=0; k