Search in sources :

Example 1 with Feature

use of de.bwaldvogel.liblinear.Feature in project dkpro-tc by dkpro.

the class LiblinearLoadModelConnector method runPrediction.

@Override
protected File runPrediction(File infile) throws Exception {
    Problem predictionProblem = Problem.readFromFile(infile, 1.0);
    File tmp = File.createTempFile("libLinearePrediction", ".txt");
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tmp), "utf-8"));
        Feature[][] testInstances = predictionProblem.x;
        for (int i = 0; i < testInstances.length; i++) {
            Feature[] instance = testInstances[i];
            Double prediction = Linear.predict(liblinearModel, instance);
            writer.write(prediction.toString() + "\n");
        }
    } finally {
        IOUtils.closeQuietly(writer);
    }
    tmp.deleteOnExit();
    return tmp;
}
Also used : FileOutputStream(java.io.FileOutputStream) Problem(de.bwaldvogel.liblinear.Problem) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) Feature(de.bwaldvogel.liblinear.Feature) BufferedWriter(java.io.BufferedWriter)

Example 2 with Feature

use of de.bwaldvogel.liblinear.Feature in project dkpro-tc by dkpro.

the class LiblinearTestTask method runPrediction.

@Override
protected void runPrediction(TaskContext aContext, Object trainedModel) throws Exception {
    Model model = (Model) trainedModel;
    File fileTest = getTestFile(aContext);
    File predFolder = aContext.getFolder("", AccessMode.READWRITE);
    File predictionsFile = new File(predFolder, Constants.FILENAME_PREDICTIONS);
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(predictionsFile), "utf-8"));
    writer.append("#PREDICTION;GOLD" + "\n");
    Problem test = Problem.readFromFile(fileTest, 1.0);
    Feature[][] testInstances = test.x;
    for (int i = 0; i < testInstances.length; i++) {
        Feature[] instance = testInstances[i];
        Double prediction = Linear.predict(model, instance);
        writer.write(prediction + SEPARATOR_CHAR + new Double(test.y[i]));
        writer.write("\n");
    }
    writer.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) Model(de.bwaldvogel.liblinear.Model) OutputStreamWriter(java.io.OutputStreamWriter) Problem(de.bwaldvogel.liblinear.Problem) File(java.io.File) Feature(de.bwaldvogel.liblinear.Feature) BufferedWriter(java.io.BufferedWriter)

Aggregations

Feature (de.bwaldvogel.liblinear.Feature)2 Problem (de.bwaldvogel.liblinear.Problem)2 BufferedWriter (java.io.BufferedWriter)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Model (de.bwaldvogel.liblinear.Model)1