Search in sources :

Example 6 with LogisticRegression

use of edu.neu.ccs.pyramid.classification.logistic_regression.LogisticRegression in project pyramid by cheng-li.

the class ClassificationSynthesizerTest method test6.

private static void test6() {
    ClassificationSynthesizer classificationSynthesizer = ClassificationSynthesizer.getBuilder().setNumDataPoints(1000).setNumFeatures(20).setNoiseSD(0.00000001).build();
    ClfDataSet trainSet = classificationSynthesizer.multivarLine();
    ClfDataSet testSet = classificationSynthesizer.multivarLine();
    TRECFormat.save(trainSet, new File(TMP, "line6/train.trec"));
    TRECFormat.save(testSet, new File(TMP, "line6/test.trec"));
    RidgeLogisticTrainer trainer = RidgeLogisticTrainer.getBuilder().setGaussianPriorVariance(1).build();
    LogisticRegression logisticRegression = trainer.train(trainSet);
    System.out.println(Accuracy.accuracy(logisticRegression, trainSet));
    System.out.println(Accuracy.accuracy(logisticRegression, testSet));
    System.out.println(logisticRegression.getWeights().getWeightsForClass(0));
}
Also used : ClfDataSet(edu.neu.ccs.pyramid.dataset.ClfDataSet) RidgeLogisticTrainer(edu.neu.ccs.pyramid.classification.logistic_regression.RidgeLogisticTrainer) LogisticRegression(edu.neu.ccs.pyramid.classification.logistic_regression.LogisticRegression) File(java.io.File)

Example 7 with LogisticRegression

use of edu.neu.ccs.pyramid.classification.logistic_regression.LogisticRegression in project pyramid by cheng-li.

the class ClassificationSynthesizerTest method test5.

private static void test5() {
    ClassificationSynthesizer classificationSynthesizer = ClassificationSynthesizer.getBuilder().setNumDataPoints(1000).setNumFeatures(10).setNoiseSD(0.00000001).build();
    ClfDataSet trainSet = classificationSynthesizer.multivarLine();
    ClfDataSet testSet = classificationSynthesizer.multivarLine();
    TRECFormat.save(trainSet, new File(TMP, "line5/train.trec"));
    TRECFormat.save(testSet, new File(TMP, "line5/test.trec"));
    RidgeLogisticTrainer trainer = RidgeLogisticTrainer.getBuilder().setGaussianPriorVariance(1).build();
    LogisticRegression logisticRegression = trainer.train(trainSet);
    System.out.println(Accuracy.accuracy(logisticRegression, trainSet));
    System.out.println(Accuracy.accuracy(logisticRegression, testSet));
    System.out.println(logisticRegression.getWeights().getWeightsForClass(0));
}
Also used : ClfDataSet(edu.neu.ccs.pyramid.dataset.ClfDataSet) RidgeLogisticTrainer(edu.neu.ccs.pyramid.classification.logistic_regression.RidgeLogisticTrainer) LogisticRegression(edu.neu.ccs.pyramid.classification.logistic_regression.LogisticRegression) File(java.io.File)

Example 8 with LogisticRegression

use of edu.neu.ccs.pyramid.classification.logistic_regression.LogisticRegression in project pyramid by cheng-li.

the class LKTreeBoostTest method logisticTest.

static void logisticTest() throws Exception {
    ClfDataSet dataSet = TRECFormat.loadClfDataSet(new File(DATASETS, "/spam/trec_data/train.trec"), DataSetType.CLF_SPARSE, true);
    System.out.println(dataSet.getMetaInfo());
    ClfDataSet testSet = TRECFormat.loadClfDataSet(new File(DATASETS, "/spam/trec_data/test.trec"), DataSetType.CLF_DENSE, true);
    LKBoost lkBoost = new LKBoost(2);
    LKBoostOptimizer trainer = new LKBoostOptimizer(lkBoost, dataSet);
    trainer.initialize();
    LogisticRegression logisticRegression = new LogisticRegression(dataSet.getNumClasses(), dataSet.getNumFeatures());
    ElasticNetLogisticTrainer logisticTrainer = ElasticNetLogisticTrainer.newBuilder(logisticRegression, dataSet).setEpsilon(0.01).setL1Ratio(0.9).setRegularization(0.001).build();
    logisticTrainer.optimize();
    System.out.println("logistic regression accuracy = " + Accuracy.accuracy(logisticRegression, testSet));
    System.out.println("num feature used = " + LogisticRegressionInspector.numOfUsedFeaturesCombined(logisticRegression));
    //        lktbTrainer.addLogisticRegression(logisticRegression);
    System.out.println("boosting accuracy = " + Accuracy.accuracy(lkBoost, testSet));
    for (int i = 0; i < 100; i++) {
        trainer.iterate();
        System.out.println("iteration " + i);
        System.out.println("boosting accuracy = " + Accuracy.accuracy(lkBoost, testSet));
    }
}
Also used : ElasticNetLogisticTrainer(edu.neu.ccs.pyramid.classification.logistic_regression.ElasticNetLogisticTrainer) LogisticRegression(edu.neu.ccs.pyramid.classification.logistic_regression.LogisticRegression) File(java.io.File)

Example 9 with LogisticRegression

use of edu.neu.ccs.pyramid.classification.logistic_regression.LogisticRegression in project pyramid by cheng-li.

the class MLLogisticRegressionInspector method topFeatures.

