@@ -0,0 +1,1849 @@
+package edu.neu.ccs.pyramid.classification.logistic_regression;%0A%0Aimport edu.neu.ccs.pyramid.classification.ProbabilityEstimator;%0Aimport edu.neu.ccs.pyramid.util.MathUtil;%0Aimport org.apache.mahout.math.DenseVector;%0Aimport org.apache.mahout.math.RandomAccessSparseVector;%0Aimport org.apache.mahout.math.Vector;%0A%0A/**%0A * Created by chengli on 11/28/14.%0A */%0Apublic class LogisticRegression implements ProbabilityEstimator %7B%0A    public Vector weights;%0A%0A    @Override%0A    public int getNumClasses() %7B%0A        return 2;%0A    %7D%0A%0A    @Override%0A    public int predict(Vector vector) %7B%0A        Vector withConstant = addConstant(vector);%0A        double product = this.weights.dot(withConstant);%0A//        System.out.println(%22product = %22+product);%0A        if (product%3E=0)%7B%0A            return 1;%0A        %7D else %7B%0A            return 0;%0A        %7D%0A    %7D%0A%0A    @Override%0A    public double%5B%5D predictClassProbs(Vector vector) %7B%0A        Vector withConstant = addConstant(vector);%0A        double score = this.weights.dot(withConstant);%0A        double pro1;%0A        double logNominator = score;%0A        double%5B%5D exps = %7B0,score%7D;%0A        double logDenominator = MathUtil.logSumExp(exps);%0A        pro1 = Math.exp(logNominator - logDenominator);%0A        double%5B%5D probs = new double%5B2%5D;%0A        probs%5B0%5D = 1-pro1;%0A        probs%5B1%5D = pro1;%0A        return probs;%0A    %7D%0A%0A    private static Vector addConstant(Vector vector)%7B%0A        Vector vector1;%0A        if (vector.isDense())%7B%0A            vector1 = new DenseVector(vector.size()+1);%0A        %7D else %7B%0A            vector1 = new RandomAccessSparseVector(vector.size()+1);%0A        %7D%0A        vector1.set(0,1);%0A        for (Vector.Element element: vector.nonZeroes())%7B%0A            int index = element.index();%0A            double value = element.get();%0A            vector1.set(index+1, value);%0A        %7D%0A        return vector1;%0A    %7D%0A%7D%0A

add class LogisticRegression