Browse files

Adding Ridge Regression. Does not seem to outperform Perceptron. I tried

different gamma values [1,2,3,4,5].
  • Loading branch information...
1 parent 3e27a86 commit 0170948917ee83aeacf62186d173f1e310237093 @blr246 committed Nov 14, 2011
Showing with 79 additions and 1 deletion.
  1. +5 −1 arch/DatingServer/src/server/Start.java
  2. +74 −0 ridge_regression.py
View
6 arch/DatingServer/src/server/Start.java
@@ -117,11 +117,15 @@ private static Double getScores(List<Double> oC){
private static List<Double> parseCandidate(String Candidate){
String[] list = Candidate.substring(1, Candidate.length()-1).split(",");
+ assert(list.length==noOfArrtributes) : "Number of values passed for candidate is not equal to N but " + list.length+"\nCandidate "+Candidate;
+
DecimalFormat fourDecimalFormat = new DecimalFormat("#.####");
List<Double> oC = new ArrayList<Double>();
for(int i=0;i<noOfArrtributes;i++){
- oC.add(Double.valueOf(fourDecimalFormat.format(Double.valueOf(list[i]))));
+ assert((Double.valueOf(fourDecimalFormat.format(Double.valueOf(list[i]))))>=0) : "Candidate value is less than 0!!!\nCandidate "+Candidate;
+ assert((Double.valueOf(fourDecimalFormat.format(Double.valueOf(list[i]))))<=1) : "Candidate value is greater than 1!!!\nCandidate "+Candidate;
+ oC.add(Double.valueOf(fourDecimalFormat.format(Double.valueOf(list[i]))));
}
return oC;
}
View
74 ridge_regression.py
@@ -0,0 +1,74 @@
+import perceptron
+import numpy as np
+import copy
+
+class RidgeMatchmaker(object):
+ def __init__(self, gamma=2.):
+ self._gamma = gamma
+
+ def new_client(self, examples):
+ """
+ The Matchmaker works with one client at a time. Pass an initial list
+ of examples so that the matchmaker can begin its search.
+ """
+
+ dims = len(examples[0][0])
+ self._examples = copy.deepcopy(examples)
+ # Create observation matrix.
+ self._A = np.array([e[0] for e in examples])
+ self._b = np.array([np.array([e[1]]) for e in examples])
+ # Add examples that weights sum to zero.
+ for mul in np.arange(-1.0, 1.1, 0.25):
+ e = np.ones(dims) * mul
+ self._A = np.vstack([self._A, e])
+ self._b = np.vstack([self._b, np.array([0.])])
+ self._examples.append(e)
+ # Create regularization matrix.
+ self._Gamma = np.identity(self._A.shape[1]) * self._gamma
+ # Solve for w.
+ M = np.dot(self._A.T, self._A) + np.dot(self._Gamma.T, self._Gamma)
+ self._w_estimate = np.dot(np.linalg.inv(M),
+ np.dot(self._A.T, self._b)).reshape((dims,))
+
+ def find_date(self):
+ """ Create a date. """
+
+ dims = len(self.w_estimate)
+ w_est_date = np.asarray(self._w_estimate > 0.0,
+ dtype=int).reshape((dims,))
+ mk_date = lambda: w_est_date
+ # Guarantee uniqueness.
+ dates = [e[0] for e in self.examples]
+ date_not_unique = lambda date: any((date == d).all() for d in dates)
+ while True:
+ # Get a date.
+ date = mk_date()
+ # Check same as any previous
+ if date_not_unique(date):
+ noise = np.random.normal(size=dims)
+ mutate = np.asarray(np.abs(noise) > 2., dtype=int)
+ date = (date + mutate) % 2
+ if not date_not_unique(date):
+ return date
+ else:
+ return date
+
+ def rate_example(self, d, r, iterations=400):
+ """ Rate example d with the actual Dater preference r. """
+
+ self._examples.append((d, r))
+ num_example = len(self.examples)
+ self._A = np.vstack([self._A, d])
+ self._b = np.vstack([self._b, np.array([r])])
+ # Solve for w.
+ M = np.dot(self._A.T, self._A) + np.dot(self._Gamma.T, self._Gamma)
+ self._w_estimate = np.dot(np.linalg.inv(M), np.dot(self._A.T, self._b))
+
+ def get_examples(self):
+ return self._examples
+ def get_w_estimate(self):
+ return self._w_estimate
+ examples = property(get_examples, doc="Dates retured with rate_example().")
+ w_estimate = property(get_w_estimate, doc="Currently estimated weights.")
+
+

0 comments on commit 0170948

Please sign in to comment.