Package org.apache.mahout.cf.taste.similarity

Examples of org.apache.mahout.cf.taste.similarity.ItemSimilarity


    writeLines(testFile, data);
  }

  @Test
  public void testLoadFromFile() throws Exception {
    ItemSimilarity similarity = new FileItemSimilarity(testFile);

    assertEquals(0.125, similarity.itemSimilarity(1L, 5L), EPSILON);
    assertEquals(0.125, similarity.itemSimilarity(5L, 1L), EPSILON);
    assertEquals(0.5, similarity.itemSimilarity(1L, 7L), EPSILON);
    assertEquals(0.5, similarity.itemSimilarity(7L, 1L), EPSILON);

    assertTrue(Double.isNaN(similarity.itemSimilarity(7L, 8L)));

    double[] valuesForOne = similarity.itemSimilarities(1L, new long[] { 5L, 7L });
    assertNotNull(valuesForOne);
    assertEquals(2, valuesForOne.length);
    assertEquals(0.125, valuesForOne[0], EPSILON);
    assertEquals(0.5, valuesForOne[1], EPSILON);
  }
View Full Code Here


            new Double[][] {
                    {1.0, 2.0},
                    {2.0, 5.0},
                    {3.0, 6.0},
            });
    ItemSimilarity otherSimilarity = new PearsonCorrelationSimilarity(dataModel);
    ItemSimilarity itemSimilarity = new GenericItemSimilarity(otherSimilarity, dataModel);
    assertCorrelationEquals(1.0, itemSimilarity.itemSimilarity(0, 0));
    assertCorrelationEquals(0.960768922830523, itemSimilarity.itemSimilarity(0, 1));
  }
View Full Code Here

    assertEquals(0.5, valuesForOne[1], EPSILON);
  }

  @Test
  public void testNoRefreshAfterFileUpdate() throws Exception {
    ItemSimilarity similarity = new FileItemSimilarity(testFile, 0L);

    /* call a method to make sure the original file is loaded*/
    similarity.itemSimilarity(1L, 5L);

    /* change the underlying file,
     * we have to wait at least a second to see the change in the file's lastModified timestamp */
    Thread.sleep(2000L);
    writeLines(testFile, changedData);

    /* we shouldn't see any changes in the data as we have not yet refreshed */
    assertEquals(0.5, similarity.itemSimilarity(1L, 7L), EPSILON);
    assertEquals(0.5, similarity.itemSimilarity(7L, 1L), EPSILON);
    assertTrue(Double.isNaN(similarity.itemSimilarity(7L, 8L)));
  }
View Full Code Here

    assertTrue(Double.isNaN(similarity.itemSimilarity(7L, 8L)));
  }

  @Test
  public void testRefreshAfterFileUpdate() throws Exception {
    ItemSimilarity similarity = new FileItemSimilarity(testFile, 0L);

    /* call a method to make sure the original file is loaded */
    similarity.itemSimilarity(1L, 5L);

    /* change the underlying file,
     * we have to wait at least a second to see the change in the file's lastModified timestamp */
    Thread.sleep(2000L);
    writeLines(testFile, changedData);

    similarity.refresh(null);

    /* we should now see the changes in the data */
    assertEquals(0.9, similarity.itemSimilarity(1L, 7L), EPSILON);
    assertEquals(0.9, similarity.itemSimilarity(7L, 1L), EPSILON);
    assertEquals(0.125, similarity.itemSimilarity(1L, 5L), EPSILON);
    assertEquals(0.125, similarity.itemSimilarity(5L, 1L), EPSILON);

    assertFalse(Double.isNaN(similarity.itemSimilarity(7L, 8L)));
    assertEquals(0.112, similarity.itemSimilarity(7L, 8L), EPSILON);
    assertEquals(0.112, similarity.itemSimilarity(8L, 7L), EPSILON);
  }
View Full Code Here

    assertEquals(0.5, valuesForOne[1], EPSILON);
  }

  @Test
  public void testToString() throws Exception {
    ItemSimilarity similarity = new FileItemSimilarity(testFile);
    assertTrue(similarity.toString().length() > 0);
  }
View Full Code Here

    if (args.length > 1) {
      howMany = Integer.parseInt(args[1]);
    }

    System.out.println("Run Items");
    ItemSimilarity similarity = new EuclideanDistanceSimilarity(model);
    Recommender recommender = new GenericItemBasedRecommender(model, similarity); // Use an item-item recommender
    for (int i = 0; i < LOOPS; i++){
      LoadStatistics loadStats = LoadEvaluator.runLoad(recommender, howMany);
      System.out.println(loadStats);
    }
View Full Code Here

                    {0.2, 0.3, 0.3, 0.6},
                    {0.4, 0.4, 0.5, 0.9},
                    {0.1, 0.4, 0.5, 0.8, 0.9, 1.0},
                    {0.2, 0.3, 0.6, 0.7, 0.1, 0.2},
            });
    ItemSimilarity similarity = new PearsonCorrelationSimilarity(dataModel);
    Optimizer optimizer = new ConjugateGradientOptimizer();
    Recommender recommender = new KnnItemBasedRecommender(dataModel, similarity, optimizer, 5);
    List<RecommendedItem> fewRecommended = recommender.recommend(1, 2);
    List<RecommendedItem> moreRecommended = recommender.recommend(1, 4);
    for (int i = 0; i < fewRecommended.size(); i++) {
View Full Code Here

            new Double[][] {
                    {0.1, 0.2},
                    {0.2, 0.3, 0.3, 0.6},
                    {0.4, 0.5, 0.5, 0.9},
            });
    ItemSimilarity similarity = new PearsonCorrelationSimilarity(dataModel);
    Optimizer optimizer = new ConjugateGradientOptimizer();
    Recommender recommender = new KnnItemBasedRecommender(dataModel, similarity, optimizer, 5);
    List<RecommendedItem> originalRecommended = recommender.recommend(1, 2);
    List<RecommendedItem> rescoredRecommended =
        recommender.recommend(1, 2, new ReversingRescorer<Long>());
View Full Code Here

    assertEquals(0.1f, firstRecommended.getValue(), EPSILON);
  }

  private static Recommender buildRecommender() throws TasteException {
    DataModel dataModel = getDataModel();
    ItemSimilarity similarity = new PearsonCorrelationSimilarity(dataModel);
    Optimizer optimizer = new ConjugateGradientOptimizer();
    return new KnnItemBasedRecommender(dataModel, similarity, optimizer, 5);
  }
View Full Code Here

      for (int j = i + 1; j < 6; j++) {
        similarities.add(
            new GenericItemSimilarity.ItemItemSimilarity(i, j, 1.0 / (1.0 + (double) i + (double) j)));
      }
    }
    ItemSimilarity similarity = new GenericItemSimilarity(similarities);
    Recommender recommender = new GenericItemBasedRecommender(dataModel, similarity);
    List<RecommendedItem> fewRecommended = recommender.recommend(1, 2);
    List<RecommendedItem> moreRecommended = recommender.recommend(1, 4);
    for (int i = 0; i < fewRecommended.size(); i++) {
      assertEquals(fewRecommended.get(i).getItemID(), moreRecommended.get(i).getItemID());
View Full Code Here

TOP

Related Classes of org.apache.mahout.cf.taste.similarity.ItemSimilarity

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.