Example usage for weka.core Instances insertAttributeAt

List of usage examples for weka.core Instances insertAttributeAt

Introduction

In this page you can find the example usage for weka.core Instances insertAttributeAt.

Prototype



public void insertAttributeAt(Attribute att, int position) 

Source Link

Document

Inserts an attribute at the given position (0 to numAttributes()) and sets all values to be missing.

Usage

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

License:Open Source License

@POST
@Path("prediction")
public Response prediction(PredictionRequest request) {

    try {//from w w w.j  av a  2  s  . c o m
        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();
        }

        String base64Model = (String) request.getRawModel();
        byte[] modelBytes = Base64.getDecoder().decode(base64Model);
        ByteArrayInputStream bais = new ByteArrayInputStream(modelBytes);
        ObjectInput in = new ObjectInputStream(bais);
        WekaModel model = (WekaModel) in.readObject();

        Classifier classifier = model.getClassifier();
        Instances data = InstanceUtils.createFromDataset(request.getDataset());
        List<String> additionalInfo = (List) request.getAdditionalInfo();
        String dependentFeature = additionalInfo.get(0);
        String dependentFeatureName = additionalInfo.get(1);
        data.insertAttributeAt(new Attribute(dependentFeature), data.numAttributes());
        List<LinkedHashMap<String, Object>> predictions = new ArrayList<>();
        //            data.stream().forEach(instance -> {
        //                try {
        //                    double prediction = classifier.classifyInstance(instance);
        //                    Map<String, Object> predictionMap = new HashMap<>();
        //                    predictionMap.put("Weka MLR prediction of " + dependentFeature, prediction);
        //                    predictions.add(predictionMap);
        //                } catch (Exception ex) {
        //                    Logger.getLogger(WekaMLR.class.getName()).log(Level.SEVERE, null, ex);
        //                }
        //            });

        for (int i = 0; i < data.numInstances(); i++) {
            Instance instance = data.instance(i);
            try {
                double prediction = classifier.classifyInstance(instance);
                LinkedHashMap<String, Object> predictionMap = new LinkedHashMap<>();
                predictionMap.put("Weka MLR prediction of " + dependentFeatureName, prediction);
                predictions.add(predictionMap);
            } catch (Exception ex) {
                Logger.getLogger(WekaMLR.class.getName()).log(Level.SEVERE, null, ex);
                return Response.status(Response.Status.BAD_REQUEST)
                        .entity("Error while gettting predictions. " + ex.getMessage()).build();
            }
        }

        PredictionResponse response = new PredictionResponse();
        response.setPredictions(predictions);
        return Response.ok(response).build();
    } catch (Exception ex) {
        Logger.getLogger(WekaMLR.class.getName()).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("prediction")
public Response prediction(PredictionRequest request) {
    try {//from w w w .  j a va2 s. c  o  m
        if (request.getDataset().getDataEntry().isEmpty()
                || request.getDataset().getDataEntry().get(0).getValues().isEmpty()) {
            return Response.status(Response.Status.BAD_REQUEST)
                    .entity("Dataset is empty. Cannot make predictions on empty dataset.").build();
        }

        String base64Model = (String) request.getRawModel();
        byte[] modelBytes = Base64.getDecoder().decode(base64Model);
        ByteArrayInputStream bais = new ByteArrayInputStream(modelBytes);
        ObjectInput in = new ObjectInputStream(bais);
        WekaModel model = (WekaModel) in.readObject();

        Classifier classifier = model.getClassifier();
        Instances data = InstanceUtils.createFromDataset(request.getDataset());
        String dependentFeature = (String) request.getAdditionalInfo();
        data.insertAttributeAt(new Attribute(dependentFeature), data.numAttributes());
        data.setClass(data.attribute(dependentFeature));
        List<LinkedHashMap<String, Object>> predictions = new ArrayList<>();
        //            data.stream().forEach(instance -> {
        //                try {
        //                    double prediction = classifier.classifyInstance(instance);
        //                    Map<String, Object> predictionMap = new HashMap<>();
        //                    predictionMap.put("Weka PLS prediction of " + dependentFeature, prediction);
        //                    predictions.add(predictionMap);
        //                } catch (Exception ex) {
        //                    Logger.getLogger(WekaSVM.class.getName()).log(Level.SEVERE, null, ex);
        //                }
        //            });

        for (int i = 0; i < data.numInstances(); i++) {
            Instance instance = data.instance(i);
            try {
                double prediction = classifier.classifyInstance(instance);
                LinkedHashMap<String, Object> predictionMap = new LinkedHashMap<>();
                predictionMap.put("Weka PLS prediction of " + dependentFeature, prediction);
                predictions.add(predictionMap);
            } catch (Exception ex) {
                Logger.getLogger(WekaMLR.class.getName()).log(Level.SEVERE, null, ex);
                return Response.status(Response.Status.BAD_REQUEST)
                        .entity("Error while gettting predictions. " + ex.getMessage()).build();
            }
        }

        PredictionResponse response = new PredictionResponse();
        response.setPredictions(predictions);
        return Response.ok(response).build();
    } catch (IOException | ClassNotFoundException ex) {
        Logger.getLogger(WekaSVM.class.getName()).log(Level.SEVERE, null, ex);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ex.getMessage()).build();
    }
}

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

