}
@Test
public void completeJobImplicitToyExample() throws Exception {
Matrix observations = new SparseRowMatrix(4, 4, new Vector[] {
new DenseVector(new double[] { 5.0, 5.0, 2.0, 0 }),
new DenseVector(new double[] { 2.0, 0, 3.0, 5.0 }),
new DenseVector(new double[] { 0, 5.0, 0, 3.0 }),
new DenseVector(new double[] { 3.0, 0, 0, 5.0 }) });
Matrix preferences = new SparseRowMatrix(4, 4, new Vector[] {
new DenseVector(new double[] { 1.0, 1.0, 1.0, 0 }),
new DenseVector(new double[] { 1.0, 0, 1.0, 1.0 }),
new DenseVector(new double[] { 0, 1.0, 0, 1.0 }),
new DenseVector(new double[] { 1.0, 0, 0, 1.0 }) });
writeLines(inputFile, preferencesAsText(observations));
ParallelALSFactorizationJob alsFactorization = new ParallelALSFactorizationJob();
alsFactorization.setConf(conf);
int numFeatures = 3;
int numIterations = 5;
double lambda = 0.065;
double alpha = 20;
alsFactorization.run(new String[] { "--input", inputFile.getAbsolutePath(), "--output", outputDir.getAbsolutePath(),
"--tempDir", tmpDir.getAbsolutePath(), "--lambda", String.valueOf(lambda),
"--implicitFeedback", String.valueOf(true), "--alpha", String.valueOf(alpha),
"--numFeatures", String.valueOf(numFeatures), "--numIterations", String.valueOf(numIterations) });
Matrix u = MathHelper.readMatrix(conf, new Path(outputDir.getAbsolutePath(), "U/part-m-00000"),
observations.numRows(), numFeatures);
Matrix m = MathHelper.readMatrix(conf, new Path(outputDir.getAbsolutePath(), "M/part-m-00000"),
observations.numCols(), numFeatures);
StringBuilder info = new StringBuilder();
info.append("\nObservations - users x items\n");
info.append(MathHelper.nice(observations));
info.append("\nA - users x items\n\n");
info.append(MathHelper.nice(preferences));
info.append("\nU - users x features\n\n");
info.append(MathHelper.nice(u));
info.append("\nM - items x features\n\n");
info.append(MathHelper.nice(m));
Matrix Ak = u.times(m.transpose());
info.append("\nAk - users x items\n\n");
info.append(MathHelper.nice(Ak));
info.append('\n');
log.info(info.toString());
RunningAverage avg = new FullRunningAverage();
Iterator<MatrixSlice> sliceIterator = preferences.iterateAll();
while (sliceIterator.hasNext()) {
MatrixSlice slice = sliceIterator.next();
for (Vector.Element e : slice.vector()) {
if (!Double.isNaN(e.get())) {
double pref = e.get();