@Override public int maxIndex() { return this.mainVector.maxIndex(); }
@Override public int maxIndex() { return this.vector.maxIndex(); }
@Override public int maxIndex() { return this.vector.maxIndex(); }
/** * @return the class index, this takes binary classification into account as * well as multi class classification. */ private int getOutcomeClassIndex(DoubleVector out) { int classIndex = 0; if (binaryClassification) { classIndex = (int) out.get(0); } else { classIndex = out.maxIndex(); } return classIndex; }
/** * @return an array from 0-outcome dimension that has a count on every feature * index representing how often it occurred. */ private int[] getPossibleClasses(List<DoubleVector> outcome) { int[] clzs = new int[outcomeDimension]; for (DoubleVector out : outcome) { if (binaryClassification) { clzs[(int) out.get(0)]++; } else { clzs[out.maxIndex()]++; } } return clzs; }
static boolean correctPrediction(int classIndex, DoubleVector outcome) { return outcome.getLength() == 1 ? ((int) outcome.get(0)) == classIndex : outcome.maxIndex() == classIndex; }
/** * @return the outcome state as integer that can be treated as index. */ private int getOutcomeState(DoubleVector out) { int index; if (out.getDimension() == 2) { index = (int) out.get(0); // simple cast is enough here } else { // assume that the max index is correctly set to // 1, no other state was ticked on. index = out.maxIndex(); } return index; }
/** * Chooses the state by a uniformly distributed random number, so higher * probable states are more likely to happen. * * @return the index of the next state. */ private static int chooseState(Random random, DoubleVector probabilities) { final double r = random.nextDouble(); Iterator<DoubleVectorElement> iterateNonZero = probabilities .iterateNonZero(); DoubleVectorElement next = null; while (iterateNonZero.hasNext()) { next = iterateNonZero.next(); if (r <= Math.exp(next.getValue())) { return next.getIndex(); } } // return the max if we haven't escaped yet return probabilities.maxIndex(); }
.maxIndex(); Deque<DoubleVector> deque = multiQueues[key]; if (deque == null) {
private static FeatureOutcomePair makeBinary(FeatureOutcomePair input, int targetClassIndex) { DoubleVector outcome = input.getOutcome(); if (outcome.maxIndex() == targetClassIndex) { return new FeatureOutcomePair(input.getFeature(), POSITIVE); } return new FeatureOutcomePair(input.getFeature(), NEGATIVE); } }
private void observe(DoubleVector document, DoubleVector outcome, int numDistinctClasses, int[] tokenPerClass, int[] numDocumentsPerClass) { int predictedClass = outcome.maxIndex(); if (numDistinctClasses == 2) { predictedClass = (int) outcome.get(0); } tokenPerClass[predictedClass] += document.getLength(); numDocumentsPerClass[predictedClass]++; Iterator<DoubleVectorElement> iterateNonZero = document.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); double currentCount = probabilityMatrix.get(predictedClass, next.getIndex()); probabilityMatrix.set(predictedClass, next.getIndex(), currentCount + next.getValue()); } }
private void observe(DoubleVector document, DoubleVector outcome, int numDistinctClasses, int[] tokenPerClass, int[] numDocumentsPerClass) { int predictedClass = outcome.maxIndex(); if (numDistinctClasses == 2) { predictedClass = (int) outcome.get(0); } synchronized (probabilityMatrix) { tokenPerClass[predictedClass] += document.getLength(); numDocumentsPerClass[predictedClass]++; } Iterator<DoubleVectorElement> iterateNonZero = document.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); // TODO this is a very granular lock that is acquired very often: // can this high contention be improved, e.g. by writing a temporary // vector and then just merging updates? synchronized (probabilityMatrix) { double currentCount = probabilityMatrix.get(predictedClass, next.getIndex()); probabilityMatrix.set(predictedClass, next.getIndex(), currentCount + next.getValue()); } } }
result.logLoss += outcomeVector .multiply(MathUtils.logVector(predicted)).sum(); int outcomeClass = outcomeVector.maxIndex(); int prediction = classifier.extractPredictedClass(predicted); confusionMatrix[outcomeClass][prediction]++;
/** * @return If the number of outcomes is 2 (binary prediction) the returned * vector contains the class id (0 or 1) at the first index. If not, a * histogram of the classes that were predicted. */ @Override public DoubleVector predict(DoubleVector features) { List<VectorDistanceTuple<DoubleVector>> nearestNeighbours = getNearestNeighbours( features, k); DenseDoubleVector outcomeHistogram = new DenseDoubleVector(numOutcomes); for (VectorDistanceTuple<DoubleVector> tuple : nearestNeighbours) { int classIndex = 0; if (numOutcomes == 2) { classIndex = (int) tuple.getValue().get(0); } else { classIndex = tuple.getValue().maxIndex(); } outcomeHistogram.set(classIndex, outcomeHistogram.get(classIndex) + 1); } if (numOutcomes == 2) { return new SingleEntryDoubleVector(outcomeHistogram.maxIndex()); } else { return outcomeHistogram; } }