List of usage examples for weka.core Instance relationalValue
public Instances relationalValue(Attribute att);
From source file:fantail.core.Tools.java
License:Open Source License
public static ReasonerComponent[] getTargetObjects(Instance inst) { Instances targetBag = inst.relationalValue(inst.classIndex()); String[] names = new String[targetBag.numAttributes()]; ReasonerComponent res[] = new ReasonerComponent[names.length]; for (int i = 0; i < names.length; i++) { ReasonerComponent cl = new ReasonerComponent(i, targetBag.instance(0).value(i)); res[i] = cl;/* ww w. j a v a 2 s . co m*/ } return res; }
From source file:org.knime.knip.suise.node.boundarymodel.contourdata.IRI.java
License:Open Source License
/** * {@inheritDoc}//from w ww.j av a2 s . com */ @Override public void buildClassifier(Instances miData) throws Exception { // can classifier handle the data? getCapabilities().testWithFail(miData); final Instances tmpMiData = new Instances(miData); final Instances flatData = toSingleInstanceDataset(miData, null); int numPosBags = 0; for (Instance bag : miData) { if (bag.value(2) == 1) { numPosBags++; } } int remainingNumPosBags = numPosBags; Future<Pair<IntervalRule, Double>>[] futures = new Future[m_numThreads]; ExecutorService pool = Executors.newFixedThreadPool(m_numThreads); while (remainingNumPosBags / (double) numPosBags > 1 - m_coverRate) { final int numIterations = ((int) (m_sampleRate * remainingNumPosBags)) / m_numThreads + 1; for (int t = 0; t < m_numThreads; t++) { futures[t] = pool.submit(new Callable<Pair<IntervalRule, Double>>() { @Override public Pair<IntervalRule, Double> call() throws Exception { return createRule(flatData, tmpMiData, numIterations); } }); } // select the best rule from the threads double score = -Double.MAX_VALUE; IntervalRule rule = null; for (int f = 0; f < futures.length; f++) { if (futures[f].get().getB() > score) { score = futures[f].get().getB(); rule = futures[f].get().getA(); } } m_rules.add(rule); // only keep the bags whose instances are not covered by this rule Instances tmp = new Instances(tmpMiData); tmpMiData.clear(); boolean covered; remainingNumPosBags = 0; for (Instance bag : tmp) { covered = false; for (Instance inst : bag.relationalValue(1)) { double[] distr; distr = rule.distributionForInstance(inst); if (distr[1] > distr[0]) { covered = true; break; } } if (!covered) { tmpMiData.add(bag); if (bag.value(2) == 1) { remainingNumPosBags++; } } } flatData.clear(); toSingleInstanceDataset(tmpMiData, flatData); } pool.shutdown(); }
From source file:org.knime.knip.suise.node.boundarymodel.contourdata.IRI.java
License:Open Source License
private Instances toSingleInstanceDataset(Instances miData, Instances flatData) throws Exception { MultiInstanceToPropositional convertToProp = new MultiInstanceToPropositional(); convertToProp.setInputFormat(miData); for (int i = 0; i < miData.numInstances(); i++) { convertToProp.input(miData.instance(i)); }//from w w w . j av a 2 s.c om convertToProp.batchFinished(); if (flatData == null) { flatData = convertToProp.getOutputFormat(); flatData.deleteAttributeAt(0); // remove the bag index attribute } Instance processed; while ((processed = convertToProp.output()) != null) { processed.setDataset(null); processed.deleteAttributeAt(0); // remove the bag index attribute flatData.add(processed); } // remove class attribute // flatData.setClassIndex(-1); // flatData.deleteAttributeAt(flatData.numAttributes() - 1); // set weights int instanceIdx = 0; for (Instance bag : miData) { for (Instance instance : bag.relationalValue(1)) { flatData.get(instanceIdx).setWeight(instance.weight()); instanceIdx++; } } return flatData; }
From source file:org.knime.knip.suise.node.boundarymodel.contourdata.IRI.java
License:Open Source License
/** * {@inheritDoc}/*from w ww .j a v a 2 s.c o m*/ */ @Override public double[] distributionForInstance(Instance bag) throws Exception { Instances contents = bag.relationalValue(1); double[] res = null; boolean positive = false; for (Instance i : contents) { res = distributionForSingleInstance(i); if (res[1] > res[0]) { positive = true; break; } } if (positive) { return res; } else { return new double[] { 1.0, 0 }; } }