sreejith2904

Classifier1-Wrong Op

Oct 20th, 2013
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. package com.xurmo.classifier;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. import org.apache.mahout.classifier.sgd.L1;
  7. import org.apache.mahout.classifier.sgd.OnlineLogisticRegression;
  8. import org.apache.mahout.math.DenseVector;
  9. import org.apache.mahout.math.RandomAccessSparseVector;
  10. import org.apache.mahout.math.Vector;
  11.  
  12.  
  13.  
  14. public class SimpleClassifier {
  15.  
  16.     public static class Point {
  17.         public int x;
  18.         public int y;
  19.  
  20.         public Point(int x, int y) {
  21.             this.x = x;
  22.             this.y = y;
  23.         }
  24.  
  25.         @Override
  26.         public boolean equals(Object arg0) {
  27.             Point p = (Point) arg0;
  28.             return ((this.x == p.x) && (this.y == p.y));
  29.         }
  30.  
  31.         @Override
  32.         public String toString() {
  33.             // TODO Auto-generated method stub
  34.             return this.x + " , " + this.y;
  35.         }
  36.     }
  37.  
  38.     @SuppressWarnings("resource")
  39.     public static void main(String[] args) {
  40.  
  41.         Map<Point, Integer> points = new HashMap<SimpleClassifier.Point, Integer>();
  42.  
  43.         points.put(new Point(0, 0), 0);
  44.         points.put(new Point(1, 1), 0);
  45.         points.put(new Point(1, 0), 0);
  46.         points.put(new Point(0, 1), 0);
  47.         points.put(new Point(2, 2), 0);
  48.  
  49.         points.put(new Point(8, 8), 1);
  50.         points.put(new Point(8, 9), 1);
  51.         points.put(new Point(9, 8), 1);
  52.         points.put(new Point(9, 9), 1);
  53.  
  54.         OnlineLogisticRegression learningAlgo = new OnlineLogisticRegression();
  55.         learningAlgo = new OnlineLogisticRegression(2, 2, new L1());
  56.         learningAlgo.learningRate(1);
  57.  
  58.         // learningAlgo.alpha(1).stepOffset(1000);
  59.  
  60.         System.out.println("training model \n");
  61.         for (Point point : points.keySet()) {
  62.             Vector v = getVector(point);
  63.             System.out.println(point + " belongs to " + points.get(point));
  64.             learningAlgo.train(points.get(point), v);
  65.         }
  66.  
  67.         learningAlgo.close();
  68.  
  69.         // now classify real data
  70.         Vector v = new RandomAccessSparseVector(2);
  71.         v.set(0, 0.5);
  72.         v.set(1, 0.5);
  73.  
  74.         Vector r = learningAlgo.classifyFull(v);
  75.         System.out.println(r);
  76.  
  77.         System.out.println("ans = ");
  78.         System.out
  79.                 .println("no of categories = " + learningAlgo.numCategories());
  80.         System.out.println("no of features = " + learningAlgo.numFeatures());
  81.         System.out.println("Probability of cluster 0 = " + r.get(0));
  82.         System.out.println("Probability of cluster 1 = " + r.get(1));
  83.  
  84.     }
  85.  
  86.     public static Vector getVector(Point point) {
  87.         Vector v = new DenseVector(2);
  88.         v.set(0, point.x);
  89.         v.set(1, point.y);
  90.  
  91.         return v;
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment