use of edu.neu.ccs.pyramid.classification.logistic_regression.LogisticRegression in project pyramid by cheng-li.
the class CBMInspector method distanceFromMean.
public static double distanceFromMean(CBM bmm, int label) {
Classifier.ProbabilityEstimator[][] logistics = bmm.getBinaryClassifiers();
int numClusters = bmm.getNumComponents();
int numFeatures = ((LogisticRegression) logistics[0][0]).getNumFeatures();
Vector positiveAverageVector = new DenseVector(numFeatures);
for (int k = 0; k < numClusters; k++) {
Vector positiveVector = ((LogisticRegression) logistics[k][label]).getWeights().getWeightsWithoutBiasForClass(1);
positiveAverageVector = positiveAverageVector.plus(positiveVector);
}
positiveAverageVector = positiveAverageVector.divide(numClusters);
double dis = 0;
for (int k = 0; k < numClusters; k++) {
Vector positiveVector = ((LogisticRegression) logistics[k][label]).getWeights().getWeightsWithoutBiasForClass(1);
dis += positiveVector.minus(positiveAverageVector).norm(2);
}
return dis / numClusters;
}
use of edu.neu.ccs.pyramid.classification.logistic_regression.LogisticRegression in project pyramid by cheng-li.
the class MLPlattScaling method fitClassK.
private static LogisticRegression fitClassK(double[] scores, int[] labels) {
ClfDataSet dataSet = ClfDataSetBuilder.getBuilder().numClasses(2).numDataPoints(scores.length).numFeatures(1).dense(true).missingValue(false).build();
for (int i = 0; i < scores.length; i++) {
dataSet.setFeatureValue(i, 0, scores[i]);
dataSet.setLabel(i, labels[i]);
}
LogisticRegression logisticRegression = new LogisticRegression(2, dataSet.getNumFeatures());
ElasticNetLogisticTrainer trainer = ElasticNetLogisticTrainer.newBuilder(logisticRegression, dataSet).setRegularization(1.0E-9).setL1Ratio(0).build();
trainer.optimize();
return logisticRegression;
}
use of edu.neu.ccs.pyramid.classification.logistic_regression.LogisticRegression in project pyramid by cheng-li.
the class CBMUtilityOptimizer method updateBinaryLogisticRegression.
private void updateBinaryLogisticRegression(int componentIndex, int labelIndex) {
RidgeLogisticOptimizer ridgeLogisticOptimizer;
// System.out.println("for component "+componentIndex+" label "+labelIndex);
// System.out.println("weights="+Arrays.toString(gammasT[componentIndex]));
// System.out.println("binary target distribution="+Arrays.deepToString(binaryTargetsDistributions[labelIndex]));
// double posProb = 0;
// double negProb = 0;
// for (int i=0;i<dataSet.getNumDataPoints();i++){
// posProb += gammasT[componentIndex][i] * binaryTargetsDistributions[labelIndex][i][1];
// negProb += gammasT[componentIndex][i] * binaryTargetsDistributions[labelIndex][i][0];
// }
// System.out.println("sum pos prob = "+posProb);
// System.out.println("sum neg prob = "+negProb);
// no parallelism
ridgeLogisticOptimizer = new RidgeLogisticOptimizer((LogisticRegression) cbm.binaryClassifiers[componentIndex][labelIndex], dataSet, gammasT[componentIndex], binaryTargetsDistributions[labelIndex], priorVarianceBinary, false);
//TODO maximum iterations
ridgeLogisticOptimizer.getOptimizer().getTerminator().setMaxIteration(10);
ridgeLogisticOptimizer.optimize();
// if (logger.isDebugEnabled()){
// logger.debug("for cluster "+clusterIndex+" label "+labelIndex+" history= "+ridgeLogisticOptimizer.getOptimizer().getTerminator().getHistory());
// }
}
use of edu.neu.ccs.pyramid.classification.logistic_regression.LogisticRegression in project pyramid by cheng-li.
the class ClassificationSynthesizerTest method test1.
private static void test1() {
ClassificationSynthesizer classificationSynthesizer = ClassificationSynthesizer.getBuilder().setNumDataPoints(1000).setNumFeatures(2).setNoiseSD(0.00000001).build();
ClfDataSet trainSet = classificationSynthesizer.multivarLine();
ClfDataSet testSet = classificationSynthesizer.multivarLine();
TRECFormat.save(trainSet, new File(TMP, "line1/train.trec"));
TRECFormat.save(testSet, new File(TMP, "line1/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 test2.
private static void test2() {
ClassificationSynthesizer classificationSynthesizer = ClassificationSynthesizer.getBuilder().setNumDataPoints(100).setNumFeatures(2).setNoiseSD(0.00000001).build();
ClfDataSet trainSet = classificationSynthesizer.multivarLine();
ClfDataSet testSet = classificationSynthesizer.multivarLine();
TRECFormat.save(trainSet, new File(TMP, "line2/train.trec"));
TRECFormat.save(testSet, new File(TMP, "line2/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));
}
Aggregations