Search in sources :

Example 1 with RegressionEvaluation

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);
    }
}
Also used : UsageException(com.oracle.labs.mlrg.olcut.config.UsageException) RegressionFactory(org.tribuo.regression.RegressionFactory) Dataset(org.tribuo.Dataset) SparseVector(org.tribuo.math.la.SparseVector) RegressionEvaluation(org.tribuo.regression.evaluation.RegressionEvaluation) Regressor(org.tribuo.regression.Regressor) ConfigurationManager(com.oracle.labs.mlrg.olcut.config.ConfigurationManager) Map(java.util.Map)

Example 2 with RegressionEvaluation

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);
    }
}
Also used : Dataset(org.tribuo.Dataset) MutableDataset(org.tribuo.MutableDataset) RegressionEvaluation(org.tribuo.regression.evaluation.RegressionEvaluation) Model(org.tribuo.Model) SparseModel(org.tribuo.SparseModel) Regressor(org.tribuo.regression.Regressor) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.jupiter.api.Test)

Example 3 with RegressionEvaluation

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);
    }
}
Also used : Dataset(org.tribuo.Dataset) RegressionEvaluation(org.tribuo.regression.evaluation.RegressionEvaluation) Model(org.tribuo.Model) Regressor(org.tribuo.regression.Regressor) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.jupiter.api.Test)

Example 4 with RegressionEvaluation

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;
}
Also used : RegressionEvaluation(org.tribuo.regression.evaluation.RegressionEvaluation) List(java.util.List) Regressor(org.tribuo.regression.Regressor)

Example 5 with RegressionEvaluation

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);
}
Also used : Dataset(org.tribuo.Dataset) RegressionEvaluation(org.tribuo.regression.evaluation.RegressionEvaluation) Regressor(org.tribuo.regression.Regressor) Test(org.junit.jupiter.api.Test)

Aggregations

Regressor (org.tribuo.regression.Regressor)35 RegressionEvaluation (org.tribuo.regression.evaluation.RegressionEvaluation)35 Dataset (org.tribuo.Dataset)21 Test (org.junit.jupiter.api.Test)14 List (java.util.List)9 ConfigurationManager (com.oracle.labs.mlrg.olcut.config.ConfigurationManager)7 UsageException (com.oracle.labs.mlrg.olcut.config.UsageException)7 RegressionFactory (org.tribuo.regression.RegressionFactory)7 ObjectInputStream (java.io.ObjectInputStream)6 Model (org.tribuo.Model)6 MutableDataset (org.tribuo.MutableDataset)4 RegressionEvaluator (org.tribuo.regression.evaluation.RegressionEvaluator)4 ArrayList (java.util.ArrayList)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 Example (org.tribuo.Example)3 Prediction (org.tribuo.Prediction)3 Label (org.tribuo.classification.Label)3 ArrayExample (org.tribuo.impl.ArrayExample)3 SquaredLoss (org.tribuo.regression.sgd.objectives.SquaredLoss)3 Pair (com.oracle.labs.mlrg.olcut.util.Pair)2