@@ -0,0 +1,2230 @@
+package com.cu.bigdata.moviereview;%0A%0Aimport com.google.common.collect.Lists;%0A%0Aimport org.apache.mahout.classifier.sgd.CrossFoldLearner;%0Aimport org.apache.mahout.classifier.sgd.L1;%0Aimport org.apache.mahout.classifier.sgd.ModelSerializer;%0Aimport org.apache.mahout.classifier.sgd.OnlineLogisticRegression;%0Aimport org.apache.mahout.math.DenseVector;%0Aimport org.apache.mahout.math.RandomAccessSparseVector;%0Aimport org.apache.mahout.math.Vector;%0A%0Aimport java.io.FileInputStream;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.util.Collections;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.Set;%0A%0Apublic class LogisticRegressionModel %7B%0A%09private OnlineLogisticRegression learningAlgo;%0A%09%0A%09public LogisticRegressionModel(int NumCate,int NumFeature,double lambda,int learningRate)%7B%0A%09%09learningAlgo = new OnlineLogisticRegression(NumCate, NumFeature, new L1());%0A        learningAlgo.lambda(lambda);%0A        learningAlgo.learningRate(learningRate);%0A%09%7D%0A%09%0A%09public LogisticRegressionModel(String model_path) throws IOException%7B%0A        InputStream in = new FileInputStream(model_path);%0A        learningAlgo = ModelSerializer.readBinary(in, OnlineLogisticRegression.class);%0A        in.close();%0A%09%7D%0A%09%0A%09public boolean LRTrainModel(List%3CDataInstance%3E trainset,String model_path) throws IOException%7B%0A%09%09for(DataInstance instance:trainset)%7B%0A%09%09%09learningAlgo.train(instance.lable, instance.features);%0A%09%09%7D%0A%09%09ModelSerializer.writeBinary(model_path, learningAlgo);%0A        learningAlgo.close();%0A%09%09return true;%0A%09%7D%0A%09%0A%09public int LRClassify(Vector feature)%7B%0A%09%09Vector r = learningAlgo.classifyFull(feature);%0A%09%09return r.maxValueIndex();%0A%09%7D%0A%09%0A%09public void LRTest(List%3CDataInstance%3E testset)%7B%0A%09%09int total = 0;%0A%09%09int success = 0;%0A%0A%09%09for (DataInstance vector : testset) %7B%0A%09%09%09Vector prediction = learningAlgo.classifyFull(vector.features);%0A%09%09%09%0A%09%09%09if (prediction.maxValueIndex()==vector.lable) %7B%0A%09%09%09%09success++;%0A%09%09%09%7D%0A%09%09%09total++;%0A%09%09%7D%0A%0A%09%09System.out.println(%22Total: %22 + total);%0A%09%09System.out.println(%22Success: %22 + success + %22 : %22 + (total - success));%0A%09%09System.out.println(%22Fail: %22 + (total - success));%0A%09%09System.out.println(%22Precise: %22 + ((double) success / total));%0A%09%7D%0A%09%0A%7D%0A

add lr