List of usage examples for weka.core Instances add
@Override public boolean add(Instance instance)
From source file:aaa.util.test.CreateArff.java
License:Open Source License
/** * Generates the Instances object and outputs it in ARFF format to stdout. * * @param args ignored/*from w w w . java2 s .c o m*/ * @throws Exception if generation of instances fails */ public static void main(String[] args) throws Exception { ArrayList<Attribute> atts; ArrayList<Attribute> attsRel; ArrayList<String> attVals; ArrayList<String> attValsRel; Instances data; Instances dataRel; double[] vals; double[] valsRel; int i; // 1. set up attributes atts = new ArrayList<Attribute>(); // - numeric atts.add(new Attribute("att1")); // - nominal attVals = new ArrayList<String>(); for (i = 0; i < 5; i++) attVals.add("val" + (i + 1)); atts.add(new Attribute("att2", attVals)); // - string atts.add(new Attribute("att3", (ArrayList<String>) null)); // - date atts.add(new Attribute("att4", "yyyy-MM-dd")); // - relational attsRel = new ArrayList<Attribute>(); // -- numeric attsRel.add(new Attribute("att5.1")); // -- nominal attValsRel = new ArrayList<String>(); for (i = 0; i < 5; i++) attValsRel.add("val5." + (i + 1)); attsRel.add(new Attribute("att5.2", attValsRel)); dataRel = new Instances("att5", attsRel, 0); atts.add(new Attribute("att5", dataRel, 0)); // 2. create Instances object data = new Instances("MyRelation", atts, 0); // 3. fill with data // first instance vals = new double[data.numAttributes()]; // - numeric vals[0] = Math.PI; // - nominal vals[1] = attVals.indexOf("val3"); // - string vals[2] = data.attribute(2).addStringValue("This is a string!"); // - date vals[3] = data.attribute(3).parseDate("2001-11-09"); // - relational dataRel = new Instances(data.attribute(4).relation(), 0); // -- first instance valsRel = new double[2]; valsRel[0] = Math.PI + 1; valsRel[1] = attValsRel.indexOf("val5.3"); dataRel.add(new DenseInstance(1.0, valsRel)); // -- second instance valsRel = new double[2]; valsRel[0] = Math.PI + 2; valsRel[1] = attValsRel.indexOf("val5.2"); dataRel.add(new DenseInstance(1.0, valsRel)); vals[4] = data.attribute(4).addRelation(dataRel); // add data.add(new DenseInstance(1.0, vals)); // second instance vals = new double[data.numAttributes()]; // important: needs NEW array! // - numeric vals[0] = Math.E; // - nominal vals[1] = attVals.indexOf("val1"); // - string vals[2] = data.attribute(2).addStringValue("And another one!"); // - date vals[3] = data.attribute(3).parseDate("2000-12-01"); // - relational dataRel = new Instances(data.attribute(4).relation(), 0); // -- first instance valsRel = new double[2]; valsRel[0] = Math.E + 1; valsRel[1] = attValsRel.indexOf("val5.4"); dataRel.add(new DenseInstance(1.0, valsRel)); // -- second instance valsRel = new double[2]; valsRel[0] = Math.E + 2; valsRel[1] = attValsRel.indexOf("val5.1"); dataRel.add(new DenseInstance(1.0, valsRel)); vals[4] = data.attribute(4).addRelation(dataRel); // add data.add(new DenseInstance(1.0, vals)); // 4. output data System.out.println(data); }
From source file:ab.demo.AIAssignment2.java
License:Open Source License
public GameState solve() { // capture Image BufferedImage screenshot = ActionRobot.doScreenShot(); // process image Vision vision = new Vision(screenshot); // find the slingshot Rectangle sling = vision.findSlingshotMBR(); // confirm the slingshot while (sling == null && aRobot.getState() == GameState.PLAYING) { System.out.println("No slingshot detected. Please remove pop up or zoom out"); ActionRobot.fullyZoomOut();/*from w w w . j a v a 2 s . co m*/ screenshot = ActionRobot.doScreenShot(); vision = new Vision(screenshot); sling = vision.findSlingshotMBR(); } // get all the pigs List<ABObject> pigs = vision.findPigsMBR(); List<ABObject> blocks = vision.findBlocksMBR(); GameState state = aRobot.getState(); // if there is a sling, then play, otherwise just skip. if (sling != null) { if (!pigs.isEmpty()) { //if there are pigs in the level Point releasePoint = null; Shot shot = new Shot(); int dx, dy; { //random pick up a pig ABObject pig = pigs.get(randomGenerator.nextInt(pigs.size())); Point _tpt = pig.getCenter(); // estimate the trajectory ArrayList<Point> pts = tp.estimateLaunchPoint(sling, _tpt); //define all of the wood, ice and stone in the stage ArrayList<ABObject> wood = new ArrayList<ABObject>(); ArrayList<ABObject> stone = new ArrayList<ABObject>(); ArrayList<ABObject> ice = new ArrayList<ABObject>(); ArrayList<ABObject> tnt = new ArrayList<ABObject>(); //initialise counters to store how many times the trajectory intersects blocks of these types int woodCount = 0; int stoneCount = 0; int iceCount = 0; int pigsCount = 0; int tntCount = 0; //populate the wood, stone and ice ArrayLists with the correct materials for (int i = 0; i < blocks.size(); i++) { if (blocks.get(i).type == ABType.Wood) wood.add(blocks.get(i)); if (blocks.get(i).type == ABType.Stone) stone.add(blocks.get(i)); if (blocks.get(i).type == ABType.Ice) ice.add(blocks.get(i)); if (blocks.get(i).type == ABType.TNT) tnt.add(blocks.get(i)); } //check whether the trajectory intersects any wood blocks for (int i = 0; i < wood.size(); i++) { for (int j = 0; j < pts.size(); j++) { if (wood.get(i).contains(pts.get(j))) { System.out.println("Trajectory intersects some wood"); woodCount++; } if (pig.contains(pts.get(j))) //if we find the pig on this point j = pts.size() - 1; //stop looking for wood on the trajectory (escape for loop) } } //check whether the trajectory intersects any ice blocks for (int i = 0; i < ice.size(); i++) { for (int j = 0; j < pts.size(); j++) { if (ice.get(i).contains(pts.get(j))) { System.out.println("Trajectory intersects some ice"); iceCount++; } if (pig.contains(pts.get(j))) //if we find the pig on this point j = pts.size() - 1; //stop looking for ice on the trajectory (escape for loop) } } //check whether the trajectory intersects any stone blocks for (int i = 0; i < stone.size(); i++) { for (int j = 0; j < pts.size(); j++) { if (stone.get(i).contains(pts.get(j))) { System.out.println("Trajectory intersects some stone"); stoneCount++; } if (pig.contains(pts.get(j))) //if we find the pig on this point j = pts.size() - 1; //stop looking for stone on the trajectory (escape for loop) } } //how many pigs the trajectory intersects (this should always be at least 1) for (int i = 0; i < pigs.size(); i++) { for (int j = 0; j < pts.size(); j++) { if (pigs.get(i).contains(pts.get(j))) { System.out.println("Trajectory intersects a pig"); pigsCount++; } } } //how many tnt blocks the trajectory intersects for (int i = 0; i < tnt.size(); i++) { for (int j = 0; j < pts.size(); j++) { if (tnt.get(i).contains(pts.get(j))) { System.out.println("Trajectory intersects some tnt"); tntCount++; } if (pig.contains(pts.get(j))) //if we find the pig on this point j = pts.size() - 1; //stop looking for tnt on the trajectory } } StringBuilder sb = new StringBuilder(); sb.append(pigsCount + "," + woodCount + "," + iceCount + "," + stoneCount + "," + tntCount + ",?"); String dataEntry = sb.toString(); try (PrintWriter out = new PrintWriter( new BufferedWriter(new FileWriter("dataset/birds.level.arff", true)))) { out.println(dataEntry); } catch (IOException e) { System.out.println("Error - dataset/birds.level.arff file not found or in use!"); } //indicator of if the agent should continue this shot or not (used in the bayes classifier) ArrayList<Boolean> takeShot = new ArrayList<Boolean>(); try { DataSource source = new DataSource("dataset/birds.data.arff"); //initialise the learning set for the agent Instances data = source.getDataSet(); // setting class attribute if the data format does not provide this information // For example, the XRFF format saves the class attribute information as well if (data.classIndex() == -1) data.setClassIndex(data.numAttributes() - 1); DataSource thisLevel = new DataSource("dataset/birds.level.arff"); //initialise the data retrieved from the current level for the agent Instances thisLevelData = thisLevel.getDataSet(); if (thisLevelData.classIndex() == -1) thisLevelData.setClassIndex(data.numAttributes() - 1); //build a new NaiveBayes classifier NaiveBayes bayes = new NaiveBayes(); bayes.buildClassifier(data); for (int i = 0; i < thisLevelData.numInstances(); i++) { //for all instances in the current level double label = bayes.classifyInstance(thisLevelData.instance(i)); //generate an outcome/classify an instance in the current level thisLevelData.instance(i).setClassValue(label); //store this outcome System.out.println(thisLevelData.instance(i).stringValue(5)); //print it if (thisLevelData.instance(i).stringValue(5) != "?") { //if there is a decisive choice as to whether a shot should be taken data.add(thisLevelData.instance(i)); //store it if (thisLevelData.instance(i).stringValue(5) == "yes") {//if the classifier classifies a good shot, store it takeShot.add(true); } else { //if no, store this too takeShot.add(false); } } } //add all non ? entries to the learning set BufferedWriter writer = new BufferedWriter(new FileWriter("dataset/birds.data.arff")); writer.write(data.toString()); writer.flush(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.out.println("Exception caught - file handle error"); } //TODO: roll a random number to determine whether we take a shot or not. //populated using the bayesian classification above. //if we roll true, continue with the random pig shot as usual. //if not, take a new random pig and try again. //TODO: implement a failsafe so the agent does not get stuck randomly choosing pigs which the bayesian classification does not allow. Random rng = new Random(takeShot.size()); if (takeShot.get(rng.nextInt())) System.out.println("Taking this shot."); else { System.out.println("Not taking this shot. Finding another random pig."); return state; } // if the target is very close to before, randomly choose a // point near it if (prevTarget != null && distance(prevTarget, _tpt) < 10) { double _angle = randomGenerator.nextDouble() * Math.PI * 2; _tpt.x = _tpt.x + (int) (Math.cos(_angle) * 10); _tpt.y = _tpt.y + (int) (Math.sin(_angle) * 10); System.out.println("Randomly changing to " + _tpt); } prevTarget = new Point(_tpt.x, _tpt.y); // do a high shot when entering a level to find an accurate velocity if (firstShot && pts.size() > 1) { releasePoint = pts.get(1); } else if (pts.size() == 1) releasePoint = pts.get(0); else if (pts.size() == 2) { // randomly choose between the trajectories, with a 1 in // 6 chance of choosing the high one if (randomGenerator.nextInt(6) == 0) releasePoint = pts.get(1); else releasePoint = pts.get(0); } else if (pts.isEmpty()) { System.out.println("No release point found for the target"); System.out.println("Try a shot with 45 degree"); releasePoint = tp.findReleasePoint(sling, Math.PI / 4); } // Get the reference point Point refPoint = tp.getReferencePoint(sling); //Calculate the tapping time according the bird type if (releasePoint != null) { double releaseAngle = tp.getReleaseAngle(sling, releasePoint); System.out.println("Release Point: " + releasePoint); System.out.println("Release Angle: " + Math.toDegrees(releaseAngle)); int tapInterval = 0; switch (aRobot.getBirdTypeOnSling()) { case RedBird: tapInterval = 0; break; // start of trajectory case YellowBird: tapInterval = 65 + randomGenerator.nextInt(25); break; // 65-90% of the way case WhiteBird: tapInterval = 70 + randomGenerator.nextInt(20); break; // 70-90% of the way case BlackBird: tapInterval = 70 + randomGenerator.nextInt(20); break; // 70-90% of the way case BlueBird: tapInterval = 65 + randomGenerator.nextInt(20); break; // 65-85% of the way default: tapInterval = 60; } int tapTime = tp.getTapTime(sling, releasePoint, _tpt, tapInterval); dx = (int) releasePoint.getX() - refPoint.x; dy = (int) releasePoint.getY() - refPoint.y; shot = new Shot(refPoint.x, refPoint.y, dx, dy, 0, tapTime); } else { System.err.println("No Release Point Found"); return state; } } // check whether the slingshot is changed. the change of the slingshot indicates a change in the scale. { ActionRobot.fullyZoomOut(); screenshot = ActionRobot.doScreenShot(); vision = new Vision(screenshot); Rectangle _sling = vision.findSlingshotMBR(); if (_sling != null) { double scale_diff = Math.pow((sling.width - _sling.width), 2) + Math.pow((sling.height - _sling.height), 2); if (scale_diff < 25) { if (dx < 0) { aRobot.cshoot(shot); state = aRobot.getState(); if (state == GameState.PLAYING) { screenshot = ActionRobot.doScreenShot(); vision = new Vision(screenshot); List<Point> traj = vision.findTrajPoints(); tp.adjustTrajectory(traj, sling, releasePoint); firstShot = false; } } } else System.out.println( "Scale is changed, can not execute the shot, will re-segement the image"); } else System.out .println("no sling detected, can not execute the shot, will re-segement the image"); } } } return state; }
From source file:activeSegmentation.feature.FeatureExtraction.java
License:Open Source License
/** * Add training samples from a rectangular roi * //from www .j a v a2 s. c o m * @param trainingData set of instances to add to * @param classIndex class index value * @param sliceNum number of 2d slice being processed * @param r shape roi * @return number of instances added */ private int addRectangleRoiInstances(final Instances trainingData, int classIndex, int sliceNum, Roi r) { int numInstances = 0; final Rectangle rect = r.getBounds(); final Polygon poly = r.getPolygon(); final int x0 = rect.x; final int y0 = rect.y; final int lastX = x0 + rect.width; final int lastY = y0 + rect.height; for (int x = x0; x < lastX; x++) for (int y = y0; y < lastY; y++) { if (poly.contains(new Point(x0, y0))) { trainingData.add(filterManager.createInstance(x, y, classIndex, sliceNum)); } // increase number of instances for this class numInstances++; } return numInstances; }
From source file:activeSegmentation.feature.FeatureExtraction.java
License:Open Source License
/** * Add training samples from a rectangular roi * /*w w w .j a v a 2s . com*/ * @param trainingData set of instances to add to * @param classIndex class index value * @param sliceNum number of 2d slice being processed * @param r shape roi * @return number of instances added */ private Instances addRectangleRoiInstances(int sliceNum, List<String> classLabels, int classes) { Instances testingData; ArrayList<Attribute> attributes = createFeatureHeader(); attributes.add(new Attribute(Common.CLASS, addClasstoHeader(classes, classLabels))); System.out.println(attributes.toString()); // create initial set of instances testingData = new Instances(Common.INSTANCE_NAME, attributes, 1); // Set the index of the class attribute testingData.setClassIndex(filterManager.getNumOfFeatures()); for (int x = 0; x < originalImage.getWidth(); x++) { for (int y = 0; y < originalImage.getHeight(); y++) { testingData.add(filterManager.createInstance(x, y, 0, sliceNum)); } } // increase number of instances for this class System.out.println("SIZe" + testingData.size()); System.out.println(testingData.get(1).toString()); return testingData; }
From source file:adams.data.conversion.SpreadSheetToWekaInstances.java
License:Open Source License
/** * Performs the actual conversion./* w ww . j a va2 s . com*/ * * @return the converted data * @throws Exception if something goes wrong with the conversion */ @Override protected Object doConvert() throws Exception { Instances result; SpreadSheet sheet; DenseInstance inst; ArrayList<Attribute> atts; HashSet<String> unique; ArrayList<String> labels; Row row; Cell cell; int i; int n; double[] values; Collection<ContentType> types; ContentType type; boolean added; int[] classIndices; sheet = (SpreadSheet) m_Input; // create header atts = new ArrayList<>(); for (i = 0; i < sheet.getColumnCount(); i++) { added = false; types = sheet.getContentTypes(i); if (types.contains(ContentType.DOUBLE)) types.remove(ContentType.LONG); if (types.contains(ContentType.LONG)) { types.add(ContentType.DOUBLE); types.remove(ContentType.LONG); } if (types.size() == 1) { type = (ContentType) types.toArray()[0]; if (type == ContentType.DOUBLE) { atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent())); added = true; } else if (type == ContentType.DATE) { atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent(), Constants.TIMESTAMP_FORMAT)); added = true; } else if (type == ContentType.TIME) { atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent(), Constants.TIME_FORMAT)); added = true; } } if (!added) { unique = new HashSet<>(); for (n = 0; n < sheet.getRowCount(); n++) { row = sheet.getRow(n); cell = row.getCell(i); if ((cell != null) && !cell.isMissing()) unique.add(cell.getContent()); } if ((unique.size() > m_MaxLabels) || (m_MaxLabels < 1)) { atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent(), (FastVector) null)); } else { labels = new ArrayList<>(unique); Collections.sort(labels); atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent(), labels)); } } } result = new Instances(Environment.getInstance().getProject(), atts, sheet.getRowCount()); if (sheet.hasName()) result.setRelationName(sheet.getName()); // add data for (n = 0; n < sheet.getRowCount(); n++) { row = sheet.getRow(n); values = new double[result.numAttributes()]; for (i = 0; i < result.numAttributes(); i++) { cell = row.getCell(i); values[i] = weka.core.Utils.missingValue(); if ((cell != null) && !cell.isMissing()) { if (result.attribute(i).type() == Attribute.DATE) { if (cell.isTime()) values[i] = cell.toTime().getTime(); else values[i] = cell.toDate().getTime(); } else if (result.attribute(i).isNumeric()) { values[i] = Utils.toDouble(cell.getContent()); } else if (result.attribute(i).isString()) { values[i] = result.attribute(i).addStringValue(cell.getContent()); } else { values[i] = result.attribute(i).indexOfValue(cell.getContent()); } } } inst = new DenseInstance(1.0, values); result.add(inst); } if (sheet instanceof Dataset) { classIndices = ((Dataset) sheet).getClassAttributeIndices(); if (classIndices.length > 0) result.setClassIndex(classIndices[0]); } return result; }
From source file:adams.data.conversion.TimeseriesToWekaInstances.java
License:Open Source License
/** * Performs the actual conversion.// ww w . j a v a 2 s .co m * * @return the converted data * @throws Exception if something goes wrong with the conversion */ @Override protected Object doConvert() throws Exception { Instances result; ArrayList<Attribute> atts; Instance inst; Timeseries series; TimeseriesPoint point; double[] value; series = (Timeseries) m_Input; atts = new ArrayList<Attribute>(); atts.add(new Attribute("Timestamp", m_Format.getValue())); atts.add(new Attribute("Value")); result = new Instances(series.getID(), atts, series.size()); for (Object obj : series.toList()) { point = (TimeseriesPoint) obj; value = new double[2]; value[0] = point.getTimestamp().getTime(); value[1] = point.getValue(); inst = new DenseInstance(1.0, value); result.add(inst); } return result; }
From source file:adams.data.instancesanalysis.pls.PLS1.java
License:Open Source License
/** * Performs predictions on the data.// w w w .ja v a 2 s. c om * * @param data the input data * @return the predicted data */ protected Instances predict(Instances data) { Instances result; Instances tmpInst; int i; int j; Matrix x; Matrix X; Matrix T; Matrix t; result = new Instances(getOutputFormat()); for (i = 0; i < data.numInstances(); i++) { // work on each instance tmpInst = new Instances(data, 0); tmpInst.add((Instance) data.instance(i).copy()); x = MatrixHelper.getX(tmpInst); X = new Matrix(1, getNumComponents()); T = new Matrix(1, getNumComponents()); for (j = 0; j < getNumComponents(); j++) { MatrixHelper.setVector(x, X, j); // 1. step: tj = xj * wj t = x.times(MatrixHelper.getVector(m_W, j)); MatrixHelper.setVector(t, T, j); // 2. step: xj+1 = xj - tj*pj^T (tj is 1x1 matrix!) x = x.minus(MatrixHelper.getVector(m_P, j).transpose().times(t.get(0, 0))); } switch (m_PredictionType) { case ALL: tmpInst = MatrixHelper.toInstances(getOutputFormat(), T, T.times(m_b_hat)); break; case NONE: case EXCEPT_CLASS: tmpInst = MatrixHelper.toInstances(getOutputFormat(), T, MatrixHelper.getY(tmpInst)); break; default: throw new IllegalStateException("Unhandled prediction type: " + m_PredictionType); } result.add(tmpInst.instance(0)); } return result; }
From source file:adams.flow.sink.WekaInstanceViewer.java
License:Open Source License
/** * Returns the displayed instances as ARFF. * * @param panel the panel to obtain the data form * @return the generated ARFF content or null if no data available *///from w w w. ja va 2 s. co m protected static String supplyText(InstancePanel panel) { InstanceContainerManager manager; weka.core.Instances data; int i; if (panel == null) return null; manager = panel.getContainerManager(); if (manager.countVisible() == 0) return null; data = new weka.core.Instances(manager.getVisible(0).getData().getDatasetHeader()); for (i = 0; i < manager.countVisible(); i++) data.add(manager.getVisible(i).getData().toInstance()); return data.toString(); }
From source file:adams.flow.transformer.WekaBootstrapping.java
License:Open Source License
/** * Executes the flow item./*from ww w .j a v a 2 s . c om*/ * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; SpreadSheet sheet; Row row; Evaluation evalAll; Evaluation eval; WekaEvaluationContainer cont; TIntList indices; Random random; int i; int iteration; int size; List<Prediction> preds; Instances header; Instances data; ArrayList<Attribute> atts; Instance inst; boolean numeric; int classIndex; Double[] errors; Double[] errorsRev; Percentile<Double> perc; Percentile<Double> percRev; TIntList subset; result = null; if (m_InputToken.getPayload() instanceof Evaluation) { evalAll = (Evaluation) m_InputToken.getPayload(); } else { cont = (WekaEvaluationContainer) m_InputToken.getPayload(); evalAll = (Evaluation) cont.getValue(WekaEvaluationContainer.VALUE_EVALUATION); } if ((evalAll.predictions() == null) || (evalAll.predictions().size() == 0)) result = "No predictions available!"; if (result == null) { // init spreadsheet sheet = new DefaultSpreadSheet(); row = sheet.getHeaderRow(); row.addCell("S").setContentAsString("Subsample"); for (EvaluationStatistic s : m_StatisticValues) row.addCell(s.toString()).setContentAsString(s.toString()); for (i = 0; i < m_Percentiles.length; i++) { switch (m_ErrorCalculation) { case ACTUAL_MINUS_PREDICTED: row.addCell("perc-AmP-" + i).setContentAsString("Percentile-AmP-" + m_Percentiles[i]); break; case PREDICTED_MINUS_ACTUAL: row.addCell("perc-PmA-" + i).setContentAsString("Percentile-PmA-" + m_Percentiles[i]); break; case ABSOLUTE: row.addCell("perc-Abs-" + i).setContentAsString("Percentile-Abs-" + m_Percentiles[i]); break; case BOTH: row.addCell("perc-AmP-" + i).setContentAsString("Percentile-AmP-" + m_Percentiles[i]); row.addCell("perc-PmA-" + i).setContentAsString("Percentile-PmA-" + m_Percentiles[i]); break; default: throw new IllegalStateException("Unhandled error calculation: " + m_ErrorCalculation); } } // set up bootstrapping preds = evalAll.predictions(); random = new Random(m_Seed); indices = new TIntArrayList(); size = (int) Math.round(preds.size() * m_Percentage); header = evalAll.getHeader(); numeric = header.classAttribute().isNumeric(); m_ClassIndex.setData(header.classAttribute()); if (numeric) classIndex = -1; else classIndex = m_ClassIndex.getIntIndex(); for (i = 0; i < preds.size(); i++) indices.add(i); // create fake evalutions subset = new TIntArrayList(); for (iteration = 0; iteration < m_NumSubSamples; iteration++) { if (isStopped()) { sheet = null; break; } // determine subset.clear(); if (m_WithReplacement) { for (i = 0; i < size; i++) subset.add(indices.get(random.nextInt(preds.size()))); } else { indices.shuffle(random); for (i = 0; i < size; i++) subset.add(indices.get(i)); } // create dataset from predictions errors = new Double[size]; errorsRev = new Double[size]; atts = new ArrayList<>(); atts.add(header.classAttribute().copy("Actual")); data = new Instances(header.relationName() + "-" + (iteration + 1), atts, size); data.setClassIndex(0); for (i = 0; i < subset.size(); i++) { inst = new DenseInstance(preds.get(subset.get(i)).weight(), new double[] { preds.get(subset.get(i)).actual() }); data.add(inst); switch (m_ErrorCalculation) { case ACTUAL_MINUS_PREDICTED: errors[i] = preds.get(subset.get(i)).actual() - preds.get(subset.get(i)).predicted(); break; case PREDICTED_MINUS_ACTUAL: errorsRev[i] = preds.get(subset.get(i)).predicted() - preds.get(subset.get(i)).actual(); break; case ABSOLUTE: errors[i] = Math .abs(preds.get(subset.get(i)).actual() - preds.get(subset.get(i)).predicted()); break; case BOTH: errors[i] = preds.get(subset.get(i)).actual() - preds.get(subset.get(i)).predicted(); errorsRev[i] = preds.get(subset.get(i)).predicted() - preds.get(subset.get(i)).actual(); break; default: throw new IllegalStateException("Unhandled error calculation: " + m_ErrorCalculation); } } // perform "fake" evaluation try { eval = new Evaluation(data); for (i = 0; i < subset.size(); i++) { if (numeric) eval.evaluateModelOnceAndRecordPrediction( new double[] { preds.get(subset.get(i)).predicted() }, data.instance(i)); else eval.evaluateModelOnceAndRecordPrediction( ((NominalPrediction) preds.get(subset.get(i))).distribution().clone(), data.instance(i)); } } catch (Exception e) { result = handleException( "Failed to create 'fake' Evaluation object (iteration: " + (iteration + 1) + ")!", e); break; } // add row row = sheet.addRow(); row.addCell("S").setContent(iteration + 1); for (EvaluationStatistic s : m_StatisticValues) { try { row.addCell(s.toString()).setContent(EvaluationHelper.getValue(eval, s, classIndex)); } catch (Exception e) { getLogger().log(Level.SEVERE, "Failed to calculate statistic in iteration #" + (iteration + 1) + ": " + s, e); row.addCell(s.toString()).setMissing(); } } for (i = 0; i < m_Percentiles.length; i++) { perc = new Percentile<>(); perc.addAll(errors); percRev = new Percentile<>(); percRev.addAll(errorsRev); switch (m_ErrorCalculation) { case ACTUAL_MINUS_PREDICTED: row.addCell("perc-AmP-" + i).setContent(perc.getPercentile(m_Percentiles[i].doubleValue())); break; case PREDICTED_MINUS_ACTUAL: row.addCell("perc-PmA-" + i) .setContent(percRev.getPercentile(m_Percentiles[i].doubleValue())); break; case ABSOLUTE: row.addCell("perc-Abs-" + i).setContent(perc.getPercentile(m_Percentiles[i].doubleValue())); break; case BOTH: row.addCell("perc-AmP-" + i).setContent(perc.getPercentile(m_Percentiles[i].doubleValue())); row.addCell("perc-PmA-" + i) .setContent(percRev.getPercentile(m_Percentiles[i].doubleValue())); break; default: throw new IllegalStateException("Unhandled error calculation: " + m_ErrorCalculation); } } } if ((result == null) && (sheet != null)) m_OutputToken = new Token(sheet); } return result; }
From source file:adams.flow.transformer.WekaDatasetsMerge.java
License:Open Source License
/** * Creates an Instances dataset, containing a copy of the single instance * provided.// w ww . jav a 2 s. c o m * * @param instance The instance to create a dataset for. * @return The created dataset. */ protected Instances datasetForSingleInstance(Instance instance) { // Create a copy of the instance's original dataset Instances dataset = new Instances(instance.dataset(), 1); // Add a copy of the provided instance dataset.add((Instance) instance.copy()); // Return the dataset return dataset; }