List of usage examples for weka.core Instance setValue
public void setValue(Attribute att, String value);
From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java
License:Open Source License
/** * Return the PCA of the 3d points// w w w.ja va2 s. co m */ public PrincipalComponents GetPCA(List<Point3d> points, boolean center_data) { // pca PrincipalComponents pca = new PrincipalComponents(); // Create the x y z attributes FastVector atts = new FastVector(); Attribute x = new Attribute("x"); Attribute y = new Attribute("y"); Attribute z = new Attribute("z"); atts.addElement(x); atts.addElement(y); atts.addElement(z); // Create instances Instances points_dataset = new Instances("PointsPCA", atts, points.size()); // iterate through all the points for (int i = 0; i < points.size(); i++) { // new intance of 3 values Instance inst = new SparseInstance(3); // get pos point Point3d pos = points.get(i); // Set instance's values for the attributes x, y, z inst.setValue(x, pos.x); inst.setValue(y, pos.y); inst.setValue(z, pos.z); // add instance to dataset points_dataset.add(inst); } // center data pca.setCenterData(center_data); try { // build evaluator pca.buildEvaluator(points_dataset); } catch (java.lang.Exception e) { e.printStackTrace(); } // System.out.println(points_dataset.toSummaryString()); // System.out.println(pca.toString()); return pca; }
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;/* ww w .jav a2 s . c o m*/ 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; 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.reusablecores.instanceFactories.DoubleArrayInstanceFactory.java
License:Open Source License
@Override public Instance transform(Object value, String name) { Instance ret = new Instance(0); ret.setDataset(new Instances(name, new FastVector(0), 1)); double[] data = (double[]) value; if ((data != null) && (data.length > 0)) { FastVector attributes = new FastVector(data.length); ret = new Instance(data.length); for (int i = 0; i < attributes.size(); ++i) { attributes.addElement(new Attribute(name + ":" + i)); ret.setValue(i, data[i]); }/*from w ww. ja v a 2s . c o m*/ ret.setDataset(new Instances(name, attributes, 1)); } return ret; }
From source file:org.mcennis.graphrat.algorithm.reusablecores.instanceFactories.DoubleInstanceFactory.java
License:Open Source License
@Override public Instance transform(Object value, String name) { Instance ret = new Instance(1); FastVector attributes = new FastVector(); attributes.addElement(new Attribute(name)); Instances meta = new Instances(name, attributes, 1); ret.setDataset(meta);/*from ww w. j a v a2 s. c om*/ ret.setValue(0, ((Double) value).doubleValue()); return ret; }
From source file:org.mcennis.graphrat.algorithm.reusablecores.instanceFactories.IntegerInstanceFactory.java
License:Open Source License
@Override public Instance transform(Object value, String name) { Instance ret = new Instance(1); FastVector attributes = new FastVector(); attributes.addElement(new Attribute(name)); Instances meta = new Instances(name, attributes, 1); ret.setDataset(meta);/*from ww w .ja va 2 s. com*/ ret.setValue(0, ((Integer) value).intValue()); return ret; }
From source file:org.mcennis.graphrat.algorithm.reusablecores.instanceFactories.LongInstanceFactory.java
License:Open Source License
@Override public Instance transform(Object value, String name) { Instance ret = new Instance(1); FastVector attributes = new FastVector(); attributes.addElement(new Attribute(name)); Instances meta = new Instances(name, attributes, 1); ret.setDataset(meta);/*from www . j av a2 s. c o m*/ ret.setValue(0, ((Long) value).longValue()); return ret; }
From source file:org.mcennis.graphrat.algorithm.reusablecores.instanceFactories.StringInstanceFactory.java
License:Open Source License
@Override public Instance transform(Object value, String name) { Instance ret = new Instance(1); FastVector string = new FastVector(1); string.addElement(value);//w w w . j a va 2 s . com FastVector attributes = new FastVector(1); attributes.addElement(new Attribute(name, string)); Instances meta = new Instances(name, attributes, 1); ret.setDataset(meta); ret.setValue(0, (String) value); return ret; }
From source file:org.mcennis.graphrat.algorithm.reusablecores.InstanceManipulation.java
License:Open Source License
/** * Takes the contents of the Instance array and creates a new Instance object * whose attributes are the attributes of the Instance objects in sequence * backed by a new Dataset relfecting the new set of attributes. * //from w w w . j a v a 2s. c om * If there is any conflict of attribute names between the Instance objects, * they are duplicated in the Instance object (which may cause difficulties * for some machine learning algorithms.) If this a problem, utilize * normalizeFieldNames instead of concatenation. * * If either array is null or if the length of the data and meta array do not * match, a new Instance object without attributes is created and returned. * * @param data array of Instance objects * @param meta array of Instances backing the data array * @return new Instance containing all the given data */ static public Instance instanceConcatenation(Instance[] data, Instances[] meta) { FastVector attributeVector = new FastVector(); LinkedList<Double> values = new LinkedList<Double>(); Instance ret = new Instance(0); ret.setDataset(new Instances("Default", new FastVector(), 0)); if ((data != null) && (meta != null) && (data.length == meta.length) && (data.length > 0)) { for (int i = 0; i < data.length; ++i) { for (int j = 0; j < meta[i].numAttributes(); ++j) { attributeVector.addElement(meta[i].attribute(j)); values.add(data[i].value(j)); } } ret = new Instance(values.size()); Iterator<Double> it = values.iterator(); for (int i = 0; i < ret.numAttributes(); ++i) { ret.setValue(i, it.next()); } ret.setDataset(new Instances(meta[0].relationName() + " Concatenated", attributeVector, 1)); } return ret; }
From source file:org.openml.webapplication.generatefolds.ArffMapping.java
License:Open Source License
public Instance createInstance(boolean train, int rowid, int repeat, int fold) { Instance instance = new DenseInstance(4); instance.setValue(attributes.get(0), train ? 0.0 : 1.0); instance.setValue(attributes.get(1), rowid); instance.setValue(attributes.get(2), repeat); instance.setValue(attributes.get(3), fold); return instance; }
From source file:org.openml.webapplication.generatefolds.ArffMapping.java
License:Open Source License
public Instance createInstance(boolean train, int rowid, int repeat, int fold, int sample) { Instance instance = new DenseInstance(5); instance.setValue(attributes.get(0), train ? 0.0 : 1.0); instance.setValue(attributes.get(1), rowid); instance.setValue(attributes.get(2), repeat); instance.setValue(attributes.get(3), fold); instance.setValue(attributes.get(4), sample); return instance; }