@Override public double[] distributionForInstance(Instance xy) throws Exception { int L = xy.classIndex(); double y[] = new double[L]; double w = 0.0; /* * e.g. K = [3,3,5] * we push y_[] from [0,0,0] to [2,2,4] over all necessary iterations. */ int K[] = getKs(xy.dataset()); if (getDebug()) System.out.println("K[] = " + Arrays.toString(K)); double y_[] = new double[L]; for (int i = 0; i < 1000000; i++) { // limit to 1m // System.out.println(""+i+" "+Arrays.toString(y_)); double w_ = A.product(super.probabilityForInstance(xy, y_)); if (w_ > w) { if (getDebug()) System.out.println("y' = " + Arrays.toString(y_) + ", :" + w_); y = Arrays.copyOf(y_, y_.length); w = w_; } if (push(y_, K, 0)) { // Done ! if (getDebug()) System.out.println("Tried all " + (i + 1) + " combinations."); break; } } return y; }
/** This is (mostly) copied from CRF4.java */ public boolean[][] labelConnectionsIn( Alphabet outputAlphabet, InstanceList trainingSet, String start) { int numLabels = outputAlphabet.size(); boolean[][] connections = new boolean[numLabels][numLabels]; for (int i = 0; i < trainingSet.size(); i++) { Instance instance = trainingSet.getInstance(i); FeatureSequence output = (FeatureSequence) instance.getTarget(); for (int j = 1; j < output.size(); j++) { int sourceIndex = outputAlphabet.lookupIndex(output.get(j - 1)); int destIndex = outputAlphabet.lookupIndex(output.get(j)); assert (sourceIndex >= 0 && destIndex >= 0); connections[sourceIndex][destIndex] = true; } } // Handle start state if (start != null) { int startIndex = outputAlphabet.lookupIndex(start); for (int j = 0; j < outputAlphabet.size(); j++) { connections[startIndex][j] = true; } } return connections; }
public int accept(int ins_num, int income_seq, String content) throws RemoteException { if (!work) return -1; Instance res = instances.get(ins_num); if (res == null) return -1; else { if (income_seq >= res.n_p) { res.n_p = income_seq; res.n_a = income_seq; res.content = content; res.decided = true; INS_NUM++; return income_seq; } } return -1; }
public static void printNeighbors(ArrayList neighbors) { int i = 0; for(Neighbor neighbor : neighbors) { Instance instance = neighbor.getInstance(); System.out.println("\nNeighbor " + (i + 1) + ", distance: " + neighbor.getDistance()); i++; for(Feature f : instance.getAttributes()) { System.out.print(f.getName() + ": "); if(f instanceof Category) { System.out.println(((Category)f).getCategory().toString()); } else if(f instanceof Distance) { System.out.println(((Distance)f).getDistance().toString()); } else if (f instanceof Expiration) { System.out.println(((Expiration)f).getExpiry().toString()); } else if (f instanceof Handset) { System.out.print(((Handset)f).getOs().toString() + ", "); System.out.println(((Handset)f).getDevice().toString()); } else if (f instanceof Offer) { System.out.println(((Offer)f).getOfferType().toString()); } else if (f instanceof WSAction) { System.out.println(((WSAction)f).getAction().toString()); } } } }
/** * Determines whether the AMI of the given instance matches the AMI of template and has the * required label (if requiredLabel is non-null) */ private boolean checkInstance( PrintStream logger, Instance existingInstance, Label requiredLabel, EC2AbstractSlave[] returnNode) { logProvision(logger, "checkInstance: " + existingInstance); if (StringUtils.isNotBlank(getIamInstanceProfile())) { if (existingInstance.getIamInstanceProfile() != null) { if (!existingInstance.getIamInstanceProfile().getArn().equals(getIamInstanceProfile())) { logProvision(logger, " false - IAM Instance profile does not match"); return false; } // Match, fall through } else { logProvision(logger, " false - Null IAM Instance profile"); return false; } } if (existingInstance .getState() .getName() .equalsIgnoreCase(InstanceStateName.Terminated.toString()) || existingInstance .getState() .getName() .equalsIgnoreCase(InstanceStateName.ShuttingDown.toString())) { logProvision(logger, " false - Instance is terminated or shutting down"); return false; } // See if we know about this and it has capacity for (EC2AbstractSlave node : NodeIterator.nodes(EC2AbstractSlave.class)) { if (node.getInstanceId().equals(existingInstance.getInstanceId())) { logProvision(logger, "Found existing corresponding Jenkins slave: " + node.getInstanceId()); if (!node.toComputer().isPartiallyIdle()) { logProvision(logger, " false - Node is not partially idle"); return false; } // REMOVEME - this was added to force provision to work, but might not allow // stopped instances to be found - need to investigate further else if (false && node.toComputer().isOffline()) { logProvision(logger, " false - Node is offline"); return false; } else if (requiredLabel != null && !requiredLabel.matches(node.getAssignedLabels())) { logProvision(logger, " false - we need a Node having label " + requiredLabel); return false; } else { logProvision(logger, " true - Node has capacity - can use it"); returnNode[0] = node; return true; } } } logProvision(logger, " true - Instance has no node, but can be used"); return true; }
private boolean containInstance(Instance sel, Slot slot, Instance instance) { if (itsInstance == null) return false; Collection instances = sel.getOwnSlotValues(slot); Iterator i = instances.iterator(); while (i.hasNext()) { Instance tmpInstance = (Instance) i.next(); if (tmpInstance.equals(instance)) return true; } return false; }
public String toString() { StringBuffer buf = new StringBuffer(); for (int i = 0; i < numSources(); i++) { String src = getSource(i); for (int j = 0; j < numInstances(src); j++) { Instance inst = getInstance(src, j); buf.append(inst.toString() + "\n"); } } return buf.toString(); }
/** * Labels the artificially generated data. * * @param artData the artificially generated instances * @exception Exception if instances cannot be labeled successfully */ protected void labelData(Instances artData) throws Exception { Instance curr; double[] probs; for (int i = 0; i < artData.numInstances(); i++) { curr = artData.instance(i); // compute the class membership probs predicted by the current ensemble probs = distributionForInstance(curr); // select class label inversely proportional to the ensemble predictions curr.setClassValue(inverseLabel(probs)); } }
/** * Computes the error in classification on the given data. * * @param data the instances to be classified * @return classification error * @exception Exception if error can not be computed successfully */ protected double computeError(Instances data) throws Exception { double error = 0.0; int numInstances = data.numInstances(); Instance curr; for (int i = 0; i < numInstances; i++) { curr = data.instance(i); // Check if the instance has been misclassified if (curr.classValue() != ((int) classifyInstance(curr))) error++; } return (error / numInstances); }
public void dump() throws RemoteException { int num_ins = instances.size(); System.out.println(getServerName() + ":"); for (int i = 1; i <= num_ins; i++) { Instance tmp = instances.get(i); System.out.println(i + ": " + tmp.toString()); } return; }
public static void main(String[] args) { ArrayList instances = null; ArrayList distances = null; ArrayList neighbors = null; WSAction.Action classification = null; Instance classificationInstance = null; FileReader reader = null; int numRuns = 0, truePositives = 0, falsePositives = 0, falseNegatives = 0, trueNegatives = 0; double precision = 0, recall = 0, fMeasure = 0; falsePositives = 1; reader = new FileReader(PATH_TO_DATA_FILE); instances = reader.buildInstances(); do { classificationInstance = extractIndividualInstance(instances); distances = calculateDistances(instances, classificationInstance); neighbors = getNearestNeighbors(distances); classification = determineMajority(neighbors); System.out.println("Gathering " + K + " nearest neighbors to:"); printClassificationInstance(classificationInstance); printNeighbors(neighbors); System.out.println("\nExpected situation result for instance: " + classification.toString()); if(classification.toString().equals(((WSAction)classificationInstance.getAttributes().get(WSACTION_INDEX)).getAction().toString())) { truePositives++; } else { falseNegatives++; } numRuns++; instances.add(classificationInstance); } while(numRuns < NUM_RUNS); precision = ((double)(truePositives / (double)(truePositives + falsePositives))); recall = ((double)(truePositives / (double)(truePositives + falseNegatives))); fMeasure = ((double)(precision * recall) / (double)(precision + recall)); System.out.println("Precision: " + precision); System.out.println("Recall: " + recall); System.out.println("F-Measure: " + fMeasure); System.out.println("Average distance: " + (double)(averageDistance / (double)(NUM_RUNS * K))); }
private boolean testInstance(int testIndex, Instance instance, Slot slot) { String value; if (specification.getValue() == null) return false; if (((String) specification.getValue()).length() < 1) return false; value = instance.getBrowserText(); return testInstance(testIndex, instance, slot, value); }
public Instance prepare(int ins_num, int income_seq) throws RemoteException { if (!work) return null; Instance res = instances.get(ins_num); if (res == null) return null; else { if (income_seq > res.n_p) { res.n_p = income_seq; return res; } else return null; } }
/** * It checks if the complex covers a given example * * @param m The example * @return boolean True if it covers the example. False in other case */ public boolean covered(Instance m) { boolean cubierto = true; double[] ejemplo = m.getMuest(); for (int i = 0; i < this.size() && cubierto; i++) { Selector s = this.getSelector(i); switch (s.getOperator()) { case 0: // equal operator double[] valor = s.getValues(); cubierto = false; for (int j = 0; (j < valor.length) && (!cubierto); j++) { cubierto = (ejemplo[s.getAttribute()] == valor[j]); } break; case 1: // distinct operator cubierto = ejemplo[s.getAttribute()] != s.getValue(); break; case 2: // lower or equal cubierto = ejemplo[s.getAttribute()] <= s.getValue(); break; case 3: // higher cubierto = ejemplo[s.getAttribute()] > s.getValue(); break; } } return cubierto; }
/** Analogous to binaryFeatureSet */ private Set<Feature> numericFeatureSet(int start, int end, Instance otherInstance) { Set<Feature> s = new HashSet<Feature>(); for (int i = start; i < end; i++) { addAll(s, unitInstance[i].numericFeatureIterator()); } if (otherInstance != null) addAll(s, otherInstance.numericFeatureIterator()); return s; }
public void updated(final Instance instance) { if (!Player.Slides.PLAYLIST.equals(listActivity.getCurrSlide())) { return; } Instance instanceToAdd = instance.accept( new InstanceVisitor<Instance>() { public Instance visit(Track track) { if (getFilter().accept(new MatchFilterVisitor<Track, Tables.Tracks>(track))) { switch (currMode) { case Album: return track.GetAlbum() == AlbumDigest.NO_ALBUM ? track : track.GetAlbum(); case Artist: return track.GetArtist() == ArtistDigest.NO_ARTIST ? track : track.GetArtist(); case Genre: return track.GetGenre() == GenreDigest.NO_GENRE ? track : track.GetGenre(); case Track: return track; case Dir: return track; default: throw new RuntimeException(currMode.name() + " not expexted here"); } } return null; } public Instance visit(SongDigest track) { throw new RuntimeException("not supported yet"); } public Instance visit(Album album) { throw new RuntimeException("not supported yet"); } public Instance visit(GenreDigest genre) { throw new RuntimeException("not supported yet"); } public Instance visit(ArtistDigest artist) { throw new RuntimeException("not supported yet"); } public Instance visit(FSobject dir) { return getDirFilter().accept(new MatchFilterVisitor<FSobject, Tables.Dirs>(dir)) && Mode.Dir.equals(currMode) ? dir : null; } }); if (instanceToAdd != null) { listAdapter.add(instanceToAdd); } }
/** * Calculates the class membership probabilities for the given test instance. * * @param instance the instance to be classified * @return predicted class probability distribution * @exception Exception if distribution can't be computed successfully */ public double[] distributionForInstance(Instance instance) throws Exception { if (instance.classAttribute().isNumeric()) { throw new UnsupportedClassTypeException("Decorate can't handle a numeric class!"); } double[] sums = new double[instance.numClasses()], newProbs; Classifier curr; for (int i = 0; i < m_Committee.size(); i++) { curr = (Classifier) m_Committee.get(i); newProbs = curr.distributionForInstance(instance); for (int j = 0; j < newProbs.length; j++) sums[j] += newProbs[j]; } if (Utils.eq(Utils.sum(sums), 0)) { return sums; } else { Utils.normalize(sums); return sums; } }
/** * Returns the fraction of correct instances of the instance's set for the rule 'regla' * * @param i Number of the rule * @return Fraction of correct instances of the instance's set for the rule 'regla' */ private double getAccuracy(int i) { Instance instancia; double Accuracy; num_cubiertas = 0; num_correctas = 0; for (int k = 0; k < instancias.getNumInstances(); k++) { instancia = instancias.getInstance(k); cubierta = regla.reglaCubreInstancia(instancia); if (cubierta) { num_cubiertas++; clase = instancia.getOutputNominalValuesInt(0); if (clase == i) num_correctas++; } } Accuracy = (double) num_correctas / (double) num_cubiertas; if (num_cubiertas == 0) Accuracy = 0; return Accuracy; }
/** * Calculates the distance between two instances * * @param test the first instance * @param train the second instance * @return the distance between the two given instances, between 0 and 1 */ protected double distance(Instance first, Instance second) { double distance = 0; int firstI, secondI; for (int p1 = 0, p2 = 0; p1 < first.numValues() || p2 < second.numValues(); ) { if (p1 >= first.numValues()) { firstI = m_instances.numAttributes(); } else { firstI = first.index(p1); } if (p2 >= second.numValues()) { secondI = m_instances.numAttributes(); } else { secondI = second.index(p2); } if (firstI == m_instances.classIndex()) { p1++; continue; } if (secondI == m_instances.classIndex()) { p2++; continue; } double diff; if (firstI == secondI) { diff = difference(firstI, first.valueSparse(p1), second.valueSparse(p2)); p1++; p2++; } else if (firstI > secondI) { diff = difference(secondI, 0, second.valueSparse(p2)); p2++; } else { diff = difference(firstI, first.valueSparse(p1), 0); p1++; } distance += diff * diff; } return Math.sqrt(distance / m_instances.numAttributes()); }
/** * Input an instance for filtering. Ordinarily the instance is processed and made available for * output immediately. Some filters require all instances be read before producing output. * * @param instance the input instance * @return true if the filtered instance may now be collected with output(). * @exception IllegalStateException if no input format has been defined. */ public boolean input(Instance instance) { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (m_NewBatch) { resetQueue(); m_NewBatch = false; } push((Instance) instance.copy()); return true; }
/** * Removes from the instance's set those instances that matches with the rule * * @param i Numebr of the rule */ private void removeInstancesCovered(int i) { for (int k = 0; k < instancias.getNumInstances(); k++) { instancia = instancias.getInstance(k); /*System.out.print(k+" "); instancia.print(); System.out.println();*/ cubierta = regla.reglaCubreInstancia(instancia); if (cubierta) { // System.out.println("CUBIERTA"); clase = instancia.getOutputNominalValuesInt(0); // if(clase==i){ instancias.removeInstance(k); instancia.print(); System.out.println(); k = k - 1; // } } } }
/** * Classifies an instance w.r.t. the partitions found. It applies a naive min-distance algorithm. * * @param instance the instance to classify * @return the cluster that contains the nearest point to the instance */ public int clusterInstance(Instance instance) throws java.lang.Exception { DoubleMatrix1D u = DoubleFactory1D.dense.make(instance.toDoubleArray()); double min_dist = Double.POSITIVE_INFINITY; int c = -1; for (int i = 0; i < v.rows(); i++) { double dist = distnorm2(u, v.viewRow(i)); if (dist < min_dist) { c = cluster[i]; min_dist = dist; } } return c; }
public void decide(int income_seq, Operation op) throws RemoteException { // set corresponding Op decided, assume no collision happens if (!work) return; if (op == null) return; Instance ins = instances.get(op.op_num); if (!ins.decided) { ins.decided = true; INS_NUM++; ins.n_a = income_seq; ins.content = op.content; } return; }
/* The main tree traversal function; if node is a leaf, * return classification; otherwise, determine whether * instance is higher or lower than this attribute-value * pair and returns testInstance of proper child */ public boolean testInstance(Instance in) { if (majority) { return majClass; } if (leaf) { return classification; } else { if (in.get((int) attribute) <= value) { return leftChild.testInstance(in); } else { return rightChild.testInstance(in); } } }
protected EC2OndemandSlave newOndemandSlave(Instance inst) throws FormException, IOException { return new EC2OndemandSlave( inst.getInstanceId(), description, remoteFS, getNumExecutors(), labels, mode, initScript, tmpDir, remoteAdmin, jvmopts, stopOnTerminate, idleTerminationMinutes, inst.getPublicDnsName(), inst.getPrivateDnsName(), EC2Tag.fromAmazonTags(inst.getTags()), parent.name, usePrivateDnsName, useDedicatedTenancy, getLaunchTimeout(), amiType); }
/** * @param selection * @return null if no track was selected, track if trak was selected */ public Track choose(Instance selection) { return selection.accept( new InstanceVisitor<Track>() { public Track visit(Track track) { return track; } public Track visit(SongDigest track) { Filter<Tables.SongsReadable> trackFilter = new FieldFilter<Tables.SongsReadable, Track, String>( Tables.SongsReadable.TITLE, Operator.EQ, track.getSongName()); return database .getTracks(new FilterSet<Tables.Tracks>(getFilter(), trackFilter)) .iterator() .next(); } public Track visit(Album album) { Filter<Tables.AlbumsReadable> filter = new FieldFilter<Tables.AlbumsReadable, Track, String>( Tables.AlbumsReadable.ALBUM, Operator.EQ, album.getAlbumId()); change(Mode.Track, filter, false); return null; } public Track visit(GenreDigest genre) { Filter<Tables.GenresReadable> filter = new FieldFilter<Tables.GenresReadable, Track, String>( Tables.GenresReadable.GENRE, Operator.EQ, genre.getGenreId()); change(Mode.Artist, filter, false); return null; } public Track visit(ArtistDigest artist) { Filter<Tables.ArtistsReadable> filter = new FieldFilter<Tables.ArtistsReadable, Track, String>( Tables.ArtistsReadable.ARTIST, Operator.EQ, artist.getArtistId()); change(Mode.Album, filter, false); return null; } public Track visit(FSobject FSobject) { Filter<Tables.FsObjects> filter = new DirFilter(Operator.LIKE, FSobject.getFullPath() + "/%"); change(Mode.Dir, filter, true); return null; } }); }
@Test public void testSplittable() throws SchedulerException { List<VM> vms = Arrays.asList(vm1, vm2, vm3); Collection<Collection<Node>> parts = new ArrayList<>(); parts.add(Arrays.asList(n1, n2)); parts.add(Arrays.asList(n3)); parts.add(Arrays.asList(n4)); Among single = new Among(vms, parts); /* N1 v1 v2 N2 v3 --- N3 v4 -- N4 v5 */ FixedNodeSetsPartitioning partitionner = new FixedNodeSetsPartitioning(parts); partitionner.setPartitions(parts); List<Instance> instances = partitionner.split( new DefaultParameters(), new Instance(mo, Collections.<SatConstraint>emptyList(), new MinMTTR())); TIntIntHashMap vmIndex = Instances.makeVMIndex(instances); TIntIntHashMap nodeIndex = Instances.makeNodeIndex(instances); splitter.split(single, new Instance(mo, new MinMTTR()), instances, vmIndex, nodeIndex); Among a = (Among) instances.get(0).getSatConstraints().iterator().next(); Assert.assertEquals(a.getGroupsOfNodes().size(), 1); Assert.assertEquals(a.getInvolvedNodes(), Arrays.asList(n1, n2)); for (Instance i : instances) { System.out.println(i.getSatConstraints()); } }
public void buildClusterer(ArrayList<String> seqDB, double[][] sm) { seqList = seqDB; this.setSimMatrix(sm); Attribute seqString = new Attribute("sequence", (FastVector) null); FastVector attrInfo = new FastVector(); attrInfo.addElement(seqString); Instances data = new Instances("data", attrInfo, 0); for (int i = 0; i < seqList.size(); i++) { Instance currentInst = new Instance(1); currentInst.setDataset(data); currentInst.setValue(0, seqList.get(i)); data.add(currentInst); } try { buildClusterer(data); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * Calculates the class membership probabilities for the given test instance. * * @param instance the instance to be classified * @return predicted class probability distribution * @throws Exception if distribution can't be computed */ public double[] distributionForInstance(Instance instance) throws Exception { DecisionTableHashKey thekey; double[] tempDist; double[] normDist; m_disTransform.input(instance); m_disTransform.batchFinished(); instance = m_disTransform.output(); m_delTransform.input(instance); m_delTransform.batchFinished(); instance = m_delTransform.output(); thekey = new DecisionTableHashKey(instance, instance.numAttributes(), false); // if this one is not in the table if ((tempDist = (double[]) m_entries.get(thekey)) == null) { if (m_useIBk) { tempDist = m_ibk.distributionForInstance(instance); } else { if (!m_classIsNominal) { tempDist = new double[1]; tempDist[0] = m_majority; } else { tempDist = m_classPriors.clone(); /*tempDist = new double [m_theInstances.classAttribute().numValues()]; tempDist[(int)m_majority] = 1.0; */ } } } else { if (!m_classIsNominal) { normDist = new double[1]; normDist[0] = (tempDist[0] / tempDist[1]); tempDist = normDist; } else { // normalise distribution normDist = new double[tempDist.length]; System.arraycopy(tempDist, 0, normDist, 0, tempDist.length); Utils.normalize(normDist); tempDist = normDist; } } return tempDist; }
/** * Gets the subset of instances that apply to a particluar branch of the split. If the branch * index is -1, the subset will consist of those instances that don't apply to any branch. * * @param branch the index of the branch * @param sourceInstances the instances from which to find the subset * @return the set of instances that apply */ public ReferenceInstances instancesDownBranch(int branch, Instances instances) { ReferenceInstances filteredInstances = new ReferenceInstances(instances, 1); if (branch == -1) { for (Enumeration e = instances.enumerateInstances(); e.hasMoreElements(); ) { Instance inst = (Instance) e.nextElement(); if (inst.isMissing(attIndex)) filteredInstances.addReference(inst); } } else if (branch == 0) { for (Enumeration e = instances.enumerateInstances(); e.hasMoreElements(); ) { Instance inst = (Instance) e.nextElement(); if (!inst.isMissing(attIndex) && inst.value(attIndex) < splitPoint) filteredInstances.addReference(inst); } } else { for (Enumeration e = instances.enumerateInstances(); e.hasMoreElements(); ) { Instance inst = (Instance) e.nextElement(); if (!inst.isMissing(attIndex) && inst.value(attIndex) >= splitPoint) filteredInstances.addReference(inst); } } return filteredInstances; }