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

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


  public void testFromCorrelation() throws Exception {
    User user1 = getUser("test1", 1.0, 2.0);
    User user2 = getUser("test2", 2.0, 5.0);
    User user3 = getUser("test3", 3.0, 6.0);
    DataModel dataModel = getDataModel(user1, user2, user3);
    ItemSimilarity otherSimilarity = new PearsonCorrelationSimilarity(dataModel);
    ItemSimilarity itemSimilarity = new GenericItemSimilarity(otherSimilarity, dataModel);
    assertCorrelationEquals(1.0,
                            itemSimilarity.itemSimilarity(dataModel.getItem("0"), dataModel.getItem("0")));
    assertCorrelationEquals(0.960768922830523,
                            itemSimilarity.itemSimilarity(dataModel.getItem("0"), dataModel.getItem("1")));
  }
View Full Code Here


    statement.close();
    connection.close();

    EasyMock.replay(dataSource, connection, statement, resultSet);

    ItemSimilarity similarity = new MySQLJDBCInMemoryItemSimilarity(dataSource);

    assertEquals(0.5, similarity.itemSimilarity(1L, 2L), EPSILON);
    assertEquals(0.4, similarity.itemSimilarity(1L, 3L), EPSILON);
    assertEquals(0.1, similarity.itemSimilarity(3L, 4L), EPSILON);
    assertTrue(Double.isNaN(similarity.itemSimilarity(1L, 4L)));

    EasyMock.verify(dataSource, connection, statement, resultSet);
  }
View Full Code Here

  private final Recommender recommender;

  public Track2Recommender(DataModel dataModel, File dataFileDirectory) throws TasteException {
    // Change this to whatever you like!
    ItemSimilarity similarity;
    try {
      similarity = new HybridSimilarity(dataModel, dataFileDirectory);
    } catch (IOException ioe) {
      throw new TasteException(ioe);
    }
View Full Code Here

  private final Recommender recommender;

  public Track1Recommender(DataModel dataModel) throws TasteException {
    // Change this to whatever you like!
    ItemSimilarity similarity = new UncenteredCosineSimilarity(dataModel);
    recommender = new GenericItemBasedRecommender(dataModel, similarity);
  }
View Full Code Here

    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

    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().isEmpty());
  }
View Full Code Here

            new Double[][] {
                    {1.0, 2.0},
                    {2.0, 5.0},
                    {3.0, 6.0},
            });
    ItemSimilarity itemSimilarity = new PearsonCorrelationSimilarity(dataModel, Weighting.WEIGHTED);
    double correlation = itemSimilarity.itemSimilarity(0, 1);
    assertCorrelationEquals(0.9901922307076306, correlation);
  }
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

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.