Package de.jungblut.math.dense

Examples of de.jungblut.math.dense.DenseDoubleVector


      this.min = min;
      this.max = max;
    }

    public DoubleVector closestPoint(DoubleVector t) {
      DoubleVector p = new DenseDoubleVector(t.getDimension());
      for (int i = 0; i < t.getDimension(); ++i) {
        if (t.get(i) <= min.get(i)) {
          p.set(i, min.get(i));
        } else if (t.get(i) >= max.get(i)) {
          p.set(i, max.get(i));
        } else {
          p.set(i, t.get(i));
        }
      }
      return p;
    }
View Full Code Here


      }
      return p;
    }

    public static HyperRectangle infiniteHyperRectangle(int dimension) {
      DoubleVector min = new DenseDoubleVector(dimension);
      DoubleVector max = new DenseDoubleVector(dimension);
      for (int i = 0; i < dimension; ++i) {
        min.set(i, Double.NEGATIVE_INFINITY);
        max.set(i, Double.POSITIVE_INFINITY);
      }

      return new HyperRectangle(min, max);
    }
View Full Code Here

   * @return the new n-dimensional dense vector vectorized via the hashing
   *         trick.
   */
  public static DoubleVector hashVectorize(DoubleVector inputFeature, int n,
      com.google.common.hash.HashFunction hashFunction) {
    DoubleVector dense = new DenseDoubleVector(n);
    Iterator<DoubleVectorElement> iterateNonZero = inputFeature
        .iterateNonZero();
    while (iterateNonZero.hasNext()) {
      DoubleVectorElement next = iterateNonZero.next();
      // get the hash as int
      int hash = hashFunction.hashInt(next.getIndex()).asInt();
      // abs it, as we don't want to have negative index access
      int bucket = Math.abs(hash) % n;
      // subtract 1 in case of negative values, else increment.
      // this replaces the second hash function proposed by Weinberger et al.
      dense.set(bucket, dense.get(bucket) + (hash < 0 ? -1d : 1d));
    }
    return dense;
  }
View Full Code Here

      // let's assume the default case ("negative") here, instead of making NPEs
      // in other areas, as the callers aren't nullsafe
      clz = 0;
    }
    if (binaryClassification) {
      return new DenseDoubleVector(new double[] { clz });
    } else {
      DoubleVector vec = outcomeDimension > 10 ? new SparseDoubleVector(
          outcomeDimension) : new DenseDoubleVector(outcomeDimension);
      vec.set(clz, 1);
      return vec;
    }
  }
View Full Code Here

    for (int i = 0; i < classifier.length; i++) {
      result[i] = classifier[i].predict(features);
    }
    int numPossibleOutcomes = result[0].getDimension() == 1 ? 2 : result[0]
        .getDimension();
    DoubleVector toReturn = new DenseDoubleVector(
        result[0].getDimension() == 1 ? 1 : numPossibleOutcomes);
    // now combine the results based on the rule
    switch (type) {
      case MAJORITY:
        double[] histogram = createPredictionHistogram(result,
            numPossibleOutcomes);
        if (numPossibleOutcomes == 2) {
          toReturn.set(0, ArrayUtils.maxIndex(histogram));
        } else {
          toReturn.set(ArrayUtils.maxIndex(histogram), 1d);
        }
        break;
      case PROBABILITY:
        histogram = createPredictionHistogram(result, numPossibleOutcomes);
        double histSum = 0;
        for (double d : histogram) {
          histSum += d;
        }
        if (numPossibleOutcomes == 2) {
          toReturn.set(0, histogram[1] / histSum);
        } else {
          for (int i = 0; i < histogram.length; i++) {
            toReturn.set(i, histogram[i] / histSum);
          }
        }
        break;
      case AVERAGE:
        for (int i = 0; i < result.length; i++) {
          toReturn = toReturn.add(result[i]);
        }
        toReturn = toReturn.divide(classifier.length);
        break;
      default:
        throw new UnsupportedOperationException("Type " + type
            + " isn't supported yet!");
    }
View Full Code Here

        System.out
            .println("Computed " + row + " / " + numDistinctClasses + "!");
      }
    }

    classPriorProbability = new DenseDoubleVector(numDistinctClasses);
    for (int i = 0; i < numDistinctClasses; i++) {
      double prior = FastMath.log(numDocumentsPerClass[i])
          - FastMath.log(numDocumentsSeen);
      classPriorProbability.set(i, prior);
    }
View Full Code Here

  }

  private DenseDoubleVector getProbabilityDistribution(DoubleVector document) {

    int numClasses = classPriorProbability.getLength();
    DenseDoubleVector distribution = new DenseDoubleVector(numClasses);
    // loop through all classes and get the max probable one
    for (int i = 0; i < numClasses; i++) {
      double probability = getProbabilityForClass(document, i);
      distribution.set(i, probability);
    }

    double maxProbability = distribution.max();
    double probabilitySum = 0.0d;
    // we normalize it back
    for (int i = 0; i < numClasses; i++) {
      double probability = distribution.get(i);
      double normalizedProbability = FastMath.exp(probability - maxProbability
          + classPriorProbability.get(i));
      distribution.set(i, normalizedProbability);
      probabilitySum += normalizedProbability;
    }

    // since the sum is sometimes not 1, we need to divide by the sum
    distribution = (DenseDoubleVector) distribution.divide(probabilitySum);

    return distribution;
  }
View Full Code Here

  }

  private DoubleVector computeHiddenActivations(DoubleVector input,
      DoubleMatrix theta) {
    // add the bias to the input
    DoubleVector biased = new DenseDoubleVector(1d, input.toArray());
    return activationFunction.apply(theta.multiplyVectorRow(biased));
  }
View Full Code Here

  /**
   * @return the transition probabilities for the states.
   */
  public DoubleVector getTransitionProbabilities(int[] stateSequence) {
    DenseDoubleVector distribution = new DenseDoubleVector(
        stateSequence.length - 1);
    for (int i = 0; i < distribution.getDimension(); i++) {
      distribution.set(i,
          transitionProbabilities.get(stateSequence[i], stateSequence[i + 1]));
    }
    return distribution;
  }
View Full Code Here

        }
      }
      nameMapping[classIndex++] = classDirString;
    }

    return new Tuple3<>(docList, new DenseDoubleVector(prediction.toArray()),
        nameMapping);
  }
View Full Code Here

TOP

Related Classes of de.jungblut.math.dense.DenseDoubleVector

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.