License:Open Source License

@POST
@Path("prediction")
public Response prediction(PredictionRequest request) {

    try {/*  w w  w.  j  a  v a2s  . c  o m*/
        if (request.getDataset().getDataEntry().isEmpty()
                || request.getDataset().getDataEntry().get(0).getValues().isEmpty()) {
            return Response.status(Response.Status.BAD_REQUEST)
                    .entity("Dataset is empty. Cannot make predictions on empty dataset.").build();
        }

        String base64Model = (String) request.getRawModel();
        byte[] modelBytes = Base64.getDecoder().decode(base64Model);
        ByteArrayInputStream bais = new ByteArrayInputStream(modelBytes);
        ObjectInput in = new ObjectInputStream(bais);
        WekaModel model = (WekaModel) in.readObject();

        Classifier classifier = model.getClassifier();
        Instances data = InstanceUtils.createFromDataset(request.getDataset());
        String dependentFeature = (String) request.getAdditionalInfo();
        data.insertAttributeAt(new Attribute(dependentFeature), data.numAttributes());
        data.setClass(data.attribute(dependentFeature));

        List<LinkedHashMap<String, Object>> predictions = new ArrayList<>();
        //            data.stream().forEach(instance -> {
        //                try {
        //                    double prediction = classifier.classifyInstance(instance);
        //                    Map<String, Object> predictionMap = new HashMap<>();
        //                    predictionMap.put("Weka MLR prediction of " + dependentFeature, prediction);
        //                    predictions.add(predictionMap);
        //                } catch (Exception ex) {
        //                    Logger.getLogger(WekaMLR.class.getName()).log(Level.SEVERE, null, ex);
        //                }
        //            });

        for (int i = 0; i < data.numInstances(); i++) {
            Instance instance = data.instance(i);
            try {
                double prediction = classifier.classifyInstance(instance);
                LinkedHashMap<String, Object> predictionMap = new LinkedHashMap<>();
                predictionMap.put("Weka RBF prediction of " + dependentFeature, prediction);
                predictions.add(predictionMap);
            } catch (Exception ex) {
                Logger.getLogger(WekaMLR.class.getName()).log(Level.SEVERE, null, ex);
                return Response.status(Response.Status.BAD_REQUEST)
                        .entity("Error while gettting predictions. " + ex.getMessage()).build();
            }
        }

        PredictionResponse response = new PredictionResponse();
        response.setPredictions(predictions);
        return Response.ok(response).build();
    } catch (IOException | ClassNotFoundException ex) {
        Logger.getLogger(WekaMLR.class.getName()).log(Level.SEVERE, null, ex);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ex.getMessage()).build();
    }
}

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

