Example usage for weka.classifiers.functions PLSClassifier setOptions

List of usage examples for weka.classifiers.functions PLSClassifier setOptions

Introduction

In this page you can find the example usage for weka.classifiers.functions PLSClassifier setOptions.

Prototype

@Override
public void setOptions(String[] options) throws Exception 

Source Link

Document

Parses the options for this object.

Usage

From source file:org.jaqpot.algorithm.resource.WekaPLS.java

License:Open Source License

@POST
@Path("training")
public Response training(TrainingRequest request) {
    try {/*from   w  ww.  j  a va 2s.  c o m*/
        if (request.getDataset().getDataEntry().isEmpty()
                || request.getDataset().getDataEntry().get(0).getValues().isEmpty()) {
            return Response.status(Response.Status.BAD_REQUEST).entity(
                    ErrorReportFactory.badRequest("Dataset is empty", "Cannot train model on empty dataset"))
                    .build();
        }
        List<String> features = request.getDataset().getDataEntry().stream().findFirst().get().getValues()
                .keySet().stream().collect(Collectors.toList());

        Instances data = InstanceUtils.createFromDataset(request.getDataset(), request.getPredictionFeature());
        Map<String, Object> parameters = request.getParameters() != null ? request.getParameters()
                : new HashMap<>();

        Integer components = Integer.parseInt(parameters.getOrDefault("components", _components).toString());
        String algorithm = parameters.getOrDefault("algorithm", _algorithm).toString();

        PLSClassifier classifier = new PLSClassifier();
        classifier.setOptions(new String[] { "-C", components.toString(), "-A", algorithm });
        classifier.buildClassifier(data);

        WekaModel model = new WekaModel();
        model.setClassifier(classifier);

        TrainingResponse response = new TrainingResponse();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(baos);
        out.writeObject(model);
        String base64Model = Base64.getEncoder().encodeToString(baos.toByteArray());
        response.setRawModel(base64Model);
        List<String> independentFeatures = features.stream()
                .filter(feature -> !feature.equals(request.getPredictionFeature()))
                .collect(Collectors.toList());
        response.setIndependentFeatures(independentFeatures);
        //            response.setPmmlModel(pmml);
        response.setAdditionalInfo(request.getPredictionFeature());
        response.setPredictedFeatures(
                Arrays.asList("Weka PLS prediction of " + request.getPredictionFeature()));

        return Response.ok(response).build();
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, null, ex);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ex.getMessage()).build();
    }
}

From source file:org.jaqpot.algorithms.resource.WekaPLS.java

License:Open Source License

@POST
@Path("training")
public Response training(TrainingRequest request) {
    try {//from ww w.  j a  v a2 s.c om
        if (request.getDataset().getDataEntry().isEmpty()
                || request.getDataset().getDataEntry().get(0).getValues().isEmpty()) {
            return Response.status(Response.Status.BAD_REQUEST)
                    .entity("Dataset is empty. Cannot train model on empty dataset.").build();
        }
        List<String> features = request.getDataset().getDataEntry().stream().findFirst().get().getValues()
                .keySet().stream().collect(Collectors.toList());

        Instances data = InstanceUtils.createFromDataset(request.getDataset(), request.getPredictionFeature());
        Map<String, Object> parameters = request.getParameters() != null ? request.getParameters()
                : new HashMap<>();

        Integer components = Integer.parseInt(parameters.getOrDefault("components", _components).toString());
        String algorithm = parameters.getOrDefault("algorithm", _algorithm).toString();

        PLSClassifier classifier = new PLSClassifier();
        classifier.setOptions(new String[] { "-C", components.toString(), "-A", algorithm });
        classifier.buildClassifier(data);

        WekaModel model = new WekaModel();
        model.setClassifier(classifier);

        TrainingResponse response = new TrainingResponse();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(baos);
        out.writeObject(model);
        String base64Model = Base64.getEncoder().encodeToString(baos.toByteArray());
        response.setRawModel(base64Model);
        List<String> independentFeatures = features.stream()
                .filter(feature -> !feature.equals(request.getPredictionFeature()))
                .collect(Collectors.toList());
        response.setIndependentFeatures(independentFeatures);
        //            response.setPmmlModel(pmml);
        response.setAdditionalInfo(request.getPredictionFeature());
        response.setPredictedFeatures(
                Arrays.asList("Weka PLS prediction of " + request.getPredictionFeature()));

        return Response.ok(response).build();
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, null, ex);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ex.getMessage()).build();
    }
}