Tuesday 15 September 2015

Mahout: ThresholdUserNeighborhood

Computes a neigbhorhood consisting of all users whose similarity to the given user meets or exceeds a certain threshold. Similarity is defined by UserSimilarity instance provided at the time of construction ThresholdUserNeighborhood instance.

Following example shows usage of NearestNUserNeighborhood.

Let’s say I had following data.

customers.csv
1,1,3
1,2,1
1,4,5
1,5,3
1,9,3
1,10,2
2,1,2
2,3,2
2,4,1
2,7,5
3,1,5
3,2,1
3,3,1
3,6,1
3,8,1
4,1,1
4,2,1
4,6,3
4,7,1
4,9,2
5,2,1
5,3,3
5,6,5
5,10,3
6,1,1
6,2,4
6,3,4
6,7,2
6,8,3
7,1,3
7,2,3
7,3,1
7,5,3
7,6,3
7,7,3
8,1,1
8,3,3
8,4,5
8,8,1
8,9,2
9,4,2
9,6,5
9,8,3
9,9,3
10,2,5
10,3,1
10,4,2
10,5,1
10,9,4
11,2,3
11,4,2
11,5,2
11,8,1
12,1,1
12,3,4
12,7,3
12,8,2
13,1,3
13,2,4
13,3,2
13,5,3
13,9,3
14,2,3
14,3,2
14,5,1
14,7,1
14,8,5
14,9,2
15,1,3
15,2,2
15,3,2
15,6,5
15,7,1
15,9,3
16,2,2
16,3,4
16,6,1
16,7,3
16,10,1
17,3,1
17,4,3
17,7,4
17,8,4
18,3,3
18,5,2
18,6,3
18,9,1
18,10,2
19,1,1
19,2,5
19,6,2
19,7,2
19,8,3
19,10,3
20,1,2
20,2,2
20,3,1
20,4,4
20,8,1


20,8,1 means user 20 bought item 8 and rated 1.

import java.io.File;
import java.io.IOException;

import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood;
import org.apache.mahout.cf.taste.impl.similarity.EuclideanDistanceSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;

public class ThresholdUserNeighborhoodEx {
 public static String dataFile = "/Users/harikrishna_gurram/customer.csv";

 public static void main(String args[]) throws IOException, TasteException {

  DataModel model = new FileDataModel(new File(dataFile));

  EuclideanDistanceSimilarity similarity = new EuclideanDistanceSimilarity(
    model);

  UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.5,
    similarity, model);

  long[] neighbors = neighborhood.getUserNeighborhood(1);
  System.out.println("Neighbors for user 1 are");
  for (long user : neighbors) {
   System.out.print(user + " ");
  }
 }
}

Output
Neighbors for user 1 are
5 16 20 15


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment