public void loadData(Review[] reviews) { for (Review aReview : reviews) { UserVector uv = users.get(aReview.user_id); if (uv == null) { uv = new UserVector(); } uv.addReview(aReview.business_id, aReview.rating); users.put(aReview.user_id, uv); } }
public double[] getAvgRatings(Cluster c) { double[] ratings = new double[dimensions]; double[] counter = new double[dimensions]; for (UserVector user : c.getUsers().values()) { HashMap<Integer, Double> vect = user.getVector(); for (Entry<Integer, Double> e : vect.entrySet()) { ratings[e.getKey()] += e.getValue(); ++counter[e.getKey()]; } } // Average Total Ratings for (int idx = 0; idx < dimensions; ++idx) { if (counter[idx] != 0) { ratings[idx] /= counter[idx]; } } return ratings; }
public void performKmeans(int K) { for (int i = 0; i < K; i++) { Cluster aCluster = new Cluster(dimensions); clusters.add(aCluster); } boolean done = false; int numberOfSteps = 0; while (!done && (numberOfSteps < 20)) { System.out.println("Step #" + (++numberOfSteps)); for (Cluster c : clusters) { c.clearUsers(); } done = true; for (int idx = 0; idx < users.size(); ++idx) { UserVector user = users.get(idx); Cluster closestCluster = null; double minDistance = 1000000.0; for (Cluster myCluster : clusters) { double[] myClusterCenter = myCluster.getCenter(); double[] userVect = user.toArray(dimensions); double dist = getDistance(userVect, myClusterCenter); if (dist < minDistance) { minDistance = dist; closestCluster = myCluster; } } closestCluster.addUser(new Integer(idx), user); } for (Cluster c : clusters) { done = done & c.calculateCenter(); } } }