List of usage examples for weka.core Instance setValue
public void setValue(Attribute att, String value);
From source file:DiversifyQuery.DivTopK.java
public Instances transformData(Instances data) throws Exception { ArrayList<LegacyShapelet> shapelets = new ArrayList<>(); for (int i = 5; i <= 1; i--) { if (DResultSet.get(i).result.size() == i) { shapelets.addAll(DResultSet.get(i).result); }// w w w . ja v a2 s .com } if (shapelets.size() < 1) { throw new Exception( "Number of shapelets initialised incorrectly - please select value of k greater than or equal to 1 (Usage: setNumberOfShapelets"); } if (data.classIndex() < 0) { throw new Exception("Require that the class be set for the ShapeletTransform"); } Instances output = determineOutputFormat(data, shapelets); // for each data, get distance to each shapelet and create new instance for (int i = 0; i < data.numInstances(); i++) { // for each data Instance toAdd = new Instance(shapelets.size() + 1); int shapeletNum = 0; for (LegacyShapelet s : shapelets) { double dist = subsequenceDistance(s.content, data.instance(i)); toAdd.setValue(shapeletNum++, dist); } toAdd.setValue(shapelets.size(), data.instance(i).classValue()); output.add(toAdd); } return output; }
From source file:DiversifyTopKShaepelet.DiversifyTopKShaepelet.java
@Override public Instances process(Instances data) throws Exception { if (this.numShapelets < 1) { throw new Exception( "Number of shapelets initialised incorrectly - please select value of k greater than or equal to 1 (Usage: setNumberOfShapelets"); }//from w w w . j a v a2s. c o m int maxPossibleLength = data.instance(0).numAttributes() - 1; if (data.classIndex() < 0) { throw new Exception("Require that the class be set for the ShapeletTransform"); } if (this.minShapeletLength < 1 || this.maxShapeletLength < 1 || this.maxShapeletLength < this.minShapeletLength || this.maxShapeletLength > maxPossibleLength) { throw new Exception("Shapelet length parameters initialised incorrectly"); } //Sort data in round robin order dataSourceIDs = new int[data.numInstances()]; for (int i = 0; i < data.numInstances(); i++) { dataSourceIDs[i] = i; } // data = roundRobinData(data, dataSourceIDs); if (this.shapeletsTrained == false) { // shapelets discovery has not yet been caried out, so do so this.shapelets = findDiversityTopKShapelets(this.numShapelets, data, this.minShapeletLength, this.maxShapeletLength); // get k shapelets ATTENTION this.shapeletsTrained = true; if (!supressOutput) { System.out.println(shapelets.size() + " Shapelets have been generated"); } } Instances output = determineOutputFormat(data); // for each data, get distance to each shapelet and create new instance for (int i = 0; i < data.numInstances(); i++) { // for each data Instance toAdd = new Instance(this.shapelets.size() + 1); int shapeletNum = 0; for (LegacyShapelet s : this.shapelets) { double dist = subseqDistance(s.content, data.instance(i)); toAdd.setValue(shapeletNum++, dist); } toAdd.setValue(this.shapelets.size(), data.instance(i).classValue()); output.add(toAdd); } return output; }
From source file:dkpro.similarity.experiments.sts2013.filter.LogFilter.java
License:Open Source License
@Override protected Instance process(Instance inst) throws Exception { Instance newInst = new DenseInstance(inst.numAttributes()); newInst.setValue(0, inst.value(0)); for (int i = 1; i < inst.numAttributes() - 1; i++) { double newVal = Math.log(inst.value(i) + 1); newInst.setValue(i, newVal);//from ww w .j a v a2 s.c o m } newInst.setValue(inst.numAttributes() - 1, inst.value(inst.numAttributes() - 1)); return newInst; }
From source file:edu.brandeis.wisedb.scheduler.training.decisiontree.DTSearcher.java
License:Open Source License
@Override public List<Action> schedule(Set<ModelQuery> toSched) { SingularMachineState start = new SingularMachineState(toSched, qtp, sla); List<Action> toR = new LinkedList<Action>(); applyLoop: while (!start.isGoalState()) { log.fine("Current state: " + start); SortedMap<String, String> features = start.getFeatures(); Instance toClassify = new Instance(attributes.length); toClassify.setDataset(wekaDataSet); for (Attribute a : attributes) { if (a.name().equals("action")) { //toClassify.setValue(a, "N"); continue; }/*from ww w . j ava2 s .c o m*/ try { if (features.get(a.name()).equals("?")) { toClassify.setMissing(a); continue; } try { double d = Double.valueOf(features.get(a.name())); toClassify.setValue(a, d); } catch (NumberFormatException e) { toClassify.setValue(a, features.get(a.name())); } } catch (IllegalArgumentException e) { e.printStackTrace(); log.warning( "Encountered previously unseen attribute value! Might need better training data... making random selection."); log.warning("Value for attribute " + a.name() + " was " + features.get(a.name())); Action rand = getPUAction(start); log.warning("Random action selected: " + rand); toR.add(rand); start.applyAction(rand); continue applyLoop; } } toClassify.setClassMissing(); log.finer("Going to classify: " + toClassify); try { double d = tree.classifyInstance(toClassify); toClassify.setClassValue(d); String action = toClassify.stringValue(toClassify.classIndex()); log.finer("Got action string: " + action); Action selected = null; for (Action a : start.getPossibleActions()) { if (actionMatches(a, action)) { selected = a; break; } } if (selected == null) { //log.warning("Could not find applicable action for string: " + action + " ... picking random action"); Action a = getPUAction(start); start.applyAction(a); toR.add(a); continue; } log.fine("Selected action: " + selected); start.applyAction(selected); toR.add(selected); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } return toR; }
From source file:edu.cmu.lti.oaqa.baseqa.providers.ml.classifiers.MekaProvider.java
License:Apache License
@Override public Map<String, Double> infer(Map<String, Double> features) throws AnalysisEngineProcessException { Instance instance = new SparseInstance(features.size()); instance.setDataset(datasetSchema);/*from w w w. j a va2s . co m*/ for (Map.Entry<String, Double> e : features.entrySet()) { Attribute attribute = datasetSchema.attribute(e.getKey()); if (attribute == null) continue; instance.setValue(attribute, e.getValue()); } double[] probs; try { probs = classifier.distributionForInstance(instance); } catch (Exception e) { throw new AnalysisEngineProcessException(e); } assert datasetSchema.classIndex() == probs.length; return IntStream.range(0, probs.length).boxed() .collect(toMap(i -> datasetSchema.attribute(i).name(), i -> probs[i])); }
From source file:edu.illinois.cs.cogcomp.lbjava.learn.WekaWrapper.java
License:Open Source License
/** * Creates a WEKA Instance object out of a {@link FeatureVector}. **//*from w w w . j ava 2 s. com*/ private Instance makeInstance(int[] exampleFeatures, double[] exampleValues, int[] exampleLabels, double[] labelValues) { // Make sure attributeInfo has been filled if (attributeInfo.size() == 0) { System.err.println("WekaWrapper: Error - makeInstance was called while attributeInfo " + "was empty."); new Exception().printStackTrace(); System.exit(1); } // Initialize an Instance object Instance inst = new Instance(attributeInfo.size()); // Acknowledge that this instance will be a member of our dataset // 'instances' inst.setDataset(instances); // Assign values for its attributes /* * Since we are iterating through this example's feature list, which does not contain the * label feature (the label feature is the first in the 'attribute' list), we start attIndex * at 1, while we start featureIndex at 0. */ for (int featureIndex = 0, attIndex = 1; featureIndex < exampleFeatures.length; ++featureIndex, ++attIndex) { Feature f = (Feature) lexicon.lookupKey(exampleFeatures[featureIndex]); Attribute att = (Attribute) attributeInfo.elementAt(attIndex); // make sure the feature's identifier and the attribute's name match if (!(att.name().equals(f.getStringIdentifier()))) { System.err.println( "WekaWrapper: Error - makeInstance encountered a misaligned " + "attribute-feature pair."); System.err.println( " " + att.name() + " and " + f.getStringIdentifier() + " should have been identical."); new Exception().printStackTrace(); System.exit(1); } if (!f.isDiscrete()) inst.setValue(attIndex, exampleValues[featureIndex]); else { // it's a discrete or conjunctive feature. String attValue = f.totalValues() == 2 ? att.value((int) exampleValues[featureIndex]) : f.getStringValue(); inst.setValue(attIndex, attValue); } } /* * Here, we assume that if either the labels FeatureVector is empty of features, or is null, * then this example is to be considered unlabeled. */ if (exampleLabels.length == 0) { inst.setClassMissing(); } else if (exampleLabels.length > 1) { System.err.println("WekaWrapper: Error - Weka Instances may only take a single class " + "value, "); new Exception().printStackTrace(); System.exit(1); } else { Feature label = labelLexicon.lookupKey(exampleLabels[0]); // make sure the name of the label feature matches the name of the 0'th // attribute if (!(label.getStringIdentifier().equals(((Attribute) attributeInfo.elementAt(0)).name()))) { System.err.println("WekaWrapper: Error - makeInstance found the wrong label name."); new Exception().printStackTrace(); System.exit(1); } if (!label.isDiscrete()) inst.setValue(0, labelValues[0]); else inst.setValue(0, label.getStringValue()); } return inst; }
From source file:edu.illinois.cs.cogcomp.saul.learn.SaulWekaWrapper.java
License:Open Source License
/** * Creates a WEKA Instance object out of a {@link FeatureVector}. **//*from w w w . ja v a2 s .c o m*/ private Instance makeInstance(LBJavaInstance instance) { // Initialize an Instance object Instance inst = new Instance(attributeInfo.size()); // Acknowledge that this instance will be a member of our dataset 'wekaInstances' inst.setDataset(wekaInstances); // set all nominal feature values to 0, which means those features are not used in this example for (int i = 1; i < attributeInfo.size(); i++) if (inst.attribute(i).isNominal()) inst.setValue(i, "0"); // Assign values for its attributes /* * Since we are iterating through this example's feature list, which does not contain the * label feature (the label feature is the first in the 'attribute' list), we set attIndex * to at exampleFeatures[featureIndices] + 1, while we start featureIndices at 0. */ for (int featureIndex = 0; featureIndex < instance.featureIndices.length; ++featureIndex) { int attIndex = instance.featureIndices[featureIndex] + 1; Feature f = lexicon.lookupKey(instance.featureIndices[featureIndex]); // if the feature does not exist, do nothing. this may occur in test set. if (f == null) continue; Attribute att = (Attribute) attributeInfo.elementAt(attIndex); // make sure the feature and the attribute match if (!(att.name().equals(f.toString()))) { System.err.println( "WekaWrapper: Error - makeInstance encountered a misaligned " + "attribute-feature pair."); System.err.println(" " + att.name() + " and " + f.toString() + " should have been identical."); new Exception().printStackTrace(); System.exit(1); } if (f.isDiscrete()) inst.setValue(attIndex, "1"); // this feature is used in this example so we set it to "1" else inst.setValue(attIndex, instance.featureValues[featureIndex]); } /* * Here, we assume that if either the labels FeatureVector is empty of features, or is null, * then this example is to be considered unlabeled. */ if (instance.labelIndices.length == 0) { inst.setClassMissing(); } else if (instance.labelIndices.length > 1) { System.err.println("WekaWrapper: Error - Weka Instances may only take a single class " + "value, "); new Exception().printStackTrace(); System.exit(1); } else { Feature label = labelLexicon.lookupKey(instance.labelIndices[0]); // make sure the label feature matches the n 0'th attribute if (!(label.getGeneratingClassifier().equals(((Attribute) attributeInfo.elementAt(0)).name()))) { System.err.println("WekaWrapper: Error - makeInstance found the wrong label name."); new Exception().printStackTrace(); System.exit(1); } if (!label.isDiscrete()) inst.setValue(0, instance.labelValues[0]); else inst.setValue(0, label.getStringValue()); } return inst; }
From source file:edu.oregonstate.eecs.mcplan.abstraction.Experiments.java
License:Open Source License
private static <A> SingleInstanceDataset<A> combineInstances(final RandomGenerator rng, final SingleInstanceDataset<A> running, final SingleInstanceDataset<A> new_, final double discount) { // final int[] idx = Fn.range( 0, running.size() ); // Fn.shuffle( rng, idx ); ////from ww w . java 2s.co m // for( int i = 0; i < running.size(); ++i ) { // final double p = rng.nextDouble(); // if( p < discount ) { // running.set( i, new_.get( idx[i] ) ); // } // } // // return running; // TODO: This is the "right" thing to do, but the dataset might get // unmanageable quickly for (final Instance inst : new_.instances) { inst.setValue(inst.classIndex(), running.action_to_int.get(new_.int_to_action.get((int) inst.classValue()))); running.instances.add(inst); } return running; }
From source file:edu.umbc.cs.maple.utils.WekaUtils.java
License:Open Source License
/** Converts the instances in the given dataset to binary, setting the specified labels to positive. * Note this method is destructive to data, directly modifying its contents. * @param data the multiclass dataset to be converted to binary. * @param positiveClassValue the class value to treat as positive. *//* ww w .java 2 s. c o m*/ public static void convertMulticlassToBinary(Instances data, String positiveClassValue) { // ensure that data is nominal if (!data.classAttribute().isNominal()) throw new IllegalArgumentException("Instances must have a nominal class."); // create the new class attribute FastVector newClasses = new FastVector(2); newClasses.addElement("Y"); newClasses.addElement("N"); Attribute newClassAttribute = new Attribute("class", newClasses); // alter the class attribute to be binary int newClassAttIdx = data.classIndex(); data.insertAttributeAt(newClassAttribute, newClassAttIdx); int classAttIdx = data.classIndex(); // set the instances classes to be binary, with the labels [Y,N] (indices 0 and 1 respectively) int numInstances = data.numInstances(); for (int instIdx = 0; instIdx < numInstances; instIdx++) { Instance inst = data.instance(instIdx); if (inst.stringValue(classAttIdx).equals(positiveClassValue)) { inst.setValue(newClassAttIdx, 0); // set it to the first class, which will be Y } else { inst.setValue(newClassAttIdx, 1); // set it to the second class, which will be 0 } } // switch the class index to the new class and delete the old class data.setClassIndex(newClassAttIdx); data.deleteAttributeAt(classAttIdx); // alter the dataset name data.setRelationName(data.relationName() + "-" + positiveClassValue); }
From source file:edu.utexas.cs.tactex.utils.RegressionUtils.java
License:Open Source License
public static Instance createInstance(ArrayList<Double> features) { Instance result = new DenseInstance(features.size()); int i = 0;/*from w ww .j av a 2s . com*/ for (Double feature : features) { result.setValue(i++, feature); } return result; }