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));
}
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));
}
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));
}
}
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;
}
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);
}
Aggregations