You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hama.apache.org by Apache Wiki <wi...@apache.org> on 2008/08/13 08:10:03 UTC
[Hama Wiki] Trivial Update of "TraditionalCollaborativeFiltering" by udanax
Dear Wiki user,
You have subscribed to a wiki page or wiki category on "Hama Wiki" for change notification.
The following page has been changed by udanax:
http://wiki.apache.org/hama/TraditionalCollaborativeFiltering
New page:
{{{
import java.math.BigInteger;
import org.apache.hama.HamaConfiguration;
import org.apache.hama.Matrix;
import org.apache.hama.Vector;
public class TraditionalCF {
public static double[][] data = {
{ 2, 5, 1, 4 },
{ 4, 1, 3, 3 },
{ 3, 4, 2, 4 }
};
public static void main(String[] args) {
HamaConfiguration conf = new HamaConfiguration();
Matrix tmpMatrix = new Matrix(conf, "input");
// Build an user by item matrix
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
tmpMatrix.set(i, j, data[i][j]);
}
}
// Get the pair set of all row key combinations
Combination x = new Combination(data.length, 2);
// Calculate |a|ยท|b|cos(q)
while (x.hasMore()) {
int[] pair = x.getNext();
System.out.print("Similarity: (" + pair[0] + ", " + pair[1] + ") = ");
Vector v1 = tmpMatrix.getRow(pair[0]);
Vector v2 = tmpMatrix.getRow(pair[1]);
double similarity = v1.dot(v2);
// Collect result
System.out.println(similarity);
}
}
static class Combination {
private int[] a;
private int n;
private int r;
private BigInteger numLeft;
private BigInteger total;
public Combination(int n, int r) {
if (r > n) {
throw new IllegalArgumentException();
}
if (n < 1) {
throw new IllegalArgumentException();
}
this.n = n;
this.r = r;
a = new int[r];
BigInteger nFact = getFactorial(n);
BigInteger rFact = getFactorial(r);
BigInteger nminusrFact = getFactorial(n - r);
total = nFact.divide(rFact.multiply(nminusrFact));
reset();
}
public void reset() {
for (int i = 0; i < a.length; i++) {
a[i] = i;
}
numLeft = new BigInteger(total.toString());
}
public BigInteger getNumLeft() {
return numLeft;
}
public boolean hasMore() {
return numLeft.compareTo(BigInteger.ZERO) == 1;
}
public BigInteger getTotal() {
return total;
}
private BigInteger getFactorial(int n) {
BigInteger fact = BigInteger.ONE;
for (int i = n; i > 1; i--) {
fact = fact.multiply(new BigInteger(Integer.toString(i)));
}
return fact;
}
public int[] getNext() {
if (numLeft.equals(total)) {
numLeft = numLeft.subtract(BigInteger.ONE);
return a;
}
int i = r - 1;
while (a[i] == n - r + i) {
i--;
}
a[i] = a[i] + 1;
for (int j = i + 1; j < r; j++) {
a[j] = a[i] + j - i;
}
numLeft = numLeft.subtract(BigInteger.ONE);
return a;
}
}
}
}}}