public static TopFeatures topFeatures(MLLogisticRegression logisticRegression, int classIndex, int limit) {
    FeatureList featureList = logisticRegression.getFeatureList();
    Vector weights = logisticRegression.getWeights().getWeightsWithoutBiasForClass(classIndex);
    Comparator<FeatureUtility> comparator = Comparator.comparing(FeatureUtility::getUtility);
    List<Feature> list = IntStream.range(0, weights.size()).mapToObj(i -> new FeatureUtility(featureList.get(i)).setUtility(weights.get(i))).filter(featureUtility -> featureUtility.getUtility() > 0).sorted(comparator.reversed()).map(FeatureUtility::getFeature).limit(limit).collect(Collectors.toList());
    TopFeatures topFeatures = new TopFeatures();
    topFeatures.setTopFeatures(list);
    topFeatures.setClassIndex(classIndex);
    LabelTranslator labelTranslator = logisticRegression.getLabelTranslator();
    topFeatures.setClassName(labelTranslator.toExtLabel(classIndex));
    return topFeatures;
}
Also used : FeatureUtility(edu.neu.ccs.pyramid.feature.FeatureUtility) FeatureUtility(edu.neu.ccs.pyramid.feature.FeatureUtility) MultiLabelPredictionAnalysis(edu.neu.ccs.pyramid.multilabel_classification.MultiLabelPredictionAnalysis) IntStream(java.util.stream.IntStream) ClassProbability(edu.neu.ccs.pyramid.classification.ClassProbability) ConstantRule(edu.neu.ccs.pyramid.regression.ConstantRule) FeatureList(edu.neu.ccs.pyramid.feature.FeatureList) Rule(edu.neu.ccs.pyramid.regression.Rule) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) IMLGradientBoosting(edu.neu.ccs.pyramid.multilabel_classification.imlgb.IMLGradientBoosting) PredictionAnalysis(edu.neu.ccs.pyramid.classification.PredictionAnalysis) List(java.util.List) Feature(edu.neu.ccs.pyramid.feature.Feature) LogisticRegression(edu.neu.ccs.pyramid.classification.logistic_regression.LogisticRegression) edu.neu.ccs.pyramid.dataset(edu.neu.ccs.pyramid.dataset) Vector(org.apache.mahout.math.Vector) TopFeatures(edu.neu.ccs.pyramid.feature.TopFeatures) LinearRule(edu.neu.ccs.pyramid.regression.LinearRule) Comparator(java.util.Comparator) ClassScoreCalculation(edu.neu.ccs.pyramid.regression.ClassScoreCalculation) TopFeatures(edu.neu.ccs.pyramid.feature.TopFeatures) FeatureList(edu.neu.ccs.pyramid.feature.FeatureList) Vector(org.apache.mahout.math.Vector) Feature(edu.neu.ccs.pyramid.feature.Feature)

Example 10 with LogisticRegression

use of edu.neu.ccs.pyramid.classification.logistic_regression.LogisticRegression in project pyramid by cheng-li.

the class CBMInspector method getMean.

public static Weights getMean(CBM bmm, int label) {
    int numClusters = bmm.getNumComponents();
    int length = ((LogisticRegression) bmm.getBinaryClassifiers()[0][0]).getWeights().getAllWeights().size();
    int numFeatures = ((LogisticRegression) bmm.getBinaryClassifiers()[0][0]).getNumFeatures();
    Vector mean = new DenseVector(length);
    for (int k = 0; k < numClusters; k++) {
        mean = mean.plus(((LogisticRegression) bmm.getBinaryClassifiers()[k][label]).getWeights().getAllWeights());
    }
    mean = mean.divide(numClusters);
    return new Weights(2, numFeatures, mean);
}
Also used : Weights(edu.neu.ccs.pyramid.classification.logistic_regression.Weights) LogisticRegression(edu.neu.ccs.pyramid.classification.logistic_regression.LogisticRegression) DenseVector(org.apache.mahout.math.DenseVector) Vector(org.apache.mahout.math.Vector) DenseVector(org.apache.mahout.math.DenseVector)

Aggregations

LogisticRegression (edu.neu.ccs.pyramid.classification.logistic_regression.LogisticRegression)19 ClfDataSet (edu.neu.ccs.pyramid.dataset.ClfDataSet)7 File (java.io.File)7 RidgeLogisticTrainer (edu.neu.ccs.pyramid.classification.logistic_regression.RidgeLogisticTrainer)6 ElasticNetLogisticTrainer (edu.neu.ccs.pyramid.classification.logistic_regression.ElasticNetLogisticTrainer)5 RidgeLogisticOptimizer (edu.neu.ccs.pyramid.classification.logistic_regression.RidgeLogisticOptimizer)5 Vector (org.apache.mahout.math.Vector)5 PriorProbClassifier (edu.neu.ccs.pyramid.classification.PriorProbClassifier)3 Classifier (edu.neu.ccs.pyramid.classification.Classifier)2 MultiLabel (edu.neu.ccs.pyramid.dataset.MultiLabel)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 IntStream (java.util.stream.IntStream)2 StopWatch (org.apache.commons.lang3.time.StopWatch)2 DenseVector (org.apache.mahout.math.DenseVector)2 ClassProbability (edu.neu.ccs.pyramid.classification.ClassProbability)1 PredictionAnalysis (edu.neu.ccs.pyramid.classification.PredictionAnalysis)1 LKBOutputCalculator (edu.neu.ccs.pyramid.classification.lkboost.LKBOutputCalculator)1 LKBoost (edu.neu.ccs.pyramid.classification.lkboost.LKBoost)1 LKBoostOptimizer (edu.neu.ccs.pyramid.classification.lkboost.LKBoostOptimizer)1