License:Open Source License

@POST
@Path("prediction")
public Response prediction(PredictionRequest request) {
    try {/*from w w  w .j  a  v a2  s.c  o m*/
        if (request.getDataset().getDataEntry().isEmpty()
                || request.getDataset().getDataEntry().get(0).getValues().isEmpty()) {
            return Response.status(Response.Status.BAD_REQUEST)
                    .entity("Dataset is empty. Cannot make predictions on empty dataset.").build();
        }

        String base64Model = (String) request.getRawModel();
        byte[] modelBytes = Base64.getDecoder().decode(base64Model);
        ByteArrayInputStream bais = new ByteArrayInputStream(modelBytes);
        ObjectInput in = new ObjectInputStream(bais);
        WekaModel model = (WekaModel) in.readObject();

        Classifier classifier = model.getClassifier();
        Instances data = InstanceUtils.createFromDataset(request.getDataset());
        String dependentFeature = (String) request.getAdditionalInfo();
        data.insertAttributeAt(new Attribute(dependentFeature), data.numAttributes());
        data.setClass(data.attribute(dependentFeature));
        List<LinkedHashMap<String, Object>> predictions = new ArrayList<>();
        //            data.stream().forEach(instance -> {
        //                try {
        //                    double prediction = classifier.classifyInstance(instance);
        //                    Map<String, Object> predictionMap = new HashMap<>();
        //                    predictionMap.put("Weka SVM prediction of " + dependentFeature, prediction);
        //                    predictions.add(predictionMap);
        //                } catch (Exception ex) {
        //                    Logger.getLogger(WekaSVM.class.getName()).log(Level.SEVERE, null, ex);
        //                }
        //            });

        for (int i = 0; i < data.numInstances(); i++) {
            Instance instance = data.instance(i);
            try {
                double prediction = classifier.classifyInstance(instance);
                LinkedHashMap<String, Object> predictionMap = new LinkedHashMap<>();
                predictionMap.put("Weka SVM prediction of " + dependentFeature, prediction);
                predictions.add(predictionMap);
            } catch (Exception ex) {
                Logger.getLogger(WekaMLR.class.getName()).log(Level.SEVERE, null, ex);
                return Response.status(Response.Status.BAD_REQUEST)
                        .entity("Error while gettting predictions. " + ex.getMessage()).build();
            }
        }

        PredictionResponse response = new PredictionResponse();
        response.setPredictions(predictions);
        return Response.ok(response).build();
    } catch (Exception ex) {
        Logger.getLogger(WekaSVM.class.getName()).log(Level.SEVERE, null, ex);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ex.getMessage()).build();
    }
}

From source file:org.knime.knip.suise.ops.BuildTrainingData.java

License:Open Source License

/**
 * {@inheritDoc}// w  w w  . j  av  a 2  s .  c o m
 */
public BinaryObjectFactory<RandomAccessibleInterval<LabelingType<L>>, Img<T>, Instances> bufferFactory() {
    return new BinaryObjectFactory<RandomAccessibleInterval<LabelingType<L>>, Img<T>, Instances>() {

        @Override
        public Instances instantiate(RandomAccessibleInterval<LabelingType<L>> inputA, Img<T> inputB) {
            // build training set
            ArrayList<Attribute> attr = new ArrayList<Attribute>();
            for (int a = 0; a < inputB.dimension(m_featDim); a++) {
                attr.add(new Attribute("attr" + a));
            }
            Instances instances = new Instances("data", attr, m_classLabels.size() * 20);

            instances.insertAttributeAt(new Attribute("class", m_classLabels), instances.numAttributes());
            instances.setClassIndex(instances.numAttributes() - 1);
            return instances;
        }

    };
}

From source file:org.mcennis.graphrat.algorithm.machinelearning.WekaClassifierMultiAttribute.java

License:Open Source License

@Override
public void execute(Graph g) {
    Actor[] source = g.getActor((String) parameter[1].getValue());
    if (source != null) {

        // create the atributes for each artist
        FastVector sourceTypes = new FastVector();
        Actor[] dest = g.getActor((String) parameter[3].getValue());
        if (dest != null) {
            // create the Instances set backing this object
            Instances masterSet = null;
            Instance[] trainingData = new Instance[source.length];
            for (int i = 0; i < source.length; ++i) {
                // First, acquire the instance objects for each actor
                Property p = null;// ww  w. j a v  a2s  .com
                if ((Boolean) parameter[10].getValue()) {
                    p = source[i].getProperty((String) parameter[2].getValue() + g.getID());
                } else {
                    p = source[i].getProperty((String) parameter[2].getValue());
                }
                if (p != null) {
                    Object[] values = p.getValue();
                    if (values.length > 0) {
                        sourceTypes.addElement(source[i].getID());
                        trainingData[i] = (Instance) ((Instance) values[0]).copy();
                        // assume that this Instance has a backing dataset 
                        // that contains all Instance objects to be tested
                        if (masterSet == null) {
                            masterSet = new Instances(trainingData[i].dataset(), source.length);
                        }
                        masterSet.add(trainingData[i]);
                        sourceTypes.addElement(source[i].getID());
                    } else {
                        trainingData[i] = null;
                        Logger.getLogger(WekaClassifierMultiAttribute.class.getName()).log(Level.WARNING,
                                "Actor " + source[i].getType() + ":" + source[i].getID()
                                        + " does not have an Instance value of property ID " + p.getType());
                    }
                } else {
                    trainingData[i] = null;
                    Logger.getLogger(WekaClassifierMultiAttribute.class.getName()).log(Level.WARNING,
                            "Actor " + source[i].getType() + ":" + source[i].getID()
                                    + " does not have a property of ID " + p.getType());
                }
            }

            Vector<Attribute> destVector = new Vector<Attribute>();
            for (int i = 0; i < dest.length; ++i) {
                FastVector type = new FastVector();
                type.addElement("false");
                type.addElement("true");
                Attribute tmp = new Attribute(dest[i].getID(), type);
                destVector.add(tmp);
                masterSet.insertAttributeAt(tmp, masterSet.numAttributes());
            }
            Attribute sourceID = new Attribute("sourceID", sourceTypes);
            masterSet.insertAttributeAt(sourceID, masterSet.numAttributes());

            //set ground truth for evaluation
            for (int i = 0; i < masterSet.numInstances(); ++i) {
                Instance inst = masterSet.instance(i);
                Actor user = g.getActor((String) parameter[i].getValue(),
                        sourceID.value((int) inst.value(sourceID)));
                if (user != null) {
                    for (int j = 0; j < dest.length; ++j) {
                        if (g.getLink((String) parameter[4].getValue(), user, dest[j]) != null) {
                            inst.setValue(sourceID, "true");
                        } else {
                            if ((Boolean) parameter[9].getValue()) {
                                inst.setValue(sourceID, "false");
                            } else {
                                inst.setValue(sourceID, Double.NaN);
                            }
                        }
                    }
                } else {
                    Logger.getLogger(WekaClassifierMultiAttribute.class.getName()).log(Level.SEVERE,
                            "Actor " + sourceID.value((int) inst.value(sourceID)) + " does not exist in graph");
                }
            }

            // perform cross fold evaluation of each classifier in turn
            String[] opts = ((String) parameter[9].getValue()).split("\\s+");
            Properties props = new Properties();
            if ((Boolean) parameter[11].getValue()) {
                props.setProperty("LinkType", (String) parameter[5].getValue() + g.getID());
            } else {
                props.setProperty("LinkType", (String) parameter[5].getValue());
            }
            props.setProperty("LinkClass", "Basic");
            try {
                for (int destCount = 0; destCount < dest.length; ++destCount) {
                    masterSet.setClass(destVector.get(destCount));
                    for (int i = 0; i < (Integer) parameter[8].getValue(); ++i) {
                        Instances test = masterSet.testCV((Integer) parameter[8].getValue(), i);
                        Instances train = masterSet.testCV((Integer) parameter[8].getValue(), i);
                        Classifier classifier = (Classifier) ((Class) parameter[7].getValue()).newInstance();
                        classifier.setOptions(opts);
                        classifier.buildClassifier(train);
                        for (int j = 0; j < test.numInstances(); ++j) {
                            String sourceName = sourceID.value((int) test.instance(j).value(sourceID));
                            double result = classifier.classifyInstance(test.instance(j));
                            String predicted = masterSet.classAttribute().value((int) result);
                            Link derived = LinkFactory.newInstance().create(props);
                            derived.set(g.getActor((String) parameter[2].getValue(), sourceName), 1.0,
                                    g.getActor((String) parameter[3].getValue(), predicted));
                            g.add(derived);
                        }
                    }
                }
            } catch (InstantiationException ex) {
                Logger.getLogger(WekaClassifierMultiAttribute.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(WekaClassifierMultiAttribute.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(WekaClassifierMultiAttribute.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else { // dest==null
            Logger.getLogger(WekaClassifierMultiAttribute.class.getName()).log(Level.WARNING,
                    "Ground truth mode '" + (String) parameter[3].getValue() + "' has no actors");
        }
    } else { // source==null
        Logger.getLogger(WekaClassifierMultiAttribute.class.getName()).log(Level.WARNING,
                "Source mode '" + (String) parameter[2].getValue() + "' has no actors");
    }
}

From source file:org.mcennis.graphrat.algorithm.machinelearning.WekaClassifierOneAttribute.java

License:Open Source License

@Override
public void execute(Graph g) {
    Actor[] source = g.getActor((String) parameter[1].getValue());
    if (source != null) {

        // create the Instance sets for each ac
        FastVector classTypes = new FastVector();
        FastVector sourceTypes = new FastVector();
        Actor[] dest = g.getActor((String) parameter[3].getValue());
        if (dest != null) {
            for (int i = 0; i < dest.length; ++i) {
                classTypes.addElement(dest[i].getID());
            }// w  ww .ja  v a  2 s.c om
            Attribute classAttribute = new Attribute((String) parameter[5].getValue(), classTypes);

            Instance[] trainingData = new Instance[source.length];
            Instances masterSet = null;
            for (int i = 0; i < source.length; ++i) {

                // First, acquire the instance objects for each actor
                Property p = null;
                if ((Boolean) parameter[9].getValue()) {
                    p = source[i].getProperty((String) parameter[2].getValue() + g.getID());
                } else {
                    p = source[i].getProperty((String) parameter[2].getValue());
                }
                if (p != null) {
                    Object[] values = p.getValue();
                    if (values.length > 0) {
                        sourceTypes.addElement(source[i].getID());
                        trainingData[i] = (Instance) ((Instance) values[0]).copy();
                        // assume that this Instance has a backing dataset 
                        // that contains all Instance objects to be tested
                        if (masterSet == null) {
                            masterSet = new Instances(trainingData[i].dataset(), source.length);
                        }
                        masterSet.add(trainingData[i]);
                    } else {
                        trainingData[i] = null;
                        Logger.getLogger(WekaClassifierOneAttribute.class.getName()).log(Level.WARNING,
                                "Actor " + source[i].getType() + ":" + source[i].getID()
                                        + " does not have an Instance value of property ID " + p.getType());
                    }
                } else {
                    trainingData[i] = null;
                    Logger.getLogger(WekaClassifierOneAttribute.class.getName()).log(Level.WARNING,
                            "Actor " + source[i].getType() + ":" + source[i].getID()
                                    + " does not have a property of ID " + p.getType());
                }

            } // for every actor, fix the instance
            Attribute sourceID = new Attribute("sourceID", sourceTypes);
            masterSet.insertAttributeAt(sourceID, masterSet.numAttributes());
            masterSet.insertAttributeAt(classAttribute, masterSet.numAttributes());
            masterSet.setClass(classAttribute);
            for (int i = 0; i < source.length; ++i) {
                if (trainingData[i] != null) {
                    trainingData[i].setValue(sourceID, source[i].getID());
                    Link[] link = g.getLinkBySource((String) parameter[4].getValue(), source[i]);
                    if (link == null) {
                        trainingData[i].setClassValue(Double.NaN);
                    } else {
                        trainingData[i].setClassValue(link[0].getDestination().getID());
                    }
                }
            }

            String[] opts = ((String) parameter[7].getValue()).split("\\s+");
            Properties props = new Properties();
            if ((Boolean) parameter[10].getValue()) {
                props.setProperty("LinkType", (String) parameter[5].getValue() + g.getID());
            } else {
                props.setProperty("LinkType", (String) parameter[5].getValue());
            }
            props.setProperty("LinkClass", "Basic");
            try {
                for (int i = 0; i < (Integer) parameter[8].getValue(); ++i) {
                    Instances test = masterSet.testCV((Integer) parameter[8].getValue(), i);
                    Instances train = masterSet.testCV((Integer) parameter[8].getValue(), i);
                    Classifier classifier = (Classifier) ((Class) parameter[6].getValue()).newInstance();
                    classifier.setOptions(opts);
                    classifier.buildClassifier(train);
                    for (int j = 0; j < test.numInstances(); ++j) {
                        String sourceName = sourceID.value((int) test.instance(j).value(sourceID));
                        double result = classifier.classifyInstance(test.instance(j));
                        String predicted = masterSet.classAttribute().value((int) result);
                        Link derived = LinkFactory.newInstance().create(props);
                        derived.set(g.getActor((String) parameter[2].getValue(), sourceName), 1.0,
                                g.getActor((String) parameter[3].getValue(), predicted));
                        g.add(derived);
                    }
                }
            } catch (InstantiationException ex) {
                Logger.getLogger(WekaClassifierOneAttribute.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(WekaClassifierOneAttribute.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(WekaClassifierOneAttribute.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else { // dest==null
            Logger.getLogger(WekaClassifierOneAttribute.class.getName()).log(Level.WARNING,
                    "Ground truth mode '" + (String) parameter[3].getValue() + "' has no actors");
        }
    } else { // source==null
        Logger.getLogger(WekaClassifierOneAttribute.class.getName()).log(Level.WARNING,
                "Source mode '" + (String) parameter[2].getValue() + "' has no actors");
    }
}

From source file:org.openml.webapplication.generatefolds.GenerateFolds.java

License:Open Source License

private static Instances addRowId(Instances instances, String name) {
    instances.insertAttributeAt(new Attribute(name), 0);
    for (int i = 0; i < instances.numInstances(); ++i)
        instances.instance(i).setValue(0, i);
    return instances;
}

From source file:tr.gov.ulakbim.jDenetX.streams.generators.multilabel.MetaMultilabelGenerator.java

License:Open Source License

/**
 * GenerateMultilabelHeader.//from  ww  w .ja v  a2s .co  m
 */
protected MultilabelInstancesHeader generateMultilabelHeader(Instances si) {
    Instances mi = new Instances(si, 0, 0);
    mi.setClassIndex(-1);
    mi.deleteAttributeAt(mi.numAttributes() - 1);
    FastVector bfv = new FastVector();
    bfv.addElement("0");
    bfv.addElement("1");
    for (int i = 0; i < this.m_N; i++) {
        mi.insertAttributeAt(new Attribute("class" + i, bfv), i);
    }
    this.multilabelStreamTemplate = mi;
    this.multilabelStreamTemplate.setRelationName("SYN_Z" + this.labelCardinalityOption.getValue() + "L"
            + this.m_N + "X" + m_A + "S" + metaRandomSeedOption.getValue() + ": -C " + this.m_N);
    this.multilabelStreamTemplate.setClassIndex(this.m_N);
    return new MultilabelInstancesHeader(multilabelStreamTemplate, m_N);
}