use of org.tribuo.regression.evaluation.RegressionEvaluation in project tribuo by oracle.
the class TrainTest method main.
/**
* Runs a TrainTest CLI.
* @param args the command line arguments
* @throws IOException if there is any error reading the examples.
*/
public static void main(String[] args) throws IOException {
//
// Use the labs format logging.
LabsLogFormatter.setAllLogFormatters();
SLMOptions o = new SLMOptions();
ConfigurationManager cm;
try {
cm = new ConfigurationManager(args, o);
} catch (UsageException e) {
logger.info(e.getMessage());
return;
}
if (o.general.trainingPath == null || o.general.testingPath == null) {
logger.info(cm.usage());
return;
}
RegressionFactory factory = new RegressionFactory();
Pair<Dataset<Regressor>, Dataset<Regressor>> data = o.general.load(factory);
Dataset<Regressor> train = data.getA();
Dataset<Regressor> test = data.getB();
SparseTrainer<Regressor> trainer;
switch(o.algorithm) {
case SFS:
trainer = new SLMTrainer(false, Math.min(train.getFeatureMap().size(), o.maxNumFeatures));
break;
case LARS:
trainer = new LARSTrainer(Math.min(train.getFeatureMap().size(), o.maxNumFeatures));
break;
case LARSLASSO:
trainer = new LARSLassoTrainer(Math.min(train.getFeatureMap().size(), o.maxNumFeatures));
break;
case SFSN:
trainer = new SLMTrainer(true, Math.min(train.getFeatureMap().size(), o.maxNumFeatures));
break;
case ELASTICNET:
trainer = new ElasticNetCDTrainer(o.alpha, o.l1Ratio, 1e-4, o.iterations, false, o.general.seed);
break;
default:
logger.warning("Unknown SLMType, found " + o.algorithm);
return;
}
logger.info("Training using " + trainer.toString());
final long trainStart = System.currentTimeMillis();
SparseModel<Regressor> model = trainer.train(train);
final long trainStop = System.currentTimeMillis();
logger.info("Finished training regressor " + Util.formatDuration(trainStart, trainStop));
logger.info("Selected features: " + model.getActiveFeatures());
Map<String, SparseVector> weights = ((SparseLinearModel) model).getWeights();
for (Map.Entry<String, SparseVector> e : weights.entrySet()) {
logger.info("Target:" + e.getKey());
logger.info("\tWeights: " + e.getValue());
logger.info("\tWeights one norm: " + e.getValue().oneNorm());
logger.info("\tWeights two norm: " + e.getValue().twoNorm());
}
final long testStart = System.currentTimeMillis();
RegressionEvaluation evaluation = factory.getEvaluator().evaluate(model, test);
final long testStop = System.currentTimeMillis();
logger.info("Finished evaluating model " + Util.formatDuration(testStart, testStop));
System.out.println(evaluation.toString());
if (o.general.outputPath != null) {
o.general.saveModel(model);
}
}
use of org.tribuo.regression.evaluation.RegressionEvaluation in project tribuo by oracle.
the class TestSLM method testRegressionReordering.
@Test
public void testRegressionReordering() throws IOException, ClassNotFoundException {
try (ObjectInputStream ois = new ObjectInputStream(TEST_REGRESSION_REORDER_LARS_MODEL.openStream())) {
@SuppressWarnings("unchecked") Model<Regressor> serializedModel = (Model<Regressor>) ois.readObject();
Pair<Dataset<Regressor>, Dataset<Regressor>> p = RegressionDataGenerator.threeDimDenseTrainTest(1.0, false);
RegressionEvaluation llEval = e.evaluate(serializedModel, p.getB());
double expectedDim1 = 0.5671244360433836;
double expectedDim2 = 0.5671244360433927;
double expectedDim3 = -2.457128076868633;
double expectedAve = -0.44095973492728563;
assertEquals(expectedDim1, llEval.r2(new Regressor(RegressionDataGenerator.firstDimensionName, Double.NaN)), 1e-6);
assertEquals(expectedDim2, llEval.r2(new Regressor(RegressionDataGenerator.secondDimensionName, Double.NaN)), 1e-6);
assertEquals(expectedDim3, llEval.r2(new Regressor(RegressionDataGenerator.thirdDimensionName, Double.NaN)), 1e-6);
assertEquals(expectedAve, llEval.averageR2(), 1e-6);
}
try (ObjectInputStream ois = new ObjectInputStream(TEST_REGRESSION_REORDER_ENET_MODEL.openStream())) {
@SuppressWarnings("unchecked") Model<Regressor> serializedModel = (Model<Regressor>) ois.readObject();
Pair<Dataset<Regressor>, Dataset<Regressor>> p = RegressionDataGenerator.threeDimDenseTrainTest(1.0, false);
RegressionEvaluation llEval = e.evaluate(serializedModel, p.getB());
double expectedDim1 = 0.5902193395184064;
double expectedDim2 = 0.5902193395184064;
double expectedDim3 = 0.2563468291737646;
double expectedAve = 0.4789285027368592;
assertEquals(expectedDim1, llEval.r2(new Regressor(RegressionDataGenerator.firstDimensionName, Double.NaN)), 1e-6);
assertEquals(expectedDim2, llEval.r2(new Regressor(RegressionDataGenerator.secondDimensionName, Double.NaN)), 1e-6);
assertEquals(expectedDim3, llEval.r2(new Regressor(RegressionDataGenerator.thirdDimensionName, Double.NaN)), 1e-6);
assertEquals(expectedAve, llEval.averageR2(), 1e-6);
}
}
use of org.tribuo.regression.evaluation.RegressionEvaluation in project tribuo by oracle.
the class TestXGBoost method testRegressionReordering.
@Test
public void testRegressionReordering() throws IOException, ClassNotFoundException {
try (ObjectInputStream ois = new ObjectInputStream(TEST_REGRESSION_REORDER_MODEL.openStream())) {
@SuppressWarnings("unchecked") Model<Regressor> serializedModel = (Model<Regressor>) ois.readObject();
Pair<Dataset<Regressor>, Dataset<Regressor>> p = RegressionDataGenerator.threeDimDenseTrainTest(1.0, false);
RegressionEvaluation llEval = e.evaluate(serializedModel, p.getB());
double expectedDim1 = 0.08085670251311738;
double expectedDim2 = 0.09825176714451844;
double expectedDim3 = -1.1534319157320798;
double expectedAve = -0.3247744820248147;
assertEquals(expectedDim1, llEval.r2(new Regressor(RegressionDataGenerator.firstDimensionName, Double.NaN)), 1e-6);
assertEquals(expectedDim2, llEval.r2(new Regressor(RegressionDataGenerator.secondDimensionName, Double.NaN)), 1e-6);
assertEquals(expectedDim3, llEval.r2(new Regressor(RegressionDataGenerator.thirdDimensionName, Double.NaN)), 1e-6);
assertEquals(expectedAve, llEval.averageR2(), 1e-6);
}
}
use of org.tribuo.regression.evaluation.RegressionEvaluation in project tribuo by oracle.
the class TestSGDLinear method testSGDLinear.
public static Model<Regressor> testSGDLinear(Pair<Dataset<Regressor>, Dataset<Regressor>> p) {
Model<Regressor> m = t.train(p.getA());
RegressionEvaluation evaluation = e.evaluate(m, p.getB());
Map<String, List<Pair<String, Double>>> features = m.getTopFeatures(3);
Assertions.assertNotNull(features);
Assertions.assertFalse(features.isEmpty());
features = m.getTopFeatures(-1);
Assertions.assertNotNull(features);
Assertions.assertFalse(features.isEmpty());
return m;
}
use of org.tribuo.regression.evaluation.RegressionEvaluation in project tribuo by oracle.
the class TestCARTJointRegressionTrainer method testThreeDenseData.
@Test
public void testThreeDenseData() {
Pair<Dataset<Regressor>, Dataset<Regressor>> p = RegressionDataGenerator.threeDimDenseTrainTest(1.0, false);
TreeModel<Regressor> llModel = t.train(p.getA());
RegressionEvaluation llEval = e.evaluate(llModel, p.getB());
double expectedDim1 = -0.6618655170782572;
double expectedDim2 = -0.6618655170782572;
double expectedDim3 = -2.072009095885796;
double expectedAve = -1.1319133766807703;
assertEquals(expectedDim1, llEval.r2(new Regressor(RegressionDataGenerator.firstDimensionName, Double.NaN)), 1e-6);
assertEquals(expectedDim2, llEval.r2(new Regressor(RegressionDataGenerator.secondDimensionName, Double.NaN)), 1e-6);
assertEquals(expectedDim3, llEval.r2(new Regressor(RegressionDataGenerator.thirdDimensionName, Double.NaN)), 1e-6);
assertEquals(expectedAve, llEval.averageR2(), 1e-6);
p = RegressionDataGenerator.threeDimDenseTrainTest(1.0, true);
llModel = t.train(p.getA());
llEval = e.evaluate(llModel, p.getB());
assertEquals(expectedDim1, llEval.r2(new Regressor(RegressionDataGenerator.firstDimensionName, Double.NaN)), 1e-6);
assertEquals(expectedDim2, llEval.r2(new Regressor(RegressionDataGenerator.secondDimensionName, Double.NaN)), 1e-6);
assertEquals(expectedDim3, llEval.r2(new Regressor(RegressionDataGenerator.thirdDimensionName, Double.NaN)), 1e-6);
assertEquals(expectedAve, llEval.averageR2(), 1e-6);
}
Aggregations