Example usage for org.apache.mahout.classifier.sgd OnlineLogisticRegression learningRate

List of usage examples for org.apache.mahout.classifier.sgd OnlineLogisticRegression learningRate

Introduction

In this page you can find the example usage for org.apache.mahout.classifier.sgd OnlineLogisticRegression learningRate.

Prototype

public OnlineLogisticRegression learningRate(double learningRate) 

Source Link

Document

Chainable configuration option.

Usage

From source file:com.sixgroup.samplerecommender.Point.java

public static void main(String[] args) {

    Map<Point, Integer> points = new HashMap<Point, Integer>();

    points.put(new Point(0, 0), 0);
    points.put(new Point(1, 1), 0);
    points.put(new Point(1, 0), 0);
    points.put(new Point(0, 1), 0);
    points.put(new Point(2, 2), 0);

    points.put(new Point(8, 8), 1);
    points.put(new Point(8, 9), 1);
    points.put(new Point(9, 8), 1);
    points.put(new Point(9, 9), 1);

    OnlineLogisticRegression learningAlgo = new OnlineLogisticRegression();
    learningAlgo = new OnlineLogisticRegression(2, 3, new L1());
    learningAlgo.lambda(0.1);//from  w  w w.  j  a  v  a2 s.  co m
    learningAlgo.learningRate(10);

    System.out.println("training model  \n");

    for (Point point : points.keySet()) {

        Vector v = getVector(point);
        System.out.println(point + " belongs to " + points.get(point));
        learningAlgo.train(points.get(point), v);
    }

    learningAlgo.close();

    Vector v = new RandomAccessSparseVector(3);
    v.set(0, 0.5);
    v.set(1, 0.5);
    v.set(2, 1);

    Vector r = learningAlgo.classifyFull(v);
    System.out.println(r);

    System.out.println("ans = ");
    System.out.println("no of categories = " + learningAlgo.numCategories());
    System.out.println("no of features = " + learningAlgo.numFeatures());
    System.out.println("Probability of cluster 0 = " + r.get(0));
    System.out.println("Probability of cluster 1 = " + r.get(1));

}

From source file:org.wso2.siddhi.extension.ModelInitializer.java

License:Open Source License

public static OnlineLogisticRegression InitializeLogisticRegression(String modelPath) {
    OnlineLogisticRegression LRmodel = null;
    FileInputStream fileInputStream = null;
    ObjectInputStream objectInputStream = null;
    double[][] modelWeights = null;
    LogisticRegressionModel LRmodelObject;
    try {//from w  ww . j a  va  2 s . com
        // get the values for hyper-parameters from model file.
        fileInputStream = new FileInputStream(modelPath);
        objectInputStream = new ObjectInputStream(fileInputStream);
        LRmodelObject = (LogisticRegressionModel) objectInputStream.readObject();
        LRmodel = new OnlineLogisticRegression(LRmodelObject.getNumCategories(), LRmodelObject.getNumFeatures(),
                new L2(1));
        LRmodel.learningRate(LRmodelObject.getLearningRate());
        LRmodel.lambda(LRmodelObject.getLambda());
        LRmodel.alpha(LRmodelObject.getAlpha());
        LRmodel.stepOffset(LRmodelObject.getStepOffset());
        LRmodel.decayExponent(LRmodelObject.getDecayExponent());
        modelWeights = LRmodelObject.getWeights();
        fileInputStream.close();
        objectInputStream.close();
        for (int i = 0; i < modelWeights.length; i++) {
            for (int j = 0; j < modelWeights[0].length; j++) {
                LRmodel.setBeta(i, j, modelWeights[i][j]);
            }
        }
    } catch (Exception e) {
        logger.error("Failed to create a Logistic Regression model from the file \"" + modelPath + "\"", e);
    } finally {
        try {
            fileInputStream.close();
            objectInputStream.close();
        } catch (IOException e) {
            logger.error("Failed to close the model input stream!", e);
        }
    }
    logger.info("Logistic Regression model execution plan successfully intialized for \"" + modelPath
            + "\" model file.");
    return LRmodel